From e3c2fba5991b78d8c7496d933d6dacbaf566845b Mon Sep 17 00:00:00 2001 From: Michel Roegl-Brunner <73236783+michelroegl-brunner@users.noreply.github.com> Date: Wed, 23 Apr 2025 09:40:58 +0200 Subject: [PATCH] Fix Workflow to close discussions (#3999) * Rework Discussion close WF * Rework Discussion close WF --- .github/workflows/close-discussion.yml | 107 +++++++++++++++++-------- 1 file changed, 72 insertions(+), 35 deletions(-) diff --git a/.github/workflows/close-discussion.yml b/.github/workflows/close-discussion.yml index a811371d4..a3047904a 100644 --- a/.github/workflows/close-discussion.yml +++ b/.github/workflows/close-discussion.yml @@ -1,8 +1,13 @@ name: Close Discussion on PR Merge on: - pull_request: - types: [closed] + push: + branches: + - main + +permissions: + contents: read + discussions: write jobs: close-discussion: @@ -11,56 +16,82 @@ jobs: steps: - name: Checkout Repository uses: actions/checkout@v4 - with: - repository: community-scripts/ProxmoxVE - ref: main - token: ${{ secrets.GITHUB_TOKEN }} - name: Set Up Node.js uses: actions/setup-node@v4 with: node-version: "20" + - name: Install Dependencies run: npm install zx @octokit/graphql - name: Close Discussion env: - GITHUB_TOKEN: ${{ secrets.PAT_MICHEL }} - PR_BODY: ${{ github.event.pull_request.body }} - PR_NUMBER: ${{ github.event.pull_request.number }} - REPO_OWNER: ${{ github.repository_owner }} - REPO_NAME: ${{ github.event.repository.name }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_SHA: ${{ github.sha }} + GITHUB_REPOSITORY: ${{ github.repository }} run: | npx zx << 'EOF' import { graphql } from "@octokit/graphql"; - (async function() { + + (async function () { try { const token = process.env.GITHUB_TOKEN; - const prBody = process.env.PR_BODY; - const prNumber = process.env.PR_NUMBER; - const owner = process.env.REPO_OWNER; - const repo = process.env.REPO_NAME; + const commitSha = process.env.GITHUB_SHA; + const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/"); - if (!token || !prBody || !prNumber || !owner || !repo) { + if (!token || !commitSha || !owner || !repo) { console.log("Missing required environment variables."); 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+)/); if (!match) { console.log("No discussion ID found in PR body."); return; } + const discussionNumber = match[1]; - 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 = ` query($owner: String!, $repo: String!, $number: Int!) { repository(owner: $owner, name: $repo) { @@ -70,21 +101,26 @@ jobs: } } `; - - const discussionResponse = await graphqlWithAuth(discussionQuery, { + + // + try { + const discussionResponse = await graphqlWithAuth(discussionQuery, { owner, repo, number: parseInt(discussionNumber, 10), - }); - - const discussionQLId = discussionResponse.repository.discussion.id; - if (!discussionQLId) { - console.log("Failed to fetch discussion GraphQL ID."); + }); + + const discussionQLId = discussionResponse.repository.discussion.id; + if (!discussionQLId) { + console.log("Failed to fetch discussion GraphQL ID."); + return; + } + } catch (error) { + console.error("Discussion not found or error occurred while fetching discussion:", error); return; } - console.log(`GraphQL Discussion ID: ${discussionQLId}`); - + // Post comment const commentMutation = ` mutation($discussionId: ID!, $body: String!) { addDiscussionComment(input: { discussionId: $discussionId, body: $body }) { @@ -106,6 +142,7 @@ jobs: console.log(`Comment Posted Successfully! Comment ID: ${commentId}`); + // Mark comment as answer const markAnswerMutation = ` mutation($id: ID!) { markDiscussionCommentAsAnswer(input: { id: $id }) { @@ -120,7 +157,7 @@ jobs: } catch (error) { console.error("Error:", error); - return; + process.exit(1); } })(); - EOF \ No newline at end of file + EOF