2025-01-09 15:36:40 +01:00
|
|
|
name: Validate and auto-fix script formatting
|
2025-01-06 08:33:13 +01:00
|
|
|
|
|
|
|
on:
|
|
|
|
push:
|
|
|
|
branches:
|
|
|
|
- main
|
2025-01-07 20:34:37 +01:00
|
|
|
pull_request_target:
|
2025-01-06 08:33:13 +01:00
|
|
|
paths:
|
|
|
|
- "**/*.sh"
|
|
|
|
- "**/*.func"
|
|
|
|
|
|
|
|
jobs:
|
|
|
|
shfmt:
|
2025-01-09 15:36:40 +01:00
|
|
|
name: Check and fix formatting
|
2025-01-06 08:33:13 +01:00
|
|
|
runs-on: ubuntu-latest
|
|
|
|
permissions:
|
2025-01-09 15:36:40 +01:00
|
|
|
contents: write
|
2025-01-06 08:33:13 +01:00
|
|
|
pull-requests: write
|
|
|
|
|
|
|
|
steps:
|
2025-01-07 20:34:37 +01:00
|
|
|
- name: Get pull request information
|
2025-01-08 16:33:09 +01:00
|
|
|
if: github.event_name == 'pull_request_target'
|
2025-01-07 20:34:37 +01:00
|
|
|
uses: actions/github-script@v7
|
|
|
|
id: pr
|
|
|
|
with:
|
|
|
|
script: |
|
|
|
|
const { data: pullRequest } = await github.rest.pulls.get({
|
|
|
|
...context.repo,
|
|
|
|
pull_number: context.payload.pull_request.number,
|
|
|
|
});
|
|
|
|
return pullRequest;
|
|
|
|
|
2025-01-06 08:33:13 +01:00
|
|
|
- name: Checkout code
|
|
|
|
uses: actions/checkout@v4
|
|
|
|
with:
|
2025-01-09 15:36:40 +01:00
|
|
|
fetch-depth: 0
|
2025-01-08 16:33:09 +01:00
|
|
|
ref: ${{ github.event_name == 'pull_request_target' && fromJSON(steps.pr.outputs.result).merge_commit_sha || '' }}
|
2025-01-06 08:33:13 +01:00
|
|
|
|
|
|
|
- name: Get changed files
|
|
|
|
id: changed-files
|
|
|
|
run: |
|
2025-01-09 15:36:40 +01:00
|
|
|
if [ "${{ github.event_name }}" == "pull_request_target" ]; then
|
2025-01-08 16:33:09 +01:00
|
|
|
echo "files=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ steps.pr.outputs.result && fromJSON(steps.pr.outputs.result).merge_commit_sha }} | grep -E '\.(sh|func)$' | xargs)" >> $GITHUB_OUTPUT
|
2025-01-06 08:33:13 +01:00
|
|
|
else
|
|
|
|
echo "files=$(git diff --name-only ${{ github.event.before }} ${{ github.event.after }} | grep -E '\.(sh|func)$' | xargs)" >> $GITHUB_OUTPUT
|
|
|
|
fi
|
|
|
|
|
|
|
|
- name: Set up Go
|
|
|
|
if: steps.changed-files.outputs.files != ''
|
|
|
|
uses: actions/setup-go@v5
|
|
|
|
|
|
|
|
- name: Install shfmt
|
|
|
|
if: steps.changed-files.outputs.files != ''
|
|
|
|
run: |
|
|
|
|
go install mvdan.cc/sh/v3/cmd/shfmt@latest
|
|
|
|
echo "$GOPATH/bin" >> $GITHUB_PATH
|
|
|
|
|
2025-01-09 15:36:40 +01:00
|
|
|
- name: Fix formatting
|
2025-01-06 08:33:13 +01:00
|
|
|
if: steps.changed-files.outputs.files != ''
|
|
|
|
run: |
|
2025-01-09 15:36:40 +01:00
|
|
|
shfmt -w ${{ steps.changed-files.outputs.files }}
|
2025-01-06 08:33:13 +01:00
|
|
|
|
2025-01-09 15:36:40 +01:00
|
|
|
- name: Commit and push changes
|
|
|
|
if: steps.changed-files.outputs.files != '' && github.event_name == 'pull_request_target'
|
|
|
|
run: |
|
|
|
|
git config user.name "github-actions[bot]"
|
|
|
|
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
|
|
|
|
git add ${{ steps.changed-files.outputs.files }}
|
|
|
|
git commit -m "chore: auto-fix formatting issues"
|
|
|
|
git push
|
2025-01-06 08:33:13 +01:00
|
|
|
|
|
|
|
- name: Post comment with results
|
2025-01-09 15:36:40 +01:00
|
|
|
if: always() && github.event_name == 'pull_request_target'
|
2025-01-06 08:33:13 +01:00
|
|
|
uses: actions/github-script@v7
|
|
|
|
with:
|
|
|
|
script: |
|
|
|
|
const result = "${{ job.status }}" === "success" ? "success" : "failure";
|
|
|
|
const issueNumber = context.payload.pull_request
|
|
|
|
? context.payload.pull_request.number
|
|
|
|
: null;
|
|
|
|
const commentIdentifier = "validate-formatting";
|
2025-01-09 15:36:40 +01:00
|
|
|
let newCommentBody = `<!-- ${commentIdentifier}-start -->\n### Script Formatting Results\n\n`;
|
2025-01-06 08:33:13 +01:00
|
|
|
|
|
|
|
if (result === "failure") {
|
2025-01-09 15:36:40 +01:00
|
|
|
newCommentBody += `:x: Formatting issues were found and automatically fixed.\n`;
|
2025-01-06 08:33:13 +01:00
|
|
|
} else {
|
2025-01-09 15:36:40 +01:00
|
|
|
newCommentBody += `:rocket: All scripts are properly formatted!\n`;
|
2025-01-06 08:33:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
newCommentBody += `\n\n<!-- ${commentIdentifier}-end -->`;
|
|
|
|
|
|
|
|
if (issueNumber) {
|
|
|
|
const { data: comments } = await github.rest.issues.listComments({
|
|
|
|
...context.repo,
|
|
|
|
issue_number: issueNumber,
|
|
|
|
});
|
|
|
|
|
|
|
|
const existingComment = comments.find(
|
2025-01-09 15:36:40 +01:00
|
|
|
(comment) => comment.user.login === "github-actions[bot]" &&
|
|
|
|
comment.body.includes(commentIdentifier)
|
2025-01-06 08:33:13 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (existingComment) {
|
|
|
|
await github.rest.issues.updateComment({
|
|
|
|
...context.repo,
|
|
|
|
comment_id: existingComment.id,
|
|
|
|
body: newCommentBody,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
await github.rest.issues.createComment({
|
|
|
|
...context.repo,
|
|
|
|
issue_number: issueNumber,
|
|
|
|
body: newCommentBody,
|
|
|
|
});
|
|
|
|
}
|