Skip to content

Commit

Permalink
docs: add recipe for linting of all commits in a PR (#36)
Browse files Browse the repository at this point in the history
* be more explicit / guide more than #24
* closes #35
* connected to #12
  • Loading branch information
marionebl authored May 4, 2017
1 parent a0914d1 commit 1e69d54
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
47 changes: 42 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,53 @@ As part of `npm test`
}
```
#### Lint all commits in Pull Request
You can lint all commits in a PR by passing all commits that
are present in `SOURCE_BRANCH` but unavailable in `BASE_BRANCH`:
```sh
conventional-changelog-lint --from=BASE_BRANCH to=SOURCE_BRANCH
```
Most of the time `BASE_BRANCH` will be `master` for Github Flow.
This assumes `SOURCE_BRANCH` is available on your local checkout.
This is not true by default for all PRs originating from clones of a repository.
Given you'd like to lint all commits in PR origination from branch `remote-test` on the
repository `github.com/other-name/test` targeting `master` on `github.com/your-name/test`:
```sh
cd test # make sure CWD is in your repository
git remote add other-name https://github.com/other-name/test.git
git fetch other-name
conventional-changelog-lint --from=master --to=other-name/test
```
See [scripts/lint:commit.sh](./scripts/lint:commit.sh#6) for an example on how to obtain `SOURCE_BRANCH` from a Github clone automatically on Travis.
#### Travis
Commit Linting on CI has to handle the following cases
* Direct commits
* Branch Pull Requests
* Fork Pull Requests
An exemplary implementation is provided as bash script working on Travis CI.
```yml
# Force full git checkout
before_install: git fetch --unshallow
# Lint all commits not in the target branch
before_script: conventional-changelog-lint --from=$TRAVIS_BRANCH to=$TRAVIS_PULL_REQUEST_BRANCH
script:
- ./scripts/lint:commit.sh # [1] scripts/lint:commit.sh
```
> \[1\]: See [scripts/lint:commit.sh](./scripts/lint:commit.sh) for reference
### API
The programming interface does not read configuration by default,
Expand Down Expand Up @@ -208,7 +245,7 @@ Perform `git fetch --shallow` before linting.
Most likely you are reading this because you where presented with an error message:
```
'Could not get git history from shallow clone.
'Could not get git history from shallow clone.
Use git fetch --shallow before linting.
Original issue: https://git.io/vyKMq\n Refer to https://git.io/vyKMv for details.'
```
Expand All @@ -217,7 +254,7 @@ Most likely you are reading this because you where presented with an error messa
git supports checking out `shallow` clones of a repository to save bandwith in times.
These limited copies do not contain a full git history. This makes `conventional-changelog-lint`
fail, especially when running on large commit ranges.
fail, especially when running on large commit ranges.
To ensure linting works every time you should convert a shallow git repo to a complete one.
Use `git fetch --shallow` to do so.
Expand All @@ -226,7 +263,7 @@ Use `git fetch --shallow` to do so.
Ensure full git checkouts on TravisCI, add to `.travis.yml`:
```yml
before_install:
before_install:
- git fetch --unshallow
```
Expand Down
12 changes: 7 additions & 5 deletions scripts/lint:commits.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@
set -e
set -u

# Add the clone as remote if this is a PR from a clone
if [[ $TRAVIS_PULL_REQUEST_SLUG != "" && $TRAVIS_PULL_REQUEST_SLUG != $TRAVIS_REPO_SLUG ]]; then
# This is a Pull Request from a different slug, hence a forked repository
git remote add "$TRAVIS_PULL_REQUEST_SLUG" "https://github.com/$TRAVIS_PULL_REQUEST_SLUG.git"
git fetch "$TRAVIS_PULL_REQUEST_SLUG"
fi

# Use REMOTE/BRANCH as comparison if applicable
if [[ $TRAVIS_PULL_REQUEST_SLUG != "" ]]; then
# Use the fetched remote pointing to the source clone for comparison
TO="$TRAVIS_PULL_REQUEST_SLUG/$TRAVIS_PULL_REQUEST_BRANCH"
else
TO="$TRAVIS_PULL_REQUEST_BRANCH"
# This is a Pull Request from the same remote, no clone repository
TO=$TRAVIS_COMMIT
fi

# Lint all commits in the PR
# - Covers fork pull requests (when TO=slug/branch)
# - Covers branch pull requests (when TO=branch)
conventional-changelog-lint --from="$TRAVIS_BRANCH" --to="$TO"

# Always lint the triggerig commit
# - Covers direct commits
conventional-changelog-lint --from="$TRAVIS_COMMIT"

0 comments on commit 1e69d54

Please sign in to comment.