Skip to content

Commit

Permalink
fix: call mergequeue after a check_suite event
Browse files Browse the repository at this point in the history
  • Loading branch information
lionelB committed Dec 20, 2024
1 parent 434eef0 commit 9c3f1e3
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 6 deletions.
24 changes: 19 additions & 5 deletions build/controllers/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,26 @@ async function processWebhook(
}
return `Ignoring ${request.payload.action} action`;
} else if (eventName === 'check_suite') {
if (request.payload.action === 'completed' && request.payload.check_suite.conclusion !== 'success') {
if (request.payload.action === 'completed') {
const repositoryName = request.payload.repository.full_name;
await pullRequestRepository.remove({
number: request.payload.pull_requests[0].number,
repositoryName,
});
if (request.payload.check_suite.conclusion !== 'success') {
await pullRequestRepository.remove({
number: request.payload.pull_requests[0].number,
repositoryName,
});
} else {
const hasReadyToMergeLabel = await githubService.isPrLabelledWith({
repositoryName,
number: request.payload.pull_requests[0].number,
label: ':rocket: Ready to Merge',
});
if (hasReadyToMergeLabel) {
await pullRequestRepository.save({
number: request.payload.pull_requests[0].number,
repositoryName,
});
}
}
await mergeQueue({ repositoryName });
}
} else {
Expand Down
6 changes: 6 additions & 0 deletions common/services/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,12 @@ const github = {
inputs,
});
},

async isPrLabelledWith({ number, repositoryName, label }) {
const octokit = _createOctokit();
const { data } = await octokit.request(`GET /repos/${repositoryName}/pulls/${number}`);
return data.labels.some((ghLabel) => ghLabel.name === label);
},
};

export default github;
42 changes: 41 additions & 1 deletion test/unit/build/controllers/github_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ Les variables d'environnement seront accessibles sur scalingo https://dashboard.

describe('when event is check_suite', function () {
describe("when action is 'completed' and conclusion is not 'success'", function () {
it('should call pullRequestRepository.remove() method', async function () {
it('should call pullRequestRepository.update() method', async function () {
const repositoryName = '1024pix/pix-sample-repo';
const request = {
headers: {
Expand Down Expand Up @@ -342,6 +342,46 @@ Les variables d'environnement seront accessibles sur scalingo https://dashboard.
expect(mergeQueue).to.be.calledOnceWithExactly({ repositoryName });
});
});

describe("when action is 'completed' and conclusion is'success'", function () {
it('should check PR as Ready to Merge label before saved it', async function () {
const repositoryName = '1024pix/pix-sample-repo';
const prNumber = 123;
const request = {
headers: {
'x-github-event': 'check_suite',
},
payload: {
action: 'completed',
pull_requests: [{ number: prNumber }],
repository: { full_name: repositoryName },
check_suite: { conclusion: 'success' },
},
};

const mergeQueue = sinon.stub();
const pullRequestRepository = { save: sinon.stub() };
const githubService = { isPrLabelledWith: sinon.stub() };

githubService.isPrLabelledWith
.resolves(false)
.withArgs({
repositoryName,
number: prNumber,
label: ':rocket: Ready to Merge',
})
.resolves(true);
// when
await githubController.processWebhook(request, { githubService, pullRequestRepository, mergeQueue });

// then
expect(pullRequestRepository.save).to.be.calledOnceWithExactly({
number: prNumber,
repositoryName,
});
expect(mergeQueue).to.be.calledOnceWithExactly({ repositoryName });
});
});
});
});
});
Expand Down
60 changes: 60 additions & 0 deletions test/unit/build/services/github_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -691,4 +691,64 @@ describe('Unit | Build | github-test', function () {
expect(githubNock.isDone()).to.be.true;
});
});

describe('#isPrLabelledWith', function () {
let githubNock;
const repositoryName = '1024pix/pix';
const prNumber = 123;

beforeEach(function () {
const response = {
labels: [
{
id: 123,
node_id: 'MDU6TGFiZWwyMDgwNDU5NDY=',
url: 'https://api.github.com/repos/octocat/Hello-World/labels/%3Apanda%3A label',
name: ':panda: label',
description: 'Something is like a panda',
color: 'FFFFFF',
default: true,
},
{
id: 123,
node_id: 'MDU6TGFiZWwyMDgwNDU5NDY=',
url: 'https://api.github.com/repos/octocat/Hello-World/labels/working',
name: 'working',
description: 'Something is working',
color: 'f29efef',
default: true,
},
],
};
githubNock = nock('https://api.github.com')
.get(`/repos/${repositoryName}/pulls/${prNumber}`)
.reply(200, response);
});

describe('when PR is labelled with `:panda: label`', function () {
it('should return true for :panda: label', async function () {
const label = ':panda: label';

const hasPandaLabel = await githubService.isPrLabelledWith({
number: prNumber,
repositoryName,
label,
});
expect(hasPandaLabel).to.be.true;
expect(githubNock.isDone()).to.be.true;
});

it('should return false for bug', async function () {
const label = 'bug';

const hasBugLabel = await githubService.isPrLabelledWith({
number: prNumber,
repositoryName,
label,
});
expect(hasBugLabel).to.be.false;
expect(githubNock.isDone()).to.be.true;
});
});
});
});

0 comments on commit 9c3f1e3

Please sign in to comment.