Skip to content

Commit 294fdd4

Browse files
committed
Refactor whitespace and commit message helpers
1 parent 0a29b29 commit 294fdd4

13 files changed

+323
-140
lines changed
Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,50 @@
11
name: Commit messages check
22
on:
3-
pull_request:
4-
workflow_call:
3+
pull_request:
4+
workflow_call:
55

66
jobs:
7-
gitlint:
8-
name: Check commit messages
9-
runs-on: ubuntu-latest
10-
steps:
11-
- name: Checkout repository
12-
uses: actions/checkout@v4
13-
with:
14-
fetch-depth: 0
15-
- name: Install dependencies
16-
run: |
17-
pip install --upgrade gitlint
18-
- name: Lint git commit messages
19-
shell: bash
20-
# run the linter and tee the output to a file, this will make the check fail but allow us to use the results in summary
21-
run: gitlint --ignore body-is-missing --commits origin/$GITHUB_BASE_REF.. 2>&1 | tee check_results.log
22-
- name: Propegate Error Summary
23-
if: always()
24-
shell: bash
25-
# put the output of the commit message linting into the summary for the job and in an environment variable
26-
run: |
27-
# Change the commit part of the log into a markdown link to the commit
28-
commitsUrl="https:\/\/github.com\/${{ github.repository_owner }}\/${{ github.event.repository.name }}\/commit\/"
29-
sed -i "s/Commit \([0-9a-f]\{7,40\}\)/[commit \1]($commitsUrl\1)/g" check_results.log
30-
# Put the results into the job summary
31-
cat check_results.log >> "$GITHUB_STEP_SUMMARY"
32-
# Put the results into a multi-line environment variable to use in the next step
33-
echo "check_results<<###LINT_DELIMITER###" >> "$GITHUB_ENV"
34-
echo "$(cat check_results.log)" >> "$GITHUB_ENV"
35-
echo "###LINT_DELIMITER###" >> "$GITHUB_ENV"
36-
# add a comment on the PR if the commit message linting failed
37-
- name: Comment on PR
38-
if: failure()
39-
uses: marocchino/sticky-pull-request-comment@v2
40-
with:
41-
header: Commit Comment
42-
message: |
43-
⚠️ Commit Message Format Issues ⚠️
44-
${{ env.check_results }}
45-
- name: Clear PR Comment
46-
if: success()
47-
uses: marocchino/sticky-pull-request-comment@v2
48-
with:
49-
header: Commit Comment
50-
hide: true
51-
hide_classify: "RESOLVED"
52-
7+
gitlint:
8+
name: Check commit messages
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout repository
12+
uses: actions/checkout@v4
13+
with:
14+
fetch-depth: 0
15+
- name: Install dependencies
16+
run: |
17+
pip install --upgrade gitlint
18+
- name: Lint git commit messages
19+
shell: bash
20+
run: bash ./Build/Agent/commit-messages.sh
21+
- name: Propegate Error Summary
22+
if: always()
23+
shell: bash
24+
# put the output of the commit message linting into the summary for the job and in an environment variable
25+
run: |
26+
# Change the commit part of the log into a markdown link to the commit
27+
commitsUrl="https:\/\/github.com\/${{ github.repository_owner }}\/${{ github.event.repository.name }}\/commit\/"
28+
sed -i "s/Commit \([0-9a-f]\{7,40\}\)/[commit \1]($commitsUrl\1)/g" check_results.log
29+
# Put the results into the job summary
30+
cat check_results.log >> "$GITHUB_STEP_SUMMARY"
31+
# Put the results into a multi-line environment variable to use in the next step
32+
echo "check_results<<###LINT_DELIMITER###" >> "$GITHUB_ENV"
33+
echo "$(cat check_results.log)" >> "$GITHUB_ENV"
34+
echo "###LINT_DELIMITER###" >> "$GITHUB_ENV"
35+
# add a comment on the PR if the commit message linting failed
36+
- name: Comment on PR
37+
if: failure()
38+
uses: marocchino/sticky-pull-request-comment@v2
39+
with:
40+
header: Commit Comment
41+
message: |
42+
⚠️ Commit Message Format Issues ⚠️
43+
${{ env.check_results }}
44+
- name: Clear PR Comment
45+
if: success()
46+
uses: marocchino/sticky-pull-request-comment@v2
47+
with:
48+
header: Commit Comment
49+
hide: true
50+
hide_classify: "RESOLVED"
Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,22 @@
11
name: check-whitespace
22

33
on:
4-
pull_request:
5-
types: [opened, synchronize]
4+
pull_request:
5+
types: [opened, synchronize]
66

77
# Avoid unnecessary builds
88
concurrency:
9-
group: ${{ github.workflow }}-${{ github.ref }}
10-
cancel-in-progress: true
9+
group: ${{ github.workflow }}-${{ github.ref }}
10+
cancel-in-progress: true
1111

1212
jobs:
13-
check-whitespace:
14-
runs-on: ubuntu-latest
15-
steps:
16-
- uses: actions/checkout@v4
17-
with:
18-
fetch-depth: 0
13+
check-whitespace:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
with:
18+
fetch-depth: 0
1919

20-
- name: git log --check
21-
id: check_out
22-
run: |
23-
echo "Starting the script."
24-
baseSha=${{ github.event.pull_request.base.sha }}
25-
git log --check --pretty=format:"---% h% s" ${baseSha}.. | tee check-results.log
26-
problems=()
27-
commit=
28-
commitText=
29-
commitTextmd=
30-
# Use git log --check to look for whitespace errors in each commit of this PR
31-
log_output=$(cat check-results.log)
32-
echo "${log_output}"
33-
# Use a for loop to iterate over lines of log_output
34-
IFS=$'\n'
35-
for line in $log_output; do
36-
echo "Line: ${line}"
37-
case "${line}" in
38-
"--- "*)
39-
IFS=' ' read -r _ commit commitText <<< "$line"
40-
commitTextmd="[${commit}](https://github.com/${{ github.repository }}/commit/${commit}) ${commitText}"
41-
;;
42-
"")
43-
;;
44-
*:[1-9]*:*) # contains file and line number information - This indicates that a whitespace error was found
45-
file="${line%%:*}"
46-
afterFile="${line#*:}" # Remove the first colon and everything before it
47-
lineNumber="${afterFile%%:*}" # Remove anything after and including the first remaining colon to get only the line number
48-
problems+=("[${commitTextmd}]")
49-
problems+=("[${line}](https://github.com/${{ github.repository }}/blob/${{github.event.pull_request.head.ref}}/${file}#L${lineNumber})")
50-
problems+=""
51-
;;
52-
esac
53-
done
54-
if test ${#problems[*]} -gt 0; then
55-
echo "⚠️ Please review the Summary output for further information." >> $GITHUB_STEP_SUMMARY
56-
echo "### A whitespace issue was found in one or more of the commits." >> $GITHUB_STEP_SUMMARY
57-
echo "" >> $GITHUB_STEP_SUMMARY
58-
echo "Errors:" >> $GITHUB_STEP_SUMMARY
59-
for i in "${problems[@]}"; do
60-
echo "${i}" >> $GITHUB_STEP_SUMMARY
61-
done
62-
exit 1
63-
fi
64-
echo "No problems found"
20+
- name: Run whitespace check script
21+
shell: bash
22+
run: bash ./Build/Agent/check-whitespace.sh

.vscode/tasks.json

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,43 @@
1818
{
1919
"label": "Whitespace check (CI parity)",
2020
"type": "shell",
21-
"command": "./Build/Agent/check-whitespace.ps1",
22-
"options": {
23-
"shell": {
24-
"executable": "powershell.exe",
25-
"args": [
26-
"-NoProfile",
27-
"-ExecutionPolicy",
28-
"Bypass",
29-
"-Command"
30-
]
21+
"command": "./Build/Agent/check-and-fix-whitespace.sh",
22+
"windows": {
23+
"command": "./Build/Agent/check-and-fix-whitespace.ps1",
24+
"options": {
25+
"shell": {
26+
"executable": "powershell.exe",
27+
"args": [
28+
"-NoProfile",
29+
"-ExecutionPolicy",
30+
"Bypass",
31+
"-Command"
32+
]
33+
}
3134
}
3235
},
33-
"detail": "Runs git log --check from PR base (or origin default) and fails on whitespace issues; mirrors .github/workflows/check-whitespace.yml",
36+
"detail": "Runs git log --check from nearest origin/release/* (or origin default), then runs the fixer to normalize whitespace.",
3437
"problemMatcher": []
3538
},
3639
{
3740
"label": "Commit messages check (CI parity)",
3841
"type": "shell",
39-
"command": "./Build/Agent/commit-messages.ps1",
40-
"options": {
41-
"shell": {
42-
"executable": "powershell.exe",
43-
"args": [
44-
"-NoProfile",
45-
"-ExecutionPolicy",
46-
"Bypass",
47-
"-Command"
48-
]
42+
"command": "./Build/Agent/commit-messages.sh",
43+
"windows": {
44+
"command": "./Build/Agent/commit-messages.ps1",
45+
"options": {
46+
"shell": {
47+
"executable": "powershell.exe",
48+
"args": [
49+
"-NoProfile",
50+
"-ExecutionPolicy",
51+
"Bypass",
52+
"-Command"
53+
]
54+
}
4955
}
5056
},
51-
"detail": "Installs gitlint and lints commits from PR base (or origin default); mirrors .github/workflows/CommitMessage.yml",
57+
"detail": "Installs gitlint and lints commits since the nearest origin/release/* (or origin default).",
5258
"problemMatcher": []
5359
},
5460
{

Build/Agent/GitHelpers.ps1

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env pwsh
2+
# Shared Git helpers for Build/Agent PowerShell scripts
3+
$ErrorActionPreference = 'Stop'
4+
5+
function Get-DefaultBranchName {
6+
$line = git remote show origin 2>$null | Select-String 'HEAD branch:' | Select-Object -First 1
7+
if ($line) { return ($line -split ':')[1].Trim() }
8+
return $null
9+
}
10+
11+
function Get-DefaultBranchRef {
12+
$name = Get-DefaultBranchName
13+
if ($name) { return "origin/$name" }
14+
return 'origin/develop'
15+
}
16+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env pwsh
2+
$ErrorActionPreference = 'Continue'
3+
4+
& "$PSScriptRoot/check-whitespace.ps1"
5+
$ec = $LASTEXITCODE
6+
7+
& "$PSScriptRoot/fix-whitespace.ps1"
8+
9+
exit $ec
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
set +e
3+
bash "$(dirname "$0")/check-whitespace.sh"
4+
ec=$?
5+
bash "$(dirname "$0")/fix-whitespace.sh"
6+
exit $ec

Build/Agent/check-whitespace.ps1

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33

44
$ErrorActionPreference = 'Stop'
55

6-
function Get-DefaultBranch {
7-
$line = git remote show origin 2>$null | Select-String 'HEAD branch:' | Select-Object -First 1
8-
if ($line) { return ($line -split ':')[1].Trim() }
9-
return $null
10-
}
6+
# Import shared git helpers
7+
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
8+
. (Join-Path $ScriptDir 'GitHelpers.ps1')
119

1210
function Get-RepoPath {
1311
$url = git remote get-url origin 2>$null
@@ -21,7 +19,7 @@ git fetch origin 2>$null | Out-Null
2119

2220
$baseRef = $env:GITHUB_BASE_REF
2321
if (-not $baseRef -or $baseRef -eq '') {
24-
$baseRef = Get-DefaultBranch
22+
$baseRef = (Get-DefaultBranchName)
2523
}
2624
if (-not $baseRef) {
2725
Write-Error 'Unable to determine base branch (GITHUB_BASE_REF not set and origin HEAD unknown).'

Build/Agent/check-whitespace.sh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
5+
# shellcheck source=lib_git.sh
6+
source "${SCRIPT_DIR}/lib_git.sh"
7+
8+
# Determine base commit/ref
9+
BASE_SHA="${1:-}"
10+
if [[ -z "$BASE_SHA" ]]; then
11+
if [[ -n "${GITHUB_EVENT_PULL_REQUEST_BASE_SHA:-}" ]]; then
12+
BASE_SHA="$GITHUB_EVENT_PULL_REQUEST_BASE_SHA"
13+
elif [[ -n "${GITHUB_BASE_REF:-}" ]]; then
14+
BASE_SHA="origin/${GITHUB_BASE_REF}"
15+
else
16+
BASE_SHA="$(git_default_branch_ref)"
17+
fi
18+
fi
19+
20+
echo "Checking whitespace with git log --check from ${BASE_SHA}..HEAD"
21+
git log --check --pretty=format:"---% h% s" "${BASE_SHA}.." | tee check-results.log
22+
23+
# Parse results to prepare a summary if running in GitHub Actions
24+
problems=()
25+
commit=""
26+
commitText=""
27+
commitTextmd=""
28+
while IFS='' read -r line || [[ -n "$line" ]]; do
29+
case "$line" in
30+
"--- "*)
31+
# format: --- <sha> <subject>
32+
read -r _ commit commitText <<<"$line"
33+
if [[ -n "${GITHUB_REPOSITORY:-}" ]]; then
34+
commitTextmd="[${commit}](https://github.com/${GITHUB_REPOSITORY}/commit/${commit}) ${commitText}"
35+
else
36+
commitTextmd="${commit} ${commitText}"
37+
fi
38+
;;
39+
"") ;;
40+
*:[1-9]*:*)
41+
file="${line%%:*}"
42+
afterFile="${line#*:}"
43+
lineNumber="${afterFile%%:*}"
44+
problems+=("[${commitTextmd}]")
45+
if [[ -n "${GITHUB_REPOSITORY:-}" ]] && [[ -n "${GITHUB_REF_NAME:-}" ]]; then
46+
problems+=("[${line}](https://github.com/${GITHUB_REPOSITORY}/blob/${GITHUB_REF_NAME}/${file}#L${lineNumber})")
47+
else
48+
problems+=("${line}")
49+
fi
50+
problems+=("")
51+
;;
52+
esac
53+
done < check-results.log
54+
55+
if [[ ${#problems[@]} -gt 0 ]]; then
56+
echo "Whitespace issues were found." >&2
57+
if [[ -n "${GITHUB_STEP_SUMMARY:-}" ]]; then
58+
{
59+
echo "⚠️ Please review the Summary output for further information."
60+
echo "### A whitespace issue was found in one or more of the commits."
61+
echo
62+
echo "Errors:"
63+
for i in "${problems[@]}"; do
64+
echo "$i"
65+
done
66+
} >> "$GITHUB_STEP_SUMMARY"
67+
fi
68+
exit 1
69+
fi
70+
71+
echo "No problems found"
72+
exit 0

0 commit comments

Comments
 (0)