Skip to content

Commit

Permalink
bugfix: Only respond to primary 'open/reopen' event on PR creation (d…
Browse files Browse the repository at this point in the history
  • Loading branch information
dabrady authored Apr 6, 2022
1 parent d85d687 commit 164ea63
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 36 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
testEnvironment: 'node',
roots: ['<rootDir>/src/', '<rootDir>/test/'],
transform: {
'^.+\\.tsx?$': 'ts-jest'
Expand Down
23 changes: 18 additions & 5 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,5 @@
"/lib/*"
]
},
"jest": {
"testEnvironment": "node"
},
"heroku-run-build-script": true
}
22 changes: 18 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,30 @@ import { PullRequestEvent, PullRequestReviewEvent } from '@octokit/webhooks-type

module.exports = (app: Probot) => {
app.on(['pull_request.opened', 'pull_request.reopened', 'pull_request.labeled', 'pull_request.edited', 'pull_request_review'], async (context) => {
// reading configuration
const config: any = await context.config('autoapproval.yml')
context.log(config, '\n\nLoaded config')
context.log('Repo: %s', context.payload.repository.full_name)

const pr = context.payload.pull_request
context.log('PR: %s', pr.html_url)
const prLabels: string[] = pr.labels.map((label: any) => label.name)
context.log('Action: %s', context.payload.action)

// NOTE(dabrady) When a PR is first opened, it can fire several different kinds of events if the author e.g. requests
// reviewers or adds labels during creation. This triggers parallel runs of our GitHub App, so we need to filter out
// those simultaneous events and focus just on the re/open event in this scenario.
//
// These simultaneous events contain the same pull request data in their payloads, and specify the 'updated at'
// timestamp to be the same as the 'created at' timestamp for the pull request. We can use this to distinguish events
// that are fired during creation from events fired later on.
if (!['opened', 'reopened'].includes(context.payload.action) && pr.created_at === pr.updated_at) {
context.log('Ignoring additional creation event: %s', context.payload.action)
return
}

// reading configuration
const config: any = await context.config('autoapproval.yml')
context.log(config, '\n\nLoaded config')

// determine if the PR has any "blacklisted" labels
const prLabels: string[] = pr.labels.map((label: any) => label.name)
let blacklistedLabels: string[] = []
if (config.blacklisted_labels !== undefined) {
blacklistedLabels = config.blacklisted_labels
Expand Down
29 changes: 29 additions & 0 deletions test/fixtures/pull_request.labeled.on_open.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"action": "labeled",
"pull_request": {
"node_id": "MDExOlB1bGxSZXF1ZN0NjExMzU2MTgy",
"number": 1,
"html_url": "https://github.com/dkhmelenko/autoapproval/pull/1",
"created_at": "2022-03-30T15:17:37.000+02:00",
"updated_at": "2022-03-30T15:17:37.000+02:00",
"user": {
"login": "dkhmelenko"
},
"labels": [
{
"id": 2222,
"node_id": "XDU6TGFiZWw5NDk3Mzc1MDU=",
"name": "cool label",
"color": "d73a4a",
"default": true
}
]
},
"repository": {
"name": "autoapproval",
"full_name": "dkhmelenko/autoapproval",
"owner": {
"login": "dkhmelenko"
}
}
}
2 changes: 2 additions & 0 deletions test/fixtures/pull_request.opened.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"node_id": "MDExOlB1bGxSZXF1ZN0NjExMzU2MTgy",
"number": 1,
"html_url": "https://github.com/dkhmelenko/autoapproval/pull/1",
"created_at": "2022-03-30T15:17:37.000+02:00",
"updated_at": "2022-03-30T15:17:37.000+02:00",
"user": {
"login": "dkhmelenko"
},
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/pull_request_opened_multiple_labels.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"node_id": "MDExOlB1bGxSZXF1ZN0NjExMzU2MTgy",
"number": 1,
"html_url": "https://github.com/dkhmelenko/autoapproval/pull/1",
"created_at": "2022-03-30T15:17:37.000+02:00",
"updated_at": "2022-03-30T15:17:37.000+02:00",
"user": {
"login": "dkhmelenko"
},
Expand Down
48 changes: 25 additions & 23 deletions test/fixtures/pull_request_review.dismissed.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
{
"action": "dismissed",
"pull_request": {
"number": 1,
"html_url": "https://github.com/dkhmelenko/autoapproval/pull/1",
"user": {
"login": "dkhmelenko"
},
"labels": [
{
"id": 2222,
"node_id": "XDU6TGFiZWw5NDk3Mzc1MDU=",
"name": "wip",
"color": "d73a4a",
"default": true
}
]
"action": "dismissed",
"pull_request": {
"number": 1,
"html_url": "https://github.com/dkhmelenko/autoapproval/pull/1",
"created_at": "2022-03-30T15:17:37.000+02:00",
"updated_at": "2022-03-30T21:03:14.000+02:00",
"user": {
"login": "dkhmelenko"
},
"repository": {
"name": "autoapproval",
"full_name": "dkhmelenko/autoapproval",
"owner": {
"login": "dkhmelenko"
}
"labels": [
{
"id": 2222,
"node_id": "XDU6TGFiZWw5NDk3Mzc1MDU=",
"name": "wip",
"color": "d73a4a",
"default": true
}
}
]
},
"repository": {
"name": "autoapproval",
"full_name": "dkhmelenko/autoapproval",
"owner": {
"login": "dkhmelenko"
}
}
}
Loading

0 comments on commit 164ea63

Please sign in to comment.