Skip to content

Commit cb5ec77

Browse files
committed
Add tests for obsolete and updated labels
1 parent 9c74081 commit cb5ec77

File tree

2 files changed

+220
-38
lines changed

2 files changed

+220
-38
lines changed

src/reporter/github/index.test.js

Lines changed: 106 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect } from 'chai';
22
import nock from 'nock';
33

4-
import { LABELS } from '../labels.js';
4+
import { LABELS, MANAGED_BY_OTA_MARKER } from '../labels.js';
55

66
import GitHub from './index.js';
77

@@ -24,31 +24,119 @@ describe('GitHub', function () {
2424
});
2525

2626
describe('#initialize', () => {
27-
const scopes = [];
27+
context('when some labels are missing', () => {
28+
const scopes = [];
2829

29-
before(async () => {
30-
const existingLabels = MANAGED_LABELS.slice(0, -2);
30+
before(async () => {
31+
const existingLabels = MANAGED_LABELS.slice(0, -2).map(label => ({
32+
...label,
33+
description: `${label.description} ${MANAGED_BY_OTA_MARKER}`,
34+
}));
3135

32-
nock('https://api.github.com')
33-
.get('/repos/owner/repo/labels')
34-
.query(true)
35-
.reply(200, existingLabels);
36+
nock('https://api.github.com')
37+
.get('/repos/owner/repo/labels')
38+
.query(true)
39+
.reply(200, existingLabels);
40+
41+
const missingLabels = MANAGED_LABELS.slice(-2);
3642

37-
const missingLabels = MANAGED_LABELS.slice(-2);
43+
for (const label of missingLabels) {
44+
scopes.push(nock('https://api.github.com')
45+
.post('/repos/owner/repo/labels', body => body.name === label.name)
46+
.reply(200, label));
47+
}
3848

39-
for (const label of missingLabels) {
40-
scopes.push(nock('https://api.github.com')
41-
.post('/repos/owner/repo/labels', body => body.name === label.name)
42-
.reply(200, label));
43-
}
49+
await github.initialize();
50+
});
51+
52+
after(nock.cleanAll);
4453

45-
await github.initialize();
54+
it('should create missing labels', () => {
55+
scopes.forEach(scope => expect(scope.isDone()).to.be.true);
56+
});
4657
});
4758

48-
after(nock.cleanAll);
59+
context('when some labels are obsolete', () => {
60+
const deleteScopes = [];
61+
62+
before(async () => {
63+
const existingLabels = [
64+
...MANAGED_LABELS.map(label => ({
65+
...label,
66+
description: `${label.description} ${MANAGED_BY_OTA_MARKER}`,
67+
})),
68+
// Add an obsolete label that should be removed
69+
{
70+
name: 'obsolete label',
71+
color: 'FF0000',
72+
description: `This label is no longer used ${MANAGED_BY_OTA_MARKER}`,
73+
},
74+
];
75+
76+
nock('https://api.github.com')
77+
.get('/repos/owner/repo/labels')
78+
.query(true)
79+
.reply(200, existingLabels);
80+
81+
// Mock the delete call for the obsolete label
82+
deleteScopes.push(nock('https://api.github.com')
83+
.delete('/repos/owner/repo/labels/obsolete%20label')
84+
.reply(200));
85+
86+
// Mock the second getRepositoryLabels call after deletion
87+
nock('https://api.github.com')
88+
.get('/repos/owner/repo/labels')
89+
.query(true)
90+
.reply(200, MANAGED_LABELS.map(label => ({
91+
...label,
92+
description: `${label.description} ${MANAGED_BY_OTA_MARKER}`,
93+
})));
94+
95+
await github.initialize();
96+
});
97+
98+
after(nock.cleanAll);
99+
100+
it('should remove obsolete managed labels', () => {
101+
deleteScopes.forEach(scope => expect(scope.isDone()).to.be.true);
102+
});
103+
});
104+
105+
context('when some labels have changed descriptions', () => {
106+
const updateScopes = [];
49107

50-
it('should create missing labels', () => {
51-
scopes.forEach(scope => expect(scope.isDone()).to.be.true);
108+
before(async () => {
109+
const originalTestLabels = MANAGED_LABELS.slice(-2);
110+
const testLabels = originalTestLabels.map(label => ({
111+
...label,
112+
description: `${label.description} - obsolete description`,
113+
}));
114+
115+
nock('https://api.github.com')
116+
.persist()
117+
.get('/repos/owner/repo/labels')
118+
.query(true)
119+
.reply(200, [ ...MANAGED_LABELS.slice(0, -2), ...testLabels ].map(label => ({
120+
...label,
121+
description: `${label.description} ${MANAGED_BY_OTA_MARKER}`,
122+
})));
123+
124+
for (const label of originalTestLabels) {
125+
updateScopes.push(nock('https://api.github.com')
126+
.patch(`/repos/owner/repo/labels/${encodeURIComponent(label.name)}`, body =>
127+
body.description === `${label.description} ${MANAGED_BY_OTA_MARKER}`)
128+
.reply(200, label));
129+
}
130+
await github.initialize();
131+
});
132+
133+
after(() => {
134+
nock.cleanAll();
135+
});
136+
137+
it('should update labels with changed descriptions', () => {
138+
updateScopes.forEach(scope => expect(scope.isDone()).to.be.true);
139+
});
52140
});
53141
});
54142

src/reporter/gitlab/index.test.js

Lines changed: 114 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect } from 'chai';
22
import nock from 'nock';
33

4-
import { LABELS } from '../labels.js';
4+
import { LABELS, MANAGED_BY_OTA_MARKER } from '../labels.js';
55

66
import GitLab from './index.js';
77

@@ -18,34 +18,128 @@ describe('GitLab', function () {
1818
});
1919

2020
describe('#initialize', () => {
21-
const scopes = [];
21+
context('when some labels are missing', () => {
22+
const scopes = [];
2223

23-
before(async () => {
24-
const existingLabels = MANAGED_LABELS.slice(0, -2);
24+
before(async () => {
25+
const existingLabels = MANAGED_LABELS.slice(0, -2).map(label => ({
26+
...label,
27+
description: `${label.description} ${MANAGED_BY_OTA_MARKER}`,
28+
}));
2529

26-
nock(gitlab.apiBaseURL)
27-
.get(`/projects/${encodeURIComponent('owner/repo')}`)
28-
.reply(200, { id: PROJECT_ID });
30+
nock(gitlab.apiBaseURL)
31+
.get(`/projects/${encodeURIComponent('owner/repo')}`)
32+
.reply(200, { id: PROJECT_ID });
2933

30-
nock(gitlab.apiBaseURL)
31-
.get(`/projects/${PROJECT_ID}/labels?with_counts=true`)
32-
.reply(200, existingLabels);
34+
nock(gitlab.apiBaseURL)
35+
.get(`/projects/${PROJECT_ID}/labels?with_counts=true`)
36+
.reply(200, existingLabels);
3337

34-
const missingLabels = MANAGED_LABELS.slice(-2);
38+
const missingLabels = MANAGED_LABELS.slice(-2);
3539

36-
for (const label of missingLabels) {
37-
scopes.push(nock(gitlab.apiBaseURL)
38-
.post(`/projects/${PROJECT_ID}/labels`)
39-
.reply(200, { name: label.name }));
40-
}
40+
for (const label of missingLabels) {
41+
scopes.push(nock(gitlab.apiBaseURL)
42+
.post(`/projects/${PROJECT_ID}/labels`)
43+
.reply(200, { name: label.name }));
44+
}
4145

42-
await gitlab.initialize();
46+
await gitlab.initialize();
47+
});
48+
49+
after(nock.cleanAll);
50+
51+
it('should create missing labels', () => {
52+
scopes.forEach(scope => expect(scope.isDone()).to.be.true);
53+
});
4354
});
4455

45-
after(nock.cleanAll);
56+
context('when some labels are obsolete', () => {
57+
const deleteScopes = [];
58+
59+
before(async () => {
60+
const existingLabels = [
61+
...MANAGED_LABELS.map(label => ({
62+
...label,
63+
description: `${label.description} ${MANAGED_BY_OTA_MARKER}`,
64+
})),
65+
// Add an obsolete label that should be removed
66+
{
67+
name: 'obsolete label',
68+
color: '#FF0000',
69+
description: `This label is no longer used ${MANAGED_BY_OTA_MARKER}`,
70+
},
71+
];
72+
73+
nock(gitlab.apiBaseURL)
74+
.get(`/projects/${encodeURIComponent('owner/repo')}`)
75+
.reply(200, { id: PROJECT_ID });
76+
77+
nock(gitlab.apiBaseURL)
78+
.get(`/projects/${PROJECT_ID}/labels?with_counts=true`)
79+
.reply(200, existingLabels);
4680

47-
it('should create missing labels', () => {
48-
scopes.forEach(scope => expect(scope.isDone()).to.be.true);
81+
// Mock the delete call for the obsolete label
82+
deleteScopes.push(nock(gitlab.apiBaseURL)
83+
.delete(`/projects/${PROJECT_ID}/labels/${encodeURIComponent('obsolete label')}`)
84+
.reply(200));
85+
86+
// Mock the second getRepositoryLabels call after deletion
87+
nock(gitlab.apiBaseURL)
88+
.get(`/projects/${PROJECT_ID}/labels?with_counts=true`)
89+
.reply(200, MANAGED_LABELS.map(label => ({
90+
...label,
91+
description: `${label.description} ${MANAGED_BY_OTA_MARKER}`,
92+
})));
93+
94+
await gitlab.initialize();
95+
});
96+
97+
after(nock.cleanAll);
98+
99+
it('should remove obsolete managed labels', () => {
100+
deleteScopes.forEach(scope => expect(scope.isDone()).to.be.true);
101+
});
102+
});
103+
104+
context('when some labels have changed descriptions', () => {
105+
const updateScopes = [];
106+
107+
before(async () => {
108+
const originalTestLabels = MANAGED_LABELS.slice(-2);
109+
const testLabels = originalTestLabels.map(label => ({
110+
...label,
111+
description: `${label.description} - obsolete description`,
112+
}));
113+
114+
nock(gitlab.apiBaseURL)
115+
.get(`/projects/${encodeURIComponent('owner/repo')}`)
116+
.reply(200, { id: PROJECT_ID });
117+
118+
nock(gitlab.apiBaseURL)
119+
.persist()
120+
.get(`/projects/${PROJECT_ID}/labels?with_counts=true`)
121+
.reply(200, [ ...MANAGED_LABELS.slice(0, -2), ...testLabels ].map(label => ({
122+
...label,
123+
description: `${label.description} ${MANAGED_BY_OTA_MARKER}`,
124+
})));
125+
126+
for (const label of originalTestLabels) {
127+
updateScopes.push(nock(gitlab.apiBaseURL)
128+
.put(`/projects/${PROJECT_ID}/labels/${encodeURIComponent(label.name)}`, body =>
129+
body.description === `${label.description} ${MANAGED_BY_OTA_MARKER}`)
130+
.reply(200, { name: label.name, color: `#${label.color}` }));
131+
}
132+
133+
await gitlab.initialize();
134+
});
135+
136+
after(() => {
137+
nock.cleanAll();
138+
});
139+
140+
it('should update labels with changed descriptions', () => {
141+
updateScopes.forEach(scope => expect(scope.isDone()).to.be.true);
142+
});
49143
});
50144
});
51145

0 commit comments

Comments
 (0)