11name : Close Discussion on PR Merge
22
33on :
4- pull_request :
5- types : [closed]
4+ push :
5+ branches :
6+ - main
7+
8+ permissions :
9+ contents : read
10+ discussions : write
611
712jobs :
813 close-discussion :
@@ -11,56 +16,82 @@ jobs:
1116 steps :
1217 - name : Checkout Repository
1318 uses : actions/checkout@v4
14- with :
15- repository : community-scripts/ProxmoxVE
16- ref : main
17- token : ${{ secrets.GITHUB_TOKEN }}
1819
1920 - name : Set Up Node.js
2021 uses : actions/setup-node@v4
2122 with :
2223 node-version : " 20"
24+
2325 - name : Install Dependencies
2426 run : npm install zx @octokit/graphql
2527
2628 - name : Close Discussion
2729 env :
28- GITHUB_TOKEN : ${{ secrets.PAT_MICHEL }}
29- PR_BODY : ${{ github.event.pull_request.body }}
30- PR_NUMBER : ${{ github.event.pull_request.number }}
31- REPO_OWNER : ${{ github.repository_owner }}
32- REPO_NAME : ${{ github.event.repository.name }}
30+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
31+ GITHUB_SHA : ${{ github.sha }}
32+ GITHUB_REPOSITORY : ${{ github.repository }}
3333 run : |
3434 npx zx << 'EOF'
3535 import { graphql } from "@octokit/graphql";
36- (async function() {
36+
37+ (async function () {
3738 try {
3839 const token = process.env.GITHUB_TOKEN;
39- const prBody = process.env.PR_BODY;
40- const prNumber = process.env.PR_NUMBER;
41- const owner = process.env.REPO_OWNER;
42- const repo = process.env.REPO_NAME;
40+ const commitSha = process.env.GITHUB_SHA;
41+ const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");
4342
44- if (!token || !prBody || !prNumber || !owner || !repo) {
43+ if (!token || !commitSha || !owner || !repo) {
4544 console.log("Missing required environment variables.");
4645 process.exit(1);
4746 }
4847
48+ const graphqlWithAuth = graphql.defaults({
49+ headers: { authorization: `Bearer ${token}` },
50+ });
51+
52+ // Find PR from commit SHA
53+ const searchQuery = `
54+ query($owner: String!, $repo: String!, $sha: GitObjectID!) {
55+ repository(owner: $owner, name: $repo) {
56+ object(oid: $sha) {
57+ ... on Commit {
58+ associatedPullRequests(first: 1) {
59+ nodes {
60+ number
61+ body
62+ }
63+ }
64+ }
65+ }
66+ }
67+ }
68+ `;
69+
70+ const prResult = await graphqlWithAuth(searchQuery, {
71+ owner,
72+ repo,
73+ sha: commitSha,
74+ });
75+
76+ const pr = prResult.repository.object.associatedPullRequests.nodes[0];
77+ if (!pr) {
78+ console.log("No PR found for this commit.");
79+ return;
80+ }
81+
82+ const prNumber = pr.number;
83+ const prBody = pr.body;
84+
4985 const match = prBody.match(/#(\d+)/);
5086 if (!match) {
5187 console.log("No discussion ID found in PR body.");
5288 return;
5389 }
54- const discussionNumber = match[1];
5590
91+ const discussionNumber = match[1];
5692 console.log(`Extracted Discussion Number: ${discussionNumber}`);
57- console.log(`PR Number: ${prNumber}`);
58- console.log(`Repository: ${owner}/${repo}`);
59-
60- const graphqlWithAuth = graphql.defaults({
61- headers: { authorization: `Bearer ${token}` },
62- });
6393
94+ // Fetch GraphQL discussion ID
6495 const discussionQuery = `
6596 query($owner: String!, $repo: String!, $number: Int!) {
6697 repository(owner: $owner, name: $repo) {
@@ -70,21 +101,26 @@ jobs:
70101 }
71102 }
72103 `;
73-
74- const discussionResponse = await graphqlWithAuth(discussionQuery, {
104+
105+ //
106+ try {
107+ const discussionResponse = await graphqlWithAuth(discussionQuery, {
75108 owner,
76109 repo,
77110 number: parseInt(discussionNumber, 10),
78- });
79-
80- const discussionQLId = discussionResponse.repository.discussion.id;
81- if (!discussionQLId) {
82- console.log("Failed to fetch discussion GraphQL ID.");
111+ });
112+
113+ const discussionQLId = discussionResponse.repository.discussion.id;
114+ if (!discussionQLId) {
115+ console.log("Failed to fetch discussion GraphQL ID.");
116+ return;
117+ }
118+ } catch (error) {
119+ console.error("Discussion not found or error occurred while fetching discussion:", error);
83120 return;
84121 }
85122
86- console.log(`GraphQL Discussion ID: ${discussionQLId}`);
87-
123+ // Post comment
88124 const commentMutation = `
89125 mutation($discussionId: ID!, $body: String!) {
90126 addDiscussionComment(input: { discussionId: $discussionId, body: $body }) {
@@ -106,6 +142,7 @@ jobs:
106142
107143 console.log(`Comment Posted Successfully! Comment ID: ${commentId}`);
108144
145+ // Mark comment as answer
109146 const markAnswerMutation = `
110147 mutation($id: ID!) {
111148 markDiscussionCommentAsAnswer(input: { id: $id }) {
@@ -120,7 +157,7 @@ jobs:
120157
121158 } catch (error) {
122159 console.error("Error:", error);
123- return ;
160+ process.exit(1) ;
124161 }
125162 })();
126- EOF
163+ EOF
0 commit comments