|
1 | 1 | import { type Config, defaultConfig } from "../types/config"; |
2 | 2 | import * as yaml from "js-yaml"; |
3 | | -import { readFile } from "./filesystem"; |
| 3 | +import { readFile, readRemoteFile } from "./filesystem"; |
4 | 4 | import { merge } from "./merge"; |
| 5 | +import { info } from "@actions/core"; |
| 6 | +import * as url from "node:url"; |
5 | 7 |
|
6 | | -export const readConfig = (config: Config, userConfigPath: string): Config => { |
| 8 | +export const readConfig = async ( |
| 9 | + config: Config, |
| 10 | + userConfigPath: string, |
| 11 | +): Promise<Config> => { |
7 | 12 | const content: string = readFile(config, userConfigPath); |
| 13 | + const remoteConfig: Config = await readRemoteConfig( |
| 14 | + config.repository?.owner, |
| 15 | + userConfigPath, |
| 16 | + ); |
8 | 17 |
|
9 | 18 | if (content === "") { |
10 | | - return <Config>merge(defaultConfig, config); |
| 19 | + return <Config>merge(defaultConfig, remoteConfig, config); |
11 | 20 | } |
12 | 21 |
|
13 | 22 | const userConfig = <Config>yaml.load(content); |
14 | 23 |
|
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 | + } |
16 | 51 | }; |
0 commit comments