add a format pr github action
Jörg Thalheim committed
Jun 13, 2025 at 07:28 UTC
a1e3be8ab3d7647586c54d58452145fcbe3c7aa9
2 files changed
+137
.github/scripts/format-and-absorb.sh
new
+29
@@ -0,0 +1,29 @@
1
+#!/usr/bin/env -S nix shell --inputs-from . nixpkgs#bash nixpkgs#git-absorb --command bash
2
+# shellcheck shell=bash
3
+set -euo pipefail
4
+
5
+# This script runs nix fmt and git absorb to update a pull request
6
+# It's designed to be run in a GitHub Actions workflow
7
+
8
+echo "::group::Running nix fmt"
9
+nix fmt
10
+echo "::endgroup::"
11
+
12
+echo "::group::Checking for changes"
13
+if git diff --quiet; then
14
+ echo "No formatting changes needed"
15
+ exit 0
16
+fi
17
+echo "::endgroup::"
18
+
19
+echo "::group::Running git absorb"
20
+# Run git absorb with --force to automatically absorb changes
21
+git add -A
22
+git absorb --force --base origin/main
23
+echo "::endgroup::"
24
+
25
+echo "::group::Pushing changes"
26
+git push
27
+echo "::endgroup::"
28
+
29
+echo "Successfully formatted code and absorbed changes!"
.github/workflows/format-pr.yml
new
+108
@@ -0,0 +1,108 @@
1
+name: Format PR
2
+
3
+on:
4
+ issue_comment:
5
+ types: [created]
6
+ workflow_dispatch:
7
+ inputs:
8
+ pr_number:
9
+ description: "PR number to format"
10
+ required: true
11
+ type: number
12
+
13
+permissions:
14
+ contents: write
15
+ pull-requests: write
16
+
17
+jobs:
18
+ format:
19
+ if: |
20
+ github.event.issue.pull_request &&
21
+ github.event.comment.body == '/format'
22
+ runs-on: ubuntu-latest
23
+ steps:
24
+ - name: Check if user has write access
25
+ uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
26
+ with:
27
+ script: |
28
+ const permission = await github.rest.repos.getCollaboratorPermissionLevel({
29
+ owner: context.repo.owner,
30
+ repo: context.repo.repo,
31
+ username: context.payload.comment.user.login,
32
+ });
33
+
34
+ if (!['admin', 'write'].includes(permission.data.permission)) {
35
+ await github.rest.issues.createComment({
36
+ owner: context.repo.owner,
37
+ repo: context.repo.repo,
38
+ issue_number: context.issue.number,
39
+ body: '❌ You need write access to run this command.'
40
+ });
41
+ core.setFailed('User lacks write permission');
42
+ }
43
+
44
+ - name: React to comment
45
+ uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
46
+ with:
47
+ script: |
48
+ await github.rest.reactions.createForIssueComment({
49
+ owner: context.repo.owner,
50
+ repo: context.repo.repo,
51
+ comment_id: context.payload.comment.id,
52
+ content: 'rocket'
53
+ });
54
+
55
+ - name: Get PR branch
56
+ id: pr
57
+ uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
58
+ with:
59
+ script: |
60
+ const pr = await github.rest.pulls.get({
61
+ owner: context.repo.owner,
62
+ repo: context.repo.repo,
63
+ pull_number: context.issue.number,
64
+ });
65
+ core.setOutput('head_ref', pr.data.head.ref);
66
+ core.setOutput('head_sha', pr.data.head.sha);
67
+
68
+ - name: Checkout PR
69
+ uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
70
+ with:
71
+ ref: ${{ steps.pr.outputs.head_ref }}
72
+ fetch-depth: 0
73
+
74
+ - name: Install Nix
75
+ uses: cachix/install-nix-action@17fe5fb4a23ad6cbbe47d6b3f359611ad276644c # v31
76
+
77
+ - name: Setup Cachix
78
+ uses: cachix/cachix-action@0fc020193b5a1fa3ac4575aa3a7d3aa6a35435ad # v16
79
+ with:
80
+ name: nixos-infra-dev
81
+ authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}"
82
+
83
+ - name: Run format and absorb
84
+ run: ./.github/scripts/format-and-absorb.sh
85
+
86
+ - name: Comment on success
87
+ if: success()
88
+ uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
89
+ with:
90
+ script: |
91
+ await github.rest.issues.createComment({
92
+ owner: context.repo.owner,
93
+ repo: context.repo.repo,
94
+ issue_number: context.issue.number,
95
+ body: '✅ Successfully formatted and absorbed changes!'
96
+ });
97
+
98
+ - name: Comment on failure
99
+ if: failure()
100
+ uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
101
+ with:
102
+ script: |
103
+ await github.rest.issues.createComment({
104
+ owner: context.repo.owner,
105
+ repo: context.repo.repo,
106
+ issue_number: context.issue.number,
107
+ body: '❌ Failed to format and absorb changes. Check the workflow logs for details.'
108
+ });