From 0d82041204325ef2d11d6674f1682d7b88825b1f Mon Sep 17 00:00:00 2001 From: Zehir Date: Wed, 15 May 2024 22:27:49 +0000 Subject: [PATCH 1/2] Add interaction dispatcher --- .github/workflows/ci-ddf-store.yml | 11 +-- .github/workflows/interactions-dispatcher.yml | 90 +++++++++++++++++++ 2 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/interactions-dispatcher.yml diff --git a/.github/workflows/ci-ddf-store.yml b/.github/workflows/ci-ddf-store.yml index c85ff5971d..f47cc7a871 100644 --- a/.github/workflows/ci-ddf-store.yml +++ b/.github/workflows/ci-ddf-store.yml @@ -1,11 +1,6 @@ # This is a basic workflow to help you get started with Actions -name: Continuous integration DDF Bundles - -permissions: - contents: write - pull-requests: write - issues: write +name: CI DDF Bundles # Controls when the action will run. on: @@ -87,7 +82,7 @@ jobs: ci-ddf-bundles: # The type of runner that the job will run on runs-on: ubuntu-latest - name: Continuous integration DDF Bundles + name: CI DDF Bundles # Steps represent a sequence of tasks that will be executed as part of the job steps: @@ -163,7 +158,7 @@ jobs: # Github artifact upload-artifact-enabled: true upload-artifact-filter: 'added,modified' - upload-artifact-retention-days: 3 + upload-artifact-retention-days: 30 # DDF Store # Bundles are uploaded only on the push event when the PR is merged diff --git a/.github/workflows/interactions-dispatcher.yml b/.github/workflows/interactions-dispatcher.yml new file mode 100644 index 0000000000..28940137c9 --- /dev/null +++ b/.github/workflows/interactions-dispatcher.yml @@ -0,0 +1,90 @@ +name: Interactions dispatcher + +on: + workflow_run: + workflows: ["CI DDF Bundles"] + +jobs: + use_output: + runs-on: ubuntu-latest + steps: + - uses: actions/github-script@v7 + name: 'Interactions dispatcher' + + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const fs = require('fs'); + + async function upsertComment(owner, repo, issue_number, marker, body) { + const {data: comments} = await github.rest.issues.listComments( + {owner, repo, issue_number}); + + body = `${marker}\n${body}`; + + const existing = comments.filter((c) => c.body.includes(marker)); + if (existing.length > 0) { + const last = existing[existing.length - 1]; + core.info(`Updating comment ${last.id}`); + await github.rest.issues.updateComment({ + owner, repo, + body, + comment_id: last.id, + }); + } else { + core.info(`Creating a comment in issue / PR #${issue_number}`); + await github.rest.issues.createComment({issue_number, body, owner, repo}); + } + } + + async function run(){ + const {owner, repo} = context.repo; + + core.info("Fetching artifacts list") + const allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ + owner, + repo, + run_id: context.payload.workflow_run.id, + }); + + const matchArtifact = allArtifacts.data.artifacts.filter((artifact) => { + return artifact.name === "interaction_data" + })[0]; + + if(matchArtifact === undefined) { + core.info("No artifact found, stopping the action") + return + } + + core.info(`Downloading artifact #${matchArtifact.id}`) + const download = await github.rest.actions.downloadArtifact({ + owner, + repo, + artifact_id: matchArtifact.id, + archive_format: 'zip', + }); + + await github.rest.actions.deleteArtifact({ + owner, + repo, + artifact_id: matchArtifact.id + }) + + fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/interaction_data.zip`, Buffer.from(download.data)); + + await exec.exec(`unzip ${process.env.GITHUB_WORKSPACE}/interaction_data.zip`); + + const interaction_data = JSON.parse(fs.readFileSync(`${process.env.GITHUB_WORKSPACE}/interaction_data.json`)); + + await Promise.all(interaction_data.map(async (data) => { + const {mode, marker, issue_number, body} = data; + + if(mode === 'upsert') + await upsertComment(owner, repo, issue_number, marker, body); + else if(mode === 'insert') + await github.rest.issues.createComment({issue_number, body, owner, repo}); + + })); + } + + run(); From 23d68f6d820c62919f3d12944f089953c86f8ece Mon Sep 17 00:00:00 2001 From: Zehir Date: Wed, 15 May 2024 22:31:21 +0000 Subject: [PATCH 2/2] Renammed workflow file --- .github/workflows/{ci-ddf-store.yml => ci-ddf-bundles.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/{ci-ddf-store.yml => ci-ddf-bundles.yml} (100%) diff --git a/.github/workflows/ci-ddf-store.yml b/.github/workflows/ci-ddf-bundles.yml similarity index 100% rename from .github/workflows/ci-ddf-store.yml rename to .github/workflows/ci-ddf-bundles.yml