From d2237753e9f93967dcb7b94607af51e371821ffd Mon Sep 17 00:00:00 2001 From: granny Date: Fri, 26 Jan 2024 03:14:28 -0800 Subject: [PATCH] [ci-skip] add PR jar workflows used by Paper --- .github/workflows/build_pr.yml | 81 ++++++++++++++++++++++++++++++++ .github/workflows/pr_comment.yml | 78 ++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+) create mode 100644 .github/workflows/build_pr.yml create mode 100644 .github/workflows/pr_comment.yml diff --git a/.github/workflows/build_pr.yml b/.github/workflows/build_pr.yml new file mode 100644 index 000000000..3b67914b0 --- /dev/null +++ b/.github/workflows/build_pr.yml @@ -0,0 +1,81 @@ +# Here lie dragons! +# +# This action builds a jar to be updated in the body +# of the PR relating to this action. +# +# Created by PaperMC contributors for Paper, modified by granny + +name: Build Pl3xMap PR +on: + pull_request: + types: + - opened + - reopened + - synchronize + - labeled + +jobs: + build: + # Run on all label events (won't be duplicated) or all push events or on PR syncs not from the same repo + if: (github.event_name == 'pull_request' && github.event.action == 'labeled') || github.event_name != 'pull_request' || github.repository != github.event.pull_request.head.repo.full_name + runs-on: ubuntu-latest + strategy: + matrix: + java: [17] + fail-fast: true + steps: + - uses: actions/checkout@v4 + - name: JDK ${{ matrix.java }} + uses: actions/setup-java@v4 + with: + java-version: ${{ matrix.java }} + distribution: 'temurin' + + - name: Validate Gradle wrapper + uses: gradle/wrapper-validation-action@v1 + - name: Setup Gradle + uses: gradle/gradle-build-action@v2 + + - name: Configure Build + uses: actions/github-script@v6 + id: determine + with: + script: | + const {owner, repo} = context.repo; + const event_name = "${{ github.event_name }}"; + const event = ${{ toJSON(github.event) }}; + const ref_type = "${{ github.ref_type }}"; + const ref_name = "${{ github.ref_name }}"; + const result = { + action: "build" + }; + + if (event_name === "pull_request" && event.pull_request.labels.find((l) => l.name === "build-pr-jar")) { + result["pr"] = event.pull_request.number; + result["action"] = "pl3xmap"; + core.notice(`This is a pull request action with a build pl3xmap label (${JSON.stringify(result)})`); + return result; + } + core.notice("This will not build a pl3xmap jar"); + return result; + + - name: Build + run: ./gradlew build --stacktrace + + - name: Upload Pl3xMap Jar + if: fromJSON(steps.determine.outputs.result).action == 'pl3xmap' + uses: actions/upload-artifact@v4 + with: + name: pl3xmap-${{ fromJSON(steps.determine.outputs.result).pr }} + path: build/libs/Pl3xMap-*-SNAPSHOT.jar + event_file: + name: "Event File" + # Only run on PRs if the source branch is on someone else's repo + if: ${{ github.event_name != 'pull_request' || github.repository != github.event.pull_request.head.repo.full_name }} + runs-on: ubuntu-latest + steps: + - name: Upload + uses: actions/upload-artifact@v4 + with: + name: Event File + path: ${{ github.event_path }} diff --git a/.github/workflows/pr_comment.yml b/.github/workflows/pr_comment.yml new file mode 100644 index 000000000..f0c8b60f6 --- /dev/null +++ b/.github/workflows/pr_comment.yml @@ -0,0 +1,78 @@ +# This workflow run on the completion of the +# build workflow but only does anything if the +# triggering workflow uploaded an artifact. +# +# Do note that it is then the trigger workflow that +# determines if this will update the PR text body. All +# this workflow does is check if an uploaded artifact +# exists and there is a PR tied to the previous workflow. +# +# Created by PaperMC contributors for Paper, modified by granny + +name: Comment on pull request +on: + workflow_run: + workflows: ['Build Pl3xMap PR'] + types: [completed] +jobs: + pr_comment: + if: github.event.workflow_run.conclusion == 'success' + runs-on: ubuntu-latest + steps: + - uses: actions/github-script@v6 + with: + # This snippet is public-domain, taken from + # https://github.com/oprypin/nightly.link/blob/master/.github/workflows/pr-comment.yml + # Modified extensively by Machine_Maker + script: | + async function updatePR(owner, repo, issue_number, purpose, body) { + const { data } = await github.rest.issues.get({ owner, repo, issue_number }); + core.debug(JSON.stringify(data, null, 2)); + + const marker = ``; + + let new_body = data.body ? data.body.trim().split(marker)[0].trim() : ""; + new_body += `\n${marker}\n---\n${body}`; + + core.info(`Updating the text body of PR #${issue_number} in ${owner}/${repo}`); + await github.rest.issues.update({ owner, repo, issue_number, body: new_body }); + } + + const { owner, repo } = context.repo; + const run_id = ${{ github.event.workflow_run.id }}; + const repo_id = ${{ github.event.repository.id }}; + + let pulls = []; + const event_type = "${{ github.event.workflow_run.event}}"; + if (event_type === "push") { // if push, it's from the same repo which means `pull_requests` is populated + pulls = ${{ toJSON(github.event.workflow_run.pull_requests) }}; + } else { + const pr_branch = "${{ github.event.workflow_run.head_branch }}"; + const pr_sha = "${{ github.event.workflow_run.head_sha }}"; + const pr_owner = "${{ github.event.workflow_run.head_repository.owner.login }}"; + const { data } = await github.rest.pulls.list({ owner, repo, head: `${pr_owner}:${pr_branch}`, state: "open" }); + core.debug(JSON.stringify(data, null, 2)); + pulls = data.filter((pr) => pr.head.sha === pr_sha && pr.labels.find((l) => l.name === "build-pr-jar")); + } + + if (!pulls.length) { + return core.notice("This workflow doesn't have any pull requests!"); + } else if (pulls.length > 1) { + core.info(JSON.stringify(pulls, null, 2)); + return core.error("Found multiple matching PRs"); + } + const pull_request = pulls[0]; + + const artifacts = await github.paginate(github.rest.actions.listWorkflowRunArtifacts, { owner, repo, run_id }); + if (!artifacts.length) { + return core.info("Skipping comment due to no artifact found"); + } + const artifact = artifacts.find((art) => art.name === `pl3xmap-${pull_request.number}`); + if (!artifact) { + return core.info("Skipping comment to no matching artifact found"); + } + + const link = `https://nightly.link/${owner}/${repo}/actions/artifacts/${artifact.id}.zip`; + const body = `Download the pl3xmap jar for this pull request: [${artifact.name}.zip](${link})`; + core.info(`Adding a link to ${link}`); + await updatePR(owner, repo, pull_request.number, "pl3xmap-pr-build", body);