copy-edit viget posts
This commit is contained in:
@@ -2,11 +2,10 @@
|
||||
title: "Simple Commit Linting for Issue Number in GitHub Actions"
|
||||
date: 2023-04-28T00:00:00+00:00
|
||||
draft: false
|
||||
needs_review: true
|
||||
canonical_url: https://www.viget.com/articles/simple-commit-linting-for-issue-number-in-github-actions/
|
||||
---
|
||||
|
||||
I don\'t believe there is **a** right way to do software; I think teams
|
||||
I don't believe there is **a** right way to do software; I think teams
|
||||
can be effective (or ineffective!) in a lot of different ways using all
|
||||
sorts of methodologies and technologies. But one hill upon which I will
|
||||
die is this: referencing tickets in commit messages pays enormous
|
||||
@@ -20,8 +19,8 @@ like `fix bug` or `PR feedback` or, heaven forbid, `oops`.
|
||||
|
||||
In a recent [project
|
||||
retrospective](https://www.viget.com/articles/get-the-most-out-of-your-internal-retrospectives/),
|
||||
the team identified that we weren\'t being as consistent with this as
|
||||
we\'d like, and decided to take action. I figured some sort of commit
|
||||
the team identified that we weren't being as consistent with this as
|
||||
we'd like, and decided to take action. I figured some sort of commit
|
||||
linting would be a good candidate for [continuous
|
||||
integration](https://www.viget.com/articles/maintenance-matters-continuous-integration/)
|
||||
--- when a team member pushes a branch up to GitHub, check the commits
|
||||
@@ -33,7 +32,7 @@ commits begin with either `[#XXX]` (an issue number) or `[n/a]` --- and
|
||||
rather difficult to reconfigure. After struggling with it for a few
|
||||
hours, I decided to just DIY it with a simple inline script. If you just
|
||||
want something you can drop into a GitHub Actions YAML file to lint your
|
||||
commits, here it is (but stick around and I\'ll break it down and then
|
||||
commits, here it is (but stick around and I'll break it down and then
|
||||
show how to do it in a few other languages):
|
||||
|
||||
``` yaml
|
||||
@@ -66,18 +65,18 @@ A few notes:
|
||||
primary development branch) --- by default, your Action only knows
|
||||
about the current branch.
|
||||
- `git log --format=format:%s HEAD ^origin/main` is going to give you
|
||||
the first line of every commit that\'s in the source branch but not
|
||||
the first line of every commit that's in the source branch but not
|
||||
in `main`; those are the commits we want to lint.
|
||||
- With that list of commits, we loop through each message and compare
|
||||
it with the regular expression `/^\[(#\d+|n\/a)\]/`, i.e. does this
|
||||
message begin with either `[#XXX]` (where `X` are digits) or
|
||||
`[n/a]`?
|
||||
- If any message does **not** match, print an error out to standard
|
||||
error (that\'s `warn`) and exit with a non-zero status (so that the
|
||||
error (that's `warn`) and exit with a non-zero status (so that the
|
||||
GitHub Action fails).
|
||||
|
||||
If you want to try this out locally (or perhaps modify the script to
|
||||
validate messages in a different way), here\'s a `docker run` command
|
||||
validate messages in a different way), here's a `docker run` command
|
||||
you can use:
|
||||
|
||||
``` bash
|
||||
@@ -96,18 +95,14 @@ Note that running this command should output nothing since these are all
|
||||
valid commit messages; modify one of the messages if you want to see the
|
||||
failure state.
|
||||
|
||||
[]{#other-languages}
|
||||
## Other Languages
|
||||
|
||||
## Other Languages [\#](#other-languages "Direct link to Other Languages"){.anchor aria-label="Direct link to Other Languages"}
|
||||
|
||||
Since there\'s a very real possibility you might not otherwise install
|
||||
Since there's a very real possibility you might not otherwise install
|
||||
Ruby in your GitHub Actions, and because I weirdly enjoy writing the
|
||||
same code in a bunch of different languages, here are scripts for
|
||||
several of Viget\'s other favorites:
|
||||
several of Viget's other favorites:
|
||||
|
||||
[]{#javaScript}
|
||||
|
||||
### JavaScript [\#](#javaScript "Direct link to JavaScript"){.anchor aria-label="Direct link to JavaScript"}
|
||||
### JavaScript
|
||||
|
||||
``` bash
|
||||
git log --format=format:%s HEAD ^origin/main | node -e "
|
||||
@@ -135,9 +130,7 @@ echo '[#123] Message 1
|
||||
"
|
||||
```
|
||||
|
||||
[]{#php}
|
||||
|
||||
### PHP [\#](#php "Direct link to PHP"){.anchor aria-label="Direct link to PHP"}
|
||||
### PHP
|
||||
|
||||
``` bash
|
||||
git log --format=format:%s HEAD ^origin/main | php -r '
|
||||
@@ -163,9 +156,7 @@ echo '[#123] Message 1
|
||||
'
|
||||
```
|
||||
|
||||
[]{#python}
|
||||
|
||||
### Python [\#](#python "Direct link to Python"){.anchor aria-label="Direct link to Python"}
|
||||
### Python
|
||||
|
||||
``` bash
|
||||
git log --format=format:%s HEAD ^origin/main | python -c '
|
||||
@@ -198,10 +189,10 @@ for msg in sys.stdin:
|
||||
------------------------------------------------------------------------
|
||||
|
||||
So there you have it: simple GitHub Actions commit linting in most of
|
||||
Viget\'s favorite languages (try as I might, I could not figure out how
|
||||
Viget's favorite languages (try as I might, I could not figure out how
|
||||
to do this in [Elixir](https://elixir-lang.org/), at least not in a
|
||||
concise way). As I said up front, writing good tickets and then
|
||||
referencing them in commit messages so that they can easily be surfaced
|
||||
with `git blame` pays **huge** dividends over the life of a codebase. If
|
||||
you\'re not already in the habit of doing this, well, the best time to
|
||||
you're not already in the habit of doing this, well, the best time to
|
||||
start was `Initial commit`, but the second best time is today.
|
||||
|
||||
Reference in New Issue
Block a user