Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): support reading from local file if it exists #48

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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