Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Commit

Permalink
refactor: remove need for GitHub client
Browse files Browse the repository at this point in the history
This removes the need for the at-actions/github client enitrely, the
reasoning for this relates to [1]. This commit works towards a minimal
GitHub Actions Action that runs SVGO. In later iterations the client may
be added back but it is important to remember the following: the client
is very limited when running in the context of a Pull Request from a
fork - it cannot, in the context of this commit, read comments on the
Pull Request or post a comment to a Pull Request.

--
1. #354
  • Loading branch information
ericcornelissen committed May 1, 2021
1 parent afb96e2 commit b1d17d5
Show file tree
Hide file tree
Showing 10 changed files with 4 additions and 377 deletions.
7 changes: 0 additions & 7 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ description: 'Automatically run SVGO with GitHub Actions'
author: 'Eric Cornelissen'

inputs:
repo-token:
description: 'The GITHUB_TOKEN secret'
required: true
comment:
description: 'Enable comments on Pull Requests with an optimization summary'
default: false
required: false
configuration-path:
description: 'The path for the svgo-action configurations'
default: '.github/svgo-action.yml'
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js

Large diffs are not rendered by default.

18 changes: 0 additions & 18 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ export const EVENT_PUSH = "push";
export const EVENT_REPOSITORY_DISPATCH = "repository_dispatch";
export const EVENT_SCHEDULE = "schedule";
export const EVENT_WORKFLOW_DISPATCH = "workflow_dispatch";
export const COMMENTABLE_EVENTS: string[] = [
EVENT_PULL_REQUEST,
];
export const SUPPORTED_EVENTS: string[] = [
EVENT_PULL_REQUEST,
EVENT_PUSH,
Expand All @@ -16,27 +13,15 @@ export const SUPPORTED_EVENTS: string[] = [
];

// Action inputs
export const INPUT_NAME_COMMENT = "comment";
export const INPUT_NAME_CONFIG_PATH = "configuration-path";
export const INPUT_NAME_DRY_RUN = "dry-run";
export const INPUT_NAME_IGNORE = "ignore";
export const INPUT_NAME_REPO_TOKEN = "repo-token";
export const INPUT_NAME_SVGO_OPTIONS = "svgo-options";
export const INPUT_NAME_SVGO_VERSION = "svgo-version";
export const INPUT_NOT_REQUIRED = { required: false };
export const INPUT_REQUIRED = { required: true };

// Action defaults
export const DEFAULT_CONFIG_PATH = ".github/svgo-action.yml";
export const DEFAULT_COMMENT = `
SVG(s) automatically optimized using [SVGO] :sparkles:
{{filesTable}}
{{warnings}}
[SVGO]: https://github.com/svg/svgo
`;
export const DEFAULT_SVGO_OPTIONS = "svgo.config.js";

// Action outputs
Expand All @@ -48,6 +33,3 @@ export const OUTPUT_NAME_SVG_COUNT = "SVG_COUNT";
// File encodings
export const BASE64 = "base64";
export const UTF8 = "utf-8";

// Special values
export const PR_NOT_FOUND = -1;
86 changes: 0 additions & 86 deletions src/github-api.ts

This file was deleted.

9 changes: 1 addition & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import type { Octokit } from "@octokit/core";

import * as core from "@actions/core";
import * as github from "@actions/github";

import { INPUT_NAME_REPO_TOKEN, INPUT_REQUIRED } from "./constants";
import main from "./main";

const token: string = core.getInput(INPUT_NAME_REPO_TOKEN, INPUT_REQUIRED);
const client: Octokit = github.getOctokit(token);

main(client, github.context);
main(github.context);
41 changes: 0 additions & 41 deletions src/inputs.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { Inputs, RawActionConfig } from "./types";

import {
DEFAULT_COMMENT,
INPUT_NAME_COMMENT,
INPUT_NAME_DRY_RUN,
INPUT_NAME_IGNORE,
INPUT_NAME_SVGO_OPTIONS,
Expand All @@ -19,8 +17,6 @@ const TRUE = "true";
const DEFAULT_SVGO_VERSION = 2;

export class ActionConfig {
public readonly comment: string;
public readonly enableComments: boolean;
public readonly ignoreGlob: string;
public readonly isDryRun: boolean;
public readonly svgoOptionsPath: string;
Expand All @@ -29,35 +25,11 @@ export class ActionConfig {
constructor(inputs: Inputs, config: RawActionConfig = { }) {
this.isDryRun = ActionConfig.getDryRunValue(inputs, config);

this.comment = ActionConfig.getCommentValue(inputs, config);
this.enableComments = ActionConfig.getEnableComments(
inputs,
config,
this.isDryRun,
);
this.ignoreGlob = ActionConfig.getIgnoreGlob(inputs, config);
this.svgoOptionsPath = ActionConfig.getSvgoOptionsPath(inputs, config);
this.svgoVersion = ActionConfig.getSvgoVersion(inputs, config);
}

private static getCommentValue(
inputs: Inputs,
config: RawActionConfig,
): string {
const value = (config.comment !== undefined) ?
config.comment : inputs.getInput(INPUT_NAME_COMMENT, INPUT_NOT_REQUIRED);

if (typeof value === STRING && value !== TRUE) {
// If the value is (the string) `"false"` comments will be disabled, so it
// does not matter that the comment template is `"false"`. If the value is
// (the string) `"true"`, we interpret it as (the boolean) `true`, so the
// default template should be used.
return value as string;
} else {
return DEFAULT_COMMENT;
}
}

private static getDryRunValue(
inputs: Inputs,
config: RawActionConfig,
Expand All @@ -70,19 +42,6 @@ export class ActionConfig {
);
}

private static getEnableComments(
inputs: Inputs,
config: RawActionConfig,
dryRun: boolean,
): boolean {
return this.normalizeBoolOption(
inputs,
config.comment as boolean,
INPUT_NAME_COMMENT,
true,
) && !dryRun;
}

private static getIgnoreGlob(
inputs: Inputs,
config: RawActionConfig,
Expand Down
28 changes: 2 additions & 26 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
/* eslint-disable security/detect-non-literal-fs-filename */

import type { Context } from "@actions/github/lib/context";
import type { Octokit } from "@octokit/core";

import type { RawActionConfig, Warnings } from "./types";
import type { Context, RawActionConfig, Warnings } from "./types";

import * as core from "@actions/core";

import {
COMMENTABLE_EVENTS,
DEFAULT_CONFIG_PATH,
DEFAULT_SVGO_OPTIONS,
INPUT_NAME_CONFIG_PATH,
INPUT_NOT_REQUIRED,
SUPPORTED_EVENTS,
} from "./constants";
import * as fs from "./file-system";
import { createComment, getPrNumber } from "./github-api";
import { ActionConfig } from "./inputs";
import { parseJavaScript, parseYaml } from "./parser";
import { SVGOptimizer, SVGOptions } from "./svgo";
import { optimize } from "./optimize";
import { setOutputValues } from "./outputs";
import { shouldSkipRun } from "./skip-run";
import { formatComment } from "./templating";

async function getActionConfig(): Promise<[ActionConfig, Warnings]> {
const filePath = core.getInput(INPUT_NAME_CONFIG_PATH, INPUT_NOT_REQUIRED);
Expand Down Expand Up @@ -97,11 +90,9 @@ async function initAction(): Promise<[
}

async function run(
client: Octokit,
context: Context,
config: ActionConfig,
svgo: SVGOptimizer,
warnings: Warnings,
): Promise<void> {
try {
const event = context.eventName;
Expand All @@ -112,13 +103,6 @@ async function run(

const optimizeData = await optimize(fs, config, svgo);
setOutputValues(event, optimizeData);

if (COMMENTABLE_EVENTS.includes(event) && config.enableComments) {
const prNumber = getPrNumber(context);
core.info(`Creating comment in Pull Request #${prNumber}...`);
const comment = formatComment(config.comment, optimizeData, warnings);
await createComment(client, context, prNumber, comment);
}
} catch (error) {
core.setFailed(`action failed with error '${error}'`);
}
Expand All @@ -131,17 +115,9 @@ function logWarnings(warnings: Warnings): void {
}

export default async function main(
client: Octokit,
context: Context,
): Promise<void> {
const [{ config, svgo }, warnings] = await initAction();

const skip = await shouldSkipRun(client, context);
if (!skip.shouldSkip) {
run(client, context, config, svgo, warnings);
} else {
core.info(`Action disabled from ${skip.reason}, exiting`);
}

run(context, config, svgo);
logWarnings(warnings);
}
78 changes: 0 additions & 78 deletions src/skip-run.ts

This file was deleted.

Loading

0 comments on commit b1d17d5

Please sign in to comment.