Create issue from discussion and update discussion #12
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: Create issue and update discussion | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ steps.generate_token.outputs.token }} | |
script: | | |
const label = context.payload.label.name; | |
const discussion = context.payload.discussion; | |
// Map labels to repositories | |
const repoMap = { | |
'explorer': 'stampchain-io/BTCStampsExplorer', | |
'indexer': 'stampchain-io/btc_stamps', | |
}; | |
// Function to safely stringify content | |
const safeStringify = (obj) => JSON.stringify(obj).replace(/[^\w\s-]/g, ''); | |
if (repoMap[label]) { | |
const [owner, repo] = repoMap[label].split('/'); | |
// Generate a unique ID | |
const uniqueId = `DI-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`; | |
// Safely stringify title and body | |
const safeTitle = safeStringify(discussion.title); | |
const safeBody = safeStringify(discussion.body); | |
// Create issue | |
const issue = await github.rest.issues.create({ | |
owner: owner, | |
repo: repo, | |
title: `Discussion: ${safeTitle}`, | |
body: `Created from discussion: ${discussion.html_url}\nDiscussion-Issue-ID: ${uniqueId}\n\n${safeBody}`, | |
labels: ['from-discussion'] | |
}); | |
// Update discussion with issue link | |
const issueUrl = issue.data.html_url; | |
const updatedBody = `${safeBody}\n\n---\nIssue created: ${issueUrl}\nDiscussion-Issue-ID: ${uniqueId}`; | |
await github.graphql(` | |
mutation($discussionId: ID!, $body: String!) { | |
updateDiscussion(input: {discussionId: $discussionId, body: $body}) { | |
discussion { | |
id | |
} | |
} | |
} | |
`, { | |
discussionId: discussion.node_id, | |
body: updatedBody | |
}); | |
console.log(`Issue created: ${issueUrl}`); | |
console.log(`Discussion updated: ${discussion.html_url}`); | |
console.log(`Discussion-Issue-ID: ${uniqueId}`); |