Skip to content

Commit b94a608

Browse files
priyank-pjoyeecheung
authored andcommitted
pr_checker: Log a error is pr author has incorrect git config.
Fixes: #150
1 parent a15715a commit b94a608

File tree

5 files changed

+112
-9
lines changed

5 files changed

+112
-9
lines changed

lib/pr_checker.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ class PRChecker {
5454
this.checkCI(),
5555
this.checkPRWait(new Date()),
5656
this.checkMergeableState(),
57-
this.checkPRState()
57+
this.checkPRState(),
58+
this.checkGitConfig()
5859
];
5960

6061
if (this.data.authorIsNew()) {
@@ -277,7 +278,7 @@ class PRChecker {
277278
}
278279

279280
isOddAuthor(commit) {
280-
const { pr, collaboratorEmails } = this;
281+
const { pr } = this;
281282

282283
// They have turned on the private email feature, can't really check
283284
// anything, GitHub should know how to link that, see nodejs/node#15489
@@ -291,24 +292,30 @@ class PRChecker {
291292
return false;
292293
}
293294

294-
// The commit author is one of the collaborators, they should know
295-
// what they are doing anyway
296-
if (collaboratorEmails.has(commit.author.email)) {
297-
return false;
298-
}
299-
300295
if (commit.author.email === pr.author.email) {
301296
return false;
302297
}
303298

304299
// At this point, the commit:
305300
// 1. is not authored by the commiter i.e. author email is not in the
306301
// committer's Github account
307-
// 2. is not authored by a collaborator
308302
// 3. is not authored by the people opening the PR
309303
return true;
310304
}
311305

306+
checkGitConfig() {
307+
const { cli, commits } = this;
308+
for (let { commit } of commits) {
309+
if (commit.author.user === null) {
310+
const msg = 'Author does not have correct git config!';
311+
cli.error(msg);
312+
return false;
313+
}
314+
}
315+
316+
return true;
317+
}
318+
312319
checkCommitsAfterReview() {
313320
const {
314321
commits, reviews, cli, argv

lib/queries/PRCommits.gql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ query Commits($prid: Int!, $owner: String!, $repo: String!, $after: String) {
1111
commit {
1212
committedDate
1313
author {
14+
user {
15+
login
16+
}
1417
email
1518
name
1619
}

test/fixtures/data.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const commentsWithCI = readJSON('comments_with_ci.json');
2727
const commentsWithLGTM = readJSON('comments_with_lgtm.json');
2828

2929
const oddCommits = readJSON('odd_commits.json');
30+
const incorrectGitConfigCommits = readJSON('incorrect_git_config_commits.json');
3031
const simpleCommits = readJSON('simple_commits.json');
3132

3233
const collabArr = readJSON('collaborators.json');
@@ -79,6 +80,7 @@ module.exports = {
7980
commentsWithCI,
8081
commentsWithLGTM,
8182
oddCommits,
83+
incorrectGitConfigCommits,
8284
simpleCommits,
8385
singleCommitAfterReview,
8486
multipleCommitsAfterReview,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[
2+
{
3+
"commit": {
4+
"committedDate": "2017-10-26T12:35:26Z",
5+
"author": {
6+
"user": null,
7+
"email": "[email protected]",
8+
"name": "Their Github Account email"
9+
},
10+
"committer": {
11+
"email": "[email protected]",
12+
"name": "Their Github Account email"
13+
},
14+
"oid": "ffdef335209c77f66d933bd873950747bfe42264",
15+
"messageHeadline": "doc: some changes",
16+
"message": "Normal commit, same email for everything",
17+
"authoredByCommitter": true
18+
}
19+
},
20+
{
21+
"commit": {
22+
"committedDate": "2017-09-26T12:35:14Z",
23+
"author": {
24+
"email": "[email protected]",
25+
"name": "Their git config user.email"
26+
},
27+
"committer": {
28+
"email": "[email protected]",
29+
"name": "Their Github Account email"
30+
},
31+
"oid": "e3ad7c72e88c83b7184563e8fdb63df02e2fbacb",
32+
"messageHeadline": "doc: some changes 2",
33+
"message": "Either they have not configured git user.email, or have not add the email to their account",
34+
"authoredByCommitter": false
35+
}
36+
}
37+
]
38+

test/unit/pr_checker.test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const {
1818
multipleCommitsAfterReview,
1919
moreThanThreeCommitsAfterReview,
2020
oddCommits,
21+
incorrectGitConfigCommits,
2122
simpleCommits,
2223
commitsAfterCi,
2324
mulipleCommitsAfterCi,
@@ -53,6 +54,7 @@ describe('PRChecker', () => {
5354
let checkCommitsAfterReviewStub;
5455
let checkMergeableStateStub;
5556
let checkPRState;
57+
let checkGitConfigStub;
5658

5759
before(() => {
5860
checkReviewsStub = sinon.stub(checker, 'checkReviews');
@@ -63,6 +65,7 @@ describe('PRChecker', () => {
6365
sinon.stub(checker, 'checkCommitsAfterReview');
6466
checkMergeableStateStub = sinon.stub(checker, 'checkMergeableState');
6567
checkPRState = sinon.stub(checker, 'checkPRState');
68+
checkGitConfigStub = sinon.stub(checker, 'checkGitConfig');
6669
});
6770

6871
after(() => {
@@ -73,6 +76,7 @@ describe('PRChecker', () => {
7376
checkCommitsAfterReviewStub.restore();
7477
checkMergeableStateStub.restore();
7578
checkPRState.restore();
79+
checkGitConfigStub.restore();
7680
});
7781

7882
it('should run necessary checks', () => {
@@ -85,6 +89,7 @@ describe('PRChecker', () => {
8589
assert.strictEqual(checkCommitsAfterReviewStub.calledOnce, true);
8690
assert.strictEqual(checkMergeableStateStub.calledOnce, true);
8791
assert.strictEqual(checkPRState.calledOnce, true);
92+
assert.strictEqual(checkGitConfigStub.calledOnce, true);
8893
});
8994
});
9095

@@ -590,6 +595,54 @@ describe('PRChecker', () => {
590595
});
591596
});
592597

598+
describe('checkGitConfig', () => {
599+
it('should log an error is user has wrong git config', () => {
600+
const cli = new TestCLI();
601+
const expectedLogs = {
602+
error: [
603+
[ 'Author does not have correct git config!' ]
604+
]
605+
};
606+
607+
const data = {
608+
pr: firstTimerPrivatePR,
609+
reviewers: allGreenReviewers,
610+
comments: commentsWithLGTM,
611+
reviews: approvingReviews,
612+
commits: incorrectGitConfigCommits,
613+
collaborators,
614+
authorIsNew: () => true
615+
};
616+
617+
const checker = new PRChecker(cli, data, argv);
618+
const status = checker.checkGitConfig();
619+
620+
assert.deepStrictEqual(status, false);
621+
cli.assertCalledWith(expectedLogs);
622+
});
623+
624+
it('should return the status of true if user had correct config', () => {
625+
const cli = new TestCLI();
626+
const expectedLogs = {};
627+
628+
const data = {
629+
pr: firstTimerPrivatePR,
630+
reviewers: allGreenReviewers,
631+
comments: commentsWithLGTM,
632+
reviews: approvingReviews,
633+
commits: simpleCommits,
634+
collaborators,
635+
authorIsNew: () => true
636+
};
637+
638+
const checker = new PRChecker(cli, data, argv);
639+
const status = checker.checkGitConfig();
640+
641+
assert(status);
642+
cli.assertCalledWith(expectedLogs);
643+
});
644+
});
645+
593646
describe('checkCommitsAfterReview', () => {
594647
let cli = new TestCLI();
595648

0 commit comments

Comments
 (0)