Skip to content

Commit

Permalink
fix(ci): use github API to patch CI env
Browse files Browse the repository at this point in the history
  • Loading branch information
zetlen committed Aug 2, 2018
1 parent 5a64589 commit 2826d2b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ common_settings:
- venia-concept/node_modules
key: 'v1-npm-cache-{{ .Branch }}'

patch_environment_for_pr: &patch_environment_for_pr
name: Patch CircleCI environment to help detect pull requests
command: node .circleci/patch_circleci_environment.js >> $BASH_ENV

# Greenkeeper Lockfile wants to use the latest lockfile format at all times,
# and therefore the latest NPM at all times.
install_latest_npm: &install_latest_npm
Expand Down Expand Up @@ -70,7 +74,6 @@ common_settings:
test_result_path: &test_result_path
path: "test-results"


coveralls: &coveralls
name: Coveralls Coverage Analysis
command: npm run coveralls
Expand Down
67 changes: 67 additions & 0 deletions .circleci/patch_circleci_environment.js
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);
}
);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"junit-report-builder": "^1.3.1",
"lerna": "^3.0.0-beta.21",
"mkdirp": "^0.5.1",
"node-fetch": "^2.2.0",
"nodemon": "^1.18.3",
"prettier": "^1.13.5",
"prettier-check": "^2.0.0",
Expand Down

0 comments on commit 2826d2b

Please sign in to comment.