@@ -186,7 +186,9 @@ jobs:
186186 fi
187187
188188 updated_list=$(echo -e "$pr_list\n$new_pr_line" | sort -u)
189- new_body="## PRs linked to this issue\n\n$updated_list"
189+ new_body="## PRs linked to this issue
190+
191+ $updated_list"
190192 echo "pr_list=$new_body" >> "$GITHUB_OUTPUT"
191193 fi
192194 else
@@ -196,7 +198,9 @@ jobs:
196198 new_pr_line="$new_pr_line ✅"
197199 fi
198200
199- new_body="## PRs linked to this issue\n\n$new_pr_line"
201+ new_body="## PRs linked to this issue
202+
203+ $new_pr_line"
200204 echo "pr_list=$new_body" >> "$GITHUB_OUTPUT"
201205 fi
202206
@@ -212,4 +216,44 @@ jobs:
212216 uses : peter-evans/create-or-update-comment@v4
213217 with :
214218 comment-id : ${{ steps.check_comment.outputs.existing_comment_id }}
215- body : ${{ steps.check_comment.outputs.pr_list }}
219+ body : ${{ steps.check_comment.outputs.pr_list }}
220+
221+ - name : Link PR to issue
222+ run : |
223+ pr_url="${{ inputs.pr_url }}"
224+ pr_number=$(echo "$pr_url" | grep -o '[0-9]*$')
225+ issue_number="${{ steps.determine_issue.outputs.final_issue_number }}"
226+
227+ # Get current PR body
228+ current_pr=$(curl -s \
229+ -H "Accept: application/vnd.github+json" \
230+ -H "Authorization: Bearer ${{ env.GH_TOKEN }}" \
231+ -H "X-GitHub-Api-Version: 2022-11-28" \
232+ "https://api.github.com/repos/${{ github.repository }}/pulls/$pr_number")
233+
234+ current_body=$(echo "$current_pr" | jq -r '.body // ""')
235+
236+ # Check if issue reference already exists
237+ if ! echo "$current_body" | grep -q "#$issue_number"; then
238+ # Add issue reference to PR body
239+ if [[ -n "$current_body" && "$current_body" != "null" ]]; then
240+ new_body="$current_body
241+
242+ ---
243+ *This PR addresses issue # $issue_number*"
244+ else
245+ new_body="*This PR addresses issue # $issue_number*"
246+ fi
247+
248+ # Update PR body
249+ curl -X PATCH \
250+ -H "Accept : application/vnd.github+json" \
251+ -H "Authorization : Bearer ${{ env.GH_TOKEN }}" \
252+ -H "X-GitHub-Api-Version : 2022-11-28" \
253+ " https://api.github.com/repos/${{ github.repository }}/pulls/$pr_number" \
254+ -d "{\"body\":$(echo "$new_body" | jq -R -s .)}"
255+
256+ echo "Added issue # $issue_number reference to PR #$pr_number body"
257+ else
258+ echo "Issue # $issue_number already referenced in PR #$pr_number body"
259+ fi
0 commit comments