Skip to content

Commit

Permalink
improve propose pr workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
pitmonticone committed Oct 10, 2024
1 parent 6e61250 commit d202926
Showing 1 changed file with 75 additions and 15 deletions.
90 changes: 75 additions & 15 deletions .github/workflows/03-propose-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,88 @@ jobs:
echo "User ${{ github.event.comment.user.login }} is not assigned to this issue, exiting."
exit 0
- name: Link PR to the issue
if: env.not_assigned == 'false'
- name: Get the PR node ID
id: get_pr_node_id
run: |
QUERY=$(cat <<EOF
{
"query": "{
repository(owner: \\"${{ github.repository_owner }}\\", name: \\"${{ github.event.repository.name }}\\") {
pullRequest(number: ${{ env.pr_number }}) {
id
}
}
}"
}
EOF
)
echo "Sending query: $QUERY"
RESPONSE=$(curl -s -X POST -H "Authorization: Bearer ${{ secrets.PAT_TOKEN }}" \
-H "Content-Type: application/json" \
--data "$QUERY" https://api.github.com/graphql)
PR_NODE_ID=$(echo "$RESPONSE" | jq -r '.data.repository.pullRequest.id')
if [ -z "$PR_NODE_ID" ]; then
echo "Error: Could not retrieve PR node_id"
exit 1
else
echo "PR_NODE_ID=$PR_NODE_ID" >> $GITHUB_ENV
fi
- name: Get the Issue node ID
id: get_issue_node_id
run: |
# Capture the PR number from the environment variable
PR_NUMBER="${{ env.pr_number }}"
QUERY=$(cat <<EOF
{
"query": "{
repository(owner: \\"${{ github.repository_owner }}\\", name: \\"${{ github.event.repository.name }}\\") {
issue(number: ${{ github.event.issue.number }}) {
id
}
}
}"
}
EOF
)
echo "Sending query: $QUERY"
RESPONSE=$(curl -s -X POST -H "Authorization: Bearer ${{ secrets.PAT_TOKEN }}" \
-H "Content-Type: application/json" \
--data "$QUERY" https://api.github.com/graphql)
ISSUE_NODE_ID=$(echo "$RESPONSE" | jq -r '.data.repository.issue.id')
if [ -z "$PR_NUMBER" ]; then
echo "Error: PR number is not set. Exiting."
if [ -z "$ISSUE_NODE_ID" ]; then
echo "Error: Could not retrieve issue node_id"
exit 1
else
echo "ISSUE_NODE_ID=$ISSUE_NODE_ID" >> $GITHUB_ENV
fi
# Get the current PR body
PR_BODY=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
https://api.github.com/repos/${{ github.repository }}/pulls/${PR_NUMBER} | jq -r '.body')
- name: Link PR to the issue
if: env.not_assigned == 'false'
run: |
QUERY=$(cat <<EOF
{
"query": "mutation {
addPullRequestToIssue(input: {issueId: \\"${ISSUE_NODE_ID}\\", pullRequestId: \\"${PR_NODE_ID}\\"}) {
issue {
id
}
}
}"
}
EOF
)
echo "Sending mutation: $QUERY"
# Add the issue reference to the PR body (to close the issue when the PR is merged)
NEW_PR_BODY="$PR_BODY\n\nCloses #${{ github.event.issue.number }}"
RESPONSE=$(curl -s -X POST -H "Authorization: Bearer ${{ secrets.PAT_TOKEN }}" \
-H "Content-Type: application/json" \
--data "$QUERY" https://api.github.com/graphql)
# Update the PR body
curl -X PATCH -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-d "{\"body\": \"$NEW_PR_BODY\"}" \
https://api.github.com/repos/${{ github.repository }}/pulls/${PR_NUMBER}
echo "GraphQL Response: $RESPONSE"
- name: Log PR and issue link result
if: env.not_assigned == 'false'
Expand Down

0 comments on commit d202926

Please sign in to comment.