Skip to content

Commit

Permalink
Update squash commit message to include all PR commit authors
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdothtml authored and fusion-bot[bot] committed Jan 25, 2019
1 parent f77754c commit 0ee8afc
Show file tree
Hide file tree
Showing 8 changed files with 5,232 additions and 8 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 Uber Technologies, Inc.
Copyright (c) 2019 Uber Technologies, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
27 changes: 27 additions & 0 deletions __tests__/__fixtures__/issue_comment.created.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// webhook: https://developer.github.com/v3/activity/events/types/#webhook-payload-example-13
// webhook.issue (pull request): https://developer.github.com/v3/issues/#response
module.exports = {
action: 'created',
issue: {
html_url: 'https://github.com/fusionjs/test-repo/pull/1',
number: 1,
pull_request: {
url: 'https://api.github.com/repos/fusionjs/test-repo/pulls/1',
},
state: 'open',
title: 'Test PR',
},
comment: {
body: '!merge',
user: {
login: 'test-user',
type: 'User',
},
},
repository: {
name: 'test-repo',
owner: {
login: 'fusionjs',
},
},
};
27 changes: 27 additions & 0 deletions __tests__/__fixtures__/listCommits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// https://developer.github.com/v3/pulls/#list-commits-on-a-pull-request
module.exports = [
{
commit: {
author: {
name: 'test-user',
email: '[email protected]',
},
},
},
{
commit: {
author: {
name: 'test-user2',
email: '[email protected]',
},
},
},
{
commit: {
author: {
name: 'test-user3',
email: '[email protected]',
},
},
},
];
4 changes: 4 additions & 0 deletions __tests__/__fixtures__/reviewUserPermissionLevel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level
module.exports = {
permission: 'admin',
};
53 changes: 49 additions & 4 deletions __tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,58 @@
/** Copyright (c) 2017 Uber Technologies, Inc.
/** Copyright (c) 2019 Uber Technologies, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/* eslint-env jest */

const t = require('assert');
const nock = require('nock');
const mergePRApp = require('../index.js');
const {Probot} = require('probot');
const fixtures = {
listCommits: require('./__fixtures__/listCommits.js'),
payload: require('./__fixtures__/issue_comment.created.js'),
reviewUserPermissionLevel: require('./__fixtures__/reviewUserPermissionLevel.js'),
};

test('dummy test', () => {
t.ok(true, 'dummy assertion passed');
nock.disableNetConnect();

describe('probot-app-merge-pr', () => {
let probot;

beforeEach(() => {
probot = new Probot({});
const app = probot.load(mergePRApp);

// just return a test token
app.app = () => 'test';
});

test('merges the PR with commit authors as co-authors', async () => {
nock('https://api.github.com')
.get('/repos/fusionjs/test-repo/collaborators/test-user/permission')
.reply(200, fixtures.reviewUserPermissionLevel)
.get('/repos/fusionjs/test-repo/contents/.github/merge-pr.yml')
.reply(404)
.get('/repos/fusionjs/test-repo/pulls/1/commits')
.reply(200, fixtures.listCommits);

const mergeRequest = new Promise(resolve => {
nock('https://api.github.com')
.put('/repos/fusionjs/test-repo/pulls/1/merge', resolve)
.reply(200);
});

const expectedMessage = `https://github.com/fusionjs/test-repo/pull/1
Co-authored-by: test-user2 <[email protected]>
Co-authored-by: test-user3 <[email protected]>`;

await probot.receive({name: 'issue_comment', payload: fixtures.payload});
expect(await mergeRequest).toEqual({
commit_title: 'Test PR',
commit_message: expectedMessage,
merge_method: 'squash',
});
});
});
38 changes: 36 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** Copyright (c) 2017 Uber Technologies, Inc.
/** Copyright (c) 2019 Uber Technologies, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
Expand Down Expand Up @@ -38,11 +38,45 @@ module.exports = robot => {
}

try {
let commit_message = issue.html_url;

// add all PR commiters as co-authors
// https://help.github.com/articles/creating-a-commit-with-multiple-authors/
if (merge_method === 'squash') {
const prNumber = pull_request.url.split('/').slice(-1)[0];
const commitList = await github.pullRequests.listCommits(
context.repo({
number: prNumber,
}),
);
const authorTrailers = commitList.data.reduce((result, item) => {
const {
commit: {
author: {email, name},
},
} = item;

if (name !== user.login) {
const trailer = `Co-authored-by: ${name} <${email}>`;

if (!result.includes(trailer)) {
result.push(trailer);
}
}

return result;
}, []);

if (authorTrailers.length) {
commit_message += '\n\n' + authorTrailers.join('\n');
}
}

await github.pullRequests.merge(
context.repo({
number: issue.number,
commit_title: issue.title,
commit_message: issue.html_url,
commit_message,
merge_method,
}),
);
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@
"eslint-plugin-prettier": "2.6.2",
"jest": "23.6.0",
"localtunnel": "1.9.1",
"nock": "^10.0.6",
"prettier": "1.15.3"
},
"jest": {
"testPathIgnorePatterns": [
"__fixtures__"
],
"testURL": "http://localhost:3000/"
},
"engines": {
"node": ">= 8.6.0"
"node": ">= 8.6.0",
"yarn": ">=1.12"
},
"license": "MIT"
}
Loading

0 comments on commit 0ee8afc

Please sign in to comment.