forked from nodejs/node-core-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8084817
commit 78d4d52
Showing
8 changed files
with
214 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
function loadQuery(file) { | ||
const filePath = path.resolve(__dirname, '..', 'queries', `${file}.gql`); | ||
return fs.readFileSync(filePath, 'utf8'); | ||
} | ||
|
||
const PR_QUERY = loadQuery('PR'); | ||
const REVIEWS_QUERY = loadQuery('Reviews'); | ||
const COMMENTS_QUERY = loadQuery('PRComments'); | ||
const COMMITS_QUERY = loadQuery('PRCommits'); | ||
const USER_QUERY = loadQuery('User'); | ||
|
||
// TODO(joyeecheung): make it mockable with req.rp ? | ||
const { getCollaborators } = require('../lib/collaborators'); | ||
const { ReviewAnalyzer } = require('../lib/reviews'); | ||
|
||
class PRData { | ||
/** | ||
* @param {number} prid | ||
* @param {string} owner | ||
* @param {string} repo | ||
* @param {*} logger | ||
* @param {{request: Function, requestAll: Function}} req | ||
*/ | ||
constructor(prid, owner, repo, logger, req) { | ||
this.prid = prid; | ||
this.owner = owner; | ||
this.repo = repo; | ||
this.logger = logger; | ||
this.request = req.request; | ||
this.requestAll = req.requestAll; | ||
|
||
// Data | ||
this.collaborators = new Map(); | ||
this.pr = {}; | ||
this.reviews = []; | ||
this.comments = []; | ||
this.commits = []; | ||
this.reviewers = []; | ||
} | ||
|
||
async getAll() { | ||
await Promise.all([ | ||
this.getCollaborators(), | ||
this.getPR(), | ||
this.getReviews(), | ||
this.getComments(), | ||
this.getCommits() | ||
]); | ||
} | ||
|
||
analyzeReviewers() { | ||
this.reviewers = new ReviewAnalyzer(this).getReviewers(); | ||
} | ||
|
||
async getCollaborators() { | ||
const { owner, repo, logger } = this; | ||
logger.trace(`Getting collaborator contacts from README of ${owner}/${repo}`); | ||
this.collaborators = await getCollaborators(logger, owner, repo); | ||
} | ||
|
||
async getPR() { | ||
const { prid, owner, repo, logger, request } = this; | ||
logger.trace(`Getting PR from ${owner}/${repo}/pull/${prid}`); | ||
const prData = await request(PR_QUERY, { prid, owner, repo }); | ||
const pr = this.pr = prData.repository.pullRequest; | ||
// Get the mail | ||
logger.trace(`Getting User information for ${pr.author.login}`); | ||
const userData = await request(USER_QUERY, { login: pr.author.login }); | ||
const user = userData.user; | ||
Object.assign(this.pr.author, user); | ||
} | ||
|
||
async getReviews() { | ||
const { prid, owner, repo, logger, requestAll } = this; | ||
const vars = { prid, owner, repo }; | ||
logger.trace(`Getting reviews from ${owner}/${repo}/pull/${prid}`); | ||
this.reviews = await requestAll(REVIEWS_QUERY, vars, [ | ||
'repository', 'pullRequest', 'reviews' | ||
]); | ||
} | ||
|
||
async getComments() { | ||
const { prid, owner, repo, logger, requestAll } = this; | ||
const vars = { prid, owner, repo }; | ||
logger.trace(`Getting comments from ${owner}/${repo}/pull/${prid}`); | ||
this.comments = await requestAll(COMMENTS_QUERY, vars, [ | ||
'repository', 'pullRequest', 'comments' | ||
]); | ||
} | ||
|
||
async getCommits() { | ||
const { prid, owner, repo, logger, requestAll } = this; | ||
const vars = { prid, owner, repo }; | ||
logger.trace(`Getting commits from ${owner}/${repo}/pull/${prid}`); | ||
this.commits = await requestAll(COMMITS_QUERY, vars, [ | ||
'repository', 'pullRequest', 'commits' | ||
]); | ||
} | ||
}; | ||
|
||
module.exports = PRData; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,12 @@ const { Collaborator } = require('../../lib/collaborators'); | |
|
||
const approved = readJSON('reviewers_approved.json'); | ||
patchPrototype(approved, 'reviewer', Collaborator.prototype); | ||
const reviewers = { approved, rejected: [] }; | ||
|
||
const pr = readJSON('pr_with_fixes_and_refs.json'); | ||
const data = { | ||
repo: 'node', | ||
pr, | ||
reviewers: { approved, rejected: [] } | ||
}; | ||
|
||
const expected = `PR-URL: https://github.com/nodejs/node/pull/16438 | ||
Fixes: https://github.com/node/issues/16437 | ||
|
@@ -20,7 +23,7 @@ Reviewed-By: Bar User <[email protected]>`; | |
|
||
describe('MetadataGenerator', () => { | ||
it('should generate metadata properly', () => { | ||
const results = new MetadataGenerator('node', pr, reviewers).getMetadata(); | ||
const results = new MetadataGenerator(data).getMetadata(); | ||
assert.strictEqual(expected, results); | ||
}); | ||
}); |
Oops, something went wrong.