Last active
July 16, 2018 14:42
-
-
Save GuyHarwood/03931598a9d77e5635069a67af2be045 to your computer and use it in GitHub Desktop.
leverages github API to determine whether to build a PR request or not. Used in travis to fail builds quickly if they aren't deemed ready for building.
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
'use strict' | |
const https = require('https') | |
// set the id of your github PR label below... | |
const ciEnabledLabelId = 0 | |
const orgAndRepo = 'yourOrg/yourRepo' | |
// Example call: 'https://api.github.com/repos/myorg/myrepo/pulls/557?client_id=xxxx&client_secret=yyyy' | |
const pullRequestId = process.argv[2] | |
if (!pullRequestId) { | |
console.log('Missing argument: pull request id') | |
process.exit(1) | |
} | |
const options = { | |
hostname: 'api.github.com', | |
path: `/repos/${orgAndRepo}/pulls/${pullRequestId}?client_id=${process.env.GITHUB_API_CLIENTID}&client_secret=${process.env.GITHUB_API_SECRET}`, | |
method: 'GET', | |
headers: { | |
'User-Agent': 'node/https' | |
} | |
} | |
const parseResponse = (res) => { | |
let labels | |
let payload | |
try { | |
try { | |
payload = JSON.parse(res) | |
} catch (error) { | |
console.error(`error parsing response:${error.message}`) | |
process.exit(1) | |
} | |
if (!payload) { | |
console.error('null payload') | |
process.exit(1) | |
} | |
labels = payload.labels | |
if (!labels || labels.length === 0) { | |
console.log(`no labels found attached to PR ${pullRequestId}`) | |
console.log(payload) | |
process.exit(1) | |
} | |
} catch (err) { | |
console.error(`error parsing labels for PR ${pullRequestId}`) | |
console.log(payload) | |
console.error(err) | |
process.exit(1) | |
} | |
const ciEnabledLabel = labels.find(item => item.id === ciEnabledLabelId) | |
if (ciEnabledLabel) { | |
console.log(`CI enabled label found on PR ${pullRequestId}`) | |
process.exit(0) | |
} | |
console.log(`CI Enabled label not found on PR ${pullRequestId}`) | |
console.log(payload) | |
process.exit(1) | |
} | |
https.get(options, (response) => { | |
let data = '' | |
response.on('data', (chunk) => { | |
data += chunk | |
}) | |
response.on('end', () => { | |
parseResponse(data) | |
}) | |
}).on('error', (err) => { | |
console.error('Error: ' + err.message) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment