Skip to content

Commit

Permalink
refactor(github): unify all pull_request event with same intention
Browse files Browse the repository at this point in the history
Co-authored-by: Guillaume Lagorce <guillaume.lagorce@pix.fr>
  • Loading branch information
VincentHardouin and HEYGUL committed Sep 24, 2024
1 parent 98b4a28 commit 8d5f701
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 219 deletions.
60 changes: 10 additions & 50 deletions build/controllers/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,42 +60,7 @@ const _addMessageToPullRequest = async ({ repositoryName, pullRequestId, scaling
});
};

async function _pullRequestOpenedWebhook(
request,
scalingoClient = ScalingoClient,
addMessageToPullRequest = _addMessageToPullRequest,
githubService = commonGithubService,
) {
const payload = request.payload;
const prId = payload.number;
const ref = payload.pull_request.head.ref;
const repository = payload.pull_request.head.repo.name;
const reviewApps = repositoryToScalingoAppsReview[repository];

const { shouldContinue, message } = _handleNoRACase(request);
if (!shouldContinue) {
return message;
}

const deployedRA = await deployPullRequest(
scalingoClient,
reviewApps,
prId,
ref,
repository,
addMessageToPullRequest,
githubService,
);

logger.info({
event: 'review-app',
message: `Created RA for repo ${repository} PR ${prId}`,
});

return `Created RA on app ${deployedRA.join(', ')} with pr ${prId}`;
}

async function _pullRequestSynchronizeWebhook(
async function _handleRA(
request,
scalingoClient = ScalingoClient,
addMessageToPullRequest = _addMessageToPullRequest,
Expand Down Expand Up @@ -181,6 +146,11 @@ async function deployPullRequest(
},
{ githubService },
);

logger.info({
event: 'review-app',
message: `Created RA for repo ${repository} PR ${prId}`,
});
}
logger.info({
event: 'review-app',
Expand Down Expand Up @@ -215,23 +185,14 @@ async function _pushOnDefaultBranchWebhook(request, scalingoClient = ScalingoCli

async function processWebhook(
request,
{
pushOnDefaultBranchWebhook = _pushOnDefaultBranchWebhook,
pullRequestOpenedWebhook = _pullRequestOpenedWebhook,
pullRequestSynchronizeWebhook = _pullRequestSynchronizeWebhook,
} = {},
{ pushOnDefaultBranchWebhook = _pushOnDefaultBranchWebhook, handleRA = _handleRA } = {},
) {
const eventName = request.headers['x-github-event'];
if (eventName === 'push') {
return pushOnDefaultBranchWebhook(request);
} else if (eventName === 'pull_request') {
switch (request.payload.action) {
case 'opened':
return pullRequestOpenedWebhook(request);
case 'reopened':
return pullRequestOpenedWebhook(request);
case 'synchronize':
return pullRequestSynchronizeWebhook(request);
if (['opened', 'reopened', 'synchronize'].includes(request.payload.action)) {
return handleRA(request);
}
return `Ignoring ${request.payload.action} action`;
} else {
Expand Down Expand Up @@ -267,8 +228,7 @@ export {
_addMessageToPullRequest as addMessageToPullRequest,
getMessage,
getMessageTemplate,
_handleRA as handleRA,
processWebhook,
_pullRequestOpenedWebhook as pullRequestOpenedWebhook,
_pullRequestSynchronizeWebhook as pullRequestSynchronizeWebhook,
_pushOnDefaultBranchWebhook as pushOnDefaultBranchWebhook,
};
2 changes: 1 addition & 1 deletion test/acceptance/build/github_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('Acceptance | Build | Github', function () {
});
expect(res.statusCode).to.equal(StatusCodes.OK);
expect(res.result).to.eql(
'Created RA on app pix-api-review, pix-audit-logger-review, pix-front-review with pr 2',
'Triggered deployment of RA on app pix-api-review, pix-audit-logger-review, pix-front-review with pr 2',
);
expect(scalingoAuth.isDone()).to.be.true;
expect(scalingoDeploy1.isDone()).to.be.true;
Expand Down
Loading

0 comments on commit 8d5f701

Please sign in to comment.