Skip to content

Commit

Permalink
fix: consider pull requests from forks
Browse files Browse the repository at this point in the history
* remove overzealous shallow check from core
* disable tests for now
* use TRAVIS_COMMIT_RANGE
* always fetch both base and head remotes
* lint triggering commit
  • Loading branch information
marionebl committed Nov 30, 2017
1 parent 5190241 commit 4653c2c
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 168 deletions.
5 changes: 0 additions & 5 deletions @commitlint/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,9 @@
"babel-register": "6.26.0",
"concurrently": "3.5.1",
"cross-env": "5.1.1",
"denodeify": "1.2.1",
"dependency-check": "2.7.0",
"execa": "0.8.0",
"globby": "6.1.0",
"import-from": "2.1.0",
"path-exists": "3.0.0",
"resolve-from": "4.0.0",
"rimraf": "2.6.1",
"xo": "0.18.2"
},
Expand All @@ -83,7 +79,6 @@
"cosmiconfig": "^3.0.1",
"find-up": "^2.1.0",
"lodash": "^4.17.4",
"path-exists": "^3.0.0",
"require-uncached": "^1.0.3",
"resolve-from": "^4.0.0",
"resolve-global": "^0.1.0",
Expand Down
24 changes: 0 additions & 24 deletions @commitlint/core/src/read.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import path from 'path';
import exists from 'path-exists';
import gitRawCommits from '@marionebl/git-raw-commits';
import * as sander from '@marionebl/sander';

import toplevel from './library/toplevel';

export default getCommitMessages;

const SHALLOW_MESSAGE = [
'Could not get git history from shallow clone.',
'Use git fetch --unshallow before linting.',
'Original issue: https://git.io/vyKMq\n Refer to https://git.io/vyKMv for details.'
].join('\n');

// Get commit messages
// Object => Promise<Array<String>>
async function getCommitMessages(settings) {
Expand All @@ -22,10 +15,6 @@ async function getCommitMessages(settings) {
return getEditCommit(cwd, edit);
}

if (await isShallow(cwd)) {
throw new Error(SHALLOW_MESSAGE);
}

return getHistoryCommits({from, to}, {cwd});
}

Expand All @@ -43,19 +32,6 @@ function getHistoryCommits(options, opts = {}) {
});
}

// Check if the current repository is shallow
// (cwd: string) => Promise<Boolean>
async function isShallow(cwd) {
const top = await toplevel(cwd);

if (typeof top !== 'string') {
throw new TypeError(`Could not find git root from ${cwd}`);
}

const shallow = path.join(top, '.git/shallow');
return exists(shallow);
}

// Get recently edited commit message
// (cwd: string, edit: any) => Promise<Array<String>>
async function getEditCommit(cwd, edit) {
Expand Down
10 changes: 0 additions & 10 deletions @commitlint/core/src/read.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import test from 'ava';
import execa from 'execa';
import * as sander from '@marionebl/sander';

import pkg from '../package';
import read from './read';

test('get edit commit message specified by the `edit` flag', async t => {
Expand Down Expand Up @@ -52,12 +51,3 @@ test('get edit commit message from git subdirectory', async t => {
const actual = await read({edit: true, cwd});
t.deepEqual(actual, expected);
});

test('get history commit messages from shallow clone', async t => {
const cwd = await git.clone(pkg.repository.url, '--depth', '1');
const err = await t.throws(read({from: 'master', cwd}));

t.true(
err.message.indexOf('Could not get git history from shallow clone') > -1
);
});
5 changes: 2 additions & 3 deletions @commitlint/travis-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@
},
"dependencies": {
"@commitlint/cli": "^5.1.1",
"@marionebl/sander": "^0.6.1",
"execa": "^0.8.0",
"find-up": "^2.1.0"
"babel-runtime": "^6.26.0",
"execa": "^0.8.0"
}
}
88 changes: 54 additions & 34 deletions @commitlint/travis-cli/src/cli.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,62 @@
#!/usr/bin/env node
const sander = require('@marionebl/sander');
const execa = require('execa');
const findUp = require('find-up');
const commitlint = require('@commitlint/cli');

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

const TRAVIS_BRANCH = process.env.TRAVIS_BRANCH;
const TRAVIS_COMMIT = process.env.TRAVIS_COMMIT;
const REQUIRED = [
'TRAVIS_COMMIT',
'TRAVIS_COMMIT_RANGE',
'TRAVIS_REPO_SLUG',
'TRAVIS_PULL_REQUEST_SLUG'
];

const COMMIT = process.env.TRAVIS_COMMIT;
const REPO_SLUG = process.env.TRAVIS_REPO_SLUG;
const PR_SLUG = process.env.TRAVIS_PULL_REQUEST_SLUG || REPO_SLUG;
const RANGE = process.env.TRAVIS_COMMIT_RANGE;

main().catch(err => {
console.log(err);
process.exit(1);
});

async function main() {
if (process.env.CI !== 'true' || process.env.TRAVIS !== 'true') {
throw new Error(
`@commitlint/travis-cli is inteded to be used on Travis CI`
);
}

const gitRoot = await findUp('.git');
const missing = REQUIRED.filter(envVar => !(envVar in process.env));

if (missing.length > 0) {
const stanza = missing.length > 1 ? 'they were not' : 'it was not';
throw new Error(
`Expected ${missing.join(', ')} to be defined globally, ${stanza}.`
);
}
validate();

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

await git(['remote', 'set-branches', 'origin', TRAVIS_BRANCH]);

if (await sander.exists(gitRoot, 'shallow')) {
await git(['fetch', '--unshallow', '--quiet']);
}

await git(['checkout', TRAVIS_BRANCH, '--quiet']);
await git(['checkout', '-', '--quiet']);
// Make base and source available as dedicated remotes
await Promise.all([
() => fetch({name: 'base', url: `https://github.com/${REPO_SLUG}.git`}),
() => fetch({name: 'source', url: `https://github.com/${PR_SLUG}.git`})
]);

// Restore stashed changes if any
await pop();

await lint(['--from', TRAVIS_BRANCH, '--to', TRAVIS_COMMIT]);
// Lint all commits in TRAVIS_COMMIT_RANGE if available
if (RANGE) {
const [start, end] = RANGE.split('.').filter(Boolean);
await lint(['--from', start, '--to', end]);
}

// Always lint the triggering commit indicated by TRAVIS_COMMIT
await lint(['--from', COMMIT]);
}

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

async function fetch({name, url}) {
await git(['remote', 'add', name, url]);
await git(['fetch', name, '--quiet']);
}

async function isClean() {
const result = await git(['status', '--porcelain'], {
stdio: ['pipe', 'pipe', 'pipe']
Expand All @@ -63,7 +66,7 @@ async function isClean() {

async function lint(args, options) {
return execa(
COMMITLINT,
COMMITLINT || commitlint,
args,
Object.assign({}, {stdio: 'inherit'}, options)
);
Expand All @@ -73,6 +76,23 @@ async function stash() {
if (await isClean()) {
return async () => {};
}
await git(['stash']);
return () => git(['stash', 'pop']);
await git(['stash', '-k', '-u', '--quiet']);
return () => git(['stash', 'pop', '--quiet']);
}

function validate() {
if (process.env.CI !== 'true' || process.env.TRAVIS !== 'true') {
throw new Error(
`@commitlint/travis-cli is inteded to be used on Travis CI`
);
}

const missing = REQUIRED.filter(envVar => !(envVar in process.env));

if (missing.length > 0) {
const stanza = missing.length > 1 ? 'they were not' : 'it was not';
throw new Error(
`Expected ${missing.join(', ')} to be defined globally, ${stanza}.`
);
}
}
Loading

0 comments on commit 4653c2c

Please sign in to comment.