Skip to content

Commit

Permalink
ci: add task to cleanup docs previews on merge commits (#1647)
Browse files Browse the repository at this point in the history
Closes #1620
  • Loading branch information
brandonroberts authored Mar 22, 2019
1 parent 5a830fd commit 3de9972
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,17 @@ jobs:
at: dist
- run: yarn run deploy:builds

cleanup-previews:
<<: *run_in_node
steps:
- add_ssh_keys:
fingerprints:
- "c9:c2:b4:5e:13:23:b6:6d:d8:29:3e:68:c6:40:9c:ec"
- checkout
- restore_cache:
key: *cache_key
- run: yarn run cleanup:previews

workflows:
version: 2
build-test-deploy:
Expand Down Expand Up @@ -230,3 +241,9 @@ workflows:
filters:
branches:
only: master
- cleanup-previews:
requires:
- install
filters:
branches:
only: master
15 changes: 15 additions & 0 deletions build/cleanup-previews.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as tasks from './tasks';
import { createBuilder } from './util';
import { packages } from './config';

const cleanup = createBuilder([
['Cleanup docs previews', tasks.cleanupDocsPreviews],
]);

cleanup({
scope: '',
packages,
}).catch(err => {
console.error(err);
process.exit(1);
});
46 changes: 46 additions & 0 deletions build/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,49 @@ export async function postGithubComment() {
});
}
}

export async function cleanupDocsPreviews() {
const repoUrl = '[email protected]:ngrx/ngrx-io-previews.git';
const repoDir = `./tmp/docs-preview-cleanup`;
const token = process.env.GITHUB_API_KEY;
const octokit = require('@octokit/rest')();

octokit.authenticate({ type: 'token', token });

const q = 'repo:ngrx/platform is:pr is:closed';

const { data }: { data: { items: any[] } } = await octokit.search.issues({
q,
per_page: 100,
});

await util.cmd('rm -rf', [`${repoDir}`]);
await util.cmd('mkdir ', [`-p ${repoDir}`]);
await process.chdir(`${repoDir}`);
await util.git([`init`]);
await util.git([`remote add origin ${repoUrl}`]);
await util.git([`fetch origin master --depth=1`]);
await util.git(['checkout origin/master -b master']);

const prsToRemove = data.items.reduce(
(prev: string[], curr: { number: number }) => {
prev.push(`pr${curr.number}*`);

return prev;
},
[]
);

await util.cmd('rm -rf', [`${prsToRemove.join(' ')}`]);
await util.git([`config user.name "ngrxbot"`]);
await util.git([`config user.email "${process.env.GITHUB_BOT_EMAIL}"`]);
await util.git(['add --all']);

try {
await util.git([
`commit -m "chore: cleanup previews for closed pull requests"`,
]);
} catch (e) {}

await util.git(['push origin master --force']);
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"build": "yarn bazel build //modules/...",
"deploy:builds": "ts-node ./build/deploy-build.ts",
"deploy:preview": "ts-node ./build/deploy-preview.ts",
"cleanup:previews": "ts-node ./build/cleanup-previews.ts",
"test:unit": "node ./tests.js",
"test": "nyc yarn run test:unit",
"test:bazel": "yarn bazel test //modules/...",
Expand Down

0 comments on commit 3de9972

Please sign in to comment.