Skip to content

Commit 33b5289

Browse files
Refactor Config handling: streamline inputs parsing, enhance readConfig logic, and expand Config schema for improved repository configuration management
1 parent b12d40a commit 33b5289

4 files changed

Lines changed: 91 additions & 108 deletions

File tree

src/main.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
1-
import { cwd } from './utils/filesystem'
1+
import { cwd, readConfig } from './utils/filesystem'
22
import { context } from '@actions/github'
33
import { parse } from './utils/inputs'
44
import { info } from '@actions/core'
5+
import { Config } from './types/config'
56

67
const previewUpdater = async () => {
7-
// Inputs
8-
8+
// Welcome
99
info(`Working directory: ${ cwd }`)
1010

11-
const { owner, repo: repoName } = context.repo
12-
11+
// Inputs
1312
const {
1413
token,
15-
path,
16-
branchName,
17-
commitTitle,
18-
commitBody,
19-
commitAuthorName,
20-
commitAuthorEmail,
21-
pullRequestTitle,
22-
pullRequestBody,
23-
assignees,
24-
labels
14+
configPath
2515
} = parse()
2616

17+
// Load Config
18+
const config = readConfig(<Config>{
19+
repository: {
20+
owner: context.repo.owner,
21+
repo: context.repo.repo,
22+
token: token
23+
}
24+
}, configPath)
25+
2726
// Authenticate
2827
const repo = new Repository(owner, repoName, token)
2928
await repo.authenticate(commitAuthorName, commitAuthorEmail)

src/types/config.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,57 @@ export interface Image
1919
parameters: ImageParameters;
2020
}
2121

22+
export interface Author
23+
{
24+
name: string;
25+
email: string;
26+
}
27+
28+
export interface Commit
29+
{
30+
branch: string;
31+
title: string;
32+
body: string;
33+
author: Author
34+
}
35+
36+
export interface PullRequest
37+
{
38+
title: string;
39+
body: string;
40+
assignees: string[];
41+
labels: string[];
42+
}
43+
44+
export interface Repository
45+
{
46+
owner: string;
47+
repo: string;
48+
token: string;
49+
50+
commit: Commit;
51+
pullRequest: PullRequest;
52+
}
53+
54+
export interface Path
55+
{
56+
readme: string;
57+
}
58+
2259
export interface Config
2360
{
2461
directory: string;
62+
path: Path;
63+
2564
image: Image;
65+
repository: Repository;
2666
}
2767

2868
export const defaultConfig: Config = {
2969
directory: '',
70+
path: {
71+
readme: 'README.md'
72+
},
3073

3174
image: {
3275
url: 'https://banners.beyondco.de/{title}.png',
@@ -44,5 +87,29 @@ export const defaultConfig: Config = {
4487
title: '',
4588
description: ''
4689
}
90+
},
91+
92+
repository: {
93+
owner: '',
94+
repo: '',
95+
token: '',
96+
97+
commit: {
98+
branch: 'preview/update-{timestamp}',
99+
title: 'docs(preview): Update preview',
100+
body: '',
101+
102+
author: {
103+
name: 'github-actions',
104+
email: 'github-actions@github.com'
105+
}
106+
},
107+
108+
pullRequest: {
109+
title: 'Update preview',
110+
body: '',
111+
assignees: [],
112+
labels: []
113+
}
47114
}
48115
}

src/utils/filesystem.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ export const writeFile = (config: Config, filename: string, content: string): vo
2929
fs.writeFileSync(filePath(config, filename), content)
3030
}
3131

32-
export const readConfig = (config: Config, path: string = ''): Config => {
33-
const content = readFile(config, path || '.github/preview-updater.yml')
32+
export const readConfig = (config: Config, userConfigPath: string): Config => {
33+
const content = readFile(defaultConfig, userConfigPath)
3434

3535
if (content === '') {
36-
return defaultConfig
36+
return <Config>deepmerge(defaultConfig, config)
3737
}
3838

3939
const userConfig = <Config>yaml.load(content)
4040

41-
return <Config>deepmerge(defaultConfig, userConfig)
41+
return <Config>deepmerge(defaultConfig, userConfig, config)
4242
}

src/utils/inputs.ts

Lines changed: 6 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -5,101 +5,18 @@ export const TOKEN = {
55
env: 'INPUT_TOKEN'
66
}
77

8-
export const PATH = {
9-
name: 'path',
10-
env: 'INPUT_PATH',
11-
defaultValue: 'README.md'
12-
}
13-
14-
export const BRANCH_NAME = {
15-
name: 'branchName',
16-
env: 'INPUT_BRANCHNAME',
17-
defaultValue: 'preview/update-{timestamp}'
18-
}
19-
20-
export const COMMIT_TITLE = {
21-
name: 'commitTitle',
22-
env: 'INPUT_COMMITTITLE',
23-
defaultValue: 'docs(preview): Update preview'
24-
}
25-
26-
export const COMMIT_BODY = {
27-
name: 'commitBody',
28-
env: 'INPUT_COMMITBODY',
29-
defaultValue: ''
30-
}
31-
32-
export const COMMIT_AUTHOR_NAME = {
33-
name: 'commitAuthorName',
34-
env: 'INPUT_COMMITAUTHORNAME',
35-
defaultValue: 'github-actions'
36-
}
37-
38-
export const COMMIT_AUTHOR_EMAIL = {
39-
name: 'commitAuthorEmail',
40-
env: 'INPUT_COMMITAUTHOREMAIL',
41-
defaultValue: 'github-actions@github.com'
42-
}
43-
44-
export const PR_TITLE = {
45-
name: 'prTitle',
46-
env: 'INPUT_PRTITLE',
47-
defaultValue: 'Update preview'
48-
}
49-
50-
export const PR_BODY = {
51-
name: 'prBody',
52-
env: 'INPUT_PRBODY',
53-
defaultValue: ''
54-
}
55-
56-
export const ASSIGNEES = {
57-
name: 'assignees',
58-
env: 'INPUT_ASSIGNEES',
59-
defaultValue: ''
60-
}
61-
62-
export const LABELS = {
63-
name: 'labels',
64-
env: 'INPUT_LABELS',
65-
defaultValue: ''
8+
export const CONFIG_PATH = {
9+
name: 'configPath',
10+
env: 'INPUT_CONFIG_PATH',
11+
defaultValue: '.github/preview-updater.yml'
6612
}
6713

6814
export const parse = () => {
6915
const token = getInput(TOKEN.name, { required: true })
70-
const path = getInput(PATH.name) || PATH.defaultValue
71-
const branchName = (getInput(BRANCH_NAME.name) || BRANCH_NAME.defaultValue).replace('{timestamp}', Date.now().toString())
72-
73-
const commitTitle = getInput(COMMIT_TITLE.name) || COMMIT_TITLE.defaultValue
74-
const commitBody = getInput(COMMIT_BODY.name) || COMMIT_BODY.defaultValue
75-
76-
const commitAuthorName = getInput(COMMIT_AUTHOR_NAME.name) || COMMIT_AUTHOR_NAME.defaultValue
77-
const commitAuthorEmail = getInput(COMMIT_AUTHOR_EMAIL.name) || COMMIT_AUTHOR_EMAIL.defaultValue
78-
79-
const pullRequestTitle = getInput(PR_TITLE.name) || PR_TITLE.defaultValue
80-
const pullRequestBody = getInput(PR_BODY.name) || PR_BODY.defaultValue
81-
82-
const assignees = splitCsv(getInput(ASSIGNEES.name) || ASSIGNEES.defaultValue)
83-
const labels = splitCsv(getInput(LABELS.name) || LABELS.defaultValue)
16+
const configPath = getInput(CONFIG_PATH.name) || CONFIG_PATH.defaultValue
8417

8518
return {
8619
token,
87-
path,
88-
branchName,
89-
commitTitle,
90-
commitBody,
91-
commitAuthorName,
92-
commitAuthorEmail,
93-
pullRequestTitle,
94-
pullRequestBody,
95-
assignees,
96-
labels
20+
configPath
9721
}
9822
}
99-
100-
const splitCsv = (values: string) => {
101-
return values
102-
.split(',')
103-
.map((value) => value.trim())
104-
.filter((value) => value !== '')
105-
}

0 commit comments

Comments
 (0)