Last active
September 4, 2024 07:18
-
-
Save DavidLMS/caae7a8b2d63a6eea7f7369f45b2434a to your computer and use it in GitHub Desktop.
Persistence between runs of a GitHub Action using a Discussion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Store PR Info in Discussion | |
on: | |
pull_request: | |
types: [opened, synchronize] | |
permissions: | |
discussions: write | |
jobs: | |
update-discussion: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Set up GitHub CLI | |
run: | | |
echo "${{ secrets.GITHUB_TOKEN }}" | gh auth login --with-token | |
- name: Get Repository and Category IDs | |
id: get_ids | |
run: | | |
REPO_ID=$(gh api graphql -f query=' | |
query { | |
repository(owner: "${{ github.repository_owner }}", name: "${{ github.event.repository.name }}") { | |
id | |
} | |
}' --jq '.data.repository.id') | |
echo "REPO_ID=$REPO_ID" >> $GITHUB_ENV | |
CAT_ID=$(gh api graphql -f query=' | |
query { | |
repository(owner: "${{ github.repository_owner }}", name: "${{ github.event.repository.name }}") { | |
discussionCategories(first: 1) { | |
nodes { | |
id | |
} | |
} | |
} | |
}' --jq '.data.repository.discussionCategories.nodes[0].id') | |
echo "CAT_ID=$CAT_ID" >> $GITHUB_ENV | |
- name: Check if Discussion exists | |
id: check_discussion | |
run: | | |
DISCUSSION_ID=$(gh api graphql -f query=' | |
query { | |
repository(owner: "${{ github.repository_owner }}", name: "${{ github.event.repository.name }}") { | |
discussions(first: 100) { | |
nodes { | |
id | |
title | |
body | |
} | |
} | |
} | |
}' --jq '.data.repository.discussions.nodes[] | select(.title == "PR INFO") | .id') | |
echo "DISCUSSION_ID=$DISCUSSION_ID" >> $GITHUB_ENV | |
- name: Retrieve and Print Previous PR Info | |
if: env.DISCUSSION_ID != '' | |
run: | | |
DISCUSSION_BODY=$(gh api graphql -f query=' | |
query($id: ID!) { | |
node(id: $id) { | |
... on Discussion { | |
body | |
} | |
} | |
}' -f id="${{ env.DISCUSSION_ID }}" --jq '.data.node.body') | |
echo "Previous PR Info:" | |
echo "$DISCUSSION_BODY" | |
- name: Update Discussion with Current PR Info | |
if: env.DISCUSSION_ID != '' | |
run: | | |
NEW_BODY=$(echo -e "PR ID: ${{ github.event.pull_request.id }}\nTitle: ${{ github.event.pull_request.title }}\nAuthor: ${{ github.event.pull_request.user.login }}\nCreated At: ${{ github.event.pull_request.created_at }}\nDescription: ${{ github.event.pull_request.body }}\n") | |
gh api graphql -f query=' | |
mutation($id: ID!, $body: String!) { | |
updateDiscussion(input: {discussionId: $id, body: $body}) { | |
discussion { | |
url | |
} | |
} | |
}' -f id="${{ env.DISCUSSION_ID }}" -f body="$NEW_BODY" | |
- name: Create Discussion if it doesn't exist | |
if: env.DISCUSSION_ID == '' | |
run: | | |
gh api graphql -f query=' | |
mutation { | |
createDiscussion(input: { | |
repositoryId: "${{ env.REPO_ID }}", | |
title: "PR INFO", | |
body: "PR ID: ${{ github.event.pull_request.id }}\nTitle: ${{ github.event.pull_request.title }}\nAuthor: ${{ github.event.pull_request.user.login }}\nCreated At: ${{ github.event.pull_request.created_at }}\nDescription: ${{ github.event.pull_request.body }}", | |
categoryId: "${{ env.CAT_ID }}" | |
}) { | |
discussion { | |
url | |
} | |
} | |
}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment