1 Commits

Author SHA1 Message Date
bf7159ec33 chore: add dotenv, ocktkat 2018-10-17 11:11:31 -07:00
23 changed files with 9 additions and 573 deletions

1
.gitignore vendored
View File

@@ -9,4 +9,3 @@ coverage
.nyc_output
coverage.lcov
/lib
/data

View File

@@ -1,3 +0,0 @@
### Authors
- joe fleming ([w33ble](https://github.com/w33ble))

View File

@@ -1,52 +0,0 @@
### Changelog
#### [v1.4.0](https://git.w33ble.com/w33ble/gh-action-bot/compare/v1.3.2...v1.4.0) (1 March 2019)
- feat: make comment page count configurable [`ad3ebec`](https://git.w33ble.com/w33ble/gh-action-bot/commit/ad3ebecb6271037283b74686adbb49767f020988)
- feat: check for pending status after comment [`1863239`](https://git.w33ble.com/w33ble/gh-action-bot/commit/1863239c062de3a730adb6dad67208699056f630)
- feat: enhance history module [`5e94621`](https://git.w33ble.com/w33ble/gh-action-bot/commit/5e946213ba3968b6f8d4aacd078c145f9e225038)
- feat: add 'exec and check' helper [`de77cd3`](https://git.w33ble.com/w33ble/gh-action-bot/commit/de77cd381bc74a53ee0f90ed9675ba1b0b200e68)
- feat: support skipping based on labels [`08bd36c`](https://git.w33ble.com/w33ble/gh-action-bot/commit/08bd36c133a4f79d32af199ab829f305a599e06a)
- fix: don't truncate all history when max is hit [`bd42c4c`](https://git.w33ble.com/w33ble/gh-action-bot/commit/bd42c4c415cfd15d48a957fa8d64f6dd8c2227c5)
- fix: remove comment delete delay [`d19ab86`](https://git.w33ble.com/w33ble/gh-action-bot/commit/d19ab86a68483813947f2cd21d61d9c15dbef125)
- feat: allow custom comment delete delay [`5c842ec`](https://git.w33ble.com/w33ble/gh-action-bot/commit/5c842eccff1fe29c07f774b1a76c3c35a4bd9aa8)
- feat: allow customizing history json filename [`7bf6ff3`](https://git.w33ble.com/w33ble/gh-action-bot/commit/7bf6ff373d315f506e4823b9bfa78ce980731dfa)
- feat: add slight delay before removing pull comment [`0147ab1`](https://git.w33ble.com/w33ble/gh-action-bot/commit/0147ab1d9da3a4fe3a194bf08be0a112bfbfd402)
- fix: hide debugging output when value is '0' [`8bdab1f`](https://git.w33ble.com/w33ble/gh-action-bot/commit/8bdab1f9dbac3ad4c0933f5c859cc968d3d47a29)
- feat: support multiple matching labels [`e8959ee`](https://git.w33ble.com/w33ble/gh-action-bot/commit/e8959eea141ba523a96a538d6f32b0f8c8cccbd6)
#### [v1.3.2](https://git.w33ble.com/w33ble/gh-action-bot/compare/v1.3.1...v1.3.2) (25 October 2018)
- fix: correct month in log output [`07288be`](https://git.w33ble.com/w33ble/gh-action-bot/commit/07288bee33d6861aa53e88e856174868f37b1fd5)
#### [v1.3.1](https://git.w33ble.com/w33ble/gh-action-bot/compare/v1.2.1...v1.3.1) (23 October 2018)
- feat: add a clean command [`f3773f7`](https://git.w33ble.com/w33ble/gh-action-bot/commit/f3773f7ee7c0de42997a5b19cc5ee215da3f06ec)
- feat: track processed, comments [`31b55fc`](https://git.w33ble.com/w33ble/gh-action-bot/commit/31b55fc7c2d7042e428a12f04124e1c4cd63fd28)
- docs: add clean script to readme [`b728b8e`](https://git.w33ble.com/w33ble/gh-action-bot/commit/b728b8ef50ee1a9b79f79ccc6b27f4acaf669d15)
#### [v1.2.1](https://git.w33ble.com/w33ble/gh-action-bot/compare/v1.2.0...v1.2.1) (23 October 2018)
- feat: add more debugging output [`3db973f`](https://git.w33ble.com/w33ble/gh-action-bot/commit/3db973febc5387605279f7855976a4ec431f0362)
- fix: correctly handle errors in logger.error [`c70e8e4`](https://git.w33ble.com/w33ble/gh-action-bot/commit/c70e8e49ecb9041b22a8f4b92398339d45871ca1)
- fix: correctly check for pr author [`f4c3e02`](https://git.w33ble.com/w33ble/gh-action-bot/commit/f4c3e02c1a31aea89a565f5685dd3b879bd38b14)
#### [v1.2.0](https://git.w33ble.com/w33ble/gh-action-bot/compare/v1.1.0...v1.2.0) (23 October 2018)
- feat: add debugging output to script [`0dce327`](https://git.w33ble.com/w33ble/gh-action-bot/commit/0dce32781b0f8c138d7f8b28382289d67be730dd)
- feat: debug logging and better timestamps [`08799b4`](https://git.w33ble.com/w33ble/gh-action-bot/commit/08799b48d74d12266d5ebb85811f5d3fa046b086)
- docs: add note about using DEBUG=1 output [`872d9c7`](https://git.w33ble.com/w33ble/gh-action-bot/commit/872d9c790fc723f1b255536225be60f547cf5196)
#### [v1.1.0](https://git.w33ble.com/w33ble/gh-action-bot/compare/v1.0.1...v1.1.0) (22 October 2018)
- feat: add timestamp to log output [`725638e`](https://git.w33ble.com/w33ble/gh-action-bot/commit/725638ee6b7fbb816518239f5524f6c37056da2e)
#### [v1.0.1](https://git.w33ble.com/w33ble/gh-action-bot/compare/v1.0.0...v1.0.1) (22 October 2018)
- feat: add a force retest script [`def91e3`](https://git.w33ble.com/w33ble/gh-action-bot/commit/def91e35d3c8028b7f78772049b0e93c3c8d677e)
- fix: add error handling code [`3b44968`](https://git.w33ble.com/w33ble/gh-action-bot/commit/3b4496864b0e9bd248e6d4ea12f2608dd41c0374)
- docs: add info about force script [`eb4666c`](https://git.w33ble.com/w33ble/gh-action-bot/commit/eb4666c3df312a9b5d8eb8ff5cf58027d794b417)
- docs: add token info to readme [`4794c6b`](https://git.w33ble.com/w33ble/gh-action-bot/commit/4794c6be2673402d5a76d8c61974ba3f9c936977)
### [v1.0.0](https://git.w33ble.com/w33ble/gh-action-bot/compare/v0.1.0...v1.0.0) (18 October 2018)
- feat: allow filtering by pr author [`68cccb7`](https://git.w33ble.com/w33ble/gh-action-bot/commit/68cccb72a9801c502003c7b913844fc01721a6e3)
#### v0.1.0 (18 October 2018)
- docs: update readme, add sample env [`a28d6a2`](https://git.w33ble.com/w33ble/gh-action-bot/commit/a28d6a25096efb9c8acea3e8d26e7f7f50dfd6b5)
- feat: add retest comment, delete it [`ced41f4`](https://git.w33ble.com/w33ble/gh-action-bot/commit/ced41f4a495823205e5769e4531f923a982d29b4)
- feat: process 2 pages of events [`c117cb8`](https://git.w33ble.com/w33ble/gh-action-bot/commit/c117cb847d5cb7a6b57b705e0c192cc725c0d646)
- feat: fetch data for prs that need retest [`d1cfd7c`](https://git.w33ble.com/w33ble/gh-action-bot/commit/d1cfd7c20b17802f2de1b9153ae9a8392ed1bc44)
- feat: add history tracking [`b6b5c27`](https://git.w33ble.com/w33ble/gh-action-bot/commit/b6b5c27ffc43ff2e1c1ccf041958021cf7c8c4d4)

View File

@@ -1,28 +1,11 @@
# gh-action-bot
Action bot for Github. Checks a repo's event stream and leaves a ghost comment when your criteria is met.
Action bot for Github.
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/w33ble/gh-action-bot/master/LICENSE)
[![Project Status](https://img.shields.io/badge/status-stable-green.svg)](https://nodejs.org/api/documentation.html#documentation_stability_index)
## Usage
`yarn start owner/repo`: watch the event stream and handle matches
`yarn run force owner/repo pr_number`: force a comment on a specific pr, by number
`yanr run clean owner/repo pr_number`: removed any comments that match the owner and body requirements
You'll need to set some environment flags. Check `sample-env` for what is available, and you can copy the file to `.env` in the root and add your settings there as well.
### Personal Access Token
You will need to provide a Github [Personal Access Token](https://github.com/settings/tokens/new). It will need `write:org` access in order to create comments.
### Debugging
If the script isn't doing what you expect, you can get more output from it by prepending the script with `DEBUG=1`.
[![npm](https://img.shields.io/npm/v/gh-action-bot.svg)](https://www.npmjs.com/package/gh-action-bot)
[![Project Status](https://img.shields.io/badge/status-experimental-orange.svg)](https://nodejs.org/api/documentation.html#documentation_stability_index)
#### License
MIT © [w33ble](https://github.com/w33ble)
MIT © [w33ble](https://github.com/w33ble)

View File

View File

@@ -2,4 +2,4 @@
require = require('esm')(module);
const mod = require('./src/index.mjs').default;
mod();
module.exports = mod;

View File

@@ -1,3 +1,3 @@
import mod from './src/index.mjs';
mod();
export default mod;

View File

@@ -1,7 +1,6 @@
{
"name": "gh-action-bot",
"version": "1.4.0",
"private": true,
"version": "0.0.0",
"description": "Action bot for Github",
"main": "index",
"module": "index.mjs",
@@ -9,9 +8,7 @@
"lint": "eslint \"*.{js,mjs}\" \"src/**/*.{js,mjs}\"",
"precommit": "lint-staged",
"version": "auto-changelog -p && auto-authors && git add CHANGELOG.md AUTHORS.md",
"start": "node .",
"force": "node -r esm src/force.mjs",
"clean": "node -r esm src/clean.mjs"
"start": "node ."
},
"repository": {
"type": "git",

View File

@@ -1,7 +0,0 @@
GITHUB_ACCESS_TOKEN=xxx
COMMENT_ACTOR=username
COMMENT_BODY_REGEXP=build failed
COMMENT_BODY_REGEXP_FLAGS=i
PULL_LABEL_FILTER=
PULL_AUTHOR_FILTER=
PULL_RETEST_BODY=retest

View File

@@ -1,28 +0,0 @@
import dotenv from 'dotenv';
import logger from './lib/logger.mjs';
import createRepo from './lib/create_repo.mjs';
import getPullComments from './lib/get_pull_comments.mjs';
import { deleteComment } from './lib/comments.mjs';
dotenv.config();
async function forceRetest() {
const [ownerRepo, prNumber] = process.argv.splice(2);
const { COMMENT_BODY_REGEXP, COMMENT_BODY_REGEXP_FLAGS, COMMENT_ACTOR } = process.env;
if (!ownerRepo || !prNumber) {
throw new Error('You must specify an owner/repo combo and a pull request number');
}
const repo = createRepo(ownerRepo);
const comments = await getPullComments(repo, prNumber);
const bodyRegexp = new RegExp(COMMENT_BODY_REGEXP, COMMENT_BODY_REGEXP_FLAGS);
comments
.filter(comment => bodyRegexp.test(comment.body) && comment.owner === COMMENT_ACTOR)
.forEach(async comment => {
await deleteComment(repo, comment.id);
});
}
forceRetest().catch(err => logger.error(err));

View File

@@ -1,20 +0,0 @@
import dotenv from 'dotenv';
import logger from './lib/logger.mjs';
import createRepo from './lib/create_repo.mjs';
import createPullComment from './lib/create_pull_comment.mjs';
dotenv.config();
async function forceRetest() {
const [ownerRepo, prNumber] = process.argv.splice(2);
const { PULL_RETEST_BODY } = process.env;
if (!ownerRepo || !prNumber) {
throw new Error('You must specify an owner/repo combo and a pull request number');
}
const repo = createRepo(ownerRepo);
await createPullComment(repo, prNumber, PULL_RETEST_BODY, { delete: true });
}
forceRetest().catch(err => logger.error(err));

View File

@@ -1,135 +1,3 @@
import dotenv from 'dotenv';
import logger from './lib/logger.mjs';
import createRepo from './lib/create_repo.mjs';
import getComments from './lib/get_comments.mjs';
import getPull from './lib/get_pull.mjs';
import getCommits from './lib/get_commits.mjs';
import getCommitStatus from './lib/get_commit_status.mjs';
import createPullComment from './lib/create_pull_comment.mjs';
import execAndCheck from './lib/exec_and_check.mjs';
import History from './lib/history.mjs';
// load env vars from .env file
dotenv.config();
const sleep = async ms => new Promise(resolve => setTimeout(resolve, ms));
async function ghActionBot() {
// parse repo name from cli and create repo instance
const repo = createRepo(process.argv.splice(2)[0]);
const {
COMMENT_BODY_REGEXP,
COMMENT_BODY_REGEXP_FLAGS,
COMMENT_ACTOR,
PULL_LABEL_FILTER,
PULL_LABEL_EXCLUDE,
PULL_AUTHOR_FILTER,
PULL_RETEST_BODY,
ACTION_RETRY_DELAY = 3000,
} = process.env;
// load the history module
const history = new History();
// fetch comment info from event stream, filter for only new comments
const comments = (await getComments(repo, {
body: new RegExp(COMMENT_BODY_REGEXP, COMMENT_BODY_REGEXP_FLAGS),
actor: COMMENT_ACTOR,
})).filter(comment => !history.get(comment.id)); // skip comments already processed
if (comments.length) logger.debug(`Found ${comments.length} comments to process`);
// read pull data
const pulls = (await Promise.all(
comments.map(async comment => {
// keep track of comments that have been processed
history.add(comment);
logger.debug(`PROCESS COMMENT ON #${comment.number}`);
const pull = await getPull(repo, comment.number);
// filter out any closed pulls
if (pull.state !== 'open') {
logger.debug(`SKIP PULL #${pull.number}: state is ${pull.state}`);
return false;
}
// filter on owner
if (PULL_AUTHOR_FILTER && pull.owner !== PULL_AUTHOR_FILTER) {
logger.debug(`SKIP PULL #${pull.number}: author is ${pull.owner}`);
return false;
}
// filter on label
if (!PULL_LABEL_FILTER.split(',').every(label => pull.labels.includes(label))) {
logger.debug(
`SKIP PULL #${
pull.number
}: labels missing '${PULL_LABEL_FILTER}', found ${pull.labels.join(',')}`
);
return false;
}
// filter out matching exclusion tags
if (PULL_LABEL_EXCLUDE) {
const exclude = PULL_LABEL_EXCLUDE.split(',').some(label => pull.labels.includes(label));
if (exclude) {
logger.debug(
`SKIP PULL #${pull.number}: label matches exclusion labels '${PULL_LABEL_EXCLUDE}'`
);
return false;
}
}
return { comment, pull };
})
)).filter(Boolean);
const records = (await Promise.all(
pulls.map(async ({ pull, comment }) => {
const commit = await getCommits(repo, pull.number, true);
const buildStatus = await getCommitStatus(repo, commit.sha);
// do nothing if the build has not started, or is pending or successful
if (!buildStatus || buildStatus.state === 'pending' || buildStatus.state === 'success') {
logger.debug(`SKIP PULL #${pull.number}: build status is ${buildStatus.state}`);
return false;
}
return { comment, pull, commit, buildStatus };
})
)).filter(Boolean);
logger.log(`${records.length ? `Found ${records.length}` : `≧(´▽`)≦ No`} outstanding failures`);
await Promise.all(
records.map(record =>
execAndCheck(
() => createPullComment(repo, record.pull.number, PULL_RETEST_BODY, { delete: true }),
async () => {
logger.debug(`CHECK #${record.pull.number}: verify state change`);
// wait for the ci to restart
await sleep(ACTION_RETRY_DELAY);
// check that the commit status is now pending
const buildStatus = await getCommitStatus(repo, record.commit.sha);
return buildStatus.state === 'pending';
},
() => {
history.remove(record.comment);
logger.error(`Pull comment failed to trigger action on pull #${record.pull.number}`);
}
)
)
);
}
export default function() {
return ghActionBot().catch(err => {
logger.error(err);
return process.exit(1);
});
// es6 module code goes here
}

View File

@@ -1,12 +0,0 @@
import logger from './logger.mjs';
export async function createComment(repo, issueId, body) {
return repo.issues(issueId).comments.create({ body });
}
export async function deleteComment(repo, commentId) {
const res = await repo.issues.comments(commentId).remove();
if (res) logger.debug(`Comment ${commentId} deleted`);
else logger.error(new Error(`Failed to remove comment ${commentId}`));
return res;
}

View File

@@ -1,14 +0,0 @@
import logger from './logger.mjs';
import { createComment, deleteComment } from './comments.mjs';
export default async function createPullComment(repo, number, body, opts = {}) {
logger.log(`Adding comment to PR #${number}`);
const comment = await createComment(repo, number, body);
logger.debug(`Created comment id ${comment.id}`);
const options = Object.assign({ delete: false }, opts);
if (options.delete) await deleteComment(repo, comment.id);
return comment.id;
}

View File

@@ -1,21 +0,0 @@
/* eslint no-console: 0 */
import Octokat from 'octokat';
function usageError() {
console.error('You must provide a github repo in the form of "owner/repo"');
process.exit(1);
}
export default function(repo) {
if (typeof repo !== 'string' || repo.length === 0) usageError();
const settings = {
token: process.env.GITHUB_ACCESS_TOKEN,
};
const octo = new Octokat(settings);
const [owner, name] = repo.split('/');
if (!owner || !name) usageError();
return octo.repos(owner, name);
}

View File

@@ -1,23 +0,0 @@
export default async function execAndCheck(fn, test, onFailure = () => {}) {
let count = 0;
const tryExec = async () => {
// exec function
await fn();
// check test function result (pass/fail)
const res = await test();
if (res) return;
const { ACTION_RETRY_COUNT = 3 } = process.env;
// if retry limit is hit, call failure function
count += 1;
if (count >= ACTION_RETRY_COUNT) return onFailure(); // eslint-disable-line consistent-return
// retry the function and retest the results
return tryExec(); // eslint-disable-line consistent-return
};
return tryExec();
}

View File

@@ -1,67 +0,0 @@
import logger from './logger.mjs';
function truncate(str, len = 26) {
if (str.length <= len) return str;
return `${str.slice(0, len)}...`;
}
const getCommentEvents = async (repo, lastEvents) => {
// just fetch and return the results
if (lastEvents == null) return repo.events.fetch();
// if there is no next page, just return the passed in object
if (!lastEvents.nextPage) return lastEvents;
// fetch next items and append previous ones
const newEvents = await lastEvents.nextPage.fetch();
newEvents.items = [...lastEvents.items, ...newEvents.items];
return newEvents;
};
export default async function getEvents(repo, { body, actor, pages = 4 } = {}) {
// create a task array that we can reduce into a promise chain
const chain = [];
while (chain.length < pages) chain.push('page');
const events = await chain.reduce(
acc => acc.then(res => getCommentEvents(repo, res)),
Promise.resolve()
);
return events.items
.map(comment => {
let skip = false;
const isComment = comment.type === 'IssueCommentEvent';
const bodyMatch = isComment && (!body || body.test(comment.payload.comment.body));
const isActor = comment.actor.login === actor;
// skip events that don't match the criteria
if (!isComment) skip = true;
if (comment.payload.action !== 'created') skip = true;
if (!isActor) skip = true;
if (isComment && !bodyMatch) skip = true;
if (skip) {
if (isActor) {
logger.debug(
`SKIP EVENT ${isComment ? `#${comment.payload.issue.number}` : comment.id}: ${
comment.type
} ${comment.payload.action} by ${comment.actor.login}`,
isComment && !bodyMatch && `(${truncate(comment.payload.comment.body)})`
);
}
return false;
}
return {
id: comment.id,
number: comment.payload.issue.number,
owner: comment.payload.issue.user.login,
labels: comment.payload.issue.labels.map(label => label.name),
comment_id: comment.payload.comment.id,
comment_author: comment.payload.comment.user.login,
comment_body: comment.payload.comment.body,
};
})
.filter(Boolean);
}

View File

@@ -1,22 +0,0 @@
function getBuildStatus({ items: statuses }) {
const buildStatus = statuses.find(status => status.context === 'kibana-ci');
if (!buildStatus) return buildStatus;
return {
id: buildStatus.id,
url: buildStatus.url,
state: buildStatus.state,
};
}
export default async function getCommitStatus(repo, sha) {
const statuses = await repo.commits(sha).statuses.fetch();
const buildStatus = await (async stats => {
if (stats.lastPage) {
return getBuildStatus(await stats.lastPage.fetch());
}
return getBuildStatus(stats);
})(statuses);
return buildStatus;
}

View File

@@ -1,23 +0,0 @@
function formatCommit(commit) {
return {
sha: commit.sha,
owner: commit.committer.login,
message: commit.commit.message,
};
}
function formatCommits(commits, lastOnly) {
if (lastOnly) return formatCommit(commits.pop());
return commits.map(c => formatCommit(c));
}
export default async function getCommits(repo, id, lastOnly = true) {
const commits = await repo.pulls(id).commits.fetch();
if (commits.lastPage) {
const { items } = await commits.lastPage.fetch();
return formatCommits(items, lastOnly);
}
return formatCommits(commits.items, lastOnly);
}

View File

@@ -1,12 +0,0 @@
export default async function getPull(repo, id) {
const pull = await repo.pulls(id).fetch();
return {
id: pull.id,
url: pull.url,
number: pull.number,
owner: pull.user.login,
state: pull.state,
title: pull.title,
labels: pull.labels.map(label => label.name),
};
}

View File

@@ -1,9 +0,0 @@
export default async function getPull(repo, id) {
const { items } = await repo.issues(id).comments.fetch();
return items.map(item => ({
id: item.id,
owner: item.user.login,
body: item.body,
}));
}

View File

@@ -1,55 +0,0 @@
import fs from 'fs';
import path from 'path';
const MAX_ITEMS = 1000; // max entries in history
const TRUNCATE_AMOUNT = 200; // count of entries to remove when max is hit
export default class History {
constructor(filename = 'history.json') {
this.filePath = path.resolve('data', filename);
try {
const data = fs.readFileSync(this.filePath);
this.db = data.length === 0 ? [] : JSON.parse(data);
} catch (e) {
this.db = [];
}
}
persist() {
fs.writeFileSync(this.filePath, JSON.stringify(this.db), { encoding: 'utf8' });
}
get(id, index = false) {
if (!index) {
return this.db.find(doc => parseInt(doc.id, 10) === parseInt(id, 10));
}
return this.db.findIndex(doc => parseInt(doc.id, 10) === parseInt(id, 10));
}
add(doc) {
// prevent duplicate entries
if (this.get(doc.id)) return;
// add doc to history
this.db.push(doc);
// truncate as needed, removing first TRUNCATE_AMOUNT entries from the array
if (this.db.length > MAX_ITEMS) this.db.splice(0, TRUNCATE_AMOUNT);
// write the updated content to the file
this.persist();
}
remove(doc) {
const idx = this.get(doc.id, true);
// removing document at matched index
if (idx !== -1) {
this.db.splice(idx, 1);
// write the updated content to the file
this.persist();
}
}
}

View File

@@ -1,43 +0,0 @@
/* eslint no-console: 0 */
const zeroPad = (str, len) => {
let output = `${str}`;
if (!len || len <= output.length) return str;
while (output.length < len) {
output = `0${output}`;
}
return output;
};
const getTimestamp = () => {
const d = new Date();
return `${[d.getFullYear(), zeroPad(d.getMonth() + 1, 2), zeroPad(d.getDate(), 2)].join('/')} ${[
zeroPad(d.getHours(), 2),
zeroPad(d.getMinutes(), 2),
zeroPad(d.getSeconds(), 2),
].join(':')}`;
};
const wrapMessage = msg => `[${getTimestamp()}]: ${msg.join(' ')}`;
const logger = {
log(...args) {
console.log(wrapMessage(args));
},
warn(...args) {
console.warn(wrapMessage(args));
},
error(...args) {
if (args[0] instanceof Error) {
console.error(getTimestamp(), args[0].stack);
return;
}
console.error(wrapMessage(args));
},
debug(...args) {
if (process.env.DEBUG === '0' || !process.env.DEBUG) return;
console.log(wrapMessage(args));
},
};
export default logger;