Skip to content

Commit

Permalink
feat(config): support reading from local file if it exists (#48)
Browse files Browse the repository at this point in the history
Signed-off-by: Liam Stanley <me@liamstanley.io>
  • Loading branch information
lrstanley committed Nov 8, 2023
1 parent 98b5412 commit 6bea9ed
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ inputs:
required: false
default: '${{ github.token }}'
configuration-path:
description: 'Path to the labeler.yml configuration file'
description: "The path to the label configuration file. If the file doesn't exist at the specified path on the runner, action will read from the source repository via the Github API."
required: true
enable-versioned-regex:
description: 'Controls if versioned regex templates are being used'
Expand Down
6 changes: 4 additions & 2 deletions lib/index.js

Large diffs are not rendered by default.

35 changes: 23 additions & 12 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getInput, setFailed, debug, setOutput } from "@actions/core";
import { context, getOctokit } from "@actions/github";
import { load as loadYaml } from "js-yaml";
import fs from "fs";

type GitHubClient = ReturnType<typeof getOctokit>["rest"];

Expand Down Expand Up @@ -151,21 +152,31 @@ function regexifyConfigPath(configPath: string, version: string) {
/** Load the configuration file */
async function loadConfig(client: GitHubClient, configPath: string) {
try {
const { data } = await client.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
ref: context.sha,
path: configPath,
});
let configContent: string

if (!("content" in data)) {
throw new TypeError(
"The configuration path provided is not a valid file. Exiting"
);
}
if (fs.existsSync(configPath)) {
console.log(`Configuration file (path: ${configPath}) exists locally, loading from file`);

const configContent = Buffer.from(data.content, "base64").toString("utf8");
configContent = fs.readFileSync(configPath, { encoding: "utf8" });
} else {
console.log(`Configuration file (path: ${configPath}) does not exist locally, fetching via the API`);

const { data } = await client.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
ref: context.sha,
path: configPath,
});

if (!("content" in data)) {
throw new TypeError(
"The configuration path provided is not a valid file. Exiting"
);
}

configContent = Buffer.from(data.content, "base64").toString("utf8");
}

// loads (hopefully) a `{[label:string]: string | string[]}`, but is `any`:
const configObject = loadYaml(configContent);

Expand Down

0 comments on commit 6bea9ed

Please sign in to comment.