www-viget-com-biybeb.txt (14527B)
1 [1] Skip to Main Content 2 [2] Viget 3 4 • [3] Work 5 • [4] Services 6 • [5] Articles 7 • [6] Careers 8 • [7] Contact 9 • Open Menu 10 11 Navigation 12 13 [9] Viget Close 14 15 • Practice 16 • [11] Work 17 • [12] Services 18 • [13] Articles 19 20 We’re a full-service digital agency that’s been helping clients make lasting 21 change since 1999. 22 23 [14] Contact Us 24 25 People 26 27 • [15]Company 28 • [16]Careers 29 • [17]Code of Ethics 30 • [18]Diversity & Inclusion 31 32 More 33 34 • [19]Pointless Corp. 35 • [20]Explorations 36 • [21]Code at Viget 37 38 Featured 39 40 [22] 41 Read the Article: Revolutionizing the Web 42 43 Newsletter 44 45 Revolutionizing the Web 46 47 [23] 48 Read the Article: Elephants, Squirrels, Porcupines, and Zombies Can Help Your 49 Team Thrive 50 51 Article 52 53 Elephants, Squirrels, Porcupines, and Zombies Can Help Your Team Thrive 54 55 Simple Commit Linting for Issue Number in GitHub Actions 56 57 [Git-Tips-030718] 58 [24] David Eisinger 59 60 [25]David Eisinger, Development Director 61 62 Article Categories: [26] #Code, [27] #Tooling 63 64 Posted on April 28, 2023 65 66 • [28] 67 Share 68 • [29] 69 Share 70 • [30] 71 Post 72 73 Including relevant ticket numbers in your git commit messages is a gift to your 74 future self. Here's how to ensure you do it consistently. 75 76 I n c l u d i n g r e l e v a n t t i c k e t n u m b e r s i n y o u r g i t c 77 o m m i t m e s s a g e s i s a g i f t t o y o u r f u t u r e s e l f . H e r 78 e ' s h o w t o e n s u r e y o u d o i t c o n s i s t e n t l y . 79 80 I don't believe there is a right way to do software; I think teams can be 81 effective (or ineffective!) in a lot of different ways using all sorts of 82 methodologies and technologies. But one hill upon which I will die is this: 83 referencing tickets in commit messages pays enormous dividends over the long 84 haul and you should always do it. As someone who regularly commits code to apps 85 created in the Obama era, nothing warms my heart like running [31]:Git blame on 86 some confusing code and seeing a reference to a GitHub Issue where I can get 87 the necessary context. And, conversely, nothing sparks nerd rage like fix bug 88 or PR feedback or, heaven forbid, oops. 89 90 In a recent [32]project retrospective, the team identified that we weren't 91 being as consistent with this as we'd like, and decided to take action. I 92 figured some sort of commit linting would be a good candidate for [33] 93 continuous integration — when a team member pushes a branch up to GitHub, check 94 the commits and make sure they include a reference to a ticket. 95 96 I looked into [34]commitlint, but I found it a lot more opinionated than I am — 97 I really just want to make sure commits begin with either [#XXX] (an issue 98 number) or [n/a] — and rather difficult to reconfigure. After struggling with 99 it for a few hours, I decided to just DIY it with a simple inline script. If 100 you just want something you can drop into a GitHub Actions YAML file to lint 101 your commits, here it is (but stick around and I'll break it down and then show 102 how to do it in a few other languages): 103 104 steps: 105 - name: Checkout code 106 uses: actions/checkout@v3 107 with: 108 fetch-depth: 0 109 110 - name: Set up ruby 3.2.1 111 uses: ruby/setup-ruby@v1 112 with: 113 ruby-version: 3.2.1 114 115 - name: Lint commits 116 run: | 117 git log --format=format:%s HEAD ^origin/main | ruby -e ' 118 $stdin.each_line do |msg| 119 next if /^\[(#\d+|n\/a)\]/.match?(msg) 120 warn %(Commits must begin with [#XXX] or [n/a] (#{msg.strip})) 121 exit 1 122 end 123 ' 124 125 A few notes: 126 127 • That fetch-depth: 0 is essential in order to be able to compare the branch 128 being built with main (or whatever you call your primary development 129 branch) — by default, your Action only knows about the current branch. 130 • git log --format=format:%s HEAD ^origin/main is going to give you the first 131 line of every commit that's in the source branch but not in main; those are 132 the commits we want to lint. 133 • With that list of commits, we loop through each message and compare it with 134 the regular expression /^\[(#\d+|n\/a)\]/, i.e. does this message begin 135 with either [#XXX] (where X are digits) or [n/a]? 136 • If any message does not match, print an error out to standard error (that's 137 warn) and exit with a non-zero status (so that the GitHub Action fails). 138 139 If you want to try this out locally (or perhaps modify the script to validate 140 messages in a different way), here's a docker run command you can use: 141 142 echo '[#123] Message 1 143 [n/a] Message 2 144 [#122] Message 3' | docker run --rm -i ruby:3.2.1 ruby -e ' 145 $stdin.each_line do |msg| 146 next if /^\[(#\d+|n\/a)\]/.match?(msg) 147 warn %(Commits must begin with [#XXX] or [n/a] (#{msg.strip})) 148 exit 1 149 end 150 ' 151 152 Note that running this command should output nothing since these are all valid 153 commit messages; modify one of the messages if you want to see the failure 154 state. 155 156 Other Languages [35]# 157 158 Since there's a very real possibility you might not otherwise install Ruby in 159 your GitHub Actions, and because I weirdly enjoy writing the same code in a 160 bunch of different languages, here are scripts for several of Viget's other 161 favorites: 162 163 JavaScript [36]# 164 165 git log --format=format:%s HEAD ^origin/main | node -e " 166 let msgs = require('fs').readFileSync(0).toString().trim().split('\n'); 167 for (let msg of msgs) { 168 if (msg.match(/^\[(#\d+|n\/a)\]/)) { continue; } 169 process.stderr.write('Commits must begin with [#XXX] or [n/a] (' + msg + ')'); 170 process.exit(1); 171 } 172 " 173 174 To test: 175 176 echo '[#123] Message 1 177 [n/a] Message 2 178 [#122] Message 3' | docker run --rm -i node:18.15.0 node -e " 179 let msgs = require('fs').readFileSync(0).toString().trim().split('\n'); 180 for (let msg of msgs) { 181 if (msg.match(/^\[(#\d+|n\/a)\]/)) { continue; } 182 process.stderr.write('Commits must begin with [#XXX] or [n/a] (' + msg + ')'); 183 process.exit(1); 184 } 185 " 186 187 PHP [37]# 188 189 git log --format=format:%s HEAD ^origin/main | php -r ' 190 while ($msg = fgets(STDIN)) { 191 if (preg_match("/^\[(#\d+|n\/a)\]/", $msg)) { continue; } 192 fwrite(STDERR, "Commits must begin with #[XXX] or [n/a] (" . trim($msg) . ")\n"); 193 exit(1); 194 } 195 ' 196 197 To test: 198 199 echo '[#123] Message 1 200 [n/a] Message 2 201 [#122] Message 3' | docker run --rm -i php:8.2.4 php -r ' 202 while ($msg = fgets(STDIN)) { 203 if (preg_match("/^\[(#\d+|n\/a)\]/", $msg)) { continue; } 204 fwrite(STDERR, "Commits must begin with #[XXX] or [n/a] (" . trim($msg) . ")\n"); 205 exit(1); 206 } 207 ' 208 209 Python [38]# 210 211 git log --format=format:%s HEAD ^origin/main | python -c ' 212 import sys 213 import re 214 for msg in sys.stdin: 215 if re.match(r"^\[(#\d+|n\/a)\]", msg): 216 continue 217 print("Commits must begin with #[xxx] or [n/a] (%s)" % msg.strip(), file=sys.stderr) 218 sys.exit(1) 219 ' 220 221 To test: 222 223 echo '[#123] Message 1 224 [n/a] Message 2 225 [#122] Message 3' | docker run --rm -i python:3.11.3 python -c ' 226 import sys 227 import re 228 for msg in sys.stdin: 229 if re.match(r"^\[(#\d+|n\/a)\]", msg): 230 continue 231 print("Commits must begin with #[xxx] or [n/a] (%s)" % msg.strip(), file=sys.stderr) 232 sys.exit(1) 233 ' 234 235 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 236 237 So there you have it: simple GitHub Actions commit linting in most of Viget's 238 favorite languages (try as I might, I could not figure out how to do this in 239 [39]Elixir, at least not in a concise way). As I said up front, writing good 240 tickets and then referencing them in commit messages so that they can easily be 241 surfaced with git blame pays huge dividends over the life of a codebase. If 242 you're not already in the habit of doing this, well, the best time to start was 243 Initial commit, but the second best time is today. 244 245 [40] David Eisinger 246 247 [41]David is Viget's managing development director. From our Durham, NC, 248 office, he builds high-quality, forward-thinking software for PUMA, the World 249 Wildlife Fund, NFLPA, and many others. 250 251 [42]More articles by David 252 253 Related Articles 254 255 • [43] 256 Thoughts on Remix 257 258 Article 259 260 Thoughts on Remix 261 262 Solomon Hawk 263 264 • [44] 265 Going Headless in 2024: A View of the Headless CMS Landscape 266 267 Article 268 269 Going Headless in 2024: A View of the Headless CMS Landscape 270 271 Andrew Mosby 272 273 • [45] 274 Maintenance Matters: Good Tests 275 276 Article 277 278 Maintenance Matters: Good Tests 279 280 David Eisinger 281 282 The Viget Newsletter 283 284 Nobody likes popups, so we waited until now to recommend our newsletter, 285 featuring thoughts, opinions, and tools for building a better digital world. 286 [46]Read the current issue. 287 288 [47]Subscribe Here (opens in new window) 289 290 Site Footer 291 292 Have an unsolvable problem or audacious idea? 293 294 Let’s get to work 295 296 [48] Contact Us [49] [email protected] [50] 703.891.0670 297 298 • Practice 299 • [51]Work 300 • [52]Services 301 • [53]Articles 302 303 • People 304 • [54]Company 305 • [55]Careers 306 • [56]Code of Ethics 307 • [57]Diversity & Inclusion 308 309 • More 310 • [58]Pointless Corp. 311 • [59]Explorations 312 • [60]Code at Viget 313 314 Sign Up For Our Newsletter 315 316 A curated periodical featuring thoughts, opinions, and tools for building a 317 better digital world. 318 319 [61] Check it out 320 321 Social Links 322 323 [62] Viget 324 325 • [63] 326 • [64] 327 • [65] 328 • [66] 329 • [67] 330 • [68] 331 332 Office Locations 333 334 • [69]Washington DC Metro 335 • [70]Durham, NC 336 • [71]Boulder, CO 337 • [72]Chattanooga, TN 338 339 © 1999 – 2024 Viget Labs, LLC. [73]Terms [74]Privacy [75]MRF 340 341 • [76]Home 342 • [77]Articles 343 • [78]Simple Commit Linting for Issue Number in GitHub Actions 344 345 [79] Subscribe (opens in a new window) 346 Share 347 348 • [81] Share this page 349 • [82] Share this page 350 • [83] Post this page 351 352 353 References: 354 355 [1] https://www.viget.com/articles/simple-commit-linting-for-issue-number-in-github-actions/#content 356 [2] https://www.viget.com/ 357 [3] https://www.viget.com/work/ 358 [4] https://www.viget.com/services/ 359 [5] https://www.viget.com/articles/ 360 [6] https://www.viget.com/careers/ 361 [7] https://www.viget.com/contact/ 362 [9] https://www.viget.com/ 363 [11] https://www.viget.com/work/ 364 [12] https://www.viget.com/services/ 365 [13] https://www.viget.com/articles/ 366 [14] https://www.viget.com/contact/ 367 [15] https://www.viget.com/about/ 368 [16] https://www.viget.com/careers/ 369 [17] https://www.viget.com/code-of-ethics/ 370 [18] https://www.viget.com/diversity-equity-and-inclusion/ 371 [19] https://pointlesscorp.com/ 372 [20] https://explorations.viget.com/ 373 [21] https://code.viget.com/ 374 [22] https://www.viget.com/newsletter/revolutionizing-the-web/ 375 [23] https://www.viget.com/articles/elephants-squirrels-porcupines-and-zombies-can-help-your-team-thrive/ 376 [24] https://www.viget.com/about/team/deisinger/ 377 [25] https://www.viget.com/about/team/deisinger/ 378 [26] https://www.viget.com/articles/category/code/ 379 [27] https://www.viget.com/articles/category/tooling/ 380 [28] https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.viget.com%2Farticles%2Fsimple-commit-linting-for-issue-number-in-github-actions%2F 381 [29] http://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fwww.viget.com%2Farticles%2Fsimple-commit-linting-for-issue-number-in-github-actions%2F 382 [30] https://x.com/intent/tweet?text=Including%20relevant%20ticket%20numbers%20in%20your%20git%20commit%20messages%20is%20a%20gift%20to%20your%20future%20self.%20Here%27s%20how%20to%20ensure%20you%20do%20it%20consistently.%20https%3A%2F%2Fwww.viget.com%2Farticles%2Fsimple-commit-linting-for-issue-number-in-github-actions%2F 383 [31] https://github.com/tpope/vim-fugitive#fugitivevim 384 [32] https://www.viget.com/articles/get-the-most-out-of-your-internal-retrospectives/ 385 [33] https://www.viget.com/articles/maintenance-matters-continuous-integration/ 386 [34] https://commitlint.js.org/ 387 [35] https://www.viget.com/articles/simple-commit-linting-for-issue-number-in-github-actions/#other-languages 388 [36] https://www.viget.com/articles/simple-commit-linting-for-issue-number-in-github-actions/#javaScript 389 [37] https://www.viget.com/articles/simple-commit-linting-for-issue-number-in-github-actions/#php 390 [38] https://www.viget.com/articles/simple-commit-linting-for-issue-number-in-github-actions/#python 391 [39] https://elixir-lang.org/ 392 [40] https://www.viget.com/about/team/deisinger/ 393 [41] https://www.viget.com/about/team/deisinger/ 394 [42] https://www.viget.com/about/team/deisinger/ 395 [43] https://www.viget.com/articles/thoughts-on-remix/ 396 [44] https://www.viget.com/articles/a-view-of-the-headless-cms-landscape/ 397 [45] https://www.viget.com/articles/maintenance-matters-good-tests/ 398 [46] https://www.viget.com/newsletter 399 [47] http://eepurl.com/gtHqsj 400 [48] https://www.viget.com/contact/ 401 [49] mailto:[email protected]?subject=Hello%2C%20Viget%21 402 [50] tel:7038910670 403 [51] https://www.viget.com/work/ 404 [52] https://www.viget.com/services/ 405 [53] https://www.viget.com/articles/ 406 [54] https://www.viget.com/about/ 407 [55] https://www.viget.com/careers/ 408 [56] https://www.viget.com/code-of-ethics/ 409 [57] https://www.viget.com/diversity-equity-and-inclusion/ 410 [58] https://pointlesscorp.com/ 411 [59] https://explorations.viget.com/ 412 [60] https://code.viget.com/ 413 [61] https://www.viget.com/newsletter/ 414 [62] https://www.viget.com/ 415 [63] http://x.com/viget 416 [64] https://github.com/vigetlabs 417 [65] https://dribbble.com/viget 418 [66] https://www.instagram.com/viget/ 419 [67] https://www.linkedin.com/company/viget-labs 420 [68] https://vimeo.com/viget/collections 421 [69] https://www.viget.com/dc-metro-hq/ 422 [70] https://www.viget.com/durham/ 423 [71] https://www.viget.com/boulder/ 424 [72] https://www.viget.com/chattanooga/ 425 [73] https://www.viget.com/terms-conditions/ 426 [74] https://www.viget.com/privacy-policy/ 427 [75] https://individual.carefirst.com/individuals-families/mandates-policies/machine-readable-file.page 428 [76] https://www.viget.com/ 429 [77] https://www.viget.com/articles 430 [78] https://www.viget.com/articles/simple-commit-linting-for-issue-number-in-github-actions/#hero 431 [79] http://eepurl.com/gtHqsj 432 [81] https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.viget.com%2Farticles%2Fsimple-commit-linting-for-issue-number-in-github-actions%2F 433 [82] http://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fwww.viget.com%2Farticles%2Fsimple-commit-linting-for-issue-number-in-github-actions%2F 434 [83] https://x.com/intent/tweet?text=Including%20relevant%20ticket%20numbers%20in%20your%20git%20commit%20messages%20is%20a%20gift%20to%20your%20future%20self.%20Here%27s%20how%20to%20ensure%20you%20do%20it%20consistently.%20https%3A%2F%2Fwww.viget.com%2Farticles%2Fsimple-commit-linting-for-issue-number-in-github-actions%2F