Skip to content

Commit 4653c2c

Browse files
committed
fix: consider pull requests from forks
* remove overzealous shallow check from core * disable tests for now * use TRAVIS_COMMIT_RANGE * always fetch both base and head remotes * lint triggering commit
1 parent 5190241 commit 4653c2c

File tree

8 files changed

+140
-168
lines changed

8 files changed

+140
-168
lines changed

@commitlint/core/package.json

-5
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,9 @@
6363
"babel-register": "6.26.0",
6464
"concurrently": "3.5.1",
6565
"cross-env": "5.1.1",
66-
"denodeify": "1.2.1",
67-
"dependency-check": "2.7.0",
6866
"execa": "0.8.0",
6967
"globby": "6.1.0",
7068
"import-from": "2.1.0",
71-
"path-exists": "3.0.0",
72-
"resolve-from": "4.0.0",
7369
"rimraf": "2.6.1",
7470
"xo": "0.18.2"
7571
},
@@ -83,7 +79,6 @@
8379
"cosmiconfig": "^3.0.1",
8480
"find-up": "^2.1.0",
8581
"lodash": "^4.17.4",
86-
"path-exists": "^3.0.0",
8782
"require-uncached": "^1.0.3",
8883
"resolve-from": "^4.0.0",
8984
"resolve-global": "^0.1.0",

@commitlint/core/src/read.js

-24
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
import path from 'path';
2-
import exists from 'path-exists';
32
import gitRawCommits from '@marionebl/git-raw-commits';
43
import * as sander from '@marionebl/sander';
54

65
import toplevel from './library/toplevel';
76

87
export default getCommitMessages;
98

10-
const SHALLOW_MESSAGE = [
11-
'Could not get git history from shallow clone.',
12-
'Use git fetch --unshallow before linting.',
13-
'Original issue: https://git.io/vyKMq\n Refer to https://git.io/vyKMv for details.'
14-
].join('\n');
15-
169
// Get commit messages
1710
// Object => Promise<Array<String>>
1811
async function getCommitMessages(settings) {
@@ -22,10 +15,6 @@ async function getCommitMessages(settings) {
2215
return getEditCommit(cwd, edit);
2316
}
2417

25-
if (await isShallow(cwd)) {
26-
throw new Error(SHALLOW_MESSAGE);
27-
}
28-
2918
return getHistoryCommits({from, to}, {cwd});
3019
}
3120

@@ -43,19 +32,6 @@ function getHistoryCommits(options, opts = {}) {
4332
});
4433
}
4534

46-
// Check if the current repository is shallow
47-
// (cwd: string) => Promise<Boolean>
48-
async function isShallow(cwd) {
49-
const top = await toplevel(cwd);
50-
51-
if (typeof top !== 'string') {
52-
throw new TypeError(`Could not find git root from ${cwd}`);
53-
}
54-
55-
const shallow = path.join(top, '.git/shallow');
56-
return exists(shallow);
57-
}
58-
5935
// Get recently edited commit message
6036
// (cwd: string, edit: any) => Promise<Array<String>>
6137
async function getEditCommit(cwd, edit) {

@commitlint/core/src/read.test.js

-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import test from 'ava';
33
import execa from 'execa';
44
import * as sander from '@marionebl/sander';
55

6-
import pkg from '../package';
76
import read from './read';
87

98
test('get edit commit message specified by the `edit` flag', async t => {
@@ -52,12 +51,3 @@ test('get edit commit message from git subdirectory', async t => {
5251
const actual = await read({edit: true, cwd});
5352
t.deepEqual(actual, expected);
5453
});
55-
56-
test('get history commit messages from shallow clone', async t => {
57-
const cwd = await git.clone(pkg.repository.url, '--depth', '1');
58-
const err = await t.throws(read({from: 'master', cwd}));
59-
60-
t.true(
61-
err.message.indexOf('Could not get git history from shallow clone') > -1
62-
);
63-
});

@commitlint/travis-cli/package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@
6565
},
6666
"dependencies": {
6767
"@commitlint/cli": "^5.1.1",
68-
"@marionebl/sander": "^0.6.1",
69-
"execa": "^0.8.0",
70-
"find-up": "^2.1.0"
68+
"babel-runtime": "^6.26.0",
69+
"execa": "^0.8.0"
7170
}
7271
}

@commitlint/travis-cli/src/cli.js

+54-34
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,62 @@
11
#!/usr/bin/env node
2-
const sander = require('@marionebl/sander');
32
const execa = require('execa');
4-
const findUp = require('find-up');
3+
const commitlint = require('@commitlint/cli');
54

65
// Allow to override used bins for testing purposes
76
const GIT = process.env.TRAVIS_COMMITLINT_GIT_BIN || 'git';
8-
const COMMITLINT =
9-
process.env.TRAVIS_COMMITLINT_BIN || require('@commitlint/cli'); // eslint-disable-line import/newline-after-import
10-
const REQUIRED = ['TRAVIS_COMMIT', 'TRAVIS_BRANCH'];
7+
const COMMITLINT = process.env.TRAVIS_COMMITLINT_BIN;
118

12-
const TRAVIS_BRANCH = process.env.TRAVIS_BRANCH;
13-
const TRAVIS_COMMIT = process.env.TRAVIS_COMMIT;
9+
const REQUIRED = [
10+
'TRAVIS_COMMIT',
11+
'TRAVIS_COMMIT_RANGE',
12+
'TRAVIS_REPO_SLUG',
13+
'TRAVIS_PULL_REQUEST_SLUG'
14+
];
15+
16+
const COMMIT = process.env.TRAVIS_COMMIT;
17+
const REPO_SLUG = process.env.TRAVIS_REPO_SLUG;
18+
const PR_SLUG = process.env.TRAVIS_PULL_REQUEST_SLUG || REPO_SLUG;
19+
const RANGE = process.env.TRAVIS_COMMIT_RANGE;
1420

1521
main().catch(err => {
1622
console.log(err);
1723
process.exit(1);
1824
});
1925

2026
async function main() {
21-
if (process.env.CI !== 'true' || process.env.TRAVIS !== 'true') {
22-
throw new Error(
23-
`@commitlint/travis-cli is inteded to be used on Travis CI`
24-
);
25-
}
26-
27-
const gitRoot = await findUp('.git');
28-
const missing = REQUIRED.filter(envVar => !(envVar in process.env));
29-
30-
if (missing.length > 0) {
31-
const stanza = missing.length > 1 ? 'they were not' : 'it was not';
32-
throw new Error(
33-
`Expected ${missing.join(', ')} to be defined globally, ${stanza}.`
34-
);
35-
}
27+
validate();
3628

29+
// Stash changes in working copy if needed
3730
const pop = await stash();
3831

39-
await git(['remote', 'set-branches', 'origin', TRAVIS_BRANCH]);
40-
41-
if (await sander.exists(gitRoot, 'shallow')) {
42-
await git(['fetch', '--unshallow', '--quiet']);
43-
}
44-
45-
await git(['checkout', TRAVIS_BRANCH, '--quiet']);
46-
await git(['checkout', '-', '--quiet']);
32+
// Make base and source available as dedicated remotes
33+
await Promise.all([
34+
() => fetch({name: 'base', url: `https://github.com/${REPO_SLUG}.git`}),
35+
() => fetch({name: 'source', url: `https://github.com/${PR_SLUG}.git`})
36+
]);
4737

38+
// Restore stashed changes if any
4839
await pop();
4940

50-
await lint(['--from', TRAVIS_BRANCH, '--to', TRAVIS_COMMIT]);
41+
// Lint all commits in TRAVIS_COMMIT_RANGE if available
42+
if (RANGE) {
43+
const [start, end] = RANGE.split('.').filter(Boolean);
44+
await lint(['--from', start, '--to', end]);
45+
}
46+
47+
// Always lint the triggering commit indicated by TRAVIS_COMMIT
48+
await lint(['--from', COMMIT]);
5149
}
5250

5351
async function git(args, options) {
5452
return execa(GIT, args, Object.assign({}, {stdio: 'inherit'}, options));
5553
}
5654

55+
async function fetch({name, url}) {
56+
await git(['remote', 'add', name, url]);
57+
await git(['fetch', name, '--quiet']);
58+
}
59+
5760
async function isClean() {
5861
const result = await git(['status', '--porcelain'], {
5962
stdio: ['pipe', 'pipe', 'pipe']
@@ -63,7 +66,7 @@ async function isClean() {
6366

6467
async function lint(args, options) {
6568
return execa(
66-
COMMITLINT,
69+
COMMITLINT || commitlint,
6770
args,
6871
Object.assign({}, {stdio: 'inherit'}, options)
6972
);
@@ -73,6 +76,23 @@ async function stash() {
7376
if (await isClean()) {
7477
return async () => {};
7578
}
76-
await git(['stash']);
77-
return () => git(['stash', 'pop']);
79+
await git(['stash', '-k', '-u', '--quiet']);
80+
return () => git(['stash', 'pop', '--quiet']);
81+
}
82+
83+
function validate() {
84+
if (process.env.CI !== 'true' || process.env.TRAVIS !== 'true') {
85+
throw new Error(
86+
`@commitlint/travis-cli is inteded to be used on Travis CI`
87+
);
88+
}
89+
90+
const missing = REQUIRED.filter(envVar => !(envVar in process.env));
91+
92+
if (missing.length > 0) {
93+
const stanza = missing.length > 1 ? 'they were not' : 'it was not';
94+
throw new Error(
95+
`Expected ${missing.join(', ')} to be defined globally, ${stanza}.`
96+
);
97+
}
7898
}

0 commit comments

Comments
 (0)