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

Use graphql to search for existing pull request #105

Merged
merged 3 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
65 changes: 41 additions & 24 deletions src/compose-create-pull-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,35 +120,52 @@ export async function composeCreatePullRequest(
return null;
}

// https://docs.github.com/en/rest/git/refs#get-a-reference
const branchExists = await octokit
.request("GET /repos/{owner}/{repo}/git/ref/{ref}", {
owner: state.fork,
repo,
ref: `heads/${head}`,
})
.then(
() => true,
(error) => {
/* istanbul ignore else */
if (error.status === 404) return false;

/* istanbul ignore next */
throw error;
const branchInfo: {
repository: {
ref: {
associatedPullRequests: {
edges: [
{
node: {
url: string;
number: number;
};
}
];
};
};
};
} = await octokit.graphql(
`
query ($owner: String!, $repo: String!, $head: String!) {
repository(name: $repo, owner: $owner) {
ref(qualifiedName: $head) {
associatedPullRequests(first: 1, states: OPEN) {
edges {
node {
id
number
url
}
}
}
}
}
);
}`,
{
owner,
repo,
head,
}
);

const existingPullRequest = branchExists
? await octokit
.request("GET /search/issues", {
q: `head:${head} type:pr is:open repo:${owner}/${repo}`,
})
.then((response) => response.data.items[0])
: undefined;
const branchExists = !!branchInfo.repository.ref;
const existingPullRequest =
branchInfo.repository.ref?.associatedPullRequests?.edges?.[0]?.node;

if (existingPullRequest && !update) {
throw new Error(
`[octokit-plugin-create-pull-request] Pull request already exists: ${existingPullRequest.html_url}. Set update=true to enable updating`
`[octokit-plugin-create-pull-request] Pull request already exists: ${existingPullRequest.url}. Set update=true to enable updating`
);
}

Expand Down
22 changes: 14 additions & 8 deletions test/fixtures/create-binary-file.json
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@
},
{
"request": {
"method": "GET",
"method": "POST",
"baseUrl": "https://api.github.com",
"headers": {
"accept": "application/vnd.github.v3+json",
Expand All @@ -574,13 +574,16 @@
"previews": []
},
"request": {},
"url": "/repos/{owner}/{repo}/git/ref/{ref}",
"owner": "gr2m",
"repo": "pull-request-test",
"ref": "heads/patch"
"url": "/graphql",
"query": "\n query ($owner: String!, $repo: String!, $head: String!) {\n repository(name: $repo, owner: $owner) {\n ref(qualifiedName: $head) {\n associatedPullRequests(first: 1, states: OPEN) {\n edges {\n node {\n id\n number\n url\n }\n }\n }\n }\n }\n }",
"variables": {
"owner": "gr2m",
"repo": "pull-request-test",
"head": "patch"
}
},
"response": {
"status": 404,
"status": 200,
"headers": {
"access-control-allow-origin": "*",
"access-control-expose-headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset",
Expand Down Expand Up @@ -612,8 +615,11 @@
"x-xss-protection": "0"
},
"data": {
"error": "Not Found",
"documentation_url": "https://docs.github.com/rest/reference/git#get-a-reference"
"data": {
"repository": {
"ref": null
}
}
}
}
},
Expand Down
22 changes: 14 additions & 8 deletions test/fixtures/create-fork.json
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,7 @@
},
{
"request": {
"method": "GET",
"method": "POST",
"baseUrl": "https://api.github.com",
"headers": {
"accept": "application/vnd.github.v3+json",
Expand All @@ -1098,13 +1098,16 @@
"previews": []
},
"request": {},
"url": "/repos/{owner}/{repo}/git/ref/{ref}",
"owner": "gr2m-test",
"repo": "pull-request-test",
"ref": "heads/test-branch-u7es0"
"url": "/graphql",
"query": "\n query ($owner: String!, $repo: String!, $head: String!) {\n repository(name: $repo, owner: $owner) {\n ref(qualifiedName: $head) {\n associatedPullRequests(first: 1, states: OPEN) {\n edges {\n node {\n id\n number\n url\n }\n }\n }\n }\n }\n }",
"variables": {
"owner": "gr2m",
"repo": "pull-request-test",
"head": "test-branch-u7es0"
}
},
"response": {
"status": 404,
"status": 200,
"headers": {
"access-control-allow-origin": "*",
"access-control-expose-headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset",
Expand Down Expand Up @@ -1134,8 +1137,11 @@
"x-xss-protection": "0"
},
"data": {
"error": "Not Found",
"documentation_url": "https://docs.github.com/rest/reference/git#get-a-reference"
"data": {
"repository": {
"ref": null
}
}
}
}
},
Expand Down
22 changes: 14 additions & 8 deletions test/fixtures/custom-base.json
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@
},
{
"request": {
"method": "GET",
"method": "POST",
"baseUrl": "https://api.github.com",
"headers": {
"accept": "application/vnd.github.v3+json",
Expand All @@ -520,13 +520,16 @@
"previews": []
},
"request": {},
"url": "/repos/{owner}/{repo}/git/ref/{ref}",
"owner": "gr2m",
"repo": "pull-request-test",
"ref": "heads/test-branch-1rtg5"
"url": "/graphql",
"query": "\n query ($owner: String!, $repo: String!, $head: String!) {\n repository(name: $repo, owner: $owner) {\n ref(qualifiedName: $head) {\n associatedPullRequests(first: 1, states: OPEN) {\n edges {\n node {\n id\n number\n url\n }\n }\n }\n }\n }\n }",
"variables": {
"owner": "gr2m",
"repo": "pull-request-test",
"head": "test-branch-1rtg5"
}
},
"response": {
"status": 404,
"status": 200,
"headers": {
"access-control-allow-origin": "*",
"access-control-expose-headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset",
Expand Down Expand Up @@ -558,8 +561,11 @@
"x-xss-protection": "0"
},
"data": {
"error": "Not Found",
"documentation_url": "https://docs.github.com/rest/reference/git#get-a-reference"
"data": {
"repository": {
"ref": null
}
}
}
}
},
Expand Down
22 changes: 14 additions & 8 deletions test/fixtures/delete-files.json
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@
},
{
"request": {
"method": "GET",
"method": "POST",
"baseUrl": "https://api.github.com",
"headers": {
"accept": "application/vnd.github.v3+json",
Expand All @@ -629,13 +629,16 @@
"previews": []
},
"request": {},
"url": "/repos/{owner}/{repo}/git/ref/{ref}",
"owner": "gr2m",
"repo": "pull-request-test",
"ref": "heads/patch"
"url": "/graphql",
"query": "\n query ($owner: String!, $repo: String!, $head: String!) {\n repository(name: $repo, owner: $owner) {\n ref(qualifiedName: $head) {\n associatedPullRequests(first: 1, states: OPEN) {\n edges {\n node {\n id\n number\n url\n }\n }\n }\n }\n }\n }",
"variables": {
"owner": "gr2m",
"repo": "pull-request-test",
"head": "patch"
}
},
"response": {
"status": 404,
"status": 200,
"headers": {
"access-control-allow-origin": "*",
"access-control-expose-headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset",
Expand Down Expand Up @@ -667,8 +670,11 @@
"x-xss-protection": "0"
},
"data": {
"error": "Not Found",
"documentation_url": "https://docs.github.com/rest/reference/git#get-a-reference"
"data": {
"repository": {
"ref": null
}
}
}
}
},
Expand Down
22 changes: 14 additions & 8 deletions test/fixtures/empty-commit-message.json
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@
},
{
"request": {
"method": "GET",
"method": "POST",
"baseUrl": "https://api.github.com",
"headers": {
"accept": "application/vnd.github.v3+json",
Expand All @@ -688,13 +688,16 @@
"previews": []
},
"request": {},
"url": "/repos/{owner}/{repo}/git/ref/{ref}",
"owner": "gr2m",
"repo": "pull-request-test",
"ref": "heads/empty-commit-message"
"url": "/graphql",
"query": "\n query ($owner: String!, $repo: String!, $head: String!) {\n repository(name: $repo, owner: $owner) {\n ref(qualifiedName: $head) {\n associatedPullRequests(first: 1, states: OPEN) {\n edges {\n node {\n id\n number\n url\n }\n }\n }\n }\n }\n }",
"variables": {
"owner": "gr2m",
"repo": "pull-request-test",
"head": "empty-commit-message"
}
},
"response": {
"status": 404,
"status": 200,
"headers": {
"access-control-allow-origin": "*",
"access-control-expose-headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset",
Expand Down Expand Up @@ -726,8 +729,11 @@
"x-xss-protection": "0"
},
"data": {
"error": "Not Found",
"documentation_url": "https://docs.github.com/rest/reference/git#get-a-reference"
"data": {
"repository": {
"ref": null
}
}
}
}
},
Expand Down
22 changes: 14 additions & 8 deletions test/fixtures/empty-commits.json
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@
},
{
"request": {
"method": "GET",
"method": "POST",
"baseUrl": "https://api.github.com",
"headers": {
"accept": "application/vnd.github.v3+json",
Expand All @@ -499,13 +499,16 @@
"previews": []
},
"request": {},
"url": "/repos/{owner}/{repo}/git/ref/{ref}",
"owner": "gr2m",
"repo": "pull-request-test",
"ref": "heads/empty-files"
"url": "/graphql",
"query": "\n query ($owner: String!, $repo: String!, $head: String!) {\n repository(name: $repo, owner: $owner) {\n ref(qualifiedName: $head) {\n associatedPullRequests(first: 1, states: OPEN) {\n edges {\n node {\n id\n number\n url\n }\n }\n }\n }\n }\n }",
"variables": {
"owner": "gr2m",
"repo": "pull-request-test",
"head": "empty-files"
}
},
"response": {
"status": 404,
"status": 200,
"headers": {
"access-control-allow-origin": "*",
"access-control-expose-headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset",
Expand Down Expand Up @@ -537,8 +540,11 @@
"x-xss-protection": "0"
},
"data": {
"error": "Not Found",
"documentation_url": "https://docs.github.com/rest/reference/git#get-a-reference"
"data": {
"repository": {
"ref": null
}
}
}
}
},
Expand Down
23 changes: 15 additions & 8 deletions test/fixtures/empty-update.json
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@
},
{
"request": {
"method": "GET",
"method": "POST",
"baseUrl": "https://api.github.com",
"headers": {
"accept": "application/vnd.github.v3+json",
Expand All @@ -484,13 +484,17 @@
"previews": []
},
"request": {},
"url": "/repos/{owner}/{repo}/git/ref/{ref}",
"owner": "gr2m",
"repo": "pull-request-test",
"ref": "heads/empty-update"
"url": "/graphql",
"query": "\n query ($owner: String!, $repo: String!, $head: String!) {\n repository(name: $repo, owner: $owner) {\n ref(qualifiedName: $head) {\n associatedPullRequests(first: 1, states: OPEN) {\n edges {\n node {\n id\n number\n url\n }\n }\n }\n }\n }\n }",
"variables": {
"owner": "gr2m",
"repo": "pull-request-test",
"head": "empty-update"
}
},
"response": {
"status": 404,
"status": 200,
"url": "https://api.github.com/graphql",
"headers": {
"access-control-allow-origin": "*",
"access-control-expose-headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset",
Expand Down Expand Up @@ -522,8 +526,11 @@
"x-xss-protection": "0"
},
"data": {
"error": "Not Found",
"documentation_url": "https://docs.github.com/rest/reference/git#get-a-reference"
"data": {
"repository": {
"ref": null
}
}
}
}
},
Expand Down
Loading