Compare commits
4 Commits
v1.3.2
...
ec6edf598c
| Author | SHA1 | Date | |
|---|---|---|---|
| ec6edf598c | |||
| cc7c5968cb | |||
| 5a1d666dae | |||
| bf7159ec33 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -9,3 +9,4 @@ coverage
|
||||
.nyc_output
|
||||
coverage.lcov
|
||||
/lib
|
||||
/data
|
||||
0
data/.empty
Normal file
0
data/.empty
Normal file
2
index.js
2
index.js
@@ -2,4 +2,4 @@
|
||||
require = require('esm')(module);
|
||||
const mod = require('./src/index.mjs').default;
|
||||
|
||||
module.exports = mod;
|
||||
mod();
|
||||
|
||||
@@ -52,7 +52,9 @@
|
||||
"cjs": true
|
||||
},
|
||||
"dependencies": {
|
||||
"esm": "^3.0.17"
|
||||
"dotenv": "^6.1.0",
|
||||
"esm": "^3.0.17",
|
||||
"octokat": "^0.10.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"auto-authors": "^0.1.1",
|
||||
|
||||
@@ -1,3 +1,58 @@
|
||||
export default function() {
|
||||
// es6 module code goes here
|
||||
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 History from './lib/history.mjs';
|
||||
|
||||
export default async function() {
|
||||
// parse repo name from cli and create repo instance
|
||||
const repo = createRepo(process.argv.splice(2)[0]);
|
||||
|
||||
// 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: /build failed/i,
|
||||
actor: 'elasticmachine',
|
||||
})).filter(comment => !history.get(comment.id));
|
||||
|
||||
// read pull data, filter out any closed pulls
|
||||
const pulls = (await Promise.all(
|
||||
comments.map(async comment => {
|
||||
const pull = await getPull(repo, comment.number);
|
||||
if (pull.state !== 'open') 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')
|
||||
return false;
|
||||
return { comment, pull, commit, buildStatus };
|
||||
})
|
||||
)).filter(Boolean);
|
||||
|
||||
console.log(records);
|
||||
process.exit();
|
||||
|
||||
/*
|
||||
|
||||
TODO:
|
||||
|
||||
- [x] keep track of seen comment ids, only process new ones
|
||||
- [x] check the pr's status and only retest if no longer "Pending"
|
||||
- [ ] add a retest comment
|
||||
- POST /repos/:owner/:repo/issues/:number/comments
|
||||
- [ ] delete the build comment
|
||||
- DELETE /repos/:owner/:repo/issues/comments/:comment_id
|
||||
- [ ] delete ALL build failure comments
|
||||
- [ ] delete the retest comment
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
18
src/lib/create_repo.mjs
Normal file
18
src/lib/create_repo.mjs
Normal file
@@ -0,0 +1,18 @@
|
||||
/* 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 octo = new Octokat();
|
||||
const [owner, name] = repo.split('/');
|
||||
|
||||
if (!owner || !name) usageError();
|
||||
|
||||
return octo.repos(owner, name);
|
||||
}
|
||||
23
src/lib/get_comments.mjs
Normal file
23
src/lib/get_comments.mjs
Normal file
@@ -0,0 +1,23 @@
|
||||
export default async function getEvents(repo, { body, actor } = {}) {
|
||||
const { items: events } = await repo.events.fetch();
|
||||
|
||||
return events
|
||||
.map(comment => {
|
||||
// only comments created by specific user
|
||||
if (comment.type !== 'IssueCommentEvent') return false;
|
||||
if (comment.payload.action !== 'created') return false;
|
||||
if (comment.actor.login !== actor) return false;
|
||||
if (body && !body.test(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);
|
||||
}
|
||||
22
src/lib/get_commit_status.mjs
Normal file
22
src/lib/get_commit_status.mjs
Normal file
@@ -0,0 +1,22 @@
|
||||
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;
|
||||
}
|
||||
23
src/lib/get_commits.mjs
Normal file
23
src/lib/get_commits.mjs
Normal file
@@ -0,0 +1,23 @@
|
||||
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);
|
||||
}
|
||||
11
src/lib/get_pull.mjs
Normal file
11
src/lib/get_pull.mjs
Normal file
@@ -0,0 +1,11 @@
|
||||
export default async function getPull(repo, id) {
|
||||
const pull = await repo.pulls(id).fetch();
|
||||
return {
|
||||
id: pull.id,
|
||||
url: pull.url,
|
||||
number: pull.number,
|
||||
state: pull.state,
|
||||
title: pull.title,
|
||||
labels: pull.labels.map(label => label.name),
|
||||
};
|
||||
}
|
||||
30
src/lib/history.mjs
Normal file
30
src/lib/history.mjs
Normal file
@@ -0,0 +1,30 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
export default class History {
|
||||
constructor() {
|
||||
this.filePath = path.resolve('data', 'history.json');
|
||||
try {
|
||||
const data = fs.readFileSync(this.filePath);
|
||||
this.db = data.length === 0 ? [] : JSON.parse(data);
|
||||
} catch (e) {
|
||||
this.db = [];
|
||||
}
|
||||
}
|
||||
|
||||
get(id) {
|
||||
return this.db.find(doc => parseInt(doc.id, 10) === parseInt(id, 10));
|
||||
}
|
||||
|
||||
add(doc) {
|
||||
// prevent duplicate entries
|
||||
if (this.get(doc.id)) return;
|
||||
|
||||
// add doc to history, truncate as needed
|
||||
this.db.push(doc);
|
||||
if (this.db.length > 1000) this.db.splice(0, 1000);
|
||||
|
||||
// write the updated content to the file
|
||||
fs.writeFileSync(this.filePath, JSON.stringify(this.db), { encoding: 'utf8' });
|
||||
}
|
||||
}
|
||||
50
yarn.lock
50
yarn.lock
@@ -561,6 +561,11 @@ doctrine@^2.1.0:
|
||||
dependencies:
|
||||
esutils "^2.0.2"
|
||||
|
||||
dotenv@^6.1.0:
|
||||
version "6.1.0"
|
||||
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-6.1.0.tgz#9853b6ca98292acb7dec67a95018fa40bccff42c"
|
||||
integrity sha512-/veDn2ztgRlB7gKmE3i9f6CmDIyXAy6d5nBq+whO9SLX+Zs1sXEgFLPi+aSuWqUuusMfbi84fT8j34fs1HaYUw==
|
||||
|
||||
elegant-spinner@^1.0.1:
|
||||
version "1.0.1"
|
||||
resolved "https://registry.yarnpkg.com/elegant-spinner/-/elegant-spinner-1.0.1.tgz#db043521c95d7e303fd8f345bedc3349cfb0729e"
|
||||
@@ -571,6 +576,13 @@ emoji-regex@^6.5.1:
|
||||
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-6.5.1.tgz#9baea929b155565c11ea41c6626eaa65cef992c2"
|
||||
integrity sha512-PAHp6TxrCy7MGMFidro8uikr+zlJJKJ/Q6mm2ExZ7HwkyR9lSVFfE3kt36qcwa24BQL7y0G9axycGjK1A/0uNQ==
|
||||
|
||||
encoding@^0.1.11:
|
||||
version "0.1.12"
|
||||
resolved "https://registry.yarnpkg.com/encoding/-/encoding-0.1.12.tgz#538b66f3ee62cd1ab51ec323829d1f9480c74beb"
|
||||
integrity sha1-U4tm8+5izRq1HsMjgp0flIDHS+s=
|
||||
dependencies:
|
||||
iconv-lite "~0.4.13"
|
||||
|
||||
error-ex@^1.2.0, error-ex@^1.3.1:
|
||||
version "1.3.2"
|
||||
resolved "https://registry.yarnpkg.com/error-ex/-/error-ex-1.3.2.tgz#b4ac40648107fdcdcfae242f428bea8a14d4f1bf"
|
||||
@@ -882,6 +894,14 @@ fast-levenshtein@~2.0.4:
|
||||
resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917"
|
||||
integrity sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=
|
||||
|
||||
fetch-vcr@^1.1.0:
|
||||
version "1.1.2"
|
||||
resolved "https://registry.yarnpkg.com/fetch-vcr/-/fetch-vcr-1.1.2.tgz#8cfee49c45b464366b97fe550487dde15b948d21"
|
||||
integrity sha512-bFOx3+5YtViximcqhG05tqMlsyPRXNOmiToDCf6TyVUCKHYP/vGPmn0HUhGVNd1jI0KpElwz+RH3X/ZQo0Asfg==
|
||||
dependencies:
|
||||
node-fetch "^1.6.3"
|
||||
whatwg-fetch "^2.0.3"
|
||||
|
||||
figures@^1.7.0:
|
||||
version "1.7.0"
|
||||
resolved "https://registry.yarnpkg.com/figures/-/figures-1.7.0.tgz#cbe1e3affcf1cd44b80cadfed28dc793a9701d2e"
|
||||
@@ -1106,7 +1126,7 @@ husky@^0.14.3:
|
||||
normalize-path "^1.0.0"
|
||||
strip-indent "^2.0.0"
|
||||
|
||||
iconv-lite@^0.4.17:
|
||||
iconv-lite@^0.4.17, iconv-lite@~0.4.13:
|
||||
version "0.4.24"
|
||||
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b"
|
||||
integrity sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==
|
||||
@@ -1349,7 +1369,7 @@ is-resolvable@^1.0.0:
|
||||
resolved "https://registry.yarnpkg.com/is-resolvable/-/is-resolvable-1.1.0.tgz#fb18f87ce1feb925169c9a407c19318a3206ed88"
|
||||
integrity sha512-qgDYXFSR5WvEfuS5dMj6oTMEbrrSaM0CrFk2Yiq/gXnBvD9pMa2jGXxyhGLfvhZpuMZe18CJpFxAt3CRs42NMg==
|
||||
|
||||
is-stream@^1.1.0:
|
||||
is-stream@^1.0.1, is-stream@^1.1.0:
|
||||
version "1.1.0"
|
||||
resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44"
|
||||
integrity sha1-EtSj3U5o4Lec6428hBc66A2RykQ=
|
||||
@@ -1580,7 +1600,7 @@ lodash.uniqby@^4.7.0:
|
||||
resolved "https://registry.yarnpkg.com/lodash.uniqby/-/lodash.uniqby-4.7.0.tgz#d99c07a669e9e6d24e1362dfe266c67616af1302"
|
||||
integrity sha1-2ZwHpmnp5tJOE2Lf4mbGdhavEwI=
|
||||
|
||||
lodash@^4.17.10, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.3.0:
|
||||
lodash@^4.16.4, lodash@^4.17.10, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.3.0:
|
||||
version "4.17.11"
|
||||
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d"
|
||||
integrity sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==
|
||||
@@ -1737,7 +1757,15 @@ natural-compare@^1.4.0:
|
||||
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
|
||||
integrity sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=
|
||||
|
||||
node-fetch@^2.1.2, node-fetch@^2.2.0:
|
||||
node-fetch@^1.6.3:
|
||||
version "1.7.3"
|
||||
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-1.7.3.tgz#980f6f72d85211a5347c6b2bc18c5b84c3eb47ef"
|
||||
integrity sha512-NhZ4CsKx7cYm2vSrBAr2PvFOe6sWDf0UYLRqA6svUYg7+/TSfVAu49jYC4BvQ4Sms9SZgdqGBgroqfDhJdTyKQ==
|
||||
dependencies:
|
||||
encoding "^0.1.11"
|
||||
is-stream "^1.0.1"
|
||||
|
||||
node-fetch@^2.0.0, node-fetch@^2.1.2, node-fetch@^2.2.0:
|
||||
version "2.2.0"
|
||||
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.2.0.tgz#4ee79bde909262f9775f731e3656d0db55ced5b5"
|
||||
integrity sha512-OayFWziIxiHY8bCUyLX6sTpDH8Jsbp4FfYd1j1f7vZyfgkcOnAyM4oQR16f8a0s7Gl/viMGRey8eScYk4V4EZA==
|
||||
@@ -1818,6 +1846,15 @@ object.pick@^1.3.0:
|
||||
dependencies:
|
||||
isobject "^3.0.1"
|
||||
|
||||
octokat@^0.10.0:
|
||||
version "0.10.0"
|
||||
resolved "https://registry.yarnpkg.com/octokat/-/octokat-0.10.0.tgz#a56846178199f36ddfc8979c9749ffea65736508"
|
||||
integrity sha512-VJ21L1gMlByYMurduLYiOcI8AwlZkUV8OXRN8pMXsbkIqIVqn0tgdTfxzWM9spX4VJTTG02OgqwDTqQsOmDing==
|
||||
dependencies:
|
||||
fetch-vcr "^1.1.0"
|
||||
lodash "^4.16.4"
|
||||
node-fetch "^2.0.0"
|
||||
|
||||
once@^1.3.0:
|
||||
version "1.4.0"
|
||||
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
|
||||
@@ -2576,6 +2613,11 @@ validate-npm-package-license@^3.0.1:
|
||||
spdx-correct "^3.0.0"
|
||||
spdx-expression-parse "^3.0.0"
|
||||
|
||||
whatwg-fetch@^2.0.3:
|
||||
version "2.0.4"
|
||||
resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-2.0.4.tgz#dde6a5df315f9d39991aa17621853d720b85566f"
|
||||
integrity sha512-dcQ1GWpOD/eEQ97k66aiEVpNnapVj90/+R+SXTPYGHpYBBypfKJEQjLrvMZ7YXbKm21gXd4NcuxUTjiv1YtLng==
|
||||
|
||||
which@^1.2.10, which@^1.2.9:
|
||||
version "1.3.1"
|
||||
resolved "https://registry.yarnpkg.com/which/-/which-1.3.1.tgz#a45043d54f5805316da8d62f9f50918d3da70b0a"
|
||||
|
||||
Reference in New Issue
Block a user