Skip to content

Commit

Permalink
fix intermediate values
Browse files Browse the repository at this point in the history
  • Loading branch information
crazy-max committed Oct 26, 2024
1 parent 77c39e1 commit b3ff7bd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
19 changes: 12 additions & 7 deletions __tests__/labeler.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import {describe, expect, jest, test} from '@jest/globals';
import * as fs from 'fs';
import * as path from 'path';

import {Inputs} from '../src/context';
import {Label, Labeler, LabelStatus} from '../src/labeler';

import repoLabels from './fixtures/repoLabels.json';
const fixturesDir = path.join(__dirname, 'fixtures');

// eslint-disable-next-line @typescript-eslint/no-explicit-any
jest.spyOn(Labeler.prototype as any, 'getRepoLabels').mockImplementation((): Promise<Label[]> => {
return <Promise<Label[]>>(repoLabels as unknown);
return <Promise<Label[]>>JSON.parse(fs.readFileSync(path.join(fixturesDir, 'repoLabels.json'), 'utf-8'));
});

const cases = [
[
'labels.update.yml',
{
githubToken: 'n/a',
yamlFile: './__tests__/fixtures/labels.update.yml',
yamlFile: path.join(fixturesDir, 'labels.update.yml'),
skipDelete: true,
dryRun: true,
exclude: []
Expand All @@ -32,7 +36,7 @@ const cases = [
'labels.exclude1.yml',
{
githubToken: 'n/a',
yamlFile: './__tests__/fixtures/labels.exclude1.yml',
yamlFile: path.join(fixturesDir, 'labels.exclude1.yml'),
skipDelete: true,
dryRun: true,
exclude: ['* d*', '*enhancement', '*fix']
Expand All @@ -51,7 +55,7 @@ const cases = [
'labels.exclude2.yml',
{
githubToken: 'n/a',
yamlFile: './__tests__/fixtures/labels.exclude2.yml',
yamlFile: path.join(fixturesDir, 'labels.exclude2.yml'),
skipDelete: true,
dryRun: true,
exclude: ['*fix']
Expand All @@ -70,7 +74,7 @@ const cases = [
'labels.hexcodes.yml',
{
githubToken: 'n/a',
yamlFile: './__tests__/fixtures/labels.hexcodes.yml',
yamlFile: path.join(fixturesDir, 'labels.hexcodes.yml'),
skipDelete: true,
dryRun: true,
exclude: []
Expand Down Expand Up @@ -106,7 +110,8 @@ describe('run', () => {
delete: 0,
error: 0
};
for (const label of await labeler.labels) {
const labels = await labeler.labels;
for (const label of labels) {
switch (label.ghaction_status) {
case LabelStatus.Exclude: {
res.exclude++;
Expand Down
25 changes: 10 additions & 15 deletions src/labeler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ export class Labeler {
);
}

for (const fileLabel of await this.fileLabels) {
const fileLabels = await this.fileLabels;
for (const fileLabel of fileLabels) {
// Allow color hex codes (e.g., '#cccccc') to be set even though GitHub's API requires the # sign not to be present.
fileLabel.color = this.sanitizeColorString(fileLabel.color);

Expand Down Expand Up @@ -285,7 +286,8 @@ export class Labeler {
}

// Delete
for (const repoLabel of await this.repoLabels) {
const repoLabels = await this.repoLabels;
for (const repoLabel of repoLabels) {
if (await this.getFileLabel(repoLabel.name)) {
continue;
}
Expand All @@ -308,26 +310,19 @@ export class Labeler {
}

private async getRepoLabel(name: string): Promise<Label | undefined> {
for (const repoLabel of await this.repoLabels) {
if (name == repoLabel.name) {
return repoLabel;
}
}
return undefined;
const repoLabels = await this.repoLabels;
return repoLabels.find(repoLabel => repoLabel.name === name);
}

private async getFileLabel(name: string): Promise<Label | undefined> {
for (const fileLabel of await this.fileLabels) {
if (name == fileLabel.name || name == fileLabel.from_name) {
return fileLabel;
}
}
return undefined;
const fileLabels = await this.fileLabels;
return fileLabels.find(fileLabel => name === fileLabel.name || name === fileLabel.from_name);
}

async printRepoLabels() {
const labels = Array<Label>();
for (const repoLabel of await this.repoLabels) {
const repoLabels = await this.repoLabels;
for (const repoLabel of repoLabels) {
labels.push({
name: repoLabel.name,
color: repoLabel.color,
Expand Down

0 comments on commit b3ff7bd

Please sign in to comment.