Skip to content

Commit

Permalink
Configure the matching between the repository and the apps
Browse files Browse the repository at this point in the history
  • Loading branch information
francois2metz committed Apr 13, 2022
1 parent 6662af4 commit c00027b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
22 changes: 18 additions & 4 deletions build/controllers/github.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
const ScalingoClient = require('../../common/services/scalingo-client');

const repositoryToScalingoAppsReview = {
pix: ['pix-front-review', 'pix-api-review'],
'pix-editor': ['pix-lcms-review'],
'pix-db-replication': ['pix-datawarehouse-integration'],
'pix-db-stats': ['pix-db-stats-review'],
'pix-site': ['pix-site-review']
};

module.exports = {
async processWebhook(request) {
const eventName = request.headers['x-github-event'];
if (eventName === 'pull_request') {
const payload = request.payload;
const repository = payload.pull_request.head.name;
const prId = payload.id;
const reviewApps = repositoryToScalingoAppsReview[repository];
if (payload.pull_request.head.repo.fork) {
return 'No RA for a fork';
}
if (payload.action !== 'opened') {
return `Ignoring ${payload.action} action`;
}
if (!reviewApps) {
return 'No RA configured for this repository';
}
const client = await ScalingoClient.getInstance('reviewApps');
const appName = payload.pull_request.head.name + '-review';
const prId = payload.id;
await client.deployReviewApp(appName, prId);
return `Created RA on app ${appName} with pr ${prId}`;
for (const appName of reviewApps) {
await client.deployReviewApp(appName, prId);
}
return `Created RA on app ${reviewApps.join(', ')} with pr ${prId}`;
} else {
return `Ignoring ${eventName} event`;
}
Expand Down
30 changes: 26 additions & 4 deletions test/acceptance/build/github_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ describe('Acceptance | Build | Github', function() {
const scalingoAuth = nock('https://auth.scalingo.com')
.post('/v1/tokens/exchange')
.reply(201);
const scalingoDeploy = nock('https://api.osc-fr1.scalingo.com')
.post('/v1/apps/pix-review/scm_repo_link/manual_review_app', {pull_request_id: 1})
const scalingoDeploy1 = nock('https://api.osc-fr1.scalingo.com')
.post('/v1/apps/pix-front-review/scm_repo_link/manual_review_app', {pull_request_id: 1})
.reply(201);
const scalingoDeploy2 = nock('https://api.osc-fr1.scalingo.com')
.post('/v1/apps/pix-api-review/scm_repo_link/manual_review_app', {pull_request_id: 1})
.reply(201);

const res = await server.inject({
method: 'POST',
url: '/github/webhook',
Expand All @@ -39,13 +43,15 @@ describe('Acceptance | Build | Github', function() {
payload: body,
});
expect(res.statusCode).to.equal(200);
expect(res.result).to.eql('Created RA on app pix-review with pr 1');
expect(res.result).to.eql('Created RA on app pix-front-review, pix-api-review with pr 1');
expect(scalingoAuth.isDone()).to.be.true;
expect(scalingoDeploy.isDone()).to.be.true;
expect(scalingoDeploy1.isDone()).to.be.true;
expect(scalingoDeploy2.isDone()).to.be.true;
});

it('responds with 200 and doesn\'t create the RA on scalingo when the PR is from a fork', async () => {
body.pull_request.head.repo.fork = true;

const res = await server.inject({
method: 'POST',
url: '/github/webhook',
Expand All @@ -59,6 +65,22 @@ describe('Acceptance | Build | Github', function() {
expect(res.result).to.eql('No RA for a fork');
});

it('responds with 200 and doesn\'t create the RA on scalingo when the PR is not from a configured repo', async () => {
body.pull_request.head.name = 'pix-repository-that-dont-exist';

const res = await server.inject({
method: 'POST',
url: '/github/webhook',
headers: {
...createGithubWebhookSignatureHeader(JSON.stringify(body)),
'x-github-event': 'pull_request'
},
payload: body,
});
expect(res.statusCode).to.equal(200);
expect(res.result).to.eql('No RA configured for this repository');
});

it('responds with 200 and do nothing for other action on pull request', async () => {
body.action = 'edited';

Expand Down

0 comments on commit c00027b

Please sign in to comment.