diff --git a/.github/workflows/create_issue_from_discussion.yml b/.github/workflows/create_issue_from_discussion.yml index 87c6948..9149c99 100644 --- a/.github/workflows/create_issue_from_discussion.yml +++ b/.github/workflows/create_issue_from_discussion.yml @@ -6,7 +6,6 @@ on: jobs: create-issue-and-update-discussion: - if: contains(fromJson('["explorer", "indexer"]'), github.event.label.name) runs-on: ubuntu-latest steps: - name: Generate token @@ -33,18 +32,21 @@ jobs: 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)}`; + // Create issue const issue = await github.rest.issues.create({ owner: owner, repo: repo, title: `Discussion: ${discussion.title}`, - body: `Created from discussion: ${discussion.html_url}\n\n${discussion.body}`, + body: `Created from discussion: ${discussion.html_url}\n\nDiscussion-Issue-ID: ${uniqueId}\n\n${discussion.body}`, labels: ['from-discussion'] }); - // Update discussion with issue link using GraphQL + // Update discussion with issue link const issueUrl = issue.data.html_url; - const updatedBody = `${discussion.body}\n\n---\nIssue created: ${issueUrl}`; + const updatedBody = `${discussion.body}\n\n---\nIssue created: ${issueUrl}\nDiscussion-Issue-ID: ${uniqueId}`; await github.graphql(` mutation($discussionId: ID!, $body: String!) { @@ -61,4 +63,4 @@ jobs: console.log(`Issue created: ${issueUrl}`); console.log(`Discussion updated: ${discussion.html_url}`); - } \ No newline at end of file + console.log(`Discussion-Issue-ID: ${uniqueId}`); \ No newline at end of file diff --git a/.github/workflows/update_discussion_on_issue_close.yml b/.github/workflows/update_discussion_on_issue_close.yml index 3a30396..7e8e650 100644 --- a/.github/workflows/update_discussion_on_issue_close.yml +++ b/.github/workflows/update_discussion_on_issue_close.yml @@ -24,13 +24,16 @@ jobs: // Check if the issue was created from a discussion if (issue.labels.some(label => label.name === 'from-discussion')) { - // Extract the discussion URL from the issue body + // Extract the Discussion-Issue-ID and discussion URL from the issue body + const idMatch = issue.body.match(/Discussion-Issue-ID: (DI-[\w-]+)/); const discussionUrlMatch = issue.body.match(/Created from discussion: (https:\/\/github\.com\/.*\/discussions\/\d+)/); - if (discussionUrlMatch) { + + if (idMatch && discussionUrlMatch) { + const uniqueId = idMatch[1]; const discussionUrl = discussionUrlMatch[1]; const [, , , orgName, repoName, , discussionNumber] = discussionUrl.split('/'); - // Fetch the discussion to get its node_id + // Fetch the discussion to get its node_id and body const { repository } = await github.graphql(` query($owner: String!, $repo: String!, $number: Int!) { repository(owner: $owner, name: $repo) { @@ -49,22 +52,27 @@ jobs: const discussionId = repository.discussion.id; const currentBody = repository.discussion.body; - // Update the discussion body - const updatedBody = `${currentBody}\n\n---\nRelated issue closed: ${issue.html_url}\nClosed on: ${issue.closed_at}\nClosed by: ${issue.closed_by.login}`; - - await github.graphql(` - mutation($discussionId: ID!, $body: String!) { - updateDiscussion(input: {discussionId: $discussionId, body: $body}) { - discussion { - id + // Ensure the Discussion-Issue-ID matches + if (currentBody.includes(`Discussion-Issue-ID: ${uniqueId}`)) { + // Update the discussion body + const updatedBody = `${currentBody}\n\n---\nRelated issue closed: ${issue.html_url}\nClosed on: ${issue.closed_at}\nClosed by: ${issue.closed_by.login}`; + + await github.graphql(` + mutation($discussionId: ID!, $body: String!) { + updateDiscussion(input: {discussionId: $discussionId, body: $body}) { + discussion { + id + } } } - } - `, { - discussionId: discussionId, - body: updatedBody - }); - - console.log(`Discussion updated: ${discussionUrl}`); + `, { + discussionId: discussionId, + body: updatedBody + }); + + console.log(`Discussion updated: ${discussionUrl}`); + } else { + console.log(`Discussion-Issue-ID mismatch. No update performed.`); + } } } \ No newline at end of file diff --git a/.github/workflows/update_discussion_on_issue_edit.yml b/.github/workflows/update_discussion_on_issue_edit.yml new file mode 100644 index 0000000..f082da8 --- /dev/null +++ b/.github/workflows/update_discussion_on_issue_edit.yml @@ -0,0 +1,88 @@ +name: Update discussion when issue is edited + +on: + issues: + types: [edited] + +jobs: + update-discussion: + 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: Update discussion + uses: actions/github-script@v6 + with: + github-token: ${{ steps.generate_token.outputs.token }} + script: | + const issue = context.payload.issue; + const changes = context.payload.changes; + + // Check if the issue was created from a discussion + if (issue.labels.some(label => label.name === 'from-discussion')) { + // Extract the Discussion-Issue-ID and discussion URL from the issue body + const idMatch = issue.body.match(/Discussion-Issue-ID: (DI-[\w-]+)/); + const discussionUrlMatch = issue.body.match(/Created from discussion: (https:\/\/github\.com\/.*\/discussions\/\d+)/); + + if (idMatch && discussionUrlMatch) { + const uniqueId = idMatch[1]; + const discussionUrl = discussionUrlMatch[1]; + const [, , , orgName, repoName, , discussionNumber] = discussionUrl.split('/'); + + // Fetch the discussion to get its node_id and body + const { repository } = await github.graphql(` + query($owner: String!, $repo: String!, $number: Int!) { + repository(owner: $owner, name: $repo) { + discussion(number: $number) { + id + body + } + } + } + `, { + owner: orgName, + repo: repoName, + number: parseInt(discussionNumber) + }); + + const discussionId = repository.discussion.id; + const currentBody = repository.discussion.body; + + // Ensure the Discussion-Issue-ID matches + if (currentBody.includes(`Discussion-Issue-ID: ${uniqueId}`)) { + // Prepare update message + let updateMessage = `\n\n---\nIssue updated on: ${new Date().toISOString()}`; + if (changes.title) { + updateMessage += `\nTitle changed from "${changes.title.from}" to "${issue.title}"`; + } + if (changes.body) { + updateMessage += `\nBody was updated`; + } + + // Update the discussion body + const updatedBody = `${currentBody}${updateMessage}`; + + await github.graphql(` + mutation($discussionId: ID!, $body: String!) { + updateDiscussion(input: {discussionId: $discussionId, body: $body}) { + discussion { + id + } + } + } + `, { + discussionId: discussionId, + body: updatedBody + }); + + console.log(`Discussion updated: ${discussionUrl}`); + } else { + console.log(`Discussion-Issue-ID mismatch. No update performed.`); + } + } + } \ No newline at end of file