mirror of
				https://github.com/community-scripts/ProxmoxVE.git
				synced 2025-11-04 10:22:50 +00:00 
			
		
		
		
	Fix Workflow to close discussions (#3999)
* Rework Discussion close WF * Rework Discussion close WF
This commit is contained in:
		
				
					committed by
					
						
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							563e73e65e
						
					
				
				
					commit
					e3c2fba599
				
			
							
								
								
									
										107
									
								
								.github/workflows/close-discussion.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										107
									
								
								.github/workflows/close-discussion.yml
									
									
									
									
										vendored
									
									
								
							@@ -1,8 +1,13 @@
 | 
				
			|||||||
name: Close Discussion on PR Merge
 | 
					name: Close Discussion on PR Merge
 | 
				
			||||||
 | 
					
 | 
				
			||||||
on:
 | 
					on:
 | 
				
			||||||
  pull_request:
 | 
					  push:
 | 
				
			||||||
    types: [closed]
 | 
					    branches:
 | 
				
			||||||
 | 
					      - main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					permissions:
 | 
				
			||||||
 | 
					  contents: read
 | 
				
			||||||
 | 
					  discussions: write
 | 
				
			||||||
 | 
					
 | 
				
			||||||
jobs:
 | 
					jobs:
 | 
				
			||||||
  close-discussion:
 | 
					  close-discussion:
 | 
				
			||||||
@@ -11,56 +16,82 @@ jobs:
 | 
				
			|||||||
    steps:
 | 
					    steps:
 | 
				
			||||||
      - name: Checkout Repository
 | 
					      - name: Checkout Repository
 | 
				
			||||||
        uses: actions/checkout@v4
 | 
					        uses: actions/checkout@v4
 | 
				
			||||||
        with:
 | 
					 | 
				
			||||||
          repository: community-scripts/ProxmoxVE
 | 
					 | 
				
			||||||
          ref: main
 | 
					 | 
				
			||||||
          token: ${{ secrets.GITHUB_TOKEN }}  
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      - name: Set Up Node.js
 | 
					      - name: Set Up Node.js
 | 
				
			||||||
        uses: actions/setup-node@v4
 | 
					        uses: actions/setup-node@v4
 | 
				
			||||||
        with:
 | 
					        with:
 | 
				
			||||||
          node-version: "20"
 | 
					          node-version: "20"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      - name: Install Dependencies
 | 
					      - name: Install Dependencies
 | 
				
			||||||
        run: npm install zx @octokit/graphql
 | 
					        run: npm install zx @octokit/graphql
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      - name: Close Discussion
 | 
					      - name: Close Discussion
 | 
				
			||||||
        env:
 | 
					        env:
 | 
				
			||||||
          GITHUB_TOKEN: ${{ secrets.PAT_MICHEL }}
 | 
					          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
 | 
				
			||||||
          PR_BODY: ${{ github.event.pull_request.body }}
 | 
					          GITHUB_SHA: ${{ github.sha }}
 | 
				
			||||||
          PR_NUMBER: ${{ github.event.pull_request.number }}
 | 
					          GITHUB_REPOSITORY: ${{ github.repository }}
 | 
				
			||||||
          REPO_OWNER: ${{ github.repository_owner }}
 | 
					 | 
				
			||||||
          REPO_NAME: ${{ github.event.repository.name }}
 | 
					 | 
				
			||||||
        run: |
 | 
					        run: |
 | 
				
			||||||
          npx zx << 'EOF'
 | 
					          npx zx << 'EOF'
 | 
				
			||||||
          import { graphql } from "@octokit/graphql";
 | 
					          import { graphql } from "@octokit/graphql";
 | 
				
			||||||
          (async function() {
 | 
					
 | 
				
			||||||
 | 
					          (async function () {
 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
              const token = process.env.GITHUB_TOKEN;
 | 
					              const token = process.env.GITHUB_TOKEN;
 | 
				
			||||||
              const prBody = process.env.PR_BODY;
 | 
					              const commitSha = process.env.GITHUB_SHA;
 | 
				
			||||||
              const prNumber = process.env.PR_NUMBER;
 | 
					              const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");
 | 
				
			||||||
              const owner = process.env.REPO_OWNER;
 | 
					 | 
				
			||||||
              const repo = process.env.REPO_NAME;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
              if (!token || !prBody || !prNumber || !owner || !repo) {
 | 
					              if (!token || !commitSha || !owner || !repo) {
 | 
				
			||||||
                console.log("Missing required environment variables.");
 | 
					                console.log("Missing required environment variables.");
 | 
				
			||||||
                process.exit(1);
 | 
					                process.exit(1);
 | 
				
			||||||
              }
 | 
					              }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					              const graphqlWithAuth = graphql.defaults({
 | 
				
			||||||
 | 
					                headers: { authorization: `Bearer ${token}` },
 | 
				
			||||||
 | 
					              });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					              // Find PR from commit SHA
 | 
				
			||||||
 | 
					              const searchQuery = `
 | 
				
			||||||
 | 
					                query($owner: String!, $repo: String!, $sha: GitObjectID!) {
 | 
				
			||||||
 | 
					                  repository(owner: $owner, name: $repo) {
 | 
				
			||||||
 | 
					                    object(oid: $sha) {
 | 
				
			||||||
 | 
					                      ... on Commit {
 | 
				
			||||||
 | 
					                        associatedPullRequests(first: 1) {
 | 
				
			||||||
 | 
					                          nodes {
 | 
				
			||||||
 | 
					                            number
 | 
				
			||||||
 | 
					                            body
 | 
				
			||||||
 | 
					                          }
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                      }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                  }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					              `;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					              const prResult = await graphqlWithAuth(searchQuery, {
 | 
				
			||||||
 | 
					                owner,
 | 
				
			||||||
 | 
					                repo,
 | 
				
			||||||
 | 
					                sha: commitSha,
 | 
				
			||||||
 | 
					              });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					              const pr = prResult.repository.object.associatedPullRequests.nodes[0];
 | 
				
			||||||
 | 
					              if (!pr) {
 | 
				
			||||||
 | 
					                console.log("No PR found for this commit.");
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					              }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					              const prNumber = pr.number;
 | 
				
			||||||
 | 
					              const prBody = pr.body;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
              const match = prBody.match(/#(\d+)/);
 | 
					              const match = prBody.match(/#(\d+)/);
 | 
				
			||||||
              if (!match) {
 | 
					              if (!match) {
 | 
				
			||||||
                console.log("No discussion ID found in PR body.");
 | 
					                console.log("No discussion ID found in PR body.");
 | 
				
			||||||
                return;
 | 
					                return;
 | 
				
			||||||
              }
 | 
					              }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
              const discussionNumber = match[1];
 | 
					              const discussionNumber = match[1];
 | 
				
			||||||
 | 
					 | 
				
			||||||
              console.log(`Extracted Discussion Number: ${discussionNumber}`);
 | 
					              console.log(`Extracted Discussion Number: ${discussionNumber}`);
 | 
				
			||||||
              console.log(`PR Number: ${prNumber}`);
 | 
					 | 
				
			||||||
              console.log(`Repository: ${owner}/${repo}`);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
              const graphqlWithAuth = graphql.defaults({
 | 
					 | 
				
			||||||
                headers: { authorization: `Bearer ${token}` },
 | 
					 | 
				
			||||||
              });
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					              // Fetch GraphQL discussion ID
 | 
				
			||||||
              const discussionQuery = `
 | 
					              const discussionQuery = `
 | 
				
			||||||
                query($owner: String!, $repo: String!, $number: Int!) {
 | 
					                query($owner: String!, $repo: String!, $number: Int!) {
 | 
				
			||||||
                  repository(owner: $owner, name: $repo) {
 | 
					                  repository(owner: $owner, name: $repo) {
 | 
				
			||||||
@@ -70,21 +101,26 @@ jobs:
 | 
				
			|||||||
                  }
 | 
					                  }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
              `;
 | 
					              `;
 | 
				
			||||||
              
 | 
					
 | 
				
			||||||
              const discussionResponse = await graphqlWithAuth(discussionQuery, {
 | 
					              //
 | 
				
			||||||
 | 
					              try {
 | 
				
			||||||
 | 
					                const discussionResponse = await graphqlWithAuth(discussionQuery, {
 | 
				
			||||||
                  owner,
 | 
					                  owner,
 | 
				
			||||||
                  repo,
 | 
					                  repo,
 | 
				
			||||||
                  number: parseInt(discussionNumber, 10),
 | 
					                  number: parseInt(discussionNumber, 10),
 | 
				
			||||||
              });              
 | 
					                });
 | 
				
			||||||
              
 | 
					
 | 
				
			||||||
              const discussionQLId = discussionResponse.repository.discussion.id;
 | 
					                const discussionQLId = discussionResponse.repository.discussion.id;
 | 
				
			||||||
              if (!discussionQLId) {
 | 
					                if (!discussionQLId) {
 | 
				
			||||||
                console.log("Failed to fetch discussion GraphQL ID.");
 | 
					                  console.log("Failed to fetch discussion GraphQL ID.");
 | 
				
			||||||
 | 
					                  return;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					              } catch (error) {
 | 
				
			||||||
 | 
					                console.error("Discussion not found or error occurred while fetching discussion:", error);
 | 
				
			||||||
                return;
 | 
					                return;
 | 
				
			||||||
              }
 | 
					              }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
              console.log(`GraphQL Discussion ID: ${discussionQLId}`);
 | 
					              // Post comment
 | 
				
			||||||
              
 | 
					 | 
				
			||||||
              const commentMutation = `
 | 
					              const commentMutation = `
 | 
				
			||||||
                mutation($discussionId: ID!, $body: String!) {
 | 
					                mutation($discussionId: ID!, $body: String!) {
 | 
				
			||||||
                  addDiscussionComment(input: { discussionId: $discussionId, body: $body }) {
 | 
					                  addDiscussionComment(input: { discussionId: $discussionId, body: $body }) {
 | 
				
			||||||
@@ -106,6 +142,7 @@ jobs:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
              console.log(`Comment Posted Successfully! Comment ID: ${commentId}`);
 | 
					              console.log(`Comment Posted Successfully! Comment ID: ${commentId}`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					              // Mark comment as answer
 | 
				
			||||||
              const markAnswerMutation = `
 | 
					              const markAnswerMutation = `
 | 
				
			||||||
                mutation($id: ID!) {
 | 
					                mutation($id: ID!) {
 | 
				
			||||||
                  markDiscussionCommentAsAnswer(input: { id: $id }) {
 | 
					                  markDiscussionCommentAsAnswer(input: { id: $id }) {
 | 
				
			||||||
@@ -120,7 +157,7 @@ jobs:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            } catch (error) {
 | 
					            } catch (error) {
 | 
				
			||||||
              console.error("Error:", error);
 | 
					              console.error("Error:", error);
 | 
				
			||||||
              return;
 | 
					              process.exit(1);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
          })();
 | 
					          })();
 | 
				
			||||||
          EOF
 | 
					          EOF
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user