Skip to content

Commit 9588b72

Browse files
Merge pull request #54
Add support for fetching and merging remote configuration files
2 parents 6fd3ba6 + 1a4f8ff commit 9588b72

File tree

5 files changed

+66
-9
lines changed

5 files changed

+66
-9
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ The action is setting the following outputs:
6767

6868
## Configuration
6969

70+
> [!TIP]
71+
>
72+
> Support for working with a global settings file at the organization level (the `.github` repository).
73+
>
74+
> For example, `https://github.com/<repo>/.github/blob/main/.github/preview-updater.yml`.
75+
7076
Create `.github/preview-updater.yml` (or provide your own path via `configPath`).
7177
All fields are optional—omitted ones fall back to defaults.
7278

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const previewUpdater = async () => {
1616
const { token, configPath } = parse();
1717

1818
// Load Config
19-
const config: Config = readConfig(
19+
const config: Config = await readConfig(
2020
<Config>{
2121
directory: cwd(),
2222

src/utils/config.ts

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,51 @@
11
import { type Config, defaultConfig } from "../types/config";
22
import * as yaml from "js-yaml";
3-
import { readFile } from "./filesystem";
3+
import { readFile, readRemoteFile } from "./filesystem";
44
import { merge } from "./merge";
5+
import { info } from "@actions/core";
6+
import * as url from "node:url";
57

6-
export const readConfig = (config: Config, userConfigPath: string): Config => {
8+
export const readConfig = async (
9+
config: Config,
10+
userConfigPath: string,
11+
): Promise<Config> => {
712
const content: string = readFile(config, userConfigPath);
13+
const remoteConfig: Config = await readRemoteConfig(
14+
config.repository?.owner,
15+
userConfigPath,
16+
);
817

918
if (content === "") {
10-
return <Config>merge(defaultConfig, config);
19+
return <Config>merge(defaultConfig, remoteConfig, config);
1120
}
1221

1322
const userConfig = <Config>yaml.load(content);
1423

15-
return <Config>merge(defaultConfig, userConfig, config);
24+
return <Config>merge(defaultConfig, remoteConfig, userConfig, config);
25+
};
26+
27+
export const readRemoteConfig = async (
28+
owner: string | undefined,
29+
filename: string,
30+
): Promise<Config> => {
31+
try {
32+
if (owner === undefined) {
33+
return <Config>{};
34+
}
35+
36+
const url = `https://raw.githubusercontent.com/${owner}/.github/refs/heads/main/${filename}`;
37+
38+
const data: string = await readRemoteFile(url);
39+
40+
if (data === "") {
41+
return <Config>{};
42+
}
43+
44+
return <Config>yaml.load(data);
45+
} catch (error) {
46+
// @ts-expect-error
47+
info(`Failed to fetch remote config from ${url}: ${error.message}`);
48+
49+
return <Config>{};
50+
}
1651
};

src/utils/filesystem.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as fs from "node:fs";
22
import type { Config } from "../types/config";
3+
import { info } from "@actions/core";
34

45
export const cwd = (): string => {
56
const path = process.env.GITHUB_WORKSPACE;
@@ -17,6 +18,18 @@ const filePath = (config: Config, filename: string): string =>
1718
export const fileExists = (config: Config, filename: string): boolean =>
1819
fs.existsSync(filePath(config, filename));
1920

21+
export const readRemoteFile = async (url: string): Promise<string> => {
22+
const response: Response = await fetch(url);
23+
24+
if (!response.ok) {
25+
info(`Failed to fetch ${url} with status code ${response.status}`);
26+
27+
return "";
28+
}
29+
30+
return response.text();
31+
};
32+
2033
export const readFile = (config: Config, filename: string): string => {
2134
if (!fs.existsSync(filePath(config, filename))) {
2235
return "";

tests/unit/filesystem.test.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import { type Config, defaultConfig } from "../../src/types/config";
33
import { CONFIG_PATH } from "../../src/utils/inputs";
44
import { readConfig } from "../../src/utils/config";
55

6-
test("read config", () => {
7-
const data: Config = readConfig(rawTestConfig, CONFIG_PATH.defaultValue);
6+
test("read config", async () => {
7+
const data: Config = await readConfig(
8+
rawTestConfig,
9+
CONFIG_PATH.defaultValue,
10+
);
811

912
expect(data.directory).toBe(process.cwd());
1013

@@ -26,8 +29,8 @@ test("read config", () => {
2629
expect(data.image.parameters.icon).toBe("photograph");
2730
});
2831

29-
test("custom config", () => {
30-
const data: Config = readConfig(
32+
test("custom config", async () => {
33+
const data: Config = await readConfig(
3134
<Config>{
3235
directory: process.cwd(),
3336
},

0 commit comments

Comments
 (0)