Skip to content

Commit 71a4401

Browse files
committed
docs: add recipe for linting of all commits in a PR (#36)
* be more explicit / guide more than #24 * closes #35 * connected to #12
1 parent 2d1d538 commit 71a4401

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

packages/conventional-changelog-lint/readme.md

+42-5
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,53 @@ As part of `npm test`
6767
}
6868
```
6969
70+
#### Lint all commits in Pull Request
71+
72+
You can lint all commits in a PR by passing all commits that
73+
are present in `SOURCE_BRANCH` but unavailable in `BASE_BRANCH`:
74+
75+
```sh
76+
conventional-changelog-lint --from=BASE_BRANCH to=SOURCE_BRANCH
77+
```
78+
79+
Most of the time `BASE_BRANCH` will be `master` for Github Flow.
80+
81+
This assumes `SOURCE_BRANCH` is available on your local checkout.
82+
This is not true by default for all PRs originating from clones of a repository.
83+
84+
Given you'd like to lint all commits in PR origination from branch `remote-test` on the
85+
repository `github.com/other-name/test` targeting `master` on `github.com/your-name/test`:
86+
87+
```sh
88+
cd test # make sure CWD is in your repository
89+
git remote add other-name https://github.com/other-name/test.git
90+
git fetch other-name
91+
92+
conventional-changelog-lint --from=master --to=other-name/test
93+
```
94+
95+
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.
96+
7097
#### Travis
7198
99+
Commit Linting on CI has to handle the following cases
100+
101+
* Direct commits
102+
* Branch Pull Requests
103+
* Fork Pull Requests
104+
105+
An exemplary implementation is provided as bash script working on Travis CI.
106+
72107
```yml
73108
# Force full git checkout
74109
before_install: git fetch --unshallow
75110
76-
# Lint all commits not in the target branch
77-
before_script: conventional-changelog-lint --from=$TRAVIS_BRANCH to=$TRAVIS_PULL_REQUEST_BRANCH
111+
script:
112+
- ./scripts/lint:commit.sh # [1] scripts/lint:commit.sh
78113
```
79114
115+
> \[1\]: See [scripts/lint:commit.sh](./scripts/lint:commit.sh) for reference
116+
80117
### API
81118
82119
The programming interface does not read configuration by default,
@@ -208,7 +245,7 @@ Perform `git fetch --shallow` before linting.
208245
Most likely you are reading this because you where presented with an error message:
209246
210247
```
211-
'Could not get git history from shallow clone.
248+
'Could not get git history from shallow clone.
212249
Use git fetch --shallow before linting.
213250
Original issue: https://git.io/vyKMq\n Refer to https://git.io/vyKMv for details.'
214251
```
@@ -217,7 +254,7 @@ Most likely you are reading this because you where presented with an error messa
217254
218255
git supports checking out `shallow` clones of a repository to save bandwith in times.
219256
These limited copies do not contain a full git history. This makes `conventional-changelog-lint`
220-
fail, especially when running on large commit ranges.
257+
fail, especially when running on large commit ranges.
221258
To ensure linting works every time you should convert a shallow git repo to a complete one.
222259
Use `git fetch --shallow` to do so.
223260
@@ -226,7 +263,7 @@ Use `git fetch --shallow` to do so.
226263
Ensure full git checkouts on TravisCI, add to `.travis.yml`:
227264
228265
```yml
229-
before_install:
266+
before_install:
230267
- git fetch --unshallow
231268
```
232269

packages/conventional-changelog-lint/scripts/lint:commits.sh

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22
set -e
33
set -u
44

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

11-
# Use REMOTE/BRANCH as comparison if applicable
12-
if [[ $TRAVIS_PULL_REQUEST_SLUG != "" ]]; then
10+
# Use the fetched remote pointing to the source clone for comparison
1311
TO="$TRAVIS_PULL_REQUEST_SLUG/$TRAVIS_PULL_REQUEST_BRANCH"
1412
else
15-
TO="$TRAVIS_PULL_REQUEST_BRANCH"
13+
# This is a Pull Request from the same remote, no clone repository
14+
TO=$TRAVIS_COMMIT
1615
fi
1716

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

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

0 commit comments

Comments
 (0)