Skip to content

Commit

Permalink
Fix baseSha empty issue (#2)
Browse files Browse the repository at this point in the history
* Adding some logging to see what Github passes in with various triggers

* Log event path

* Change to debug logs

* Update the only working log to see if github is even using commit

* Add types for events, switch logs to info

* Run npm run release and add some more logs

* Remove clearing of baseSha, add some more useful logs, cleanup logs from debugging

* Cleanup

* Build 1.0.2 release

* Update readme
  • Loading branch information
rbro112 authored Feb 12, 2022
1 parent 6a0d0b6 commit d2f4fc1
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
node_modules
node_modules
lib
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
- name: Generate Android release bundle
run: ./gradlew bundleRelease
- name: Upload artifact to Emerge
uses: EmergeTools/[email protected].1
uses: EmergeTools/[email protected].2
with:
artifact_path: ./app/build/outputs/bundle/release/app-release.aab
emerge_api_key: ${{ secrets.EMERGE_API_KEY }}
Expand Down
37 changes: 26 additions & 11 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6208,7 +6208,7 @@ const utils_1 = __nccwpck_require__(314);
const core = __nccwpck_require__(186);
const github = __nccwpck_require__(438);
function getInputs() {
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
core.info('Parsing inputs...');
const artifactPath = core.getInput('artifact_path', { required: true });
if (artifactPath === '') {
Expand All @@ -6220,28 +6220,39 @@ function getInputs() {
}
// On PRs, the GITHUB_SHA refers to the merge commit instead
// of the commit that triggered this action.
// Therefore, on a PR we need to explicitly get the head sha
// Therefore, on a PR we need to explicitly get the head sha from the event json data.
let sha;
let baseSha;
let branchName;
const eventFile = fs.readFileSync((_a = process.env.GITHUB_EVENT_PATH) !== null && _a !== void 0 ? _a : '', {
encoding: 'utf8',
});
const eventFileJson = JSON.parse(eventFile);
if (process.env.GITHUB_EVENT_NAME === 'pull_request') {
const eventFile = fs.readFileSync((_a = process.env.GITHUB_EVENT_PATH) !== null && _a !== void 0 ? _a : '', {
encoding: 'utf8',
});
const eventFileJson = JSON.parse(eventFile);
sha = (_e = (_d = (_c = (_b = eventFileJson === null || eventFileJson === void 0 ? void 0 : eventFileJson.pull_request) === null || _b === void 0 ? void 0 : _b.head) === null || _c === void 0 ? void 0 : _c.sha) !== null && _d !== void 0 ? _d : process.env.GITHUB_SHA) !== null && _e !== void 0 ? _e : '';
branchName = (_f = process.env.GITHUB_HEAD_REF) !== null && _f !== void 0 ? _f : '';
baseSha = (_h = (_g = (_f = eventFileJson === null || eventFileJson === void 0 ? void 0 : eventFileJson.pull_request) === null || _f === void 0 ? void 0 : _f.base) === null || _g === void 0 ? void 0 : _g.sha) !== null && _h !== void 0 ? _h : '';
branchName = (_j = process.env.GITHUB_HEAD_REF) !== null && _j !== void 0 ? _j : '';
}
else {
sha = (_g = process.env.GITHUB_SHA) !== null && _g !== void 0 ? _g : '';
const ref = (_h = process.env.GITHUB_REF) !== null && _h !== void 0 ? _h : '';
else if (process.env.GITHUB_EVENT_NAME === 'push') {
sha = (_k = process.env.GITHUB_SHA) !== null && _k !== void 0 ? _k : '';
// Get the SHA of the previous commit, which will be the baseSha in the case of a push event.
baseSha = (_l = eventFileJson === null || eventFileJson === void 0 ? void 0 : eventFileJson.before) !== null && _l !== void 0 ? _l : '';
const ref = (_m = process.env.GITHUB_REF) !== null && _m !== void 0 ? _m : '';
if (ref !== '') {
const refSplits = ref.split('/');
branchName = refSplits[refSplits.length - 1];
}
}
else {
core.setFailed(`Unsupported action trigger: ${process.env.GITHUB_EVENT_NAME}`);
}
if (sha === '') {
core.setFailed('Could not get SHA of the head branch.');
}
if (baseSha === '') {
core.setFailed('Could not get SHA of the base branch.');
}
// branchName is optional, so we won't fail if not present
if (branchName === '') {
// Explicitly set to undefined so we won't send an empty string to the Emerge API
branchName = undefined;
Expand All @@ -6251,7 +6262,7 @@ function getInputs() {
core.setFailed('Could not get repository name.');
}
// Required for PRs
const refName = (_j = process.env.GITHUB_REF) !== null && _j !== void 0 ? _j : '';
const refName = (_o = process.env.GITHUB_REF) !== null && _o !== void 0 ? _o : '';
const prNumber = (0, utils_1.getPRNumber)(refName);
if (refName.includes('pull') && !prNumber) {
core.setFailed('Could not get prNumber for a PR triggered build.');
Expand All @@ -6271,6 +6282,7 @@ function getInputs() {
filename,
emergeApiKey,
sha,
baseSha,
repoName,
prNumber,
buildType,
Expand Down Expand Up @@ -6331,9 +6343,11 @@ function run() {
prNumber: inputs.prNumber,
branch: inputs.branchName,
sha: inputs.sha,
baseSha: inputs.baseSha,
repoName: inputs.repoName,
buildType: inputs.buildType,
};
core.debug(`requestBody: ${JSON.stringify(requestBody)}`);
const response = yield (0, node_fetch_1.default)('https://api.emergetools.com/upload', {
method: 'post',
headers: {
Expand All @@ -6350,6 +6364,7 @@ function run() {
}
const file = fs.readFileSync(inputs.artifactPath);
const headers = new node_fetch_1.Headers({ 'Content-Type': 'application/zip' });
core.info(`Uploading artifact at path ${inputs.artifactPath}...`);
yield (0, node_fetch_1.default)(uploadURL, {
method: 'PUT',
body: file,
Expand Down
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "emerge-upload-action",
"version": "1.0.0",
"version": "1.0.2",
"description": "Upload an artifact to Emerge.",
"main": "dist/index.js",
"scripts": {
Expand All @@ -26,4 +26,4 @@
"eslint-plugin-github": "^4.1.1",
"typescript": "^4.3.5"
}
}
}
12 changes: 4 additions & 8 deletions src/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ 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...');

Expand All @@ -25,7 +21,7 @@ function getInputs(): UploadInputs {

// On PRs, the GITHUB_SHA refers to the merge commit instead
// of the commit that triggered this action.
// Therefore, on a PR we need to explicitly get the head sha
// Therefore, on a PR we need to explicitly get the head sha from the event json data.
let sha;
let baseSha;
let branchName;
Expand All @@ -41,9 +37,6 @@ function getInputs(): UploadInputs {
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 !== '') {
Expand All @@ -57,6 +50,9 @@ function getInputs(): UploadInputs {
if (sha === '') {
core.setFailed('Could not get SHA of the head branch.');
}
if (baseSha === '') {
core.setFailed('Could not get SHA of the base branch.');
}
// branchName is optional, so we won't fail if not present
if (branchName === '') {
// Explicitly set to undefined so we won't send an empty string to the Emerge API
Expand Down
2 changes: 2 additions & 0 deletions src/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async function run(): Promise<void> {
buildType: inputs.buildType,
};

core.debug(`requestBody: ${JSON.stringify(requestBody)}`);
const response = await fetch('https://api.emergetools.com/upload', {
method: 'post',
headers: {
Expand All @@ -36,6 +37,7 @@ async function run(): Promise<void> {
const file = fs.readFileSync(inputs.artifactPath);

const headers = new Headers({ 'Content-Type': 'application/zip' });
core.info(`Uploading artifact at path ${inputs.artifactPath}...`);
await fetch(uploadURL, {
method: 'PUT',
body: file,
Expand Down

0 comments on commit d2f4fc1

Please sign in to comment.