Skip to content

Commit

Permalink
Merge pull request #234 from balena-io/ab77-patch-1
Browse files Browse the repository at this point in the history
Allow pull_request_target event
  • Loading branch information
ab77 authored Dec 1, 2022
2 parents e6c14e1 + d315dee commit 3df7484
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions src/action.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import * as core from '@actions/core';
import { context as contextType } from '@actions/github';
import * as core from "@actions/core";
import { context as contextType } from "@actions/github";

import * as versionbot from './versionbot-utils';
import * as balena from './balena-utils';
import * as git from './git';
import * as github from './github-utils';
import { Inputs, RepoContext } from './types';
import * as versionbot from "./versionbot-utils";
import * as balena from "./balena-utils";
import * as git from "./git";
import * as github from "./github-utils";
import { Inputs, RepoContext } from "./types";

const ALLOWED_EVENTS = ["pull_request_target", "pull_request"];

export async function run(
context: typeof contextType,
inputs: Inputs,
inputs: Inputs
): Promise<void> {
// If the payload does not have a repository object then fail early (the events we are interested in always have this)
if (!context.payload.repository) {
throw new Error('Workflow payload was missing repository object');
throw new Error("Workflow payload was missing repository object");
}

// Get the master branch so we can infer intent
const target =
inputs.defaultBranch || context.payload.repository.master_branch;
// Collect repo context
const repoContext: RepoContext = {
owner: context.payload.repository.owner.login || '',
name: context.payload.repository.name || '',
owner: context.payload.repository.owner.login || "",
name: context.payload.repository.name || "",
sha: context.payload.pull_request?.head.sha || context.sha,
pullRequest: context.payload.pull_request
? {
Expand All @@ -38,7 +40,7 @@ export async function run(
// Version of release built
let rawVersion: string | null = null;

if (context.payload.action === 'closed') {
if (context.payload.action === "closed") {
// If a pull request was closed and merged then just finalize the release!
if (repoContext.pullRequest && repoContext.pullRequest.merged) {
// Get the previous release built
Expand All @@ -48,10 +50,10 @@ export async function run(
});
if (!previousRelease) {
throw new Error(
'Action reached point of finalizing a release but did not find one',
"Action reached point of finalizing a release but did not find one"
);
} else if (previousRelease.isFinal) {
core.info('Release is already finalized so skipping.');
core.info("Release is already finalized so skipping.");
return;
}

Expand All @@ -64,18 +66,18 @@ export async function run(
try {
await github.createTag(repoContext, rawVersion);
} catch (e: any) {
if (e.message !== 'Reference already exists') {
if (e.message !== "Reference already exists") {
throw e;
}
core.info('Git reference already exists.');
core.info("Git reference already exists.");
return;
}
}

return;
} else {
// If the pull request was closed but not merged then do nothing
core.info('Pull request was closed but not merged, nothing to do.');
core.info("Pull request was closed but not merged, nothing to do.");
return;
}
}
Expand All @@ -92,9 +94,9 @@ export async function run(

// If we are pushing directly to the target branch then just build a release without draft flag
if (
context.eventName === 'push' &&
context.eventName === "push" &&
(context.ref === `refs/heads/${target}` ||
context.ref.startsWith('refs/tags/'))
context.ref.startsWith("refs/tags/"))
) {
// TODO: Update this to use ref_type & ref_name once that becomes available
// See: https://github.com/actions/toolkit/pull/935/files
Expand All @@ -107,11 +109,11 @@ export async function run(
...(!!tagName && { tag: tagName }),
},
};
} else if (context.eventName !== 'pull_request') {
// Make sure the only events now are Pull Requests
if (context.eventName === 'push') {
} else if (!ALLOWED_EVENTS.includes(context.eventName)) {
// Make sure the only events now are ones we expect
if (context.eventName === "push") {
throw new Error(
`Push workflow only works with ${target} branch. Event tried pushing to: ${context.ref}`,
`Push workflow only works with ${target} branch. Event tried pushing to: ${context.ref}`
);
}
throw new Error(`Unsure how to proceed with event: ${context.eventName}`);
Expand All @@ -138,17 +140,17 @@ export async function run(

// Now that we built a release set the expected outputs
rawVersion = await balena.getReleaseVersion(releaseId);
core.setOutput('version', rawVersion);
core.setOutput('release_id', releaseId);
core.setOutput("version", rawVersion);
core.setOutput("release_id", releaseId);

if (inputs.createTag && buildOptions.draft === false) {
try {
await github.createTag(repoContext, rawVersion);
} catch (e: any) {
if (e.message !== 'Reference already exists') {
if (e.message !== "Reference already exists") {
throw e;
}
core.info('Git reference already exists.');
core.info("Git reference already exists.");
}
}
}

0 comments on commit 3df7484

Please sign in to comment.