Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store the build output as artifact and post an automated comment
- Loading branch information
1 parent
1efe9f6
commit 4a87438
Showing
4 changed files
with
85 additions
and
15 deletions.
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
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,65 @@ | ||
#!/usr/bin/env node | ||
|
||
const path = require('path'); | ||
const fetch = require('node-fetch'); | ||
|
||
const authToken = process.env.GH_AUTH_TOKEN; | ||
if (!authToken) { | ||
throw new Error('Could not find auth token.'); | ||
} | ||
|
||
const prNumber = path.basename( | ||
process.env.CIRCLE_PULL_REQUEST || process.env.CI_PULL_REQUEST || '' | ||
); | ||
if (!prNumber) { | ||
console.log('No pull request number found'); | ||
process.exit(0); | ||
} | ||
|
||
const headline = '### Thank you for your contribution! ❤️'; | ||
|
||
postComment(); | ||
|
||
async function postComment() { | ||
const existingId = await findExistingComment(); | ||
console.log(existingId ? `Update comment ${existingId}` : 'Create new comment'); | ||
const installPath = await getInstallPath(); | ||
console.log(`Install to ${installPath}`); | ||
const path = existingId ? `pulls/comments/${existingId}` : `pulls/${prNumber}/comments`; | ||
const method = existingId ? 'PATCH' : 'POST'; | ||
console.log( | ||
await ( | ||
await fetch(getApiUrl(path), { | ||
method, | ||
body: JSON.stringify({ | ||
body: `${headline} | ||
You can try out this pull request locally via | ||
\`\`\` | ||
npm install ${installPath} | ||
\`\`\` | ||
or load it into the REPL: | ||
https://rollupjs.org/repl/?circleci=${process.env.CIRCLE_BUILD_NUM} | ||
` | ||
}) | ||
}) | ||
).json() | ||
); | ||
} | ||
|
||
async function findExistingComment() { | ||
const comments = await (await fetch(getApiUrl(`pulls/${prNumber}/comments`), {})).json(); | ||
const existingComment = comments.find(comment => comment.body.startsWith(headline)); | ||
return existingComment && existingComment.id; | ||
} | ||
|
||
async function getInstallPath() { | ||
const prInfo = await (await fetch(getApiUrl(`pulls/${prNumber}`), {})).json(); | ||
return `${prInfo.head.repo.full_name}#${prInfo.head.ref}`; | ||
} | ||
|
||
function getApiUrl(path) { | ||
return `https://${authToken}:x-oauth-basic@api.github.com/repos/rollup/rollup/${path}`; | ||
} |