From 1e0e1926009abe583c26a520f4727e0be4577605 Mon Sep 17 00:00:00 2001 From: Till Frankenbach <81414045+merydian@users.noreply.github.com> Date: Mon, 10 Jun 2024 00:34:13 +0200 Subject: [PATCH] Print link to test-version zip in PR discussions (#312) --- .github/workflows/build.yml | 41 ++++++-- .github/workflows/build_artifact_comment.yml | 104 +++++++++++++++++++ .github/workflows/testing.yml | 2 +- requirements/packaging.txt | 1 + setup.cfg | 5 + 5 files changed, 145 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/build_artifact_comment.yml create mode 100644 requirements/packaging.txt diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5e1696c..88f4c70 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,6 +1,8 @@ name: Build -on: push +on: + push: + pull_request: jobs: build: @@ -9,20 +11,45 @@ jobs: steps: - name: Get source code - uses: actions/checkout@v2 + uses: actions/checkout@v4 + with: + # To fetch tags + fetch-depth: 0 - name: Set up Python - uses: actions/setup-python@v2 + uses: actions/setup-python@v5 with: - python-version: "3.9" + python-version: "3.10" + cache: "pip" + cache-dependency-path: "requirements/packaging.txt" + + # - name: Install Qt lrelease + # run: | + # sudo apt-get update + # sudo apt-get install qttools5-dev-tools + + - name: Install Python requirements + run: pip install -r requirements/packaging.txt + + - name: Set plugin version environment variables + run: | + TAG=$(git describe --tags $(git rev-list --tags --max-count=1)) + echo "VERSION=$(echo ${TAG} | awk -F. -v OFS=. 'NF==1{print ++$NF}; NF>1{if(length($NF+1)>length($NF))$(NF-1)++; $NF=sprintf("%0*d", length($NF), ($NF+1)%(10^length($NF))); print}')-alpha" >> $GITHUB_ENV - name: Build package run: | - python helper.py package dev-${GITHUB_SHA} + qgis-plugin-ci --no-validation package ${{ env.VERSION }} mkdir tmp - unzip koordinates.zip -d tmp + unzip koordinates.${{ env.VERSION }}.zip -d tmp + + - name: Save PR number to zips + run: | + cd tmp + echo ${{ github.event.number }} | tee pr_number + echo ${{ github.event.pull_request.head.sha }} | tee git_commit + echo ${{ github.event.number }} - uses: actions/upload-artifact@v2 with: - name: koordinates_package + name: koordinates_package.${{ env.VERSION }} path: tmp diff --git a/.github/workflows/build_artifact_comment.yml b/.github/workflows/build_artifact_comment.yml new file mode 100644 index 0000000..8442ca4 --- /dev/null +++ b/.github/workflows/build_artifact_comment.yml @@ -0,0 +1,104 @@ +name: Write build artifact comments + +on: + workflow_run: + workflows: ["Build"] + types: + - completed + +permissions: + contents: read + +jobs: + on-success: + + permissions: + pull-requests: write + + runs-on: ubuntu-latest + steps: + - name: Get source code + uses: actions/checkout@v4 + with: + # To fetch tags + fetch-depth: 0 + + - name: 'Set plugin version environment variables' + run: | + TAG=$(git describe --tags $(git rev-list --tags --max-count=1)) + echo "VERSION=$(echo ${TAG} | awk -F. -v OFS=. 'NF==1{print ++$NF}; NF>1{if(length($NF+1)>length($NF))$(NF-1)++; $NF=sprintf("%0*d", length($NF), ($NF+1)%(10^length($NF))); print}')-alpha" >> $GITHUB_ENV + echo "SHA_SHORT=$(git rev-parse --short HEAD)" >> $GITHUB_ENV + - name: 'Download artifact' + id: download_artifact + uses: actions/github-script@v7 + with: + script: | + let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ + owner: context.repo.owner, + repo: context.repo.repo, + run_id: context.payload.workflow_run.id, + }); + let matchArtifacts = allArtifacts.data.artifacts.filter((artifact) => { + return artifact.name == 'koordinates_package.${{ env.VERSION }}' + }); + matchArtifacts.forEach((artifact) => { + }); + if (matchArtifacts.length>0) + { + let download = await github.rest.actions.downloadArtifact({ + owner: context.repo.owner, + repo: context.repo.repo, + artifact_id: matchArtifacts[0].id, + archive_format: 'zip', + }); + let fs = require('fs'); + fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/koordinates_package.${{ env.VERSION }}.zip`, Buffer.from(download.data)); + core.setOutput('artifact_id', matchArtifacts[0].id); + } + else + { + core.setOutput('artifact_id', 0); + } + + - name: 'Unzip artifact' + if: fromJSON(steps.download_artifact.outputs.artifact_id) > 0 + run: | + unzip -n koordinates_package.${{ env.VERSION }} + + - name: 'Post artifact download link as comment on PR' + if: fromJSON(steps.download_artifact.outputs.artifact_id) > 0 + uses: actions/github-script@v7 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + let fs = require('fs'); + let issue_number = Number(fs.readFileSync('./pr_number')); + let git_sha = String(fs.readFileSync('./git_commit')).trim(); + const prComments = await github.rest.issues.listComments({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: issue_number, + }); + const PREFIX = "## Plugin ready!"; + let body = PREFIX + "\n\n" + + "A test version of this PR is available for testing [here](https://github.com/" + context.repo.owner + "/" + context.repo.repo + "/suites/" + context.payload.workflow_run.check_suite_id + "/artifacts/${{steps.download_artifact.outputs.artifact_id}})."; + body += "\n\n*(Built from commit " + git_sha + ")*"; + + const winBuildComment = prComments.data?.find(c => c.body.startsWith(PREFIX)); + if (!!winBuildComment) { + // update the existing comment + await github.rest.issues.updateComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: winBuildComment.id, + body: body + }); + } else { + // submit a new comment + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: issue_number, + body: body + }); + } diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index cb758f7..707a3c7 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -26,7 +26,7 @@ jobs: strategy: matrix: - docker_tags: [release-3_22, release-3_28, release-3_30, latest] + docker_tags: [release-3_22, release-3_28, release-3_30] steps: diff --git a/requirements/packaging.txt b/requirements/packaging.txt new file mode 100644 index 0000000..98a7602 --- /dev/null +++ b/requirements/packaging.txt @@ -0,0 +1 @@ +qgis-plugin-ci>=2.7.0 diff --git a/setup.cfg b/setup.cfg index e8c7cea..c9595fa 100644 --- a/setup.cfg +++ b/setup.cfg @@ -5,3 +5,8 @@ exclude = helper.py [isort] profile = black + +[qgis-plugin-ci] +plugin_path = koordinates +github_organization_slug = koordinates +project_slug = koordinates-qgis-plugin