From 316f4e9857e4d4ef847322f1a51d01224628f2d8 Mon Sep 17 00:00:00 2001 From: Kristie Tom Date: Wed, 5 Aug 2020 17:06:17 -0400 Subject: [PATCH 1/5] feat(core-library): do not recreate pr when ref already has a pr --- src/github-handler/fork-handler.ts | 1 - src/github-handler/pull-request-handler.ts | 10 +- test/fixtures/list-pulls-response.json | 495 +++++++++++++++++++++ test/pr.ts | 135 ++++-- 4 files changed, 599 insertions(+), 42 deletions(-) create mode 100644 test/fixtures/list-pulls-response.json diff --git a/src/github-handler/fork-handler.ts b/src/github-handler/fork-handler.ts index ef8f993e..46304912 100644 --- a/src/github-handler/fork-handler.ts +++ b/src/github-handler/fork-handler.ts @@ -38,7 +38,6 @@ async function fork( }) ).data; logger.info(`Fork successfully exists on ${upstream.repo}`); - // TODO autosync return { repo: forkedRepo.name, owner: forkedRepo.owner.login, diff --git a/src/github-handler/pull-request-handler.ts b/src/github-handler/pull-request-handler.ts index 94630aad..f76ba608 100644 --- a/src/github-handler/pull-request-handler.ts +++ b/src/github-handler/pull-request-handler.ts @@ -37,7 +37,15 @@ async function openPullRequest( maintainersCanModify = true, upstreamPrimary: string = DEFAULT_PRIMARY ): Promise { - // TODO - autosync fork, update branch, and re-apply changes + const existingPullRequests = (await octokit.pulls.list({ + owner: upstream.owner, + repo: origin.repo, + head: `${origin.owner}:${origin.branch}` + })).data; + if (existingPullRequests.length) { + logger.info(`Found existing pull request for reference ${origin.owner}:${origin.branch}. Skipping creating a new pull request.`); + return; + } const pullResponseData = ( await octokit.pulls.create({ owner: upstream.owner, diff --git a/test/fixtures/list-pulls-response.json b/test/fixtures/list-pulls-response.json new file mode 100644 index 00000000..e55fccfe --- /dev/null +++ b/test/fixtures/list-pulls-response.json @@ -0,0 +1,495 @@ +[ + { + "url": "https://api.github.com/repos/octocat/Hello-World/pulls/1347", + "id": 1, + "node_id": "MDExOlB1bGxSZXF1ZXN0MQ==", + "html_url": "https://github.com/octocat/Hello-World/pull/1347", + "diff_url": "https://github.com/octocat/Hello-World/pull/1347.diff", + "patch_url": "https://github.com/octocat/Hello-World/pull/1347.patch", + "issue_url": "https://api.github.com/repos/octocat/Hello-World/issues/1347", + "commits_url": "https://api.github.com/repos/octocat/Hello-World/pulls/1347/commits", + "review_comments_url": "https://api.github.com/repos/octocat/Hello-World/pulls/1347/comments", + "review_comment_url": "https://api.github.com/repos/octocat/Hello-World/pulls/comments{/number}", + "comments_url": "https://api.github.com/repos/octocat/Hello-World/issues/1347/comments", + "statuses_url": "https://api.github.com/repos/octocat/Hello-World/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e", + "number": 1347, + "state": "open", + "locked": true, + "title": "Amazing new feature", + "user": { + "login": "octocat", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "body": "Please pull these awesome changes in!", + "labels": [ + { + "id": 208045946, + "node_id": "MDU6TGFiZWwyMDgwNDU5NDY=", + "url": "https://api.github.com/repos/octocat/Hello-World/labels/bug", + "name": "bug", + "description": "Something isn't working", + "color": "f29513", + "default": true + } + ], + "milestone": { + "url": "https://api.github.com/repos/octocat/Hello-World/milestones/1", + "html_url": "https://github.com/octocat/Hello-World/milestones/v1.0", + "labels_url": "https://api.github.com/repos/octocat/Hello-World/milestones/1/labels", + "id": 1002604, + "node_id": "MDk6TWlsZXN0b25lMTAwMjYwNA==", + "number": 1, + "state": "open", + "title": "v1.0", + "description": "Tracking milestone for version 1.0", + "creator": { + "login": "octocat", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "open_issues": 4, + "closed_issues": 8, + "created_at": "2011-04-10T20:09:31Z", + "updated_at": "2014-03-03T18:58:10Z", + "closed_at": "2013-02-12T13:22:01Z", + "due_on": "2012-10-09T23:39:01Z" + }, + "active_lock_reason": "too heated", + "created_at": "2011-01-26T19:01:12Z", + "updated_at": "2011-01-26T19:01:12Z", + "closed_at": "2011-01-26T19:01:12Z", + "merged_at": "2011-01-26T19:01:12Z", + "merge_commit_sha": "e5bd3914e2e596debea16f433f57875b5b90bcd6", + "assignee": { + "login": "octocat", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "assignees": [ + { + "login": "octocat", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + { + "login": "hubot", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/hubot_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/hubot", + "html_url": "https://github.com/hubot", + "followers_url": "https://api.github.com/users/hubot/followers", + "following_url": "https://api.github.com/users/hubot/following{/other_user}", + "gists_url": "https://api.github.com/users/hubot/gists{/gist_id}", + "starred_url": "https://api.github.com/users/hubot/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/hubot/subscriptions", + "organizations_url": "https://api.github.com/users/hubot/orgs", + "repos_url": "https://api.github.com/users/hubot/repos", + "events_url": "https://api.github.com/users/hubot/events{/privacy}", + "received_events_url": "https://api.github.com/users/hubot/received_events", + "type": "User", + "site_admin": true + } + ], + "requested_reviewers": [ + { + "login": "other_user", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/other_user_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/other_user", + "html_url": "https://github.com/other_user", + "followers_url": "https://api.github.com/users/other_user/followers", + "following_url": "https://api.github.com/users/other_user/following{/other_user}", + "gists_url": "https://api.github.com/users/other_user/gists{/gist_id}", + "starred_url": "https://api.github.com/users/other_user/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/other_user/subscriptions", + "organizations_url": "https://api.github.com/users/other_user/orgs", + "repos_url": "https://api.github.com/users/other_user/repos", + "events_url": "https://api.github.com/users/other_user/events{/privacy}", + "received_events_url": "https://api.github.com/users/other_user/received_events", + "type": "User", + "site_admin": false + } + ], + "requested_teams": [ + { + "id": 1, + "node_id": "MDQ6VGVhbTE=", + "url": "https://api.github.com/teams/1", + "html_url": "https://api.github.com/teams/justice-league", + "name": "Justice League", + "slug": "justice-league", + "description": "A great team.", + "privacy": "closed", + "permission": "admin", + "members_url": "https://api.github.com/teams/1/members{/member}", + "repositories_url": "https://api.github.com/teams/1/repos", + "parent": {} + } + ], + "head": { + "label": "octocat:new-topic", + "ref": "new-topic", + "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", + "user": { + "login": "octocat", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "repo": { + "id": 1296269, + "node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5", + "name": "Hello-World", + "full_name": "octocat/Hello-World", + "owner": { + "login": "octocat", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "private": false, + "html_url": "https://github.com/octocat/Hello-World", + "description": "This your first repo!", + "fork": false, + "url": "https://api.github.com/repos/octocat/Hello-World", + "archive_url": "http://api.github.com/repos/octocat/Hello-World/{archive_format}{/ref}", + "assignees_url": "http://api.github.com/repos/octocat/Hello-World/assignees{/user}", + "blobs_url": "http://api.github.com/repos/octocat/Hello-World/git/blobs{/sha}", + "branches_url": "http://api.github.com/repos/octocat/Hello-World/branches{/branch}", + "collaborators_url": "http://api.github.com/repos/octocat/Hello-World/collaborators{/collaborator}", + "comments_url": "http://api.github.com/repos/octocat/Hello-World/comments{/number}", + "commits_url": "http://api.github.com/repos/octocat/Hello-World/commits{/sha}", + "compare_url": "http://api.github.com/repos/octocat/Hello-World/compare/{base}...{head}", + "contents_url": "http://api.github.com/repos/octocat/Hello-World/contents/{+path}", + "contributors_url": "http://api.github.com/repos/octocat/Hello-World/contributors", + "deployments_url": "http://api.github.com/repos/octocat/Hello-World/deployments", + "downloads_url": "http://api.github.com/repos/octocat/Hello-World/downloads", + "events_url": "http://api.github.com/repos/octocat/Hello-World/events", + "forks_url": "http://api.github.com/repos/octocat/Hello-World/forks", + "git_commits_url": "http://api.github.com/repos/octocat/Hello-World/git/commits{/sha}", + "git_refs_url": "http://api.github.com/repos/octocat/Hello-World/git/refs{/sha}", + "git_tags_url": "http://api.github.com/repos/octocat/Hello-World/git/tags{/sha}", + "git_url": "git:github.com/octocat/Hello-World.git", + "issue_comment_url": "http://api.github.com/repos/octocat/Hello-World/issues/comments{/number}", + "issue_events_url": "http://api.github.com/repos/octocat/Hello-World/issues/events{/number}", + "issues_url": "http://api.github.com/repos/octocat/Hello-World/issues{/number}", + "keys_url": "http://api.github.com/repos/octocat/Hello-World/keys{/key_id}", + "labels_url": "http://api.github.com/repos/octocat/Hello-World/labels{/name}", + "languages_url": "http://api.github.com/repos/octocat/Hello-World/languages", + "merges_url": "http://api.github.com/repos/octocat/Hello-World/merges", + "milestones_url": "http://api.github.com/repos/octocat/Hello-World/milestones{/number}", + "notifications_url": "http://api.github.com/repos/octocat/Hello-World/notifications{?since,all,participating}", + "pulls_url": "http://api.github.com/repos/octocat/Hello-World/pulls{/number}", + "releases_url": "http://api.github.com/repos/octocat/Hello-World/releases{/id}", + "ssh_url": "git@github.com:octocat/Hello-World.git", + "stargazers_url": "http://api.github.com/repos/octocat/Hello-World/stargazers", + "statuses_url": "http://api.github.com/repos/octocat/Hello-World/statuses/{sha}", + "subscribers_url": "http://api.github.com/repos/octocat/Hello-World/subscribers", + "subscription_url": "http://api.github.com/repos/octocat/Hello-World/subscription", + "tags_url": "http://api.github.com/repos/octocat/Hello-World/tags", + "teams_url": "http://api.github.com/repos/octocat/Hello-World/teams", + "trees_url": "http://api.github.com/repos/octocat/Hello-World/git/trees{/sha}", + "clone_url": "https://github.com/octocat/Hello-World.git", + "mirror_url": "git:git.example.com/octocat/Hello-World", + "hooks_url": "http://api.github.com/repos/octocat/Hello-World/hooks", + "svn_url": "https://svn.github.com/octocat/Hello-World", + "homepage": "https://github.com", + "language": "null", + "forks_count": 9, + "stargazers_count": 80, + "watchers_count": 80, + "size": 108, + "default_branch": "master", + "open_issues_count": 0, + "is_template": true, + "topics": [ + "octocat", + "atom", + "electron", + "api" + ], + "has_issues": true, + "has_projects": true, + "has_wiki": true, + "has_pages": false, + "has_downloads": true, + "archived": false, + "disabled": false, + "visibility": "public", + "pushed_at": "2011-01-26T19:06:43Z", + "created_at": "2011-01-26T19:01:12Z", + "updated_at": "2011-01-26T19:14:43Z", + "permissions": { + "admin": false, + "push": false, + "pull": true + }, + "allow_rebase_merge": true, + "template_repository": {}, + "temp_clone_token": "ABTLWHOULUVAXGTRYU7OC2876QJ2O", + "allow_squash_merge": true, + "delete_branch_on_merge": true, + "allow_merge_commit": true, + "subscribers_count": 42, + "network_count": 0 + } + }, + "base": { + "label": "octocat:master", + "ref": "master", + "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", + "user": { + "login": "octocat", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "repo": { + "id": 1296269, + "node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5", + "name": "Hello-World", + "full_name": "octocat/Hello-World", + "owner": { + "login": "octocat", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "private": false, + "html_url": "https://github.com/octocat/Hello-World", + "description": "This your first repo!", + "fork": false, + "url": "https://api.github.com/repos/octocat/Hello-World", + "archive_url": "http://api.github.com/repos/octocat/Hello-World/{archive_format}{/ref}", + "assignees_url": "http://api.github.com/repos/octocat/Hello-World/assignees{/user}", + "blobs_url": "http://api.github.com/repos/octocat/Hello-World/git/blobs{/sha}", + "branches_url": "http://api.github.com/repos/octocat/Hello-World/branches{/branch}", + "collaborators_url": "http://api.github.com/repos/octocat/Hello-World/collaborators{/collaborator}", + "comments_url": "http://api.github.com/repos/octocat/Hello-World/comments{/number}", + "commits_url": "http://api.github.com/repos/octocat/Hello-World/commits{/sha}", + "compare_url": "http://api.github.com/repos/octocat/Hello-World/compare/{base}...{head}", + "contents_url": "http://api.github.com/repos/octocat/Hello-World/contents/{+path}", + "contributors_url": "http://api.github.com/repos/octocat/Hello-World/contributors", + "deployments_url": "http://api.github.com/repos/octocat/Hello-World/deployments", + "downloads_url": "http://api.github.com/repos/octocat/Hello-World/downloads", + "events_url": "http://api.github.com/repos/octocat/Hello-World/events", + "forks_url": "http://api.github.com/repos/octocat/Hello-World/forks", + "git_commits_url": "http://api.github.com/repos/octocat/Hello-World/git/commits{/sha}", + "git_refs_url": "http://api.github.com/repos/octocat/Hello-World/git/refs{/sha}", + "git_tags_url": "http://api.github.com/repos/octocat/Hello-World/git/tags{/sha}", + "git_url": "git:github.com/octocat/Hello-World.git", + "issue_comment_url": "http://api.github.com/repos/octocat/Hello-World/issues/comments{/number}", + "issue_events_url": "http://api.github.com/repos/octocat/Hello-World/issues/events{/number}", + "issues_url": "http://api.github.com/repos/octocat/Hello-World/issues{/number}", + "keys_url": "http://api.github.com/repos/octocat/Hello-World/keys{/key_id}", + "labels_url": "http://api.github.com/repos/octocat/Hello-World/labels{/name}", + "languages_url": "http://api.github.com/repos/octocat/Hello-World/languages", + "merges_url": "http://api.github.com/repos/octocat/Hello-World/merges", + "milestones_url": "http://api.github.com/repos/octocat/Hello-World/milestones{/number}", + "notifications_url": "http://api.github.com/repos/octocat/Hello-World/notifications{?since,all,participating}", + "pulls_url": "http://api.github.com/repos/octocat/Hello-World/pulls{/number}", + "releases_url": "http://api.github.com/repos/octocat/Hello-World/releases{/id}", + "ssh_url": "git@github.com:octocat/Hello-World.git", + "stargazers_url": "http://api.github.com/repos/octocat/Hello-World/stargazers", + "statuses_url": "http://api.github.com/repos/octocat/Hello-World/statuses/{sha}", + "subscribers_url": "http://api.github.com/repos/octocat/Hello-World/subscribers", + "subscription_url": "http://api.github.com/repos/octocat/Hello-World/subscription", + "tags_url": "http://api.github.com/repos/octocat/Hello-World/tags", + "teams_url": "http://api.github.com/repos/octocat/Hello-World/teams", + "trees_url": "http://api.github.com/repos/octocat/Hello-World/git/trees{/sha}", + "clone_url": "https://github.com/octocat/Hello-World.git", + "mirror_url": "git:git.example.com/octocat/Hello-World", + "hooks_url": "http://api.github.com/repos/octocat/Hello-World/hooks", + "svn_url": "https://svn.github.com/octocat/Hello-World", + "homepage": "https://github.com", + "language": "null", + "forks_count": 9, + "stargazers_count": 80, + "watchers_count": 80, + "size": 108, + "default_branch": "master", + "open_issues_count": 0, + "is_template": true, + "topics": [ + "octocat", + "atom", + "electron", + "api" + ], + "has_issues": true, + "has_projects": true, + "has_wiki": true, + "has_pages": false, + "has_downloads": true, + "archived": false, + "disabled": false, + "visibility": "public", + "pushed_at": "2011-01-26T19:06:43Z", + "created_at": "2011-01-26T19:01:12Z", + "updated_at": "2011-01-26T19:14:43Z", + "permissions": { + "admin": false, + "push": false, + "pull": true + }, + "allow_rebase_merge": true, + "template_repository": {}, + "temp_clone_token": "ABTLWHOULUVAXGTRYU7OC2876QJ2O", + "allow_squash_merge": true, + "delete_branch_on_merge": true, + "allow_merge_commit": true, + "subscribers_count": 42, + "network_count": 0 + } + }, + "_links": { + "self": { + "href": "https://api.github.com/repos/octocat/Hello-World/pulls/1347" + }, + "html": { + "href": "https://github.com/octocat/Hello-World/pull/1347" + }, + "issue": { + "href": "https://api.github.com/repos/octocat/Hello-World/issues/1347" + }, + "comments": { + "href": "https://api.github.com/repos/octocat/Hello-World/issues/1347/comments" + }, + "review_comments": { + "href": "https://api.github.com/repos/octocat/Hello-World/pulls/1347/comments" + }, + "review_comment": { + "href": "https://api.github.com/repos/octocat/Hello-World/pulls/comments{/number}" + }, + "commits": { + "href": "https://api.github.com/repos/octocat/Hello-World/pulls/1347/commits" + }, + "statuses": { + "href": "https://api.github.com/repos/octocat/Hello-World/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e" + } + }, + "author_association": "OWNER", + "draft": false + } + ] \ No newline at end of file diff --git a/test/pr.ts b/test/pr.ts index b048633b..9b068f77 100644 --- a/test/pr.ts +++ b/test/pr.ts @@ -23,60 +23,115 @@ before(() => { }); describe('Opening a pull request', async () => { + const sandbox = sinon.createSandbox(); const upstream = {owner: 'upstream-owner', repo: 'upstream-repo'}; const origin = { owner: 'origin-owner', repo: 'origin-repo', branch: 'pr-test-branch', }; - const responseData = await import('./fixtures/create-pr-response.json'); const description = { title: 'PR-TITLE', body: 'PR-DESCRIPTION', }; + afterEach(() => { + sandbox.restore(); + }); - describe('Octokit create function', () => { - it('Is called with the correct values and defaults', async () => { - const createRefResponse = { - headers: {}, - status: 200, - url: 'http://fake-url.com', - data: responseData, - }; - // setup - const stub = sinon - .stub(octokit.pulls, 'create') - .resolves(createRefResponse); - // tests - await openPullRequest(octokit, upstream, origin, description); - sinon.assert.calledOnceWithExactly(stub, { - owner: upstream.owner, - repo: origin.repo, - title: description.title, - head: 'origin-owner:pr-test-branch', - base: 'master', - body: description.body, - maintainer_can_modify: true, - }); - // restore - stub.restore(); + it('Invokes octokit pull create when there is not an existing pull request open', async () => { + // setup + const responseCreatePullData = await import('./fixtures/create-pr-response.json'); + const createPrResponse = { + headers: {}, + status: 200, + url: 'http://fake-url.com', + data: responseCreatePullData, + }; + const listPullResponse = { + headers: {}, + status: 200, + url: 'http://fake-url.com', + data: [], + }; + sandbox + .stub(octokit.pulls, 'list') + .resolves(listPullResponse); + const stub = sandbox + .stub(octokit.pulls, 'create') + .resolves(createPrResponse); + // tests + await openPullRequest(octokit, upstream, origin, description); + sandbox.assert.calledOnceWithExactly(stub, { + owner: upstream.owner, + repo: origin.repo, + title: description.title, + head: 'origin-owner:pr-test-branch', + base: 'master', + body: description.body, + maintainer_can_modify: true, }); }); - describe('Open PR function', () => { - it('Passes up the error message with a throw when octokit fails', async () => { - // setup - const errorMsg = 'Error message'; - const stub = sinon.stub(octokit.pulls, 'create').rejects(Error(errorMsg)); - try { - await openPullRequest(octokit, upstream, origin, description); - expect.fail(); - } catch (err) { - expect(err.message).to.equal(errorMsg); - } finally { - // restore - stub.restore(); - } + it('Does not invoke octokit pull create when there is an existing pull request open', async () => { + // setup + const responseListPullData = await import('./fixtures/list-pulls-response.json'); + const listPullResponse = { + headers: {}, + status: 200, + url: 'http://fake-url.com', + data: responseListPullData, + }; + + const conflictingOrigin = { + owner: 'octocat', + repo: 'Hello-World', + branch: 'new-topic', + }; + const listStub = sandbox + .stub(octokit.pulls, 'list') + .resolves(listPullResponse); + const createStub = sandbox + .stub(octokit.pulls, 'create'); + // tests + await openPullRequest(octokit, upstream, conflictingOrigin, description); + sandbox.assert.calledOnceWithExactly(listStub, { + owner: upstream.owner, + repo: conflictingOrigin.repo, + head: 'octocat:new-topic' }); + sandbox.assert.notCalled(createStub); + }); + + it('Passes up the error message with a throw when octokit list pull fails', async () => { + // setup + const errorMsg = 'Error message'; + sandbox.stub(octokit.pulls, 'list').rejects(Error(errorMsg)); + try { + await openPullRequest(octokit, upstream, origin, description); + expect.fail(); + } catch (err) { + expect(err.message).to.equal(errorMsg); + } + }); + + it('Passes up the error message with a throw when octokit create pull fails', async () => { + // setup + const listPullResponse = { + headers: {}, + status: 200, + url: 'http://fake-url.com', + data: [], + }; + sandbox + .stub(octokit.pulls, 'list') + .resolves(listPullResponse); + const errorMsg = 'Error message'; + sandbox.stub(octokit.pulls, 'create').rejects(Error(errorMsg)); + try { + await openPullRequest(octokit, upstream, origin, description); + expect.fail(); + } catch (err) { + expect(err.message).to.equal(errorMsg); + } }); }); From 772477d7b09d26c7c49aea76dc251e61b01cfd2c Mon Sep 17 00:00:00 2001 From: Kristie Tom Date: Wed, 5 Aug 2020 17:12:06 -0400 Subject: [PATCH 2/5] lint --- src/github-handler/pull-request-handler.ts | 16 ++++++++------ test/pr.ts | 25 +++++++++++----------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/github-handler/pull-request-handler.ts b/src/github-handler/pull-request-handler.ts index f76ba608..66689bbf 100644 --- a/src/github-handler/pull-request-handler.ts +++ b/src/github-handler/pull-request-handler.ts @@ -37,13 +37,17 @@ async function openPullRequest( maintainersCanModify = true, upstreamPrimary: string = DEFAULT_PRIMARY ): Promise { - const existingPullRequests = (await octokit.pulls.list({ - owner: upstream.owner, - repo: origin.repo, - head: `${origin.owner}:${origin.branch}` - })).data; + const existingPullRequests = ( + await octokit.pulls.list({ + owner: upstream.owner, + repo: origin.repo, + head: `${origin.owner}:${origin.branch}`, + }) + ).data; if (existingPullRequests.length) { - logger.info(`Found existing pull request for reference ${origin.owner}:${origin.branch}. Skipping creating a new pull request.`); + logger.info( + `Found existing pull request for reference ${origin.owner}:${origin.branch}. Skipping creating a new pull request.` + ); return; } const pullResponseData = ( diff --git a/test/pr.ts b/test/pr.ts index 9b068f77..397ae5ce 100644 --- a/test/pr.ts +++ b/test/pr.ts @@ -13,7 +13,7 @@ // limitations under the License. import {expect} from 'chai'; -import {describe, it, before} from 'mocha'; +import {describe, it, before, afterEach} from 'mocha'; import {octokit, setup} from './util'; import * as sinon from 'sinon'; import {openPullRequest} from '../src/github-handler/pull-request-handler'; @@ -40,7 +40,9 @@ describe('Opening a pull request', async () => { it('Invokes octokit pull create when there is not an existing pull request open', async () => { // setup - const responseCreatePullData = await import('./fixtures/create-pr-response.json'); + const responseCreatePullData = await import( + './fixtures/create-pr-response.json' + ); const createPrResponse = { headers: {}, status: 200, @@ -53,9 +55,7 @@ describe('Opening a pull request', async () => { url: 'http://fake-url.com', data: [], }; - sandbox - .stub(octokit.pulls, 'list') - .resolves(listPullResponse); + sandbox.stub(octokit.pulls, 'list').resolves(listPullResponse); const stub = sandbox .stub(octokit.pulls, 'create') .resolves(createPrResponse); @@ -74,14 +74,16 @@ describe('Opening a pull request', async () => { it('Does not invoke octokit pull create when there is an existing pull request open', async () => { // setup - const responseListPullData = await import('./fixtures/list-pulls-response.json'); + const responseListPullData = await import( + './fixtures/list-pulls-response.json' + ); const listPullResponse = { headers: {}, status: 200, url: 'http://fake-url.com', data: responseListPullData, }; - + const conflictingOrigin = { owner: 'octocat', repo: 'Hello-World', @@ -90,14 +92,13 @@ describe('Opening a pull request', async () => { const listStub = sandbox .stub(octokit.pulls, 'list') .resolves(listPullResponse); - const createStub = sandbox - .stub(octokit.pulls, 'create'); + const createStub = sandbox.stub(octokit.pulls, 'create'); // tests await openPullRequest(octokit, upstream, conflictingOrigin, description); sandbox.assert.calledOnceWithExactly(listStub, { owner: upstream.owner, repo: conflictingOrigin.repo, - head: 'octocat:new-topic' + head: 'octocat:new-topic', }); sandbox.assert.notCalled(createStub); }); @@ -122,9 +123,7 @@ describe('Opening a pull request', async () => { url: 'http://fake-url.com', data: [], }; - sandbox - .stub(octokit.pulls, 'list') - .resolves(listPullResponse); + sandbox.stub(octokit.pulls, 'list').resolves(listPullResponse); const errorMsg = 'Error message'; sandbox.stub(octokit.pulls, 'create').rejects(Error(errorMsg)); try { From 692155328412619d094801f57ea7bdfed84a395c Mon Sep 17 00:00:00 2001 From: Kristie Tom Date: Wed, 5 Aug 2020 17:45:21 -0400 Subject: [PATCH 3/5] checks for similar-named prs --- src/github-handler/pull-request-handler.ts | 18 +++-- test/pr.ts | 88 ++++++++++++++++++++++ 2 files changed, 98 insertions(+), 8 deletions(-) diff --git a/src/github-handler/pull-request-handler.ts b/src/github-handler/pull-request-handler.ts index 66689bbf..77fca88f 100644 --- a/src/github-handler/pull-request-handler.ts +++ b/src/github-handler/pull-request-handler.ts @@ -37,14 +37,16 @@ async function openPullRequest( maintainersCanModify = true, upstreamPrimary: string = DEFAULT_PRIMARY ): Promise { - const existingPullRequests = ( - await octokit.pulls.list({ - owner: upstream.owner, - repo: origin.repo, - head: `${origin.owner}:${origin.branch}`, - }) - ).data; - if (existingPullRequests.length) { + const head = `${origin.owner}:${origin.branch}`; + const existingPullRequest = + ( + await octokit.pulls.list({ + owner: upstream.owner, + repo: origin.repo, + head, + }) + ).data.findIndex(pr => pr.head.label === head) >= 0; + if (existingPullRequest) { logger.info( `Found existing pull request for reference ${origin.owner}:${origin.branch}. Skipping creating a new pull request.` ); diff --git a/test/pr.ts b/test/pr.ts index 397ae5ce..1680152a 100644 --- a/test/pr.ts +++ b/test/pr.ts @@ -72,6 +72,94 @@ describe('Opening a pull request', async () => { }); }); + it('Invokes octokit pull create when there are similar refs with pull requests open, but not exact refs', async () => { + // setup + const responseCreatePullData = await import( + './fixtures/create-pr-response.json' + ); + const responseListPullData = await import( + './fixtures/list-pulls-response.json' + ); + const createPrResponse = { + headers: {}, + status: 200, + url: 'http://fake-url.com', + data: responseCreatePullData, + }; + const listPullResponse = { + headers: {}, + status: 200, + url: 'http://fake-url.com', + data: responseListPullData, + }; + sandbox.stub(octokit.pulls, 'list').resolves(listPullResponse); + const stub = sandbox + .stub(octokit.pulls, 'create') + .resolves(createPrResponse); + + const similarOrigin1 = { + owner: 'octocat', + repo: 'Hello-World', + branch: 'new-topic-1', + }; + const similarOrigin2 = { + owner: 'octocat', + repo: 'Hello-World', + branch: 'new-topic1', + }; + const similarOrigin3 = { + owner: 'octocat', + repo: 'Hello-World', + branch: 'New-topic', + }; + const similarOrigin4 = { + owner: 'octocat', + repo: 'Hello-World', + branch: 'new-topi', + }; + // tests + await openPullRequest(octokit, upstream, similarOrigin1, description); + sandbox.assert.calledWithExactly(stub, { + owner: upstream.owner, + repo: similarOrigin1.repo, + title: description.title, + head: 'octocat:new-topic-1', + base: 'master', + body: description.body, + maintainer_can_modify: true, + }); + await openPullRequest(octokit, upstream, similarOrigin2, description); + sandbox.assert.calledWithExactly(stub, { + owner: upstream.owner, + repo: similarOrigin2.repo, + title: description.title, + head: 'octocat:new-topic1', + base: 'master', + body: description.body, + maintainer_can_modify: true, + }); + await openPullRequest(octokit, upstream, similarOrigin3, description); + sandbox.assert.calledWithExactly(stub, { + owner: upstream.owner, + repo: similarOrigin3.repo, + title: description.title, + head: 'octocat:New-topic', + base: 'master', + body: description.body, + maintainer_can_modify: true, + }); + await openPullRequest(octokit, upstream, similarOrigin4, description); + sandbox.assert.calledWithExactly(stub, { + owner: upstream.owner, + repo: similarOrigin4.repo, + title: description.title, + head: 'octocat:new-topi', + base: 'master', + body: description.body, + maintainer_can_modify: true, + }); + }); + it('Does not invoke octokit pull create when there is an existing pull request open', async () => { // setup const responseListPullData = await import( From 85bf72aef886239f02c68f041c2c38099bf78131 Mon Sep 17 00:00:00 2001 From: Kristie Tom Date: Thu, 6 Aug 2020 11:08:42 -0400 Subject: [PATCH 4/5] split up test cases --- test/pr.ts | 117 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 101 insertions(+), 16 deletions(-) diff --git a/test/pr.ts b/test/pr.ts index 1680152a..b534f85d 100644 --- a/test/pr.ts +++ b/test/pr.ts @@ -72,7 +72,7 @@ describe('Opening a pull request', async () => { }); }); - it('Invokes octokit pull create when there are similar refs with pull requests open, but not exact refs', async () => { + it('Invokes octokit pull create when there are similar refs with pull requests open, but not exact refs: special character and number', async () => { // setup const responseCreatePullData = await import( './fixtures/create-pr-response.json' @@ -102,21 +102,6 @@ describe('Opening a pull request', async () => { repo: 'Hello-World', branch: 'new-topic-1', }; - const similarOrigin2 = { - owner: 'octocat', - repo: 'Hello-World', - branch: 'new-topic1', - }; - const similarOrigin3 = { - owner: 'octocat', - repo: 'Hello-World', - branch: 'New-topic', - }; - const similarOrigin4 = { - owner: 'octocat', - repo: 'Hello-World', - branch: 'new-topi', - }; // tests await openPullRequest(octokit, upstream, similarOrigin1, description); sandbox.assert.calledWithExactly(stub, { @@ -128,6 +113,39 @@ describe('Opening a pull request', async () => { body: description.body, maintainer_can_modify: true, }); + }); + + it('Invokes octokit pull create when there are similar refs with pull requests open, but not exact refs: extra number', async () => { + // setup + const responseCreatePullData = await import( + './fixtures/create-pr-response.json' + ); + const responseListPullData = await import( + './fixtures/list-pulls-response.json' + ); + const createPrResponse = { + headers: {}, + status: 200, + url: 'http://fake-url.com', + data: responseCreatePullData, + }; + const listPullResponse = { + headers: {}, + status: 200, + url: 'http://fake-url.com', + data: responseListPullData, + }; + sandbox.stub(octokit.pulls, 'list').resolves(listPullResponse); + const stub = sandbox + .stub(octokit.pulls, 'create') + .resolves(createPrResponse); + + const similarOrigin2 = { + owner: 'octocat', + repo: 'Hello-World', + branch: 'new-topic1', + }; + // tests await openPullRequest(octokit, upstream, similarOrigin2, description); sandbox.assert.calledWithExactly(stub, { owner: upstream.owner, @@ -138,6 +156,40 @@ describe('Opening a pull request', async () => { body: description.body, maintainer_can_modify: true, }); + }); + + it('Invokes octokit pull create when there are similar refs with pull requests open, but not exact refs: capitalization', async () => { + // setup + const responseCreatePullData = await import( + './fixtures/create-pr-response.json' + ); + const responseListPullData = await import( + './fixtures/list-pulls-response.json' + ); + const createPrResponse = { + headers: {}, + status: 200, + url: 'http://fake-url.com', + data: responseCreatePullData, + }; + const listPullResponse = { + headers: {}, + status: 200, + url: 'http://fake-url.com', + data: responseListPullData, + }; + sandbox.stub(octokit.pulls, 'list').resolves(listPullResponse); + const stub = sandbox + .stub(octokit.pulls, 'create') + .resolves(createPrResponse); + + const similarOrigin3 = { + owner: 'octocat', + repo: 'Hello-World', + branch: 'New-topic', + }; + + // tests await openPullRequest(octokit, upstream, similarOrigin3, description); sandbox.assert.calledWithExactly(stub, { owner: upstream.owner, @@ -148,6 +200,39 @@ describe('Opening a pull request', async () => { body: description.body, maintainer_can_modify: true, }); + }); + + it('Invokes octokit pull create when there are similar refs with pull requests open, but not exact refs: subsequence', async () => { + // setup + const responseCreatePullData = await import( + './fixtures/create-pr-response.json' + ); + const responseListPullData = await import( + './fixtures/list-pulls-response.json' + ); + const createPrResponse = { + headers: {}, + status: 200, + url: 'http://fake-url.com', + data: responseCreatePullData, + }; + const listPullResponse = { + headers: {}, + status: 200, + url: 'http://fake-url.com', + data: responseListPullData, + }; + sandbox.stub(octokit.pulls, 'list').resolves(listPullResponse); + const stub = sandbox + .stub(octokit.pulls, 'create') + .resolves(createPrResponse); + + const similarOrigin4 = { + owner: 'octocat', + repo: 'Hello-World', + branch: 'new-topi', + }; + // tests await openPullRequest(octokit, upstream, similarOrigin4, description); sandbox.assert.calledWithExactly(stub, { owner: upstream.owner, From 3d40556505f47ac40bf1aaca5973cb412b56d2fe Mon Sep 17 00:00:00 2001 From: Kristie Tom Date: Thu, 6 Aug 2020 13:39:13 -0400 Subject: [PATCH 5/5] re-org tests --- test/pr.ts | 234 +++++++++++++++++++++-------------------------------- 1 file changed, 93 insertions(+), 141 deletions(-) diff --git a/test/pr.ts b/test/pr.ts index b534f85d..68d395a0 100644 --- a/test/pr.ts +++ b/test/pr.ts @@ -72,14 +72,16 @@ describe('Opening a pull request', async () => { }); }); - it('Invokes octokit pull create when there are similar refs with pull requests open, but not exact refs: special character and number', async () => { - // setup + describe('When there are similar refs with pull requests open, the current new and unique ref still opens a pr', async () => { const responseCreatePullData = await import( './fixtures/create-pr-response.json' ); const responseListPullData = await import( './fixtures/list-pulls-response.json' ); + afterEach(() => { + sandbox.restore(); + }); const createPrResponse = { headers: {}, status: 200, @@ -92,156 +94,106 @@ describe('Opening a pull request', async () => { url: 'http://fake-url.com', data: responseListPullData, }; - sandbox.stub(octokit.pulls, 'list').resolves(listPullResponse); - const stub = sandbox - .stub(octokit.pulls, 'create') - .resolves(createPrResponse); - const similarOrigin1 = { - owner: 'octocat', - repo: 'Hello-World', - branch: 'new-topic-1', - }; - // tests - await openPullRequest(octokit, upstream, similarOrigin1, description); - sandbox.assert.calledWithExactly(stub, { - owner: upstream.owner, - repo: similarOrigin1.repo, - title: description.title, - head: 'octocat:new-topic-1', - base: 'master', - body: description.body, - maintainer_can_modify: true, + it('Invokes octokit pull create when the similar ref has a special character and number', async () => { + // setup + sandbox.stub(octokit.pulls, 'list').resolves(listPullResponse); + const stub = sandbox + .stub(octokit.pulls, 'create') + .resolves(createPrResponse); + + const similarOrigin1 = { + owner: 'octocat', + repo: 'Hello-World', + branch: 'new-topic-1', + }; + // tests + await openPullRequest(octokit, upstream, similarOrigin1, description); + sandbox.assert.calledOnceWithExactly(stub, { + owner: upstream.owner, + repo: similarOrigin1.repo, + title: description.title, + head: 'octocat:new-topic-1', + base: 'master', + body: description.body, + maintainer_can_modify: true, + }); }); - }); - it('Invokes octokit pull create when there are similar refs with pull requests open, but not exact refs: extra number', async () => { - // setup - const responseCreatePullData = await import( - './fixtures/create-pr-response.json' - ); - const responseListPullData = await import( - './fixtures/list-pulls-response.json' - ); - const createPrResponse = { - headers: {}, - status: 200, - url: 'http://fake-url.com', - data: responseCreatePullData, - }; - const listPullResponse = { - headers: {}, - status: 200, - url: 'http://fake-url.com', - data: responseListPullData, - }; - sandbox.stub(octokit.pulls, 'list').resolves(listPullResponse); - const stub = sandbox - .stub(octokit.pulls, 'create') - .resolves(createPrResponse); + it('Invokes octokit pull create when the existing ref has an extra number', async () => { + // setup + sandbox.stub(octokit.pulls, 'list').resolves(listPullResponse); + const stub = sandbox + .stub(octokit.pulls, 'create') + .resolves(createPrResponse); - const similarOrigin2 = { - owner: 'octocat', - repo: 'Hello-World', - branch: 'new-topic1', - }; - // tests - await openPullRequest(octokit, upstream, similarOrigin2, description); - sandbox.assert.calledWithExactly(stub, { - owner: upstream.owner, - repo: similarOrigin2.repo, - title: description.title, - head: 'octocat:new-topic1', - base: 'master', - body: description.body, - maintainer_can_modify: true, + const similarOrigin2 = { + owner: 'octocat', + repo: 'Hello-World', + branch: 'new-topic1', + }; + // tests + await openPullRequest(octokit, upstream, similarOrigin2, description); + sandbox.assert.calledOnceWithExactly(stub, { + owner: upstream.owner, + repo: similarOrigin2.repo, + title: description.title, + head: 'octocat:new-topic1', + base: 'master', + body: description.body, + maintainer_can_modify: true, + }); }); - }); - it('Invokes octokit pull create when there are similar refs with pull requests open, but not exact refs: capitalization', async () => { - // setup - const responseCreatePullData = await import( - './fixtures/create-pr-response.json' - ); - const responseListPullData = await import( - './fixtures/list-pulls-response.json' - ); - const createPrResponse = { - headers: {}, - status: 200, - url: 'http://fake-url.com', - data: responseCreatePullData, - }; - const listPullResponse = { - headers: {}, - status: 200, - url: 'http://fake-url.com', - data: responseListPullData, - }; - sandbox.stub(octokit.pulls, 'list').resolves(listPullResponse); - const stub = sandbox - .stub(octokit.pulls, 'create') - .resolves(createPrResponse); + it('Invokes octokit pull create when the existing ref has different capitalization', async () => { + // setup + sandbox.stub(octokit.pulls, 'list').resolves(listPullResponse); + const stub = sandbox + .stub(octokit.pulls, 'create') + .resolves(createPrResponse); - const similarOrigin3 = { - owner: 'octocat', - repo: 'Hello-World', - branch: 'New-topic', - }; + const similarOrigin3 = { + owner: 'octocat', + repo: 'Hello-World', + branch: 'New-topic', + }; - // tests - await openPullRequest(octokit, upstream, similarOrigin3, description); - sandbox.assert.calledWithExactly(stub, { - owner: upstream.owner, - repo: similarOrigin3.repo, - title: description.title, - head: 'octocat:New-topic', - base: 'master', - body: description.body, - maintainer_can_modify: true, + // tests + await openPullRequest(octokit, upstream, similarOrigin3, description); + sandbox.assert.calledOnceWithExactly(stub, { + owner: upstream.owner, + repo: similarOrigin3.repo, + title: description.title, + head: 'octocat:New-topic', + base: 'master', + body: description.body, + maintainer_can_modify: true, + }); }); - }); - it('Invokes octokit pull create when there are similar refs with pull requests open, but not exact refs: subsequence', async () => { - // setup - const responseCreatePullData = await import( - './fixtures/create-pr-response.json' - ); - const responseListPullData = await import( - './fixtures/list-pulls-response.json' - ); - const createPrResponse = { - headers: {}, - status: 200, - url: 'http://fake-url.com', - data: responseCreatePullData, - }; - const listPullResponse = { - headers: {}, - status: 200, - url: 'http://fake-url.com', - data: responseListPullData, - }; - sandbox.stub(octokit.pulls, 'list').resolves(listPullResponse); - const stub = sandbox - .stub(octokit.pulls, 'create') - .resolves(createPrResponse); + it('Invokes octokit pull create when the existing ref is a subsequence', async () => { + // setup + sandbox.stub(octokit.pulls, 'list').resolves(listPullResponse); + const stub = sandbox + .stub(octokit.pulls, 'create') + .resolves(createPrResponse); - const similarOrigin4 = { - owner: 'octocat', - repo: 'Hello-World', - branch: 'new-topi', - }; - // tests - await openPullRequest(octokit, upstream, similarOrigin4, description); - sandbox.assert.calledWithExactly(stub, { - owner: upstream.owner, - repo: similarOrigin4.repo, - title: description.title, - head: 'octocat:new-topi', - base: 'master', - body: description.body, - maintainer_can_modify: true, + const similarOrigin4 = { + owner: 'octocat', + repo: 'Hello-World', + branch: 'new-topi', + }; + // tests + await openPullRequest(octokit, upstream, similarOrigin4, description); + sandbox.assert.calledOnceWithExactly(stub, { + owner: upstream.owner, + repo: similarOrigin4.repo, + title: description.title, + head: 'octocat:new-topi', + base: 'master', + body: description.body, + maintainer_can_modify: true, + }); }); });