From 0bcd4fc179830c3a7e12b9cc635af756a2239747 Mon Sep 17 00:00:00 2001 From: DevIos01 Date: Sat, 23 Dec 2023 12:31:31 +0100 Subject: [PATCH] Passing PRNumber To Different Workflow --- .github/workflows/check_plagiarism.yml | 9 +++++++ .github/workflows/workflow_run.yml | 34 +++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/.github/workflows/check_plagiarism.yml b/.github/workflows/check_plagiarism.yml index cf179e154a..2d7806fcab 100644 --- a/.github/workflows/check_plagiarism.yml +++ b/.github/workflows/check_plagiarism.yml @@ -54,6 +54,15 @@ jobs: name: compare50-results path: saved_dir/ + - name: Save PR number to file + run: echo ${{ github.event.pull_request.number }} > pr_number.txt + + - name: Upload PR Number as Artifact + uses: actions/upload-artifact@v4 + with: + name: pr-number + path: pr_number.txt + - name: Upload Plagiarism Report as Artifact if: always() uses: actions/upload-artifact@v4 diff --git a/.github/workflows/workflow_run.yml b/.github/workflows/workflow_run.yml index a8435f1dc9..8b5e4cacf1 100644 --- a/.github/workflows/workflow_run.yml +++ b/.github/workflows/workflow_run.yml @@ -36,6 +36,36 @@ jobs: console.log(`Artifact name: ${artifact.name}, ID: ${artifact.id}, Size: ${artifact.size_in_bytes} bytes`); } + - name: Download PR Number Artifact + uses: actions/github-script@v7 + with: + github-token: ${{secrets.GITHUB_TOKEN}} + script: | + const runId = ${{ github.event.workflow_run.id }}; + const artifactName = 'pr-number'; + + console.log(`Downloading artifact ${artifactName} from workflow run ID: ${runId}`); + const artifactData = await github.rest.actions.downloadArtifact({ + owner: context.repo.owner, + repo: context.repo.repo, + artifact_id: (await github.rest.actions.listWorkflowRunArtifacts({ + owner: context.repo.owner, + repo: context.repo.repo, + run_id: runId + })).data.artifacts.find(a => a.name === artifactName).id, + archive_format: 'zip', + }); + + const fs = require('fs'); + const path = require('path'); + const artifactPath = path.join(process.env.GITHUB_WORKSPACE, `${artifactName}.zip`); + fs.writeFileSync(artifactPath, Buffer.from(artifactData.data)); + + console.log(`Artifact ${artifactName} downloaded to ${artifactPath}`); + require('child_process').execSync(`unzip -o ${artifactPath} -d ${process.env.GITHUB_WORKSPACE}`); + const prNumber = fs.readFileSync(path.join(process.env.GITHUB_WORKSPACE, 'pr_number.txt'), 'utf8').trim(); + console.log(`PR Number: ${prNumber}`); + - name: Download Plagiarism Report Artifact from Another Workflow Run uses: actions/github-script@v7 with: @@ -64,12 +94,11 @@ jobs: archive_format: 'zip', }); - const fs = require('fs'); - const path = require('path'); const artifactPath = path.join(process.env.GITHUB_WORKSPACE, `${artifactName}.zip`); fs.writeFileSync(artifactPath, Buffer.from(artifactData.data)); console.log(`Artifact ${artifactName} downloaded to ${artifactPath}`); + require('child_process').execSync(`unzip -o ${artifactPath} -d ${process.env.GITHUB_WORKSPACE}`); - name: List Contents of Zip File run: unzip -l plagiarism-report.zip @@ -93,7 +122,6 @@ jobs: const markdownContent = fs.readFileSync(markdownPath, 'utf8'); console.log("Fetching associated pull request..."); - const prNumber = ${{ github.event.workflow_run.pull_requests[0].number }}; console.log(`Found associated pull request: #${prNumber}`); console.log("Posting the Markdown content as a comment...");