From 41cc031088fca436f644181227a5b8eb76f30875 Mon Sep 17 00:00:00 2001 From: Ryan Brooks Date: Tue, 7 Dec 2021 21:34:48 -0800 Subject: [PATCH] Update to pass baseSha to upload enpoint. Update version in readme --- README.md | 4 ++-- src/inputs.ts | 25 ++++++++++++++++++++----- src/types.ts | 1 + src/upload.ts | 1 + 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index a6dec80..6847cb9 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Add the API key to your secrets in your repository. **Do not leave this key in p ### Incorporate in your workflow Build your artifact in a step before the Emerge upload action. Pass the generated artifact's path as the `artifact_path` -argument, and your Emerge API key secret as the `emerge_api_key` argument +argument, and your Emerge API key secret as the `emerge_api_key` argument: ```yaml name: Your workflow @@ -45,7 +45,7 @@ jobs: - name: Generate Android release bundle run: ./gradlew bundleRelease - name: Upload artifact to Emerge - uses: EmergeTools/emerge-upload-action@v1.0.0 + uses: EmergeTools/emerge-upload-action@v1.0.1 with: artifact_path: ./app/build/outputs/bundle/release/app-release.aab emerge_api_key: ${{ secrets.EMERGE_API_KEY }} diff --git a/src/inputs.ts b/src/inputs.ts index b7bbdc5..b9fb526 100644 --- a/src/inputs.ts +++ b/src/inputs.ts @@ -6,6 +6,10 @@ import { getPRNumber, getAbsoluteArtifactPath } from './utils'; const core = require('@actions/core'); const github = require('@actions/github'); +// The sha set for `before` on push events if the first push to a commit. This should not ever be the case if +// pushing to main unless it's the initial commit. +const DEFAULT_PUSH_BEFORE_SHA = '0000000000000000000000000000000000000000'; + function getInputs(): UploadInputs { core.info('Parsing inputs...'); @@ -23,21 +27,31 @@ function getInputs(): UploadInputs { // of the commit that triggered this action. // Therefore, on a PR we need to explicitly get the head sha let sha; + let baseSha; let branchName; + const eventFile = fs.readFileSync(process.env.GITHUB_EVENT_PATH ?? '', { + encoding: 'utf8', + }); + const eventFileJson = JSON.parse(eventFile); if (process.env.GITHUB_EVENT_NAME === 'pull_request') { - const eventFile = fs.readFileSync(process.env.GITHUB_EVENT_PATH ?? '', { - encoding: 'utf8', - }); - const eventFileJson = JSON.parse(eventFile); sha = eventFileJson?.pull_request?.head?.sha ?? process.env.GITHUB_SHA ?? ''; + baseSha = eventFileJson?.pull_request?.base?.sha ?? ''; branchName = process.env.GITHUB_HEAD_REF ?? ''; - } else { + } else if (process.env.GITHUB_EVENT_NAME === 'push') { sha = process.env.GITHUB_SHA ?? ''; + // Get the SHA of the previous commit, which will be the baseSha in the case of a push event. + baseSha = eventFileJson?.before ?? ''; + if (eventFileJson?.baseRef === null || baseSha === DEFAULT_PUSH_BEFORE_SHA) { + baseSha = ''; + } + const ref = process.env.GITHUB_REF ?? ''; if (ref !== '') { const refSplits = ref.split('/'); branchName = refSplits[refSplits.length - 1]; } + } else { + core.setFailed(`Unsupported action trigger: ${process.env.GITHUB_EVENT_NAME}`); } if (sha === '') { @@ -78,6 +92,7 @@ function getInputs(): UploadInputs { filename, emergeApiKey, sha, + baseSha, repoName, prNumber, buildType, diff --git a/src/types.ts b/src/types.ts index 0088e78..84b8a86 100644 --- a/src/types.ts +++ b/src/types.ts @@ -5,6 +5,7 @@ export type UploadInputs = { filename: string emergeApiKey: string sha: string + baseSha: string repoName: string // Required for PRs diff --git a/src/upload.ts b/src/upload.ts index 2344e88..4c27210 100644 --- a/src/upload.ts +++ b/src/upload.ts @@ -12,6 +12,7 @@ async function run(): Promise { prNumber: inputs.prNumber, branch: inputs.branchName, sha: inputs.sha, + baseSha: inputs.baseSha, repoName: inputs.repoName, buildType: inputs.buildType, };