Repository navigation
Expand file tree
/
Copy pathaction.yaml
More file actions
187 lines (175 loc) · 7.2 KB
/
Copy pathaction.yaml
File metadata and controls
187 lines (175 loc) · 7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
name: "Handle Fix Commit"
description: "Commits changes if possible, otherwise creates a patch and comments on the PR."
outputs:
changes:
description: "Whether changes were detected"
value: ${{ steps.check_changes.outputs.changes }}
pushed:
description: "Whether changes were pushed"
value: ${{ steps.commit_and_push.outputs.pushed }}
commit_sha:
description: "The full SHA of the pushed commit"
value: ${{ steps.commit_and_push.outputs.commit_sha }}
commit_sha_short:
description: "The short SHA of the pushed commit"
value: ${{ steps.commit_and_push.outputs.commit_sha_short }}
patch_name:
description: "The name of the patch file if created"
value: ${{ steps.create_patch.outputs.patch_name }}
inputs:
tool:
description: "The tool name reported in commit messages and PR comments."
required: true
working-directory:
description: "The working directory for git operations."
required: false
default: "phlex-src"
token:
description: "The PAT to use for committing."
required: true
pr-info-ref:
description: "The ref (branch name) of the PR"
required: true
pr-info-repo:
description: "The repository of the PR"
required: true
retry-attempts:
description: "The number of times to retry pushing the commit."
required: false
default: "6"
skip-comment:
description: "Skip posting PR comments (for workflow_call usage)"
required: false
default: "false"
runs:
using: "composite"
steps:
- name: Check for changes
id: check_changes
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
if [[ -n $(git status --porcelain) ]]; then
echo "changes=true" >> "$GITHUB_OUTPUT"
else
echo "changes=false" >> "$GITHUB_OUTPUT"
fi
- name: No changes to apply
if: steps.check_changes.outputs.changes == 'false' && inputs.skip-comment != 'true'
uses: thollander/actions-comment-pull-request@24bffb9b452ba05a4f3f77933840a6a841d1b32b # v3.0.1
with:
message: "No automatic ${{ inputs.tool }} fixes were necessary."
- name: Get PR maintainer_can_modify property
id: pr-properties
if: steps.check_changes.outputs.changes == 'true'
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
// context.issue.number is only available when the root trigger is an
// issue_comment event. For workflow_dispatch triggers (and any other
// non-PR-comment trigger), default to false so the commit path still
// works for same-repo pushes via the first clause of the commit condition.
const prNumber = context.issue.number;
if (!prNumber) {
core.setOutput('maintainer_can_modify', 'false');
return;
}
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber
});
core.setOutput('maintainer_can_modify', pr.maintainer_can_modify);
- name: Commit fixes
if: >-
steps.check_changes.outputs.changes == 'true' && (inputs.pr-info-repo == github.repository ||
steps.pr-properties.outputs.maintainer_can_modify == 'true')
id: commit_and_push
shell: bash
working-directory: ${{ inputs.working-directory }}
env:
TOOL: ${{ inputs.tool }}
TOKEN: ${{ inputs.token }}
PR_REF: ${{ inputs.pr-info-ref }}
RETRY_ATTEMPTS: ${{ inputs.retry-attempts }}
run: |
git config --local user.name "github-actions[bot]"
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
trap 'rm -f ~/.git-credentials' EXIT
rm -f ~/.git-credentials
umask 077
cat <<EOF > ~/.git-credentials
https://x-access-token:$TOKEN@github.com
EOF
git config --local credential.helper 'store --file ~/.git-credentials'
git add -u
git commit -m "Apply $TOOL fixes"
for i in $(seq 1 "$RETRY_ATTEMPTS"); do
if git push origin HEAD:"$PR_REF"; then
echo "Push successful on attempt $i."
COMMIT_SHA=$(git rev-parse HEAD)
COMMIT_SHA_SHORT=$(git rev-parse --short HEAD)
echo "commit_sha=$COMMIT_SHA" >> "$GITHUB_OUTPUT"
echo "commit_sha_short=$COMMIT_SHA_SHORT" >> "$GITHUB_OUTPUT"
echo "pushed=true" >> "$GITHUB_OUTPUT"
exit 0
fi
if [ "$i" -eq "$RETRY_ATTEMPTS" ]; then
break
fi
echo "Push failed on attempt $i. Fetching and rebasing before retry..."
git fetch origin
if ! git rebase origin/"$PR_REF"; then
echo "::error::Automatic rebase failed. Please resolve conflicts manually."
exit 1
fi
echo "Waiting before retry..."
sleep $((5 * i))
done
echo "::error::Failed to push changes after multiple retries."
exit 1
- name: Notify of commit
if:
steps.commit_and_push.conclusion == 'success' && steps.commit_and_push.outputs.pushed == 'true' &&
inputs.skip-comment != 'true'
uses: thollander/actions-comment-pull-request@24bffb9b452ba05a4f3f77933840a6a841d1b32b # v3.0.1
with:
# yamllint disable rule:line-length
message: |
Automatic ${{ inputs.tool }} fixes pushed (commit ${{ steps.commit_and_push.outputs.commit_sha_short || steps.commit_and_push.outputs.commit_sha }}).
⚠️ **Note:** Some issues may require manual review and fixing.
# yamllint enable
- name: Create patch
if: >-
steps.check_changes.outputs.changes == 'true' && inputs.pr-info-repo != github.repository &&
steps.pr-properties.outputs.maintainer_can_modify != 'true'
id: create_patch
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
git diff > fix.patch
echo "patch_name=fix.patch" >> "$GITHUB_OUTPUT"
- name: Upload patch
if: steps.create_patch.outputs.patch_name
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: fix-patch
path: ${{ inputs.working-directory }}/${{ steps.create_patch.outputs.patch_name }}
- name: Comment with patch instructions
if: steps.create_patch.outputs.patch_name && inputs.skip-comment != 'true'
uses: thollander/actions-comment-pull-request@24bffb9b452ba05a4f3f77933840a6a841d1b32b # v3.0.1
with:
# yamllint disable rule:line-length
message: |
Automatic fixes are available but could not be applied directly because this PR is from a fork and "Allow edits from maintainers" is not enabled.
Please apply the patch below to fix the issues:
1. **Download the patch file:** You can download the patch from the "Artifacts" section of this workflow run.
2. **Apply the patch:**
```bash
git apply <path/to/downloaded/fix.patch>
```
3. **Commit the changes:**
```bash
git commit -am "Apply ${{ inputs.tool }} fixes"
```
# yamllint enable