Skip to content

Commit

Permalink
Project Management Automation: Support adding milestones for fork PRs. (
Browse files Browse the repository at this point in the history
#20058)

* Project Management Automation: Support adding milestones for fork PRs.

* Github: Remove unused workflow event.

* Update packages/project-management-automation/lib/add-milestone.js

Co-Authored-By: Andrew Duthie <[email protected]>

* Project Management Automation: Update tests.

* Project Management Automation: Remove `ifNotFork` from milestone handler.

Co-authored-by: Andrew Duthie <[email protected]>
  • Loading branch information
epiqueras and aduth authored Feb 6, 2020
1 parent c5d9b83 commit 164d583
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 75 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/pull-request-automation.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
on:
pull_request:
types: [opened, closed]
types: [opened]
push:
name: Pull request automation

jobs:
Expand Down
19 changes: 9 additions & 10 deletions packages/project-management-automation/lib/add-milestone.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,18 @@ const isDuplicateValidationError = ( error ) =>
/**
* Assigns the correct milestone to PRs once merged.
*
* @param {Object} payload Pull request event payload, see https://developer.github.com/v3/activity/events/types/#pullrequestevent.
* @param {Object} payload Push event payload, see https://developer.github.com/v3/activity/events/types/#pushevent.
* @param {Object} octokit Initialized Octokit REST client, see https://octokit.github.io/rest.js/.
*/
async function addMilestone( payload, octokit ) {
if ( ! payload.pull_request.merged ) {
debug( 'add-milestone: Pull request is not merged. Aborting' );
if ( payload.ref !== 'refs/heads/master' ) {
debug( 'add-milestone: Commit is not to `master`. Aborting' );
return;
}

if ( payload.pull_request.base.ref !== 'master' ) {
debug(
'add-milestone: Pull request is not based on `master`. Aborting'
);
const [ , prNumber ] = payload.commits[ 0 ].message.match( /\(#(\d+)\)$/m );
if ( ! prNumber ) {
debug( 'add-milestone: Commit is not a squashed PR. Aborting' );
return;
}

Expand All @@ -55,7 +54,7 @@ async function addMilestone( payload, octokit ) {
} = await octokit.issues.get( {
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: payload.pull_request.number,
issue_number: prNumber,
} );

if ( milestone ) {
Expand Down Expand Up @@ -133,13 +132,13 @@ async function addMilestone( payload, octokit ) {
);

debug(
`add-milestone: Adding issue #${ payload.pull_request.number } to milestone #${ number }`
`add-milestone: Adding issue #${ prNumber } to milestone #${ number }`
);

await octokit.issues.update( {
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: payload.pull_request.number,
issue_number: prNumber,
milestone: number,
} );
}
Expand Down
7 changes: 3 additions & 4 deletions packages/project-management-automation/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ const automations = [
task: ifNotFork( addFirstTimeContributorLabel ),
},
{
event: 'pull_request',
action: 'closed',
task: ifNotFork( addMilestone ),
event: 'push',
task: addMilestone,
},
];

Expand All @@ -47,7 +46,7 @@ const automations = [
for ( const { event, action, task } of automations ) {
if (
event === context.eventName &&
action === context.payload.action
( action === undefined || action === context.payload.action )
) {
try {
debug( `main: Starting task ${ task.name }` );
Expand Down
73 changes: 13 additions & 60 deletions packages/project-management-automation/lib/test/add-milestone.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,10 @@
*/
import addMilestone from '../add-milestone';

describe( 'addFirstTimeContributorLabel', () => {
it( 'does nothing if PR is not merged', async () => {
const payload = {
pull_request: {
merged: false,
},
};
const octokit = {
issues: {
get: jest.fn(),
createMilestone: jest.fn(),
listMilestonesForRepo: jest.fn(),
update: jest.fn(),
},
repos: {
getContents: jest.fn(),
},
};

await addMilestone( payload, octokit );

expect( octokit.issues.get ).not.toHaveBeenCalled();
expect( octokit.issues.createMilestone ).not.toHaveBeenCalled();
expect( octokit.issues.listMilestonesForRepo ).not.toHaveBeenCalled();
expect( octokit.issues.update ).not.toHaveBeenCalled();
expect( octokit.repos.getContents ).not.toHaveBeenCalled();
} );

describe( 'addMilestone', () => {
it( 'does nothing if base is not master', async () => {
const payload = {
pull_request: {
merged: true,
base: {
ref: 'release/5.0',
},
},
ref: 'refs/heads/not-master',
};
const octokit = {
issues: {
Expand All @@ -63,13 +31,8 @@ describe( 'addFirstTimeContributorLabel', () => {

it( 'does nothing if PR already has a milestone', async () => {
const payload = {
pull_request: {
merged: true,
base: {
ref: 'master',
},
number: 123,
},
ref: 'refs/heads/master',
commits: [ { message: '(#123)' } ],
repository: {
owner: {
login: 'WordPress',
Expand Down Expand Up @@ -100,7 +63,7 @@ describe( 'addFirstTimeContributorLabel', () => {
expect( octokit.issues.get ).toHaveBeenCalledWith( {
owner: 'WordPress',
repo: 'gutenberg',
issue_number: 123,
issue_number: '123',
} );
expect( octokit.issues.createMilestone ).not.toHaveBeenCalled();
expect( octokit.issues.listMilestonesForRepo ).not.toHaveBeenCalled();
Expand All @@ -110,13 +73,8 @@ describe( 'addFirstTimeContributorLabel', () => {

it( 'correctly milestones PRs when `package.json` has a `*.[1-8]` version', async () => {
const payload = {
pull_request: {
merged: true,
base: {
ref: 'master',
},
number: 123,
},
ref: 'refs/heads/master',
commits: [ { message: '(#123)' } ],
repository: {
owner: {
login: 'WordPress',
Expand Down Expand Up @@ -166,7 +124,7 @@ describe( 'addFirstTimeContributorLabel', () => {
expect( octokit.issues.get ).toHaveBeenCalledWith( {
owner: 'WordPress',
repo: 'gutenberg',
issue_number: 123,
issue_number: '123',
} );
expect( octokit.repos.getContents ).toHaveBeenCalledWith( {
owner: 'WordPress',
Expand All @@ -186,20 +144,15 @@ describe( 'addFirstTimeContributorLabel', () => {
expect( octokit.issues.update ).toHaveBeenCalledWith( {
owner: 'WordPress',
repo: 'gutenberg',
issue_number: 123,
issue_number: '123',
milestone: 12,
} );
} );

it( 'correctly milestones PRs when `package.json` has a `*.9` version', async () => {
const payload = {
pull_request: {
merged: true,
base: {
ref: 'master',
},
number: 123,
},
ref: 'refs/heads/master',
commits: [ { message: '(#123)' } ],
repository: {
owner: {
login: 'WordPress',
Expand Down Expand Up @@ -249,7 +202,7 @@ describe( 'addFirstTimeContributorLabel', () => {
expect( octokit.issues.get ).toHaveBeenCalledWith( {
owner: 'WordPress',
repo: 'gutenberg',
issue_number: 123,
issue_number: '123',
} );
expect( octokit.repos.getContents ).toHaveBeenCalledWith( {
owner: 'WordPress',
Expand All @@ -269,7 +222,7 @@ describe( 'addFirstTimeContributorLabel', () => {
expect( octokit.issues.update ).toHaveBeenCalledWith( {
owner: 'WordPress',
repo: 'gutenberg',
issue_number: 123,
issue_number: '123',
milestone: 12,
} );
} );
Expand Down

0 comments on commit 164d583

Please sign in to comment.