Skip to content

Commit

Permalink
Store the build output as artifact and post an automated comment
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Feb 7, 2020
1 parent 1efe9f6 commit 4a87438
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 15 deletions.
32 changes: 18 additions & 14 deletions .circleci/config.yml
@@ -1,14 +1,5 @@
version: 2

unit_tests: &unit_tests
steps:
- checkout
- restore_cache:
key: dependency-cache-{{ checksum "package-lock.json" }}
- run:
name: Run unit tests.
command: npm run ci:test

jobs:
analysis:
docker:
Expand All @@ -18,13 +9,13 @@ jobs:
- restore_cache:
key: dependency-cache-{{ checksum "package-lock.json" }}
- run:
name: Install Dependencies
name: Installing Dependencies
command: npm ci --ignore-scripts
- run:
name: Run linting.
name: Running linting
command: npm run ci:lint
- run:
name: Run NPM Security Audit
name: Running NPM Security Audit
command: npm run security
- save_cache:
key: dependency-cache-{{ checksum "package-lock.json" }}
Expand All @@ -33,7 +24,20 @@ jobs:
node-v10-latest:
docker:
- image: rollupcabal/circleci-node-v10:latest
<<: *unit_tests
steps:
- checkout
- restore_cache:
key: dependency-cache-{{ checksum "package-lock.json" }}
- run:
name: Running tests
command: npm run ci:test
- store_artifacts:
name: Storing browser build for REPL
path: /home/circleci/project/dist/rollup.browser.js
destination: rollup.browser.js
- run:
name: Post REPL comment
command: ./scripts/post-comment.js
node-v12-latest:
docker:
- image: rollupcabal/circleci-node-v12:latest
Expand All @@ -42,7 +46,7 @@ jobs:
- restore_cache:
key: dependency-cache-{{ checksum "package-lock.json" }}
- run:
name: Run tests with coverage.
name: Running tests with coverage
command: npm run ci:coverage

workflows:
Expand Down
2 changes: 1 addition & 1 deletion .eslintrc.json
Expand Up @@ -32,7 +32,7 @@
"plugin:import/warnings"
],
"parserOptions": {
"ecmaVersion": 6,
"ecmaVersion": 2018,
"sourceType": "module"
},
"settings": {
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -97,6 +97,7 @@
"micromatch": "^4.0.2",
"minimist": "^1.2.0",
"mocha": "^6.2.2",
"node-fetch": "^2.6.0",
"nyc": "^15.0.0",
"prettier": "^1.19.1",
"pretty-bytes": "^5.3.0",
Expand Down
65 changes: 65 additions & 0 deletions scripts/post-comment.js
@@ -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}`;
}

0 comments on commit 4a87438

Please sign in to comment.