Skip to content

Commit 812d67e

Browse files
committed
implement extra linting step
1 parent 1fbc624 commit 812d67e

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

.github/workflows/lintbranch.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Check that branch name conforms to GitHub naming convention:
2+
// https://docs.github.com/en/get-started/using-git/dealing-with-special-characters-in-branch-and-tag-names#naming-branches-and-tags
3+
4+
// To run self-tests,
5+
// node lintbranch.js test
6+
// TODO: deduplicate code from lintbranch.js and lintcommit.js.
7+
8+
function isValid(branchName) {
9+
const branchNameRegex = /^[a-zA-Z][a-zA-Z0-9._/-]*$/
10+
11+
return branchNameRegex.test(branchName)
12+
}
13+
14+
function run(branchName) {
15+
if (isValid(branchName)) {
16+
console.log(`Branch name "${branchName}" is valid.`)
17+
process.exit(0)
18+
} else {
19+
const helpUrl =
20+
'https://docs.github.com/en/get-started/using-git/dealing-with-special-characters-in-branch-and-tag-names#naming-branches-and-tags'
21+
console.log(`Branch name "${branchName}" is invalid see ${helpUrl} for more information.`)
22+
process.exit(1)
23+
}
24+
}
25+
26+
function _test() {
27+
const tests = {
28+
'feature/branch-name': true,
29+
feature_123: true,
30+
'my-branch': true,
31+
'123invalid-start': false,
32+
'!invalid@start': false,
33+
'': false,
34+
'another/valid-name134': true,
35+
'Hweinstock:feature/123";id;{echo,Y2F0IC9ldGMvcGFzc3dk}|{base64,-d}|{bash,-i};#': false,
36+
}
37+
38+
let passed = 0
39+
let failed = 0
40+
41+
for (const [branchName, expected] of Object.entries(tests)) {
42+
const result = isValid(branchName)
43+
if (result === expected) {
44+
console.log(`✅ Test passed for "${branchName}"`)
45+
passed++
46+
} else {
47+
console.log(`❌ Test failed for "${branchName}" (expected "${expected}", got "${result}")`)
48+
failed++
49+
}
50+
}
51+
52+
console.log(`\n${passed} tests passed, ${failed} tests failed`)
53+
}
54+
55+
function main() {
56+
const mode = process.argv[2]
57+
58+
if (mode === 'test') {
59+
_test()
60+
} else if (mode === 'run') {
61+
run(process.argv[3])
62+
} else {
63+
throw new Error(`Unknown mode: ${mode}`)
64+
}
65+
}
66+
67+
main()

.github/workflows/node.js.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ jobs:
3232
- uses: actions/setup-node@v4
3333
with:
3434
node-version: '20'
35+
- name: Check Branch title
36+
run: |
37+
node "$GITHUB_WORKSPACE/.github/workflows/lintbranch.js"
3538
- name: Check PR title
3639
run: |
3740
node "$GITHUB_WORKSPACE/.github/workflows/lintcommit.js"

0 commit comments

Comments
 (0)