1+ name : Close Discussion on PR Merge
2+
3+ on :
4+ pull_request :
5+ types : [closed]
6+
7+ jobs :
8+ close-discussion :
9+ runs-on : ubuntu-latest
10+
11+ steps :
12+ - name : Checkout Repository
13+ uses : actions/checkout@v4
14+
15+ - name : Set Up Node.js
16+ uses : actions/setup-node@v4
17+ with :
18+ node-version : " 20"
19+ - name : Install Dependencies
20+ run : npm install zx @octokit/graphql
21+
22+ - name : Close Discussion
23+ env :
24+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
25+ PR_BODY : ${{ github.event.pull_request.body }}
26+ PR_NUMBER : ${{ github.event.pull_request.number }}
27+ REPO_OWNER : ${{ github.repository_owner }}
28+ REPO_NAME : ${{ github.event.repository.name }}
29+ run : |
30+ npx zx << 'EOF'
31+ import { graphql } from "@octokit/graphql";
32+ (async function() {
33+ try {
34+ const token = process.env.GITHUB_TOKEN;
35+ const prBody = process.env.PR_BODY;
36+ const prNumber = process.env.PR_NUMBER;
37+ const owner = process.env.REPO_OWNER;
38+ const repo = process.env.REPO_NAME;
39+
40+ if (!token || !prBody || !prNumber || !owner || !repo) {
41+ console.log("Missing required environment variables.");
42+ process.exit(1);
43+ }
44+
45+ const match = prBody.match(/#(\d+)/);
46+ if (!match) {
47+ console.log("No discussion ID found in PR body.");
48+ return;
49+ }
50+ const discussionNumber = match[1];
51+
52+ console.log(`Extracted Discussion Number: ${discussionNumber}`);
53+ console.log(`PR Number: ${prNumber}`);
54+ console.log(`Repository: ${owner}/${repo}`);
55+
56+ const graphqlWithAuth = graphql.defaults({
57+ headers: { authorization: `Bearer ${token}` },
58+ });
59+
60+ const discussionQuery = `
61+ query($owner: String!, $repo: String!, $number: Int!) {
62+ repository(owner: $owner, name: $repo) {
63+ discussion(number: $number) {
64+ id
65+ }
66+ }
67+ }
68+ `;
69+
70+ const discussionResponse = await graphqlWithAuth(discussionQuery, {
71+ owner,
72+ repo,
73+ number: parseInt(discussionNumber, 10),
74+ });
75+
76+ const discussionQLId = discussionResponse.repository.discussion.id;
77+ if (!discussionQLId) {
78+ console.log("Failed to fetch discussion GraphQL ID.");
79+ return;
80+ }
81+
82+ console.log(`GraphQL Discussion ID: ${discussionQLId}`);
83+
84+ const commentMutation = `
85+ mutation($discussionId: ID!, $body: String!) {
86+ addDiscussionComment(input: { discussionId: $discussionId, body: $body }) {
87+ comment { id body }
88+ }
89+ }
90+ `;
91+
92+ const commentResponse = await graphqlWithAuth(commentMutation, {
93+ discussionId: discussionQLId,
94+ body: `Merged with PR #${prNumber}`,
95+ });
96+
97+ const commentId = commentResponse.addDiscussionComment.comment.id;
98+ if (!commentId) {
99+ console.log("Failed to post the comment.");
100+ return;
101+ }
102+
103+ console.log(`Comment Posted Successfully! Comment ID: ${commentId}`);
104+
105+ const markAnswerMutation = `
106+ mutation($id: ID!) {
107+ markDiscussionCommentAsAnswer(input: { id: $id }) {
108+ discussion { id title }
109+ }
110+ }
111+ `;
112+
113+ await graphqlWithAuth(markAnswerMutation, { id: commentId });
114+
115+ console.log("Comment marked as answer successfully!");
116+
117+ } catch (error) {
118+ console.error("Error:", error);
119+ return;
120+ }
121+ })();
122+ EOF
0 commit comments