Skip to content

Commit a068aac

Browse files
LiSminotlee
authored andcommitted
feat: delete label flag
1 parent f6edbe3 commit a068aac

File tree

6 files changed

+213
-79
lines changed

6 files changed

+213
-79
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ As JSON:
168168
"name": "mylabel",
169169
"color": "ff0000",
170170
"aliases": [],
171-
"description": "optional description"
171+
"description": "optional description",
172+
"delete": false
172173
}
173174
```
174175

@@ -179,10 +180,12 @@ As YAML:
179180
color: "ff0000"
180181
aliases: []
181182
description: optional description
183+
delete: false
182184
```
183185
184186
- The `name` property refers to the label name.
185187
- The `color` property should be a hex code, with or without the leading `#`.
188+
- The `delete` property is optional. When set to true, matches for this label will _always_ be deleted. This can be used in conjunction with [allowAddedLabels](#allowaddedlabels) to flag specific labels for deletion while leaving non-specified labels intact.
186189

187190
The `aliases` property is optional. When GitHub Label Sync is determining whether to update or delete/create a label it will use the aliases property to prevent used labels from being deleted.
188191

@@ -308,4 +311,4 @@ This software is published by the Financial Times under the [MIT licence][licens
308311
[access-tokens]: https://github.com/settings/tokens
309312
[license]: http://opensource.org/licenses/MIT
310313
[node]: https://nodejs.org/
311-
[npm]: https://www.npmjs.com/
314+
[npm]: https://www.npmjs.com/

lib/calculate-label-diff.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module.exports = calculateLabelDiff;
44

5-
function calculateLabelDiff(currentLabels, configuredLabels) {
5+
function calculateLabelDiff(currentLabels, configuredLabels, allowAddedLabels) {
66
const diff = [];
77
const resolvedLabels = [];
88
configuredLabels.forEach((configuredLabel) => {
@@ -25,12 +25,15 @@ function calculateLabelDiff(currentLabels, configuredLabels) {
2525
resolvedLabels.push(...matches);
2626

2727
// If we have no matches, the configured label is missing
28-
if (matches.length === 0) {
28+
// Do not create label if set to delete
29+
if (matches.length === 0 && !configuredLabel.delete) {
2930
return diff.push(createMissingEntry(configuredLabel));
3031
}
3132

3233
matches.forEach((matchedLabel, index) => {
3334

35+
if (configuredLabel.delete) return diff.push(createAddedEntry(matchedLabel));
36+
3437
const matchedDescription = getLabelDescription(matchedLabel);
3538
const configuredDescription = getLabelDescription(configuredLabel, matchedDescription);
3639

@@ -52,7 +55,7 @@ function calculateLabelDiff(currentLabels, configuredLabels) {
5255

5356
});
5457
currentLabels.filter(label => resolvedLabels.indexOf(label) === -1).forEach((currentLabel) => {
55-
diff.push(createAddedEntry(currentLabel));
58+
if(!allowAddedLabels) diff.push(createAddedEntry(currentLabel));
5659
});
5760
return diff;
5861
}
@@ -127,4 +130,4 @@ function createAddedEntry(actualLabel) {
127130
addedEntry.actual.description = actualDescription;
128131
}
129132
return addedEntry;
130-
}
133+
}

lib/github-label-sync.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,7 @@ function githubLabelSync(options) {
6565

6666
return apiClient.getLabels(options.repo)
6767
.then((currentLabels) => {
68-
labelDiff = calculateLabelDiff(currentLabels, options.labels).filter((diff) => {
69-
if (options.allowAddedLabels && diff.type === 'added') {
70-
return false;
71-
}
72-
return true;
73-
});
68+
labelDiff = calculateLabelDiff(currentLabels, options.labels, options.allowAddedLabels);
7469
stringifyLabelDiff(labelDiff).forEach((diffLine) => {
7570
log.info(format.diff(diffLine));
7671
});
@@ -108,4 +103,4 @@ function noop() {}
108103
/* istanbul ignore next */
109104
function echo(arg) {
110105
return arg;
111-
}
106+
}

lib/validate-label-format.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const schema = {
1010
name: { type: 'string', maxLength: 50, },
1111
color: { type: 'string', pattern: '^[a-fA-F0-9]{6}$' },
1212
description: { type: 'string', maxLength: 100 },
13+
delete: { type: 'boolean' },
1314
aliases: {
1415
type: 'array',
1516
items: { type: 'string', maxLength: 50 }
@@ -21,4 +22,4 @@ const schema = {
2122

2223
const validate = ajv.compile(schema);
2324

24-
module.exports = validate;
25+
module.exports = validate;

test/unit/lib/calculate-label-diff.test.js

Lines changed: 190 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('lib/calculate-label-diff', () => {
1616
describe('calculateLabelDiff(currentLabels, configuredLabels)', () => {
1717
let configuredLabels;
1818
let currentLabels;
19+
let allowAddedLabels;
1920
let diff;
2021

2122
it('should return an array', function() {
@@ -47,7 +48,25 @@ describe('lib/calculate-label-diff', () => {
4748
}
4849
});
4950
});
51+
});
52+
53+
describe('when a configured label set to delete does not exist in the current labels', () => {
5054

55+
beforeEach(() => {
56+
currentLabels = [];
57+
configuredLabels = [
58+
{
59+
name: 'bar',
60+
color: '00ff00',
61+
delete: true
62+
}
63+
];
64+
diff = calculateLabelDiff(currentLabels, configuredLabels);
65+
});
66+
67+
it('should not add a "missing" entry to the returned diff', () => {
68+
assert.lengthEquals(diff, 0);
69+
});
5170
});
5271

5372
describe('when a configured label with description does not exist in the current labels', () => {
@@ -142,6 +161,39 @@ describe('lib/calculate-label-diff', () => {
142161

143162
});
144163

164+
describe('when a configured label exists in the current labels and is marked for deletion', () => {
165+
166+
beforeEach(() => {
167+
currentLabels = [
168+
{
169+
name: 'foo',
170+
color: 'ff0000',
171+
}
172+
];
173+
configuredLabels = [
174+
{
175+
name: 'foo',
176+
delete: true
177+
}
178+
];
179+
diff = calculateLabelDiff(currentLabels, configuredLabels);
180+
});
181+
182+
it('should add an "added" entry to the returned diff', () => {
183+
assert.lengthEquals(diff, 1);
184+
assert.deepEqual(diff[0], {
185+
name: 'foo',
186+
type: 'added',
187+
actual: {
188+
name: 'foo',
189+
color: 'ff0000',
190+
},
191+
expected: null
192+
});
193+
});
194+
195+
});
196+
145197
describe('when a configured label with description exists in the current labels without description', () => {
146198

147199
beforeEach(() => {
@@ -436,62 +488,158 @@ describe('lib/calculate-label-diff', () => {
436488

437489
});
438490

439-
describe('when a current label does not exist in the configured labels', () => {
440-
491+
describe('when allowAddedLabels is false', () => {
441492
beforeEach(() => {
442-
currentLabels = [
443-
{
444-
name: 'foo',
445-
color: 'ff0000'
446-
}
447-
];
448-
configuredLabels = [];
449-
diff = calculateLabelDiff(currentLabels, configuredLabels);
493+
allowAddedLabels = false;
450494
});
451495

452-
it('should add an "added" entry to the returned diff', () => {
453-
assert.lengthEquals(diff, 1);
454-
assert.deepEqual(diff[0], {
455-
name: 'foo',
456-
type: 'added',
457-
actual: {
496+
describe('when a current label does not exist in the configured labels', () => {
497+
498+
beforeEach(() => {
499+
currentLabels = [
500+
{
501+
name: 'foo',
502+
color: 'ff0000'
503+
}
504+
];
505+
configuredLabels = [];
506+
diff = calculateLabelDiff(currentLabels, configuredLabels, allowAddedLabels);
507+
});
508+
509+
it('should add an "added" entry to the returned diff', () => {
510+
assert.lengthEquals(diff, 1);
511+
assert.deepEqual(diff[0], {
458512
name: 'foo',
459-
color: 'ff0000'
460-
},
461-
expected: null
513+
type: 'added',
514+
actual: {
515+
name: 'foo',
516+
color: 'ff0000'
517+
},
518+
expected: null
519+
});
462520
});
521+
522+
});
523+
524+
describe('when a current label with description does not exist in the configured labels', () => {
525+
526+
beforeEach(() => {
527+
currentLabels = [
528+
{
529+
name: 'foo',
530+
color: 'ff0000',
531+
description: 'bar'
532+
}
533+
];
534+
configuredLabels = [];
535+
diff = calculateLabelDiff(currentLabels, configuredLabels, allowAddedLabels);
536+
});
537+
538+
it('should add an "added" entry to the returned diff', () => {
539+
assert.lengthEquals(diff, 1);
540+
assert.deepEqual(diff[0], {
541+
name: 'foo',
542+
type: 'added',
543+
actual: {
544+
name: 'foo',
545+
color: 'ff0000',
546+
description: 'bar'
547+
},
548+
expected: null
549+
});
550+
});
551+
463552
});
464-
465553
});
466554

467-
describe('when a current label with description does not exist in the configured labels', () => {
468-
555+
describe('when allowAddedLabels is true', () => {
469556
beforeEach(() => {
470-
currentLabels = [
471-
{
472-
name: 'foo',
473-
color: 'ff0000',
474-
description: 'bar'
475-
}
476-
];
477-
configuredLabels = [];
478-
diff = calculateLabelDiff(currentLabels, configuredLabels);
557+
allowAddedLabels = true;
479558
});
480559

481-
it('should add an "added" entry to the returned diff', () => {
482-
assert.lengthEquals(diff, 1);
483-
assert.deepEqual(diff[0], {
484-
name: 'foo',
485-
type: 'added',
486-
actual: {
560+
describe('when a current label does not exist in the configured labels', () => {
561+
beforeEach(() => {
562+
currentLabels = [
563+
{
564+
name: 'foo',
565+
color: 'ff0000'
566+
}
567+
];
568+
configuredLabels = [];
569+
diff = calculateLabelDiff(currentLabels, configuredLabels, allowAddedLabels);
570+
});
571+
572+
it('should not add an "added" entry to the returned diff', () => {
573+
assert.lengthEquals(diff, 0);
574+
});
575+
});
576+
577+
describe('when a current label is marked for deletion in the configured labels', () => {
578+
beforeEach(() => {
579+
currentLabels = [
580+
{
581+
name: 'foo',
582+
color: 'ff0000',
583+
}
584+
];
585+
configuredLabels = [
586+
{
587+
name: 'foo',
588+
delete: true,
589+
}
590+
];
591+
diff = calculateLabelDiff(currentLabels, configuredLabels, allowAddedLabels);
592+
});
593+
594+
it('should add an "added" entry to the returned diff', () => {
595+
assert.lengthEquals(diff, 1);
596+
assert.deepEqual(diff[0], {
487597
name: 'foo',
488-
color: 'ff0000',
489-
description: 'bar'
490-
},
491-
expected: null
598+
type: 'added',
599+
actual: {
600+
name: 'foo',
601+
color: 'ff0000'
602+
},
603+
expected: null
604+
});
492605
});
493606
});
494607

608+
describe('when a current label with description is marked for deletion in the configured labels', () => {
609+
610+
beforeEach(() => {
611+
currentLabels = [
612+
{
613+
name: 'foo',
614+
color: 'ff0000',
615+
description: 'bar',
616+
delete: true
617+
}
618+
];
619+
configuredLabels = [
620+
{
621+
name: 'foo',
622+
delete: true,
623+
}
624+
];
625+
diff = calculateLabelDiff(currentLabels, configuredLabels, allowAddedLabels);
626+
});
627+
628+
it('should add an "added" entry to the returned diff', () => {
629+
assert.lengthEquals(diff, 1);
630+
assert.deepEqual(diff[0], {
631+
name: 'foo',
632+
type: 'added',
633+
actual: {
634+
name: 'foo',
635+
color: 'ff0000',
636+
description: 'bar'
637+
},
638+
expected: null
639+
});
640+
});
641+
642+
});
495643
});
496644

497645
describe('when a range of diffs are expected', () => {
@@ -583,4 +731,4 @@ describe('lib/calculate-label-diff', () => {
583731

584732
});
585733

586-
});
734+
});

0 commit comments

Comments
 (0)