Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools: add script to lint commit messages #23758

Closed
wants to merge 2 commits into from

Conversation

richardlau
Copy link
Member

Currently the "first PR commit message" linting is only done on Travis. This PR attempts to
decouple the logic from Travis and capture the logic in a bash shell script so that it can be
run locally.

I haven't (yet) added the scripts to Makefile or vcbuild.bat as the script requires git, node,
npm and npx being in $PATH.

Refs: #22452

Checklist

@nodejs-github-bot nodejs-github-bot added the tools Issues and PRs related to the tools directory. label Oct 19, 2018
MINOR_VERSION=$( grep '#define NODE_MINOR_VERSION [0-9][0-9]*' "${ROOT_DIR}/src/node_version.h" | awk '{print $(NF)}' )
PATCH_VERSION=$( grep '#define NODE_PATCH_VERSION [0-9][0-9]*' "${ROOT_DIR}/src/node_version.h" | awk '{print $(NF)}' )
# If the version is not in the CHANGELOG assume the target branch is master
if grep "${MAJOR_VERSION}\.${MINOR_VERSION}\.${PATCH_VERSION}" "${ROOT_DIR}/CHANGELOG.md" > /dev/null; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grep -q?

echo "Target branch is ${TARGET_BRANCH}"

# Make sure we're up to date
UPSTREAM=$( git remote -v | grep "github\.com.*nodejs/node (fetch)" | awk '{print $(1)}' )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if there is no such remote?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I think that's why the Travis build failed 😆 .

@richardlau
Copy link
Member Author

Haha, that didn't work on Travis:

The command "make lint" exited with 0.
0.03s$ bash tools/lint-commit-message.sh
Target branch is master
Fetching upstream remote 
fatal: No path specified. See 'man git-pull' for valid url syntax
fatal: Not a valid object name /master
No commits found to lint.

@richardlau richardlau added the wip Issues and PRs that are still a work in progress. label Oct 19, 2018
@richardlau
Copy link
Member Author

Fixed Travis:
image

@richardlau richardlau removed the wip Issues and PRs that are still a work in progress. label Oct 19, 2018
@mmarchini mmarchini added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Oct 19, 2018
echo "Fetching upstream remote ${UPSTREAM}"
git fetch "${UPSTREAM}"
COMMON_ANCESTOR=$( git merge-base "${UPSTREAM}/$TARGET_BRANCH" HEAD )
COMMITS=$( git rev-list --no-merges ${COMMON_ANCESTOR}..HEAD | tail -1 )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use FIRST_COMMIT (or at least singular COMMIT) for clarity?

COMMITS=$( git rev-list --no-merges ${COMMON_ANCESTOR}..HEAD | tail -1 )

if [ -n "${COMMITS}" ]; then
echo "Linting the commit message according to the guidelines at https://goo.gl/p2fr5Q"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd note that it only lints first commit message. (It'd stop those who don't know that from wasting their time on fixing every commit they have in the branch)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the exact message currently on Travis: #23742

I'll change it here.

echo "$( git log -1 ${COMMITS} )"
echo "${COMMITS}" | xargs npx -q core-validate-commit --no-validate-metadata
else
echo "No commits found to lint."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: there is a dot at the end, so I'd suggest to either have the dot in every echo message or in none, just for consistency.

This script attempts to guess the target upstream branch.
To manually specify, e.g. to target `canary-base`:
  TARGET_BRANCH=canary-base bash lint-commit-message.sh
@richardlau richardlau added wip Issues and PRs that are still a work in progress. and removed author ready PRs that have at least one approval, no pending requests for changes, and a CI started. labels Oct 26, 2018
@richardlau
Copy link
Member Author

Addressed nits, but it looks like in the meantime my branch and master have diverged and this is now failing to properly pick up the first commit. Removed author ready and put back in progress to prevent this being merged.

@richardlau
Copy link
Member Author

After thinking about this a bit more, I've come up with an alternative: #24030

@richardlau richardlau closed this Nov 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tools Issues and PRs related to the tools directory. wip Issues and PRs that are still a work in progress.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants