-
Notifications
You must be signed in to change notification settings - Fork 682
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ci): use github API to patch CI env
- Loading branch information
zetlen
committed
Aug 2, 2018
1 parent
5a64589
commit 2826d2b
Showing
3 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
(async env => { | ||
const url = require('url'); | ||
const qs = require('querystring'); | ||
const fetch = require('node-fetch'); | ||
|
||
const repo = process.env.CIRCLE_PROJECT_REPONAME; | ||
const owner = process.env.CIRCLE_PROJECT_USERNAME; | ||
const branch = process.env.CIRCLE_BRANCH; | ||
const token = process.env.GH_TOKEN; | ||
|
||
const query = `query getPR($owner: String!, $name: String!, $ref: String!) { | ||
repository(owner: $owner, name: $name) { | ||
ref(qualifiedName: $ref) { | ||
associatedPullRequests(last: 1) { | ||
nodes { | ||
url | ||
number | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const response = await fetch('https://api.github.com/graphql', { | ||
method: 'POST', | ||
credentials: 'include', | ||
headers: { | ||
Authorization: `bearer ${token}` | ||
}, | ||
body: JSON.stringify({ | ||
query, | ||
variables: { | ||
owner, | ||
name: repo, | ||
ref: `/refs/heads/${branch}` | ||
} | ||
}) | ||
}); | ||
|
||
const responseBody = await response.text(); | ||
|
||
try { | ||
const prs = JSON.parse(responseBody).data.repository.ref | ||
.associatedPullRequests.nodes; | ||
if (prs.length === 0) { | ||
const { url, number } = prs[0]; | ||
return ` | ||
CI_PULL_REQUEST="${url}" | ||
CI_PULL_REQUESTS="${url}" | ||
CIRCLE_PULL_REQUEST="${url}" | ||
CIRCLE_PULL_REQUESTS="${url}" | ||
CIRCLE_PR_NUMBER="${number}" | ||
`; | ||
} | ||
} catch (e) { | ||
throw new Error(responseBody); | ||
} | ||
})(process).then( | ||
out => { | ||
console.log(out); | ||
}, | ||
e => { | ||
console.error('Failed to retrieve pull request from GitHub', e); | ||
process.exit(1); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters