-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update with unique record connecting issue-discussion
- Loading branch information
1 parent
966c7d6
commit 596ee58
Showing
3 changed files
with
121 additions
and
23 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.`); | ||
} | ||
} | ||
} |