Skip to content

Commit 1f6b9c4

Browse files
Final improvements and more tests
1 parent a5761c4 commit 1f6b9c4

2 files changed

Lines changed: 89 additions & 11 deletions

File tree

src/autoupdater.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,25 @@ export class AutoUpdater {
492492
return false;
493493
} else if (mergeConflictAction === 'label') {
494494
ghCore.info('Merge conflict detected, labelling with label: ' + mergeConflictLabel);
495-
495+
// Fetch current labels on the PR
496+
const { data: prData } = await this.octokit.rest.pulls.get({
497+
owner: mergeOpts.owner as string,
498+
repo: mergeOpts.repo as string,
499+
pull_number: prNumber,
500+
});
501+
const currentLabels = prData.labels.map((l: any) => l.name).filter(Boolean);
502+
if (!currentLabels.includes(mergeConflictLabel)) {
503+
const newLabels = [...currentLabels, mergeConflictLabel];
504+
await this.octokit.rest.issues.update({
505+
owner: mergeOpts.owner as string,
506+
repo: mergeOpts.repo as string,
507+
issue_number: prNumber,
508+
labels: newLabels,
509+
});
510+
ghCore.info(`Added merge conflict label '${mergeConflictLabel}' to PR #${prNumber}.`);
511+
} else {
512+
ghCore.info(`Merge conflict label '${mergeConflictLabel}' already present on PR #${prNumber}.`);
513+
}
496514
return false;
497515
} else {
498516
// Else, throw an error so we don't continue retrying.

test/autoupdate.test.ts

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,28 +1324,88 @@ describe('test `merge`', () => {
13241324
}
13251325
});
13261326

1327-
test('label merge conflicts', async () => {
1327+
test('merge conflict label is configured and merge conflict label is already present', async () => {
13281328
(config.retryCount as jest.Mock).mockReturnValue(0);
13291329
(config.mergeConflictAction as jest.Mock).mockReturnValue('label');
1330-
(config.mergeConflictLabel as jest.Mock).mockReturnValue('conflicted');
1330+
(config.mergeConflictLabel as jest.Mock).mockReturnValue('merge-conflict');
13311331
const updater = new AutoUpdater(config, emptyEvent);
13321332

1333-
const scope = nock('https://api.github.com:443')
1333+
// Mock the PR labels to already include the merge conflict label
1334+
const prNumber = 1;
1335+
const existingLabels = [
1336+
{ id: 1, name: 'bug' },
1337+
{ id: 2, name: 'merge-conflict' },
1338+
];
1339+
1340+
// nock for merge attempt (409 merge conflict)
1341+
const mergeScope = nock('https://api.github.com:443')
13341342
.post(`/repos/${owner}/${repo}/merges`, {
13351343
commit_message: mergeOpts.commit_message,
13361344
base: mergeOpts.base,
13371345
head: mergeOpts.head,
13381346
})
1339-
.reply(409, {
1340-
message: 'Merge conflict',
1341-
});
1347+
.reply(409, { message: 'Merge conflict' });
1348+
1349+
const getScope = nock('https://api.github.com:443')
1350+
.get(`/repos/${owner}/${repo}/pulls/${prNumber}`)
1351+
.reply(200, { labels: existingLabels });
1352+
1353+
const updateScope = nock('https://api.github.com:443')
1354+
.patch(`/repos/${owner}/${repo}/issues/${prNumber}`)
1355+
.reply(200, {});
13421356

13431357
const setOutput = jest.fn();
1344-
await updater.merge(owner, 1, mergeOpts, setOutput);
1358+
const infoSpy = jest.spyOn(require('@actions/core'), 'info');
13451359

1346-
expect(scope.isDone()).toEqual(true);
1360+
await updater.merge(owner, prNumber, mergeOpts, setOutput);
13471361

1348-
expect(setOutput).toHaveBeenCalledTimes(1);
1349-
expect(setOutput).toHaveBeenCalledWith(Output.Conflicted, true);
1362+
expect(mergeScope.isDone()).toBe(true);
1363+
expect(getScope.isDone()).toBe(true);
1364+
expect(updateScope.isDone()).toBe(false);
1365+
expect(infoSpy).toHaveBeenCalledWith(
1366+
expect.stringContaining("already present")
1367+
);
1368+
});
1369+
1370+
test('merge conflict label is configured and merge conflict label is not present', async () => {
1371+
(config.retryCount as jest.Mock).mockReturnValue(0);
1372+
(config.mergeConflictAction as jest.Mock).mockReturnValue('label');
1373+
(config.mergeConflictLabel as jest.Mock).mockReturnValue('merge-conflict');
1374+
const updater = new AutoUpdater(config, emptyEvent);
1375+
1376+
// Mock the PR labels to already include the merge conflict label
1377+
const prNumber = 1;
1378+
const existingLabels = [
1379+
{ id: 1, name: 'bug' },
1380+
];
1381+
1382+
// nock for merge attempt (409 merge conflict)
1383+
const mergeScope = nock('https://api.github.com:443')
1384+
.post(`/repos/${owner}/${repo}/merges`, {
1385+
commit_message: mergeOpts.commit_message,
1386+
base: mergeOpts.base,
1387+
head: mergeOpts.head,
1388+
})
1389+
.reply(409, { message: 'Merge conflict' });
1390+
1391+
const getScope = nock('https://api.github.com:443')
1392+
.get(`/repos/${owner}/${repo}/pulls/${prNumber}`)
1393+
.reply(200, { labels: existingLabels });
1394+
1395+
const updateScope = nock('https://api.github.com:443')
1396+
.patch(`/repos/${owner}/${repo}/issues/${prNumber}`)
1397+
.reply(200, {});
1398+
1399+
const setOutput = jest.fn();
1400+
const infoSpy = jest.spyOn(require('@actions/core'), 'info');
1401+
1402+
await updater.merge(owner, prNumber, mergeOpts, setOutput);
1403+
1404+
expect(mergeScope.isDone()).toBe(true);
1405+
expect(getScope.isDone()).toBe(true);
1406+
expect(updateScope.isDone()).toBe(false);
1407+
expect(infoSpy).toHaveBeenCalledWith(
1408+
expect.stringContaining("Added merge conflict label")
1409+
);
13501410
});
13511411
});

0 commit comments

Comments
 (0)