-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
123 lines (99 loc) · 3.52 KB
/
main.ts
File metadata and controls
123 lines (99 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { cwd, readFile, writeFile } from "./utils/filesystem";
import { context, getOctokit } from "@actions/github";
import { parse } from "./utils/inputs";
import { info } from "@actions/core";
import type { Config } from "./types/config";
import { Repository } from "./utils/repository";
import { setPreview } from "./utils/preview";
import { setOutputs } from "./utils/outputs";
import { getPackageManager } from "./utils/packageManagers";
import { titleCase } from "./utils/strings";
import { readConfig } from "./utils/config";
import type { LockFile } from "./types/lockFile";
import type { Data } from "./types/data";
import type { Repository as Credentials } from "./types/repository";
import { defaultPackage, defaultPullRequest } from "./libs/defaults";
import type { GitHub } from "@actions/github/lib/utils";
const previewUpdater = async () => {
// Inputs
const { token, configPath, readmePath } = parse();
// Credentials
const credentials: Credentials = {
owner: context.repo.owner,
repo: context.repo.repo,
};
// API
const github: InstanceType<typeof GitHub> = getOctokit(token);
// Load Config
const _repo = new Repository(
<Config>{
repository: credentials,
},
github,
);
const config: Config = await readConfig(
<Config>{
directory: cwd(),
repository: credentials,
},
configPath,
_repo,
);
// Read names
const packageLock: LockFile = getPackageManager(config);
config.readme = readmePath;
config.package ||= defaultPackage;
config.data ||= <Data>{};
config.package.name ||= packageLock.name || config.repository?.repo;
config.data.title ||= titleCase(config.repository?.repo);
config.data.description ||=
packageLock.description || config.repository?.owner;
// Show working directory
info(`Working directory: ${config.directory}`);
// Authenticate
const repo = new Repository(config, github);
await repo.authenticate();
// Read file
const content: string = readFile(config, config.readme);
const preview: string = setPreview(content, config, packageLock);
if (content === preview) {
info(`File "${config.readme}" is up to date`);
return;
}
// Checkout branch
const branchExists: boolean = await repo.branchExists();
info(
`Checkout ${branchExists ? "existing" : "new"} branch named "${repo.branchName()}"`,
);
await repo.checkoutBranch(!branchExists);
// Write a file
info(`Update readme in "${config.readme}" file`);
writeFile(config, config.readme, preview);
// Stage and commit changes
await repo.stage();
await repo.commit();
await repo.push();
// Create a Pull Request
const pullRequest = await repo.createPullRequest();
// Variables
const pullRequestNumber: number = pullRequest.data.number;
const pullRequestUrl: string = pullRequest.data.html_url;
// Set labels and assignees
await repo.assignee(
pullRequestNumber,
config.repository?.pullRequest?.assignees ||
defaultPullRequest.assignees ||
[],
);
await repo.addLabels(
pullRequestNumber,
config.repository?.pullRequest?.labels ||
defaultPullRequest.labels ||
[],
);
info(
`Preview created in Pull Request #${pullRequestNumber}: ${pullRequestUrl}`,
);
setOutputs(repo.branchName(), pullRequestNumber, pullRequestUrl);
};
export default previewUpdater;