Create issue from discussion and update discussion #17
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: Create issue from discussion and update discussion | |
on: | |
discussion: | |
types: [labeled] | |
jobs: | |
create-issue-and-update-discussion: | |
if: contains(fromJson('["explorer", "indexer"]'), github.event.label.name) | |
runs-on: ubuntu-latest | |
steps: | |
- name: Generate token | |
id: generate_token | |
uses: tibdex/github-app-token@v2 | |
with: | |
app_id: ${{ secrets.APP_ID }} | |
private_key: ${{ secrets.APP_PRIVATE_KEY }} | |
- name: Get discussion details | |
id: get_discussion | |
run: | | |
echo "DISCUSSION_TITLE=$(jq -r '.discussion.title' $GITHUB_EVENT_PATH | sed 's/"/\\"/g')" >> $GITHUB_ENV | |
echo "DISCUSSION_BODY=$(jq -r '.discussion.body' $GITHUB_EVENT_PATH | sed 's/"/\\"/g')" >> $GITHUB_ENV | |
echo "DISCUSSION_URL=$(jq -r '.discussion.html_url' $GITHUB_EVENT_PATH)" >> $GITHUB_ENV | |
echo "DISCUSSION_ID=$(jq -r '.discussion.node_id' $GITHUB_EVENT_PATH)" >> $GITHUB_ENV | |
echo "LABEL=$(jq -r '.label.name' $GITHUB_EVENT_PATH)" >> $GITHUB_ENV | |
- name: Create issue | |
id: create_issue | |
env: | |
GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }} | |
run: | | |
if [ "$LABEL" == "explorer" ]; then | |
REPO="stampchain-io/BTCStampsExplorer" | |
elif [ "$LABEL" == "indexer" ]; then | |
REPO="stampchain-io/btc_stamps" | |
else | |
echo "Invalid label" | |
exit 1 | |
fi | |
UNIQUE_ID="DI-$(date +%s)-$(openssl rand -hex 4)" | |
ISSUE_BODY="Created from discussion: $DISCUSSION_URL\nDiscussion-Issue-ID: $UNIQUE_ID\n\n$DISCUSSION_BODY" | |
RESPONSE=$(curl -X POST -H "Authorization: token $GITHUB_TOKEN" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
https://api.github.com/repos/$REPO/issues \ | |
-d "{\"title\":\"Discussion: $DISCUSSION_TITLE\", \"body\":\"$ISSUE_BODY\", \"labels\":[\"from-discussion\"]}") | |
ISSUE_URL=$(echo $RESPONSE | jq -r '.html_url') | |
echo "ISSUE_URL=$ISSUE_URL" >> $GITHUB_ENV | |
echo "ISSUE_ID=$UNIQUE_ID" >> $GITHUB_ENV | |
- name: Update discussion | |
env: | |
GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }} | |
run: | | |
UPDATED_BODY="$DISCUSSION_BODY\n\n---\nIssue created: $ISSUE_URL\nDiscussion-Issue-ID: $ISSUE_ID" | |
curl -X POST -H "Authorization: token $GITHUB_TOKEN" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
https://api.github.com/graphql \ | |
-d "{\"query\":\"mutation { updateDiscussion(input: {discussionId: \\\"$DISCUSSION_ID\\\", body: \\\"$UPDATED_BODY\\\"}) { discussion { id } } }\"}" | |
- name: Log results | |
run: | | |
echo "Issue created: $ISSUE_URL" | |
echo "Discussion updated: $DISCUSSION_URL" | |
echo "Discussion-Issue-ID: $ISSUE_ID" |