Skip to content

Commit 2f3843c

Browse files
committed
init
0 parents  commit 2f3843c

9 files changed

Lines changed: 255 additions & 0 deletions

File tree

.eslintrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "standard",
3+
"rules": {
4+
"semi": ["error", "always"],
5+
"space-before-function-paren": ["error", "never"],
6+
"no-multi-spaces": ["error", { "ignoreEOLComments": true }],
7+
"camelcase": "off"
8+
}
9+
}

index.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
5+
const PR_QUERY = fs.readFileSync('./queries/PR.gql', 'utf8');
6+
const request = require('./lib/request');
7+
const logger = require('./lib/logger');
8+
const PR_ID = parseInt(process.argv[2]) || 12756;
9+
const OWNER = 'nodejs';
10+
const REPO = 'node';
11+
12+
function getReviewers(pr) {
13+
return []; // TODO
14+
}
15+
16+
function getFixes(pr) {
17+
return []; // TODO
18+
}
19+
20+
function getRefs(pr) {
21+
return []; // TODO
22+
}
23+
24+
async function main() {
25+
logger.info(`Requesting ${OWNER}/${REPO}/pull/${PR_ID}`);
26+
const data = await request(PR_QUERY, {
27+
prid: PR_ID,
28+
owner: OWNER,
29+
repo: REPO
30+
});
31+
32+
const pr = data.repository.pullRequest;
33+
const output = {
34+
prUrl: pr.url,
35+
reviewedBy: getReviewers(pr),
36+
fixes: getFixes(pr),
37+
refs: getRefs(pr)
38+
};
39+
40+
let meta = [
41+
'-------------------------------- >8 --------------------------------',
42+
`PR-URL: ${output.prUrl}`];
43+
meta = meta.concat(output.reviewedBy.map((reviewer) => {
44+
return `Reviewed-By: ${reviewer.name} <${reviewer.email}>`;
45+
}));
46+
meta = meta.concat(output.fixes.map((fix) => {
47+
return `Fixes: ${fix}`;
48+
}));
49+
meta = meta.concat(output.refs.map((ref) => {
50+
return `Refs: ${ref}`;
51+
}));
52+
meta.push(
53+
'-------------------------------- 8< --------------------------------'
54+
);
55+
56+
logger.info({ raw: meta.join('\n') }, `Generated metadta:`);
57+
}
58+
59+
main().catch((err) => {
60+
logger.error(err);
61+
process.exit(-1);
62+
});

lib/auth.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const os = require('os');
5+
const path = require('path');
6+
const authFile = path.join(os.homedir(), '.ncurc');
7+
// TODO: try-catch, validate properties
8+
const { username, token } = JSON.parse(fs.readFileSync(authFile, 'utf8'));
9+
const auth = Buffer.from(`${username}:${token}`).toString('base64');
10+
module.exports = auth;

lib/collaborators.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const rp = require('request-promise-native');
4+
5+
async function getCollaborators(owner, repo) {
6+
// This is more or less taken from
7+
// https://github.com/rvagg/iojs-tools/blob/master/pr-metadata/pr-metadata.js
8+
const RE = /\* \[(.+?)\]\(.+?\) -\s\*\*(.+?)\*\* &lt;(.+?)&gt;/mg;
9+
const url = `https://raw.githubusercontent.com/${owner}/${repo}/master/README.md`;
10+
11+
const response = await rp({
12+
url: url
13+
});
14+
15+
const members = new Map();
16+
let m;
17+
18+
while (m = RE.exec(response)) { // eslint-disable-line no-cond-assign
19+
members.set(m[1].toLowerCase(), {
20+
login: m[1],
21+
name: m[2],
22+
email: m[3]
23+
});
24+
}
25+
26+
if (!members.size) {
27+
throw new Error('Could not find any collaborators');
28+
}
29+
30+
return members;
31+
}
32+
33+
module.exports = {
34+
getCollaborators
35+
};

lib/logger.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
const pino = require('pino');
4+
const LEVELS = new Map(Object.keys(pino.levels.values)
5+
.map((key) => [pino.levels.values[key], key.toUpperCase()]));
6+
const pretty = pino.pretty({
7+
forceColor: true,
8+
formatter(obj) {
9+
const level = LEVELS.get(obj.level);
10+
let timestamp = '';
11+
if (obj.showTime) {
12+
timestamp = `[${new Date(obj.time).toISOString()}] `;
13+
}
14+
if (level === 'ERROR') {
15+
return `[${level}] ${timestamp}${obj.type} ${obj.msg}\n` +
16+
`[STACK] ${obj.stack}\n` +
17+
`[DATA] ${JSON.stringify(obj.data, null, 2)}\n`;
18+
} else if (level === 'INFO' && obj.raw) {
19+
return `[${level}] ${timestamp}${obj.msg || ''}\n ${obj.raw}`;
20+
} else {
21+
return `[${level}] ${timestamp}${obj.msg}`;
22+
}
23+
}
24+
});
25+
pretty.pipe(process.stdout);
26+
const logger = pino({
27+
name: 'node-core-utils',
28+
safe: true
29+
}, pretty);
30+
module.exports = logger;

lib/request.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const rp = require('request-promise-native');
4+
const auth = require('./auth');
5+
6+
async function request(query, variables) {
7+
const options = {
8+
uri: 'https://api.github.com/graphql',
9+
method: 'POST',
10+
headers: {
11+
'Authorization': `Basic ${auth}`,
12+
'User-Agent': 'node-check-pr'
13+
},
14+
json: true,
15+
gzip: true,
16+
body: {
17+
query: query,
18+
variables: variables
19+
}
20+
};
21+
// console.log(options);
22+
const result = await rp(options);
23+
if (result.errors) {
24+
const err = new Error('GraphQL request Error');
25+
err.data = {
26+
// query: query,
27+
variables: variables,
28+
errors: result.errors
29+
};
30+
throw err;
31+
}
32+
return result.data;
33+
}
34+
35+
module.exports = request;

lib/user.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
function isTheSamePerson(actor, b) {
4+
if (!actor || !actor.login) return false; // ghost
5+
return actor.login.toLowerCase() === b.toLowerCase();
6+
}
7+
8+
module.exports = {
9+
isTheSamePerson
10+
};

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "node-core-utils",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "Joyee Cheung <joyeec9h3@gmail.com>",
10+
"license": "MIT",
11+
"dependencies": {
12+
"pino": "^4.8.0",
13+
"request": "^2.83.0",
14+
"request-promise-native": "^1.0.5"
15+
}
16+
}

queries/PR.gql

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
query PR($prid: Int!, $owner: String!, $repo: String!) {
2+
repository(owner: $owner, name: $repo) {
3+
pullRequest(number: $prid) {
4+
url,
5+
bodyText,
6+
commits(first: 100) {
7+
totalCount
8+
nodes {
9+
commit {
10+
committedDate
11+
author {
12+
email
13+
name
14+
}
15+
committer {
16+
email
17+
name
18+
}
19+
oid
20+
message
21+
authoredByCommitter
22+
}
23+
}
24+
}
25+
comments(first: 100) {
26+
totalCount
27+
nodes {
28+
bodyText
29+
author {
30+
login
31+
}
32+
}
33+
}
34+
reviews(first: 100) {
35+
totalCount
36+
nodes {
37+
state
38+
author {
39+
login
40+
resourcePath
41+
}
42+
authorAssociation
43+
publishedAt
44+
}
45+
}
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)