3939 repo : ${{ github.repository }}
4040 base_branch : ${{ github.ref_name }}
4141 pr_branch : update/${{ github.ref_name }}
42+ team : nix-community/nixvim
4243
4344 steps :
4445 - name : Checkout repository
5758 git config user.name 'github-actions[bot]'
5859 git config user.email '41898282+github-actions[bot]@users.noreply.github.com'
5960
61+ - name : Create update branch
62+ run : |
63+ git branch -D "$pr_branch" || echo "Nothing to delete"
64+ git switch -c "$pr_branch"
65+
6066 - name : Get info on the current PR
6167 id : open_pr_info
6268 env :
@@ -161,14 +167,11 @@ jobs:
161167 echo "EOF" >> "$GITHUB_OUTPUT"
162168 fi
163169
164- - name : Create Pull Request
165- id : pr
170+ - name : Create or Update Pull Request
171+ id : updated_pr
166172 if : (!steps.changes.outputs.cancelled)
167- uses : peter-evans/create-pull-request@v6
168- with :
169- add-paths : " !**"
170- pr_branch : update/${{ github.ref_name }}
171- delete-branch : true
173+ env :
174+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
172175 title : |
173176 [${{ github.ref_name }}] Update flake.lock & generated files
174177 body : |
@@ -179,15 +182,57 @@ jobs:
179182
180183 ## Generate
181184 ${{ steps.generate.outputs.body || 'No changes' }}
185+ run : |
186+ # TODO:
187+ # - cherry-pick additional commits from already open PR
188+ # - avoid pushing if there are no changes
189+ echo "Pushing to remote branch $pr_branch"
190+ git push --force --set-upstream origin "$pr_branch"
191+
192+ if [[ -n "$pr_num" ]]; then
193+ echo "Editing existing PR #$pr_num"
194+ operation=updated
195+ gh pr edit "$pr_num" \
196+ --body "$body" \
197+ --add-assignee "$team" \
198+ --add-reviewer "$team"
199+ else
200+ echo "Creating new PR"
201+ operation=created
202+ gh pr create \
203+ --base "$base_branch" \
204+ --title "$title" \
205+ --body "$body" \
206+ --assignee "$team" \
207+ --reviewer "$team"
208+ fi
209+
210+ pr_info=$(
211+ # Get info from `gh pr view`
212+ gh pr view --json 'headRefName,number,url' --jq '
213+ to_entries[]
214+ | .key |=
215+ # Rename headRefName -> branch
216+ if . == "headRefName" then "branch"
217+ else . end
218+ | "\(.key)=\(.value)"
219+ '
220+ # Get additional info locally
221+ echo "head=$(git rev-parse HEAD)"
222+ echo "operation=$operation"
223+ )
224+ echo "PR Info:"
225+ echo "$pr_info"
226+ echo "$pr_info" >> $GITHUB_OUTPUT
182227
183228 - name : Print summary
184- if : steps.pr .outputs.pull-request- number
229+ if : steps.updated_pr .outputs.number
185230 env :
186- pr_num : ${{ steps.pr .outputs.pull-request- number }}
187- pr_url : ${{ steps.pr .outputs.pull-request- url }}
188- pr_branch : ${{ steps.pr .outputs.pull-request- branch }}
189- head : ${{ steps.pr .outputs.pull-request- head-sha }}
190- operation : ${{ steps.pr .outputs.pull-request- operation }}
231+ pr_num : ${{ steps.updated_pr .outputs.number }}
232+ pr_url : ${{ steps.updated_pr .outputs.url }}
233+ pr_branch : ${{ steps.updated_pr .outputs.branch }}
234+ head : ${{ steps.updated_pr .outputs.head }}
235+ operation : ${{ steps.updated_pr .outputs.operation }}
191236 run : |
192237 short=${head:0:6}
193238 # stdout
@@ -203,7 +248,7 @@ jobs:
203248 ) >> $GITHUB_STEP_SUMMARY
204249
205250 - name : Print cancellation summary
206- if : (!steps.pr .outputs.pull-request- number)
251+ if : (!steps.updated_pr .outputs.number)
207252 env :
208253 pr_num : ${{ steps.open_pr_info.outputs.number }}
209254 pr_url : ${{ steps.open_pr_info.outputs.url }}
0 commit comments