Skip to content

Commit

Permalink
feat: delete label flag
Browse files Browse the repository at this point in the history
  • Loading branch information
LiSmi authored and notlee committed Mar 8, 2023
1 parent f6edbe3 commit a068aac
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 79 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ As JSON:
"name": "mylabel",
"color": "ff0000",
"aliases": [],
"description": "optional description"
"description": "optional description",
"delete": false
}
```

Expand All @@ -179,10 +180,12 @@ As YAML:
color: "ff0000"
aliases: []
description: optional description
delete: false
```

- The `name` property refers to the label name.
- The `color` property should be a hex code, with or without the leading `#`.
- 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.

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.

Expand Down Expand Up @@ -308,4 +311,4 @@ This software is published by the Financial Times under the [MIT licence][licens
[access-tokens]: https://github.com/settings/tokens
[license]: http://opensource.org/licenses/MIT
[node]: https://nodejs.org/
[npm]: https://www.npmjs.com/
[npm]: https://www.npmjs.com/
11 changes: 7 additions & 4 deletions lib/calculate-label-diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module.exports = calculateLabelDiff;

function calculateLabelDiff(currentLabels, configuredLabels) {
function calculateLabelDiff(currentLabels, configuredLabels, allowAddedLabels) {
const diff = [];
const resolvedLabels = [];
configuredLabels.forEach((configuredLabel) => {
Expand All @@ -25,12 +25,15 @@ function calculateLabelDiff(currentLabels, configuredLabels) {
resolvedLabels.push(...matches);

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

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

if (configuredLabel.delete) return diff.push(createAddedEntry(matchedLabel));

const matchedDescription = getLabelDescription(matchedLabel);
const configuredDescription = getLabelDescription(configuredLabel, matchedDescription);

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

});
currentLabels.filter(label => resolvedLabels.indexOf(label) === -1).forEach((currentLabel) => {
diff.push(createAddedEntry(currentLabel));
if(!allowAddedLabels) diff.push(createAddedEntry(currentLabel));
});
return diff;
}
Expand Down Expand Up @@ -127,4 +130,4 @@ function createAddedEntry(actualLabel) {
addedEntry.actual.description = actualDescription;
}
return addedEntry;
}
}
9 changes: 2 additions & 7 deletions lib/github-label-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,7 @@ function githubLabelSync(options) {

return apiClient.getLabels(options.repo)
.then((currentLabels) => {
labelDiff = calculateLabelDiff(currentLabels, options.labels).filter((diff) => {
if (options.allowAddedLabels && diff.type === 'added') {
return false;
}
return true;
});
labelDiff = calculateLabelDiff(currentLabels, options.labels, options.allowAddedLabels);
stringifyLabelDiff(labelDiff).forEach((diffLine) => {
log.info(format.diff(diffLine));
});
Expand Down Expand Up @@ -108,4 +103,4 @@ function noop() {}
/* istanbul ignore next */
function echo(arg) {
return arg;
}
}
3 changes: 2 additions & 1 deletion lib/validate-label-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const schema = {
name: { type: 'string', maxLength: 50, },
color: { type: 'string', pattern: '^[a-fA-F0-9]{6}$' },
description: { type: 'string', maxLength: 100 },
delete: { type: 'boolean' },
aliases: {
type: 'array',
items: { type: 'string', maxLength: 50 }
Expand All @@ -21,4 +22,4 @@ const schema = {

const validate = ajv.compile(schema);

module.exports = validate;
module.exports = validate;
232 changes: 190 additions & 42 deletions test/unit/lib/calculate-label-diff.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('lib/calculate-label-diff', () => {
describe('calculateLabelDiff(currentLabels, configuredLabels)', () => {
let configuredLabels;
let currentLabels;
let allowAddedLabels;
let diff;

it('should return an array', function() {
Expand Down Expand Up @@ -47,7 +48,25 @@ describe('lib/calculate-label-diff', () => {
}
});
});
});

describe('when a configured label set to delete does not exist in the current labels', () => {

beforeEach(() => {
currentLabels = [];
configuredLabels = [
{
name: 'bar',
color: '00ff00',
delete: true
}
];
diff = calculateLabelDiff(currentLabels, configuredLabels);
});

it('should not add a "missing" entry to the returned diff', () => {
assert.lengthEquals(diff, 0);
});
});

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

});

describe('when a configured label exists in the current labels and is marked for deletion', () => {

beforeEach(() => {
currentLabels = [
{
name: 'foo',
color: 'ff0000',
}
];
configuredLabels = [
{
name: 'foo',
delete: true
}
];
diff = calculateLabelDiff(currentLabels, configuredLabels);
});

it('should add an "added" entry to the returned diff', () => {
assert.lengthEquals(diff, 1);
assert.deepEqual(diff[0], {
name: 'foo',
type: 'added',
actual: {
name: 'foo',
color: 'ff0000',
},
expected: null
});
});

});

describe('when a configured label with description exists in the current labels without description', () => {

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

});

describe('when a current label does not exist in the configured labels', () => {

describe('when allowAddedLabels is false', () => {
beforeEach(() => {
currentLabels = [
{
name: 'foo',
color: 'ff0000'
}
];
configuredLabels = [];
diff = calculateLabelDiff(currentLabels, configuredLabels);
allowAddedLabels = false;
});

it('should add an "added" entry to the returned diff', () => {
assert.lengthEquals(diff, 1);
assert.deepEqual(diff[0], {
name: 'foo',
type: 'added',
actual: {
describe('when a current label does not exist in the configured labels', () => {

beforeEach(() => {
currentLabels = [
{
name: 'foo',
color: 'ff0000'
}
];
configuredLabels = [];
diff = calculateLabelDiff(currentLabels, configuredLabels, allowAddedLabels);
});

it('should add an "added" entry to the returned diff', () => {
assert.lengthEquals(diff, 1);
assert.deepEqual(diff[0], {
name: 'foo',
color: 'ff0000'
},
expected: null
type: 'added',
actual: {
name: 'foo',
color: 'ff0000'
},
expected: null
});
});

});

describe('when a current label with description does not exist in the configured labels', () => {

beforeEach(() => {
currentLabels = [
{
name: 'foo',
color: 'ff0000',
description: 'bar'
}
];
configuredLabels = [];
diff = calculateLabelDiff(currentLabels, configuredLabels, allowAddedLabels);
});

it('should add an "added" entry to the returned diff', () => {
assert.lengthEquals(diff, 1);
assert.deepEqual(diff[0], {
name: 'foo',
type: 'added',
actual: {
name: 'foo',
color: 'ff0000',
description: 'bar'
},
expected: null
});
});

});

});

describe('when a current label with description does not exist in the configured labels', () => {

describe('when allowAddedLabels is true', () => {
beforeEach(() => {
currentLabels = [
{
name: 'foo',
color: 'ff0000',
description: 'bar'
}
];
configuredLabels = [];
diff = calculateLabelDiff(currentLabels, configuredLabels);
allowAddedLabels = true;
});

it('should add an "added" entry to the returned diff', () => {
assert.lengthEquals(diff, 1);
assert.deepEqual(diff[0], {
name: 'foo',
type: 'added',
actual: {
describe('when a current label does not exist in the configured labels', () => {
beforeEach(() => {
currentLabels = [
{
name: 'foo',
color: 'ff0000'
}
];
configuredLabels = [];
diff = calculateLabelDiff(currentLabels, configuredLabels, allowAddedLabels);
});

it('should not add an "added" entry to the returned diff', () => {
assert.lengthEquals(diff, 0);
});
});

describe('when a current label is marked for deletion in the configured labels', () => {
beforeEach(() => {
currentLabels = [
{
name: 'foo',
color: 'ff0000',
}
];
configuredLabels = [
{
name: 'foo',
delete: true,
}
];
diff = calculateLabelDiff(currentLabels, configuredLabels, allowAddedLabels);
});

it('should add an "added" entry to the returned diff', () => {
assert.lengthEquals(diff, 1);
assert.deepEqual(diff[0], {
name: 'foo',
color: 'ff0000',
description: 'bar'
},
expected: null
type: 'added',
actual: {
name: 'foo',
color: 'ff0000'
},
expected: null
});
});
});

describe('when a current label with description is marked for deletion in the configured labels', () => {

beforeEach(() => {
currentLabels = [
{
name: 'foo',
color: 'ff0000',
description: 'bar',
delete: true
}
];
configuredLabels = [
{
name: 'foo',
delete: true,
}
];
diff = calculateLabelDiff(currentLabels, configuredLabels, allowAddedLabels);
});

it('should add an "added" entry to the returned diff', () => {
assert.lengthEquals(diff, 1);
assert.deepEqual(diff[0], {
name: 'foo',
type: 'added',
actual: {
name: 'foo',
color: 'ff0000',
description: 'bar'
},
expected: null
});
});

});
});

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

});

});
});

0 comments on commit a068aac

Please sign in to comment.