Skip to content

Commit

Permalink
Merge pull request #35 from preactjs/support-push
Browse files Browse the repository at this point in the history
Support push events
  • Loading branch information
developit authored Jun 22, 2021
2 parents 1704d91 + a8de1b9 commit b855f27
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: CI

on: pull_request
on: [pull_request, push]

jobs:
build_test:
Expand Down
2 changes: 1 addition & 1 deletion index.js

Large diffs are not rendered by default.

54 changes: 39 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,37 @@ import { exec } from '@actions/exec';
import SizePlugin from 'size-plugin-core';
import { fileExists, diffTable, toBool, stripHash } from './utils.js';

/**
* @typedef {ReturnType<typeof import("@actions/github").getOctokit>} Octokit
* @typedef {typeof import("@actions/github").context} ActionContext
* @param {Octokit} octokit
* @param {ActionContext} context
* @param {string} token
*/
async function run(octokit, context, token) {
const { owner, repo, number: pull_number } = context.issue;

// const pr = (await octokit.pulls.get({ owner, repo, pull_number })).data;
const pr = context.payload.pull_request;
try {
debug('pr' + JSON.stringify(pr, null, 2));
debug('pr' + JSON.stringify(context.payload, null, 2));
} catch (e) { }
if (!pr) {
throw Error('Could not retrieve PR information. Only "pull_request" triggered workflows are currently supported.');

let baseSha, baseRef;
if (context.eventName == "push") {
baseSha = context.payload.before;
baseRef = context.payload.ref;

console.log(`Pushed new commit on top of ${baseRef} (${baseSha})`)
} else if (context.eventName == "pull_request" || context.eventName == 'pull_request_target') {
const pr = context.payload.pull_request;
baseSha = pr.base.sha;
baseRef = pr.base.ref;

console.log(`PR #${pull_number} is targeted at ${baseRef} (${baseRef})`)
} else {
throw new Error(
`Unsupported eventName in github.context: ${context.eventName}. Only "pull_request", "pull_request_target", and "push" triggered workflows are currently supported.`
);
}

if (getInput('cwd')) process.chdir(getInput('cwd'));
Expand All @@ -26,8 +47,6 @@ async function run(octokit, context, token) {
stripHash: stripHash(getInput('strip-hash'))
});

console.log(`PR #${pull_number} is targetted at ${pr.base.ref} (${pr.base.sha})`);

const buildScript = getInput('build-script') || 'build';
const cwd = process.cwd();

Expand Down Expand Up @@ -59,16 +78,14 @@ async function run(octokit, context, token) {
const newSizes = await plugin.readFromDisk(cwd);

startGroup(`[base] Checkout target branch`);
let baseRef;
try {
baseRef = context.payload.base.ref;
if (!baseRef) throw Error('missing context.payload.pull_request.base.ref');
await exec(`git fetch -n origin ${context.payload.pull_request.base.ref}`);
await exec(`git fetch -n origin ${baseRef}`);
console.log('successfully fetched base.ref');
} catch (e) {
console.log('fetching base.ref failed', e.message);
try {
await exec(`git fetch -n origin ${pr.base.sha}`);
await exec(`git fetch -n origin ${baseSha}`);
console.log('successfully fetched base.sha');
} catch (e) {
console.log('fetching base.sha failed', e.message);
Expand All @@ -86,7 +103,7 @@ async function run(octokit, context, token) {
await exec(`git reset --hard ${baseRef}`);
}
catch (e) {
await exec(`git reset --hard ${pr.base.sha}`);
await exec(`git reset --hard ${baseSha}`);
}
endGroup();

Expand Down Expand Up @@ -148,7 +165,11 @@ async function run(octokit, context, token) {
body: markdownDiff + '\n\n<a href="https://github.com/preactjs/compressed-size-action"><sub>compressed-size-action</sub></a>'
};

if (toBool(getInput('use-check'))) {
if (context.eventName !== 'pull_request' && context.eventName !== 'pull_request_target') {
console.log('No PR associated with this action run. Not posting a check or comment.');
outputRawMarkdown = false;
}
else if (toBool(getInput('use-check'))) {
if (token) {
const finish = await createCheck(octokit, context);
await finish({
Expand Down Expand Up @@ -204,7 +225,7 @@ async function run(octokit, context, token) {
console.log(`Error creating comment: ${e.message}`);
console.log(`Submitting a PR review comment instead...`);
try {
const issue = context.issue || pr;
const issue = context.issue;
await octokit.pulls.createReview({
owner: issue.owner,
repo: issue.repo,
Expand Down Expand Up @@ -233,8 +254,11 @@ async function run(octokit, context, token) {
console.log('All done!');
}


// create a check and return a function that updates (completes) it
/**
* Create a check and return a function that updates (completes) it
* @param {Octokit} octokit
* @param {ActionContext} context
*/
async function createCheck(octokit, context) {
const check = await octokit.checks.create({
...context.repo,
Expand Down

0 comments on commit b855f27

Please sign in to comment.