Skip to content

Commit b3ff7bd

Browse files
committed
fix intermediate values
1 parent 77c39e1 commit b3ff7bd

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

__tests__/labeler.test.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import {describe, expect, jest, test} from '@jest/globals';
2+
import * as fs from 'fs';
3+
import * as path from 'path';
4+
25
import {Inputs} from '../src/context';
36
import {Label, Labeler, LabelStatus} from '../src/labeler';
47

5-
import repoLabels from './fixtures/repoLabels.json';
8+
const fixturesDir = path.join(__dirname, 'fixtures');
9+
610
// eslint-disable-next-line @typescript-eslint/no-explicit-any
711
jest.spyOn(Labeler.prototype as any, 'getRepoLabels').mockImplementation((): Promise<Label[]> => {
8-
return <Promise<Label[]>>(repoLabels as unknown);
12+
return <Promise<Label[]>>JSON.parse(fs.readFileSync(path.join(fixturesDir, 'repoLabels.json'), 'utf-8'));
913
});
1014

1115
const cases = [
1216
[
1317
'labels.update.yml',
1418
{
1519
githubToken: 'n/a',
16-
yamlFile: './__tests__/fixtures/labels.update.yml',
20+
yamlFile: path.join(fixturesDir, 'labels.update.yml'),
1721
skipDelete: true,
1822
dryRun: true,
1923
exclude: []
@@ -32,7 +36,7 @@ const cases = [
3236
'labels.exclude1.yml',
3337
{
3438
githubToken: 'n/a',
35-
yamlFile: './__tests__/fixtures/labels.exclude1.yml',
39+
yamlFile: path.join(fixturesDir, 'labels.exclude1.yml'),
3640
skipDelete: true,
3741
dryRun: true,
3842
exclude: ['* d*', '*enhancement', '*fix']
@@ -51,7 +55,7 @@ const cases = [
5155
'labels.exclude2.yml',
5256
{
5357
githubToken: 'n/a',
54-
yamlFile: './__tests__/fixtures/labels.exclude2.yml',
58+
yamlFile: path.join(fixturesDir, 'labels.exclude2.yml'),
5559
skipDelete: true,
5660
dryRun: true,
5761
exclude: ['*fix']
@@ -70,7 +74,7 @@ const cases = [
7074
'labels.hexcodes.yml',
7175
{
7276
githubToken: 'n/a',
73-
yamlFile: './__tests__/fixtures/labels.hexcodes.yml',
77+
yamlFile: path.join(fixturesDir, 'labels.hexcodes.yml'),
7478
skipDelete: true,
7579
dryRun: true,
7680
exclude: []
@@ -106,7 +110,8 @@ describe('run', () => {
106110
delete: 0,
107111
error: 0
108112
};
109-
for (const label of await labeler.labels) {
113+
const labels = await labeler.labels;
114+
for (const label of labels) {
110115
switch (label.ghaction_status) {
111116
case LabelStatus.Exclude: {
112117
res.exclude++;

src/labeler.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ export class Labeler {
212212
);
213213
}
214214

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

@@ -285,7 +286,8 @@ export class Labeler {
285286
}
286287

287288
// Delete
288-
for (const repoLabel of await this.repoLabels) {
289+
const repoLabels = await this.repoLabels;
290+
for (const repoLabel of repoLabels) {
289291
if (await this.getFileLabel(repoLabel.name)) {
290292
continue;
291293
}
@@ -308,26 +310,19 @@ export class Labeler {
308310
}
309311

310312
private async getRepoLabel(name: string): Promise<Label | undefined> {
311-
for (const repoLabel of await this.repoLabels) {
312-
if (name == repoLabel.name) {
313-
return repoLabel;
314-
}
315-
}
316-
return undefined;
313+
const repoLabels = await this.repoLabels;
314+
return repoLabels.find(repoLabel => repoLabel.name === name);
317315
}
318316

319317
private async getFileLabel(name: string): Promise<Label | undefined> {
320-
for (const fileLabel of await this.fileLabels) {
321-
if (name == fileLabel.name || name == fileLabel.from_name) {
322-
return fileLabel;
323-
}
324-
}
325-
return undefined;
318+
const fileLabels = await this.fileLabels;
319+
return fileLabels.find(fileLabel => name === fileLabel.name || name === fileLabel.from_name);
326320
}
327321

328322
async printRepoLabels() {
329323
const labels = Array<Label>();
330-
for (const repoLabel of await this.repoLabels) {
324+
const repoLabels = await this.repoLabels;
325+
for (const repoLabel of repoLabels) {
331326
labels.push({
332327
name: repoLabel.name,
333328
color: repoLabel.color,

0 commit comments

Comments
 (0)