Skip to content

Commit

Permalink
chore: update with unique record connecting issue-discussion
Browse files Browse the repository at this point in the history
  • Loading branch information
reinamora137 committed Sep 11, 2024
1 parent 966c7d6 commit 596ee58
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 23 deletions.
12 changes: 7 additions & 5 deletions .github/workflows/create_issue_from_discussion.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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!) {
Expand All @@ -61,4 +63,4 @@ jobs:
console.log(`Issue created: ${issueUrl}`);
console.log(`Discussion updated: ${discussion.html_url}`);
}
console.log(`Discussion-Issue-ID: ${uniqueId}`);
44 changes: 26 additions & 18 deletions .github/workflows/update_discussion_on_issue_close.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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.`);
}
}
}
88 changes: 88 additions & 0 deletions .github/workflows/update_discussion_on_issue_edit.yml
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.`);
}
}
}

0 comments on commit 596ee58

Please sign in to comment.