From a8caafc732bfef2ba436e15766d41729cf63d6c8 Mon Sep 17 00:00:00 2001 From: Zhang Yi Jiang Date: Mon, 3 Feb 2020 16:03:37 +0800 Subject: [PATCH] Look for pull request using head_sha instead of check_suites check_suites from external repos won't include PR numbers, so we have to clunkily search for the PR using the GitHub search API instead. Let's hope there are no sha1 collisions :P --- fixture/external.check_suite.completed.json | 213 ++++++++++++++++++ ...on => internal.check_suite.completed.json} | 0 src/CircleCi.ts | 1 + src/webhook.ts | 24 +- 4 files changed, 219 insertions(+), 19 deletions(-) create mode 100644 fixture/external.check_suite.completed.json rename fixture/{check_suite.completed.json => internal.check_suite.completed.json} (100%) diff --git a/fixture/external.check_suite.completed.json b/fixture/external.check_suite.completed.json new file mode 100644 index 0000000..62d581a --- /dev/null +++ b/fixture/external.check_suite.completed.json @@ -0,0 +1,213 @@ +{ + "action": "completed", + "check_suite": { + "id": 431792014, + "node_id": "MDEwOkNoZWNrU3VpdGU0MzE3OTIwMTQ=", + "head_branch": null, + "head_sha": "d1ab23dc02b64fb02656464a4e6d0aa840f9d940", + "status": "completed", + "conclusion": "success", + "url": "https://api.github.com/repos/nusmodifications/nusmods/check-suites/431792014", + "before": null, + "after": null, + "pull_requests": [ + + ], + "app": { + "id": 18001, + "slug": "circleci-checks", + "node_id": "MDM6QXBwMTgwMDE=", + "owner": { + "login": "circleci", + "id": 1231870, + "node_id": "MDEyOk9yZ2FuaXphdGlvbjEyMzE4NzA=", + "avatar_url": "https://avatars2.githubusercontent.com/u/1231870?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/circleci", + "html_url": "https://github.com/circleci", + "followers_url": "https://api.github.com/users/circleci/followers", + "following_url": "https://api.github.com/users/circleci/following{/other_user}", + "gists_url": "https://api.github.com/users/circleci/gists{/gist_id}", + "starred_url": "https://api.github.com/users/circleci/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/circleci/subscriptions", + "organizations_url": "https://api.github.com/users/circleci/orgs", + "repos_url": "https://api.github.com/users/circleci/repos", + "events_url": "https://api.github.com/users/circleci/events{/privacy}", + "received_events_url": "https://api.github.com/users/circleci/received_events", + "type": "Organization", + "site_admin": false + }, + "name": "CircleCI Checks", + "description": "See the status of your CircleCI workflows under the Pull Request Checks tab in the GitHub UI. With checks enabled, users will now be able to go to the Checks tab in GitHub to view the full hierarchy and status of CircleCI builds.\r\n\r\n*Note* - You cannot enable GitHub Checks for CircleCI from the GitHub UI, you must log into CircleCI and enable checks for your organization. You must have admin privileges for your organization to enable checks.", + "external_url": "https://circleci.com", + "html_url": "https://github.com/apps/circleci-checks", + "created_at": "2018-09-24T21:20:05Z", + "updated_at": "2018-10-17T15:49:07Z", + "permissions": { + "checks": "write", + "metadata": "read", + "single_file": "read" + }, + "events": [ + "check_run", + "check_suite" + ] + }, + "created_at": "2020-01-31T20:20:05Z", + "updated_at": "2020-01-31T21:21:11Z", + "latest_check_runs_count": 1, + "check_runs_url": "https://api.github.com/repos/nusmodifications/nusmods/check-suites/431792014/check-runs", + "head_commit": { + "id": "d1ab23dc02b64fb02656464a4e6d0aa840f9d940", + "tree_id": "5f00b58dd1b20d20d19c1907d1f622c21fc9db64", + "message": "update venues", + "timestamp": "2020-01-31T20:09:45Z", + "author": { + "name": "shimeilee", + "email": "30607394+shimeilee@users.noreply.github.com" + }, + "committer": { + "name": "shimeilee", + "email": "30607394+shimeilee@users.noreply.github.com" + } + } + }, + "repository": { + "id": 5481861, + "node_id": "MDEwOlJlcG9zaXRvcnk1NDgxODYx", + "name": "nusmods", + "full_name": "nusmodifications/nusmods", + "private": false, + "owner": { + "login": "nusmodifications", + "id": 7428865, + "node_id": "MDEyOk9yZ2FuaXphdGlvbjc0Mjg4NjU=", + "avatar_url": "https://avatars3.githubusercontent.com/u/7428865?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/nusmodifications", + "html_url": "https://github.com/nusmodifications", + "followers_url": "https://api.github.com/users/nusmodifications/followers", + "following_url": "https://api.github.com/users/nusmodifications/following{/other_user}", + "gists_url": "https://api.github.com/users/nusmodifications/gists{/gist_id}", + "starred_url": "https://api.github.com/users/nusmodifications/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/nusmodifications/subscriptions", + "organizations_url": "https://api.github.com/users/nusmodifications/orgs", + "repos_url": "https://api.github.com/users/nusmodifications/repos", + "events_url": "https://api.github.com/users/nusmodifications/events{/privacy}", + "received_events_url": "https://api.github.com/users/nusmodifications/received_events", + "type": "Organization", + "site_admin": false + }, + "html_url": "https://github.com/nusmodifications/nusmods", + "description": "🏫 Official course planning platform for National University of Singapore.", + "fork": false, + "url": "https://api.github.com/repos/nusmodifications/nusmods", + "forks_url": "https://api.github.com/repos/nusmodifications/nusmods/forks", + "keys_url": "https://api.github.com/repos/nusmodifications/nusmods/keys{/key_id}", + "collaborators_url": "https://api.github.com/repos/nusmodifications/nusmods/collaborators{/collaborator}", + "teams_url": "https://api.github.com/repos/nusmodifications/nusmods/teams", + "hooks_url": "https://api.github.com/repos/nusmodifications/nusmods/hooks", + "issue_events_url": "https://api.github.com/repos/nusmodifications/nusmods/issues/events{/number}", + "events_url": "https://api.github.com/repos/nusmodifications/nusmods/events", + "assignees_url": "https://api.github.com/repos/nusmodifications/nusmods/assignees{/user}", + "branches_url": "https://api.github.com/repos/nusmodifications/nusmods/branches{/branch}", + "tags_url": "https://api.github.com/repos/nusmodifications/nusmods/tags", + "blobs_url": "https://api.github.com/repos/nusmodifications/nusmods/git/blobs{/sha}", + "git_tags_url": "https://api.github.com/repos/nusmodifications/nusmods/git/tags{/sha}", + "git_refs_url": "https://api.github.com/repos/nusmodifications/nusmods/git/refs{/sha}", + "trees_url": "https://api.github.com/repos/nusmodifications/nusmods/git/trees{/sha}", + "statuses_url": "https://api.github.com/repos/nusmodifications/nusmods/statuses/{sha}", + "languages_url": "https://api.github.com/repos/nusmodifications/nusmods/languages", + "stargazers_url": "https://api.github.com/repos/nusmodifications/nusmods/stargazers", + "contributors_url": "https://api.github.com/repos/nusmodifications/nusmods/contributors", + "subscribers_url": "https://api.github.com/repos/nusmodifications/nusmods/subscribers", + "subscription_url": "https://api.github.com/repos/nusmodifications/nusmods/subscription", + "commits_url": "https://api.github.com/repos/nusmodifications/nusmods/commits{/sha}", + "git_commits_url": "https://api.github.com/repos/nusmodifications/nusmods/git/commits{/sha}", + "comments_url": "https://api.github.com/repos/nusmodifications/nusmods/comments{/number}", + "issue_comment_url": "https://api.github.com/repos/nusmodifications/nusmods/issues/comments{/number}", + "contents_url": "https://api.github.com/repos/nusmodifications/nusmods/contents/{+path}", + "compare_url": "https://api.github.com/repos/nusmodifications/nusmods/compare/{base}...{head}", + "merges_url": "https://api.github.com/repos/nusmodifications/nusmods/merges", + "archive_url": "https://api.github.com/repos/nusmodifications/nusmods/{archive_format}{/ref}", + "downloads_url": "https://api.github.com/repos/nusmodifications/nusmods/downloads", + "issues_url": "https://api.github.com/repos/nusmodifications/nusmods/issues{/number}", + "pulls_url": "https://api.github.com/repos/nusmodifications/nusmods/pulls{/number}", + "milestones_url": "https://api.github.com/repos/nusmodifications/nusmods/milestones{/number}", + "notifications_url": "https://api.github.com/repos/nusmodifications/nusmods/notifications{?since,all,participating}", + "labels_url": "https://api.github.com/repos/nusmodifications/nusmods/labels{/name}", + "releases_url": "https://api.github.com/repos/nusmodifications/nusmods/releases{/id}", + "deployments_url": "https://api.github.com/repos/nusmodifications/nusmods/deployments", + "created_at": "2012-08-20T13:58:35Z", + "updated_at": "2020-01-31T20:12:57Z", + "pushed_at": "2020-01-31T20:20:04Z", + "git_url": "git://github.com/nusmodifications/nusmods.git", + "ssh_url": "git@github.com:nusmodifications/nusmods.git", + "clone_url": "https://github.com/nusmodifications/nusmods.git", + "svn_url": "https://github.com/nusmodifications/nusmods", + "homepage": "https://nusmods.com", + "size": 28802, + "stargazers_count": 304, + "watchers_count": 304, + "language": "TypeScript", + "has_issues": true, + "has_projects": true, + "has_downloads": true, + "has_wiki": false, + "has_pages": false, + "forks_count": 168, + "mirror_url": null, + "archived": false, + "disabled": false, + "open_issues_count": 122, + "license": { + "key": "mit", + "name": "MIT License", + "spdx_id": "MIT", + "url": "https://api.github.com/licenses/mit", + "node_id": "MDc6TGljZW5zZTEz" + }, + "forks": 168, + "open_issues": 122, + "watchers": 304, + "default_branch": "master" + }, + "organization": { + "login": "nusmodifications", + "id": 7428865, + "node_id": "MDEyOk9yZ2FuaXphdGlvbjc0Mjg4NjU=", + "url": "https://api.github.com/orgs/nusmodifications", + "repos_url": "https://api.github.com/orgs/nusmodifications/repos", + "events_url": "https://api.github.com/orgs/nusmodifications/events", + "hooks_url": "https://api.github.com/orgs/nusmodifications/hooks", + "issues_url": "https://api.github.com/orgs/nusmodifications/issues", + "members_url": "https://api.github.com/orgs/nusmodifications/members{/member}", + "public_members_url": "https://api.github.com/orgs/nusmodifications/public_members{/member}", + "avatar_url": "https://avatars3.githubusercontent.com/u/7428865?v=4", + "description": "NUSModifications enhances the quality of students' lives in National University of Singapore through technology." + }, + "sender": { + "login": "ghost", + "id": 10137, + "node_id": "MDQ6VXNlcjEwMTM3", + "avatar_url": "https://avatars3.githubusercontent.com/u/10137?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/ghost", + "html_url": "https://github.com/ghost", + "followers_url": "https://api.github.com/users/ghost/followers", + "following_url": "https://api.github.com/users/ghost/following{/other_user}", + "gists_url": "https://api.github.com/users/ghost/gists{/gist_id}", + "starred_url": "https://api.github.com/users/ghost/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/ghost/subscriptions", + "organizations_url": "https://api.github.com/users/ghost/orgs", + "repos_url": "https://api.github.com/users/ghost/repos", + "events_url": "https://api.github.com/users/ghost/events{/privacy}", + "received_events_url": "https://api.github.com/users/ghost/received_events", + "type": "User", + "site_admin": false + }, + "installation": { + "id": 4959151, + "node_id": "MDIzOkludGVncmF0aW9uSW5zdGFsbGF0aW9uNDk1OTE1MQ==" + } + } \ No newline at end of file diff --git a/fixture/check_suite.completed.json b/fixture/internal.check_suite.completed.json similarity index 100% rename from fixture/check_suite.completed.json rename to fixture/internal.check_suite.completed.json diff --git a/src/CircleCi.ts b/src/CircleCi.ts index b051eab..111d9e0 100644 --- a/src/CircleCi.ts +++ b/src/CircleCi.ts @@ -82,6 +82,7 @@ export default class CircleCi { params: { shallow: true, filter: 'completed', + limit: 100, }, }, ); diff --git a/src/webhook.ts b/src/webhook.ts index 481333b..860bf63 100644 --- a/src/webhook.ts +++ b/src/webhook.ts @@ -82,25 +82,11 @@ export = (app: Application) => { // to go and look for it in all check runs let pulls = check_suite.pull_requests.map(pull => pull.number); if (pulls.length === 0) { - const checkRuns = asyncFlatMap( - (response: Octokit.Response) => - response.data.check_runs, - context.github.paginate.iterator({ - url: `/repos/${GITHUB_REPO}/commits/${check_suite.head_branch}/check-suites`, - headers: { - accept: 'application/vnd.github.antiope-preview+json', - }, - }), - ); - - const checkSuite = await asyncFind( - check => check.pull_requests.length > 0, - checkRuns, - ); - - if (checkSuite != null) { - pulls = check_suite.pull_requests.map(pull => pull.number); - } + const searchResult = await context.github.search.issuesAndPullRequests({ + q: `repo:${GITHUB_REPO} type:pr is:open ${check_suite.head_sha}`, + }); + + pulls = searchResult.data.items.map(item => item.number); } // Only continue if there's a pull request we can comment on