Skip to content

Commit 6bea9ed

Browse files
authored
feat(config): support reading from local file if it exists (#48)
Signed-off-by: Liam Stanley <[email protected]>
1 parent 98b5412 commit 6bea9ed

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

action.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ inputs:
77
required: false
88
default: '${{ github.token }}'
99
configuration-path:
10-
description: 'Path to the labeler.yml configuration file'
10+
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."
1111
required: true
1212
enable-versioned-regex:
1313
description: 'Controls if versioned regex templates are being used'

lib/index.js

+4-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

+23-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getInput, setFailed, debug, setOutput } from "@actions/core";
22
import { context, getOctokit } from "@actions/github";
33
import { load as loadYaml } from "js-yaml";
4+
import fs from "fs";
45

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

@@ -151,21 +152,31 @@ function regexifyConfigPath(configPath: string, version: string) {
151152
/** Load the configuration file */
152153
async function loadConfig(client: GitHubClient, configPath: string) {
153154
try {
154-
const { data } = await client.repos.getContent({
155-
owner: context.repo.owner,
156-
repo: context.repo.repo,
157-
ref: context.sha,
158-
path: configPath,
159-
});
155+
let configContent: string
160156

161-
if (!("content" in data)) {
162-
throw new TypeError(
163-
"The configuration path provided is not a valid file. Exiting"
164-
);
165-
}
157+
if (fs.existsSync(configPath)) {
158+
console.log(`Configuration file (path: ${configPath}) exists locally, loading from file`);
166159

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

164+
const { data } = await client.repos.getContent({
165+
owner: context.repo.owner,
166+
repo: context.repo.repo,
167+
ref: context.sha,
168+
path: configPath,
169+
});
170+
171+
if (!("content" in data)) {
172+
throw new TypeError(
173+
"The configuration path provided is not a valid file. Exiting"
174+
);
175+
}
176+
177+
configContent = Buffer.from(data.content, "base64").toString("utf8");
178+
}
179+
169180
// loads (hopefully) a `{[label:string]: string | string[]}`, but is `any`:
170181
const configObject = loadYaml(configContent);
171182

0 commit comments

Comments
 (0)