From 18b0257aec5e678729dcc6a4e7035de564a75705 Mon Sep 17 00:00:00 2001 From: Integer Limit <103940576+IntegerLimit@users.noreply.github.com> Date: Sun, 16 Jun 2024 17:52:12 +1000 Subject: [PATCH] Fully Implement Fork PR Artifact Names + Code Cleanup (#765) [SKIP] --- .github/workflows/buildpack.yml | 31 ++++--------------- .github/workflows/forkprbuildpack.yml | 25 ++++----------- tools/gulpfile.ts | 2 +- .../misc/{gha.ts => makeArtifactNames.ts} | 2 ++ tools/tasks/shared/transformVersion.ts | 27 ++-------------- tools/types/transformFiles.ts | 30 ++++++++++++------ tools/utils/util.ts | 22 ++----------- 7 files changed, 40 insertions(+), 99 deletions(-) rename tools/tasks/misc/{gha.ts => makeArtifactNames.ts} (83%) diff --git a/.github/workflows/buildpack.yml b/.github/workflows/buildpack.yml index 506e485..8a87072 100644 --- a/.github/workflows/buildpack.yml +++ b/.github/workflows/buildpack.yml @@ -77,16 +77,17 @@ on: type: string required: false +env: + GITHUB_TAG: ${{ inputs.tag }} + RELEASE_TYPE: ${{ inputs.release_type }} + HEAD_REF: ${{ inputs.head_ref }} + TRUE_SHA: ${{ inputs.true_sha }} + jobs: buildSingle: name: Build Pack (${{ inputs.tag }}) runs-on: ubuntu-latest if: ${{ !inputs.separate_upload }} - env: - GITHUB_TAG: ${{ inputs.tag }} - RELEASE_TYPE: ${{ inputs.release_type }} - GITHUB_HEAD_REF: ${{ inputs.head_ref }} - TRUE_SHA: ${{ inputs.true_sha }} steps: - name: Checkout Ref @@ -158,11 +159,6 @@ jobs: name: Make Artifact Names (${{ inputs.tag }}) runs-on: ubuntu-latest if: ${{ inputs.separate_upload }} - env: - GITHUB_TAG: ${{ inputs.tag }} - RELEASE_TYPE: ${{ inputs.release_type }} - GITHUB_HEAD_REF: ${{ inputs.head_ref }} - TRUE_SHA: ${{ inputs.true_sha }} outputs: client: ${{ steps.artifactNames.outputs.client }} server: ${{ steps.artifactNames.outputs.server }} @@ -215,11 +211,6 @@ jobs: runs-on: ubuntu-latest if: ${{ inputs.separate_upload }} needs: makeNames - env: - GITHUB_TAG: ${{ inputs.tag }} - RELEASE_TYPE: ${{ inputs.release_type }} - GITHUB_HEAD_REF: ${{ inputs.head_ref }} - TRUE_SHA: ${{ inputs.true_sha }} steps: - name: Checkout Ref uses: actions/checkout@v4 @@ -280,11 +271,6 @@ jobs: runs-on: ubuntu-latest if: ${{ inputs.separate_upload }} needs: makeNames - env: - GITHUB_TAG: ${{ inputs.tag }} - RELEASE_TYPE: ${{ inputs.release_type }} - GITHUB_HEAD_REF: ${{ inputs.head_ref }} - TRUE_SHA: ${{ inputs.true_sha }} steps: - name: Checkout Ref uses: actions/checkout@v4 @@ -354,11 +340,6 @@ jobs: runs-on: ubuntu-latest if: ${{ inputs.separate_upload }} needs: makeNames - env: - GITHUB_TAG: ${{ inputs.tag }} - RELEASE_TYPE: ${{ inputs.release_type }} - GITHUB_HEAD_REF: ${{ inputs.head_ref }} - TRUE_SHA: ${{ inputs.true_sha }} steps: - name: Checkout Ref uses: actions/checkout@v4 diff --git a/.github/workflows/forkprbuildpack.yml b/.github/workflows/forkprbuildpack.yml index 9af4eb7..d5698f0 100644 --- a/.github/workflows/forkprbuildpack.yml +++ b/.github/workflows/forkprbuildpack.yml @@ -22,6 +22,11 @@ concurrency: permissions: contents: read +env: + HEAD_REF: ${{ github.head_ref }} + HEAD_REPO: ${{ github.event.pull_request.head.repo.owner.login }} + TRUE_SHA: ${{ github.event.pull_request.head.sha }} + jobs: setup: # Only continue if we are in base Nomi-CEu Repo and pull request is from fork @@ -29,15 +34,12 @@ jobs: name: Setup (${{ github.event.pull_request.head.sha }}) runs-on: ubuntu-latest environment: fork-pr-build-pack - env: - GITHUB_HEAD_REF: ${{ github.event.pull_request.head.repo.owner.login }}/${{ github.head_ref }} - TRUE_SHA: ${{ github.event.pull_request.head.sha }} outputs: client: ${{ steps.artifactNames.outputs.client }} server: ${{ steps.artifactNames.outputs.server }} lang: ${{ steps.artifactNames.outputs.lang }} mmc: ${{ steps.artifactNames.outputs.mmc }} - steps: + steps: - name: Checkout Ref uses: actions/checkout@v4 with: @@ -63,14 +65,9 @@ jobs: run: npm run gulp makeArtifactNames buildClient: - # Only continue if we are in base Nomi-CEu Repo and pull request is from fork - if: "${{ github.repository_owner == 'Nomi-CEu' && github.event.pull_request.head.repo.owner.login != 'Nomi-CEu' }}" name: Build Fork PR Client (${{ github.event.pull_request.head.sha }}) runs-on: ubuntu-latest needs: setup - env: - GITHUB_HEAD_REF: ${{ github.event.pull_request.head.repo.owner.login }}/${{ github.head_ref }} - TRUE_SHA: ${{ github.event.pull_request.head.sha }} steps: - name: Checkout Ref uses: actions/checkout@v4 @@ -106,14 +103,9 @@ jobs: compression-level: 9 buildServer: - # Only continue if we are in base Nomi-CEu Repo and pull request is from fork - if: "${{ github.repository_owner == 'Nomi-CEu' && github.event.pull_request.head.repo.owner.login != 'Nomi-CEu' }}" name: Build Fork PR Server (${{ github.event.pull_request.head.sha }}) runs-on: ubuntu-latest needs: setup - env: - GITHUB_HEAD_REF: ${{ github.event.pull_request.head.repo.owner.login }}/${{ github.head_ref }} - TRUE_SHA: ${{ github.event.pull_request.head.sha }} steps: - name: Checkout Ref uses: actions/checkout@v4 @@ -149,14 +141,9 @@ jobs: compression-level: 9 buildLang: - # Only continue if we are in base Nomi-CEu Repo and pull request is from fork - if: "${{ github.repository_owner == 'Nomi-CEu' && github.event.pull_request.head.repo.owner.login != 'Nomi-CEu' }}" name: Build Fork PR Lang and Changelogs (${{ github.event.pull_request.head.sha }}) runs-on: ubuntu-latest needs: setup - env: - GITHUB_HEAD_REF: ${{ github.event.pull_request.head.repo.owner.login }}/${{ github.head_ref }} - TRUE_SHA: ${{ github.event.pull_request.head.sha }} steps: - name: Checkout Ref uses: actions/checkout@v4 diff --git a/tools/gulpfile.ts b/tools/gulpfile.ts index 03e7c50..87dcdb5 100644 --- a/tools/gulpfile.ts +++ b/tools/gulpfile.ts @@ -57,7 +57,7 @@ export const zipAll = zip.zipAll; export default gulp.series(buildAll, zipAll); -import * as gha from "./tasks/misc/gha.ts"; +import * as gha from "#tasks/misc/makeArtifactNames.ts"; export const makeArtifactNames = gha.makeArtifactNames; export { deployCurseForge } from "./tasks/deploy/curseforge.ts"; diff --git a/tools/tasks/misc/gha.ts b/tools/tasks/misc/makeArtifactNames.ts similarity index 83% rename from tools/tasks/misc/gha.ts rename to tools/tasks/misc/makeArtifactNames.ts index 02d109f..740881c 100644 --- a/tools/tasks/misc/gha.ts +++ b/tools/tasks/misc/makeArtifactNames.ts @@ -2,6 +2,7 @@ import { modpackManifest } from "#globals"; import { makeArtifactNameBody } from "#utils/util.ts"; import sanitize from "sanitize-filename"; import { setOutput } from "@actions/core"; +import logInfo from "#utils/log.js"; export async function makeArtifactNames(): Promise { const body = makeArtifactNameBody(modpackManifest.name); @@ -14,5 +15,6 @@ export async function makeArtifactNames(): Promise { Object.keys(names).forEach((name) => { setOutput(name, sanitize(names[name].toLowerCase())); + logInfo(`Made Name for Type '${name}': '${names[name].toLowerCase()}'`); }); } diff --git a/tools/tasks/shared/transformVersion.ts b/tools/tasks/shared/transformVersion.ts index 698f122..990fa22 100644 --- a/tools/tasks/shared/transformVersion.ts +++ b/tools/tasks/shared/transformVersion.ts @@ -1,32 +1,9 @@ import { modpackManifest } from "#globals"; +import { BuildData } from "#types/transformFiles.js"; /** * Transform the version field of manifest.json. */ export default async function transformManifestVersion(): Promise { - // We're building a tag. - if (process.env.GITHUB_TAG) { - modpackManifest.version = process.env.GITHUB_TAG.replace(/^v/, ""); - } - // If Pull Request Branch Name is provided and a 'True SHA' is provided - else if (process.env.GITHUB_HEAD_REF && process.env.TRUE_SHA) { - const shortCommit = process.env.TRUE_SHA.substring(0, 7); - modpackManifest.version = `${process.env.GITHUB_HEAD_REF}-${shortCommit}`; - } - // If SHA and ref is provided, append both the branch and short SHA. - else if ( - process.env.GITHUB_SHA && - process.env.GITHUB_REF && - process.env.GITHUB_REF.startsWith("refs/heads/") - ) { - const shortCommit = process.env.GITHUB_SHA.substring(0, 7); - const branch = /refs\/heads\/(.+)/.exec(process.env.GITHUB_REF)?.[1]; - if (!branch) { - throw new Error(`Invalid git ref: ${process.env.GITHUB_REF}`); - } - - modpackManifest.version = `${branch}-${shortCommit}`; - } else { - modpackManifest.version = "manual-build"; - } + modpackManifest.version = new BuildData().rawVersion; } diff --git a/tools/types/transformFiles.ts b/tools/types/transformFiles.ts index ba0da4c..f5ae829 100644 --- a/tools/types/transformFiles.ts +++ b/tools/types/transformFiles.ts @@ -18,16 +18,28 @@ export class BuildData { process.env.GITHUB_TAG.search(/^v/) === -1 ? `v${process.env.GITHUB_TAG}` : process.env.GITHUB_TAG; + return; } + // If Pull Request Branch Name is provided and a 'True SHA' is provided - else if (process.env.GITHUB_HEAD_REF && process.env.TRUE_SHA) { + if (process.env.HEAD_REF && process.env.TRUE_SHA) { const shortCommit = process.env.TRUE_SHA.substring(0, 7); this.type = "HEAD_REF"; - this.rawVersion = `${process.env.GITHUB_HEAD_REF}-${shortCommit}`; - this.transformedVersion = `PR Build (${process.env.GITHUB_HEAD_REF} branch, ${shortCommit})`; + + // If Head Repo is provided and this is a Fork PR + if (process.env.HEAD_REPO && process.env.HEAD_REPO !== "Nomi-CEu") { + this.rawVersion = `[${process.env.HEAD_REPO.toLowerCase()}-${process.env.HEAD_REF}]-${shortCommit}`; + this.transformedVersion = `Fork PR Build (${process.env.HEAD_REPO}/${process.env.HEAD_REF} branch, ${shortCommit})`; + return; + } + + this.rawVersion = `${process.env.HEAD_REF}-${shortCommit}`; + this.transformedVersion = `PR Build (${process.env.HEAD_REF} branch, ${shortCommit})`; + return; } + // If SHA and ref is provided, append both the branch and short SHA. - else if ( + if ( process.env.GITHUB_SHA && process.env.GITHUB_REF && process.env.GITHUB_REF.startsWith("refs/heads/") @@ -41,13 +53,13 @@ export class BuildData { this.type = "GITHUB_SHA"; this.rawVersion = `${branch}-${shortCommit}`; this.transformedVersion = `Nightly Build (${branch} branch, ${shortCommit})`; + return; } + // Manual Build - else { - this.type = "MANUAL_BUILD"; - this.rawVersion = "manual-build"; - this.transformedVersion = "Manual Build"; - } + this.type = "MANUAL_BUILD"; + this.rawVersion = "manual-build"; + this.transformedVersion = "Manual Build"; } public isVersionBuild(): boolean { diff --git a/tools/utils/util.ts b/tools/utils/util.ts index 9de5815..27079b9 100644 --- a/tools/utils/util.ts +++ b/tools/utils/util.ts @@ -38,6 +38,7 @@ import axiosRetry, { } from "axios-retry"; import stream from "node:stream"; import { NomiConfig } from "#types/axios.ts"; +import { BuildData } from "#types/transformFiles.js"; const LIBRARY_REG = /^(.+?):(.+?):(.+?)$/; @@ -314,26 +315,7 @@ function retryOrThrow( * Mostly intended to be called by CI/CD. */ export function makeArtifactNameBody(baseName: string): string { - // If the tag is provided by CI, simply just glue it to the base name. - if (process.env.GITHUB_TAG) { - return `${baseName}-${process.env.GITHUB_TAG}`; - } - // If Pull Request Branch Name is provided and a 'True SHA' is provided - if (process.env.GITHUB_HEAD_REF && process.env.TRUE_SHA) { - const shortCommit = process.env.TRUE_SHA.substring(0, 7); - return `${baseName}-${process.env.GITHUB_HEAD_REF}-${shortCommit}`; - } - // If SHA and ref is provided, append both the branch and short SHA. - if ( - process.env.GITHUB_SHA && - process.env.GITHUB_REF && - process.env.GITHUB_REF.startsWith("refs/heads/") - ) { - const shortCommit = process.env.GITHUB_SHA.substring(0, 7); - const branch = /refs\/heads\/(.+)/.exec(process.env.GITHUB_REF); - if (branch) return `${baseName}-${branch[1]}-${shortCommit}`; - } - return baseName; + return `${baseName}-${new BuildData().rawVersion}`; } /**