Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project Management Automation: Support adding milestones for fork PRs. #20058

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 2 additions & 3 deletions packages/project-management-automation/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ const automations = [
task: ifNotFork( addFirstTimeContributorLabel ),
},
{
event: 'pull_request',
action: 'closed',
event: 'push',
task: ifNotFork( addMilestone ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is ifNotFork necessary anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, why wouldn't it be?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't seem we would ever have a scenario where we would receive a push event and also not immediately bail at the first condition checking the master branch (i.e. I can't see how an error would ever occur if the ifNotFork was not here). I suppose it might still be better to stop it as early as possible to avoid tying it to this specific implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forks can also have branches named master.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we receiving push events for those?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, someone could make a PR from fork/master.

Copy link
Member

@aduth aduth Feb 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, ok. I think I may have been expecting that this condition would somehow already be verifying that the commit happened on wordpress/gutenberg's master, but I see now that's not the case.

But ifNotFork was implemented for pull request events, and I think would need to be updated to support this.

payload.pull_request.head.repo.full_name ===
payload.pull_request.base.repo.full_name

Notably, payload.pull_request doesn't exist for push events.

https://developer.github.com/v3/activity/events/types/#pushevent

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then it's better to just not use it. I've removed it. No one actually makes PRs from master anyway. We can live without milestones for those edge cases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then it's better to just not use it. I've removed it. No one actually makes PRs from master anyway. We can live without milestones for those edge cases.

I don't know how to search for specific examples to share, but I've definitely seen pulls from forked master before. That said, the combination of this and a commit message which matches the (#PR) pattern to get to the point of adding the milestone would be exceedingly rare, yes. And the result would merely be a failing action, nothing too worrisome.

We might want to revisit this in the future to make the logic more durable and to support ifNotFork usage for non-pull_request events (especially given the fact the name is generic enough to easily misinterpret that it ought be supported), but I don't think it's a blocker.

},
];
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