Skip to content

Commit d715f50

Browse files
Merge pull request #92 from TheDragonCode/2.x
Refactor input handling to include `readmePath` and improve type annotations
2 parents 681c6ea + 9fdb4fd commit d715f50

File tree

8 files changed

+32
-23
lines changed

8 files changed

+32
-23
lines changed

.junie/guidelines.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Aliases:
1111
Ignore the following properties:
1212

1313
- `directory`
14+
- `readme`
1415
- `repository.owner`
1516
- `repository.repo`
1617

@@ -20,3 +21,4 @@ Steps:
2021
2. Study its dependencies.
2122
3. Memorize the overall relationship scheme of these interfaces.
2223
4. Update the JSON schema in the `resources/schema.json` file in accordance with the new requirements.
24+
5. Do not add the `default` parameter to the schema root.

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,19 @@ jobs:
4949
# Required: true
5050
token: ${{ secrets.GITHUB_TOKEN }}
5151

52-
# Path to the settings file
52+
# Path to the settings file
5353
#
5454
# By default, .github/preview-updater.yml
5555
#
5656
# Required: false
5757
config: ''
58+
59+
# Specifies the path to the README file.
60+
#
61+
# By default, README.md
62+
#
63+
# Required: false
64+
readme: ''
5865
```
5966
6067
The action is setting the following outputs:
@@ -82,8 +89,6 @@ All fields are optional—omitted ones fall back to defaults.
8289
```yaml
8390
# $schema: https://raw.githubusercontent.com/TheDragonCode/github-preview-updater/refs/heads/main/resources/schema.json
8491
85-
path: README.md # Target file to update
86-
8792
package:
8893
# Declares the use of the package manager.
8994
# It is a regular string that will be substituted into the URL address.
@@ -169,7 +174,7 @@ Ultimately, the image link will look like this:
169174

170175
## FAQ
171176

172-
> 💥 Preview Updater failed with error: Error when creating pull request from preview/banner-***: GitHub Actions is not permitted to create or approve pull requests
177+
> 💥 Preview Updater failed with error: Error when creating a pull request from preview/banner-***: GitHub Actions is not permitted to create or approve pull requests
173178

174179
Enable the "Allow GitHub Actions to create and approve pull requests" option in project settings.
175180

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ inputs:
1919
description: Path to the preview configuration YAML file.
2020
default: '.github/preview-updater.yml'
2121
required: false
22+
23+
readme:
24+
description: Specifies the path to the README file.
25+
default: 'README.md'
26+
required: false

resources/schema.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
"type": "object",
55
"additionalProperties": false,
66
"properties": {
7-
"readme": {
8-
"type": "string",
9-
"description": "Target README file where the banner will be inserted",
10-
"default": "README.md"
11-
},
127
"repository": {
138
"type": "object",
149
"additionalProperties": false,

src/libs/defaults.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Config } from "../types/config";
22
import type { Image } from "../types/image";
33
import type { Package } from "../types/package";
44
import type { Author, Commit, PullRequest } from "../types/repository";
5+
import { README_PATH } from "../utils/inputs";
56

67
export const defaultPackage: Package = {
78
manager: "auto",
@@ -39,7 +40,7 @@ export const defaultPullRequest: PullRequest = {
3940
};
4041

4142
export const defaultConfig: Config = {
42-
readme: "README.md",
43+
readme: README_PATH.defaultValue,
4344

4445
package: defaultPackage,
4546
image: defaultImage,

src/main.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,12 @@ import { readConfig } from "./utils/config";
1212
import type { LockFile } from "./types/lockFile";
1313
import type { Data } from "./types/data";
1414
import type { Repository as Credentials } from "./types/repository";
15-
import {
16-
defaultConfig,
17-
defaultPackage,
18-
defaultPullRequest,
19-
} from "./libs/defaults";
15+
import { defaultPackage, defaultPullRequest } from "./libs/defaults";
2016
import type { GitHub } from "@actions/github/lib/utils";
2117

2218
const previewUpdater = async () => {
2319
// Inputs
24-
const { token, configPath } = parse();
20+
const { token, configPath, readmePath } = parse();
2521

2622
// Credentials
2723
const credentials: Credentials = {
@@ -53,7 +49,7 @@ const previewUpdater = async () => {
5349
// Read names
5450
const packageLock: LockFile = getPackageManager(config);
5551

56-
config.readme ||= defaultConfig.readme || "README.md";
52+
config.readme = readmePath;
5753

5854
config.package ||= defaultPackage;
5955
config.data ||= <Data>{};
@@ -71,8 +67,8 @@ const previewUpdater = async () => {
7167
await repo.authenticate();
7268

7369
// Read file
74-
const content = readFile(config, config.readme);
75-
const preview = setPreview(content, config, packageLock);
70+
const content: string = readFile(config, config.readme);
71+
const preview: string = setPreview(content, config, packageLock);
7672

7773
if (content === preview) {
7874
info(`File "${config.readme}" is up to date`);
@@ -81,7 +77,7 @@ const previewUpdater = async () => {
8177
}
8278

8379
// Checkout branch
84-
const branchExists = await repo.branchExists();
80+
const branchExists: boolean = await repo.branchExists();
8581
info(
8682
`Checkout ${branchExists ? "existing" : "new"} branch named "${repo.branchName()}"`,
8783
);

src/utils/inputs.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,26 @@ import { getInput } from "@actions/core";
22

33
export const TOKEN = {
44
name: "token",
5-
env: "INPUT_TOKEN",
65
};
76

87
export const CONFIG_PATH = {
98
name: "config",
10-
env: "INPUT_CONFIG_PATH",
119
defaultValue: ".github/preview-updater.yml",
1210
};
1311

12+
export const README_PATH = {
13+
name: "readme",
14+
defaultValue: "README.md",
15+
};
16+
1417
export const parse = () => {
1518
const token = getInput(TOKEN.name, { required: true });
1619
const configPath = getInput(CONFIG_PATH.name) || CONFIG_PATH.defaultValue;
20+
const readmePath = getInput(README_PATH.name) || README_PATH.defaultValue;
1721

1822
return {
1923
token,
2024
configPath,
25+
readmePath,
2126
};
2227
};

src/utils/repository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export class Repository {
152152
});
153153
} catch (error) {
154154
// @ts-expect-error
155-
error.message = `Error when creating pull request from ${this.branchName()}: ${error.message}`;
155+
error.message = `Error when creating a pull request from ${this.branchName()}: ${error.message}`;
156156

157157
throw error;
158158
}

0 commit comments

Comments
 (0)