forked from hasundue/denopendabot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
223 lines (183 loc) · 6.21 KB
/
mod.ts
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { groupBy } from "https://deno.land/std@0.183.0/collections/group_by.ts";
import { intersect } from "https://deno.land/std@0.183.0/collections/intersect.ts";
import { withoutAll } from "https://deno.land/std@0.183.0/collections/without_all.ts";
import { Octokit } from "https://esm.sh/@octokit/core@4.2.0";
import { env } from "./mod/env.ts";
import {
CommitType,
pullRequestType,
removeIgnore,
Update,
} from "./mod/common.ts";
import { GitHubClient } from "./mod/octokit.ts";
import { getModuleUpdateSpecs, ModuleUpdate } from "./mod/module.ts";
import { getRepoUpdateSpecs, RepoUpdate } from "./mod/repo.ts";
export { VERSION } from "./mod/version.ts";
export type Options = GlobalOptions & UpdateOptions & PullRequestOptions;
interface GlobalOptions {
octokit?: Octokit;
token?: string;
userToken?: string;
baseBranch?: string;
workingBranch?: string;
release?: string;
}
const getActionToken = (options?: GlobalOptions) => {
const envToken = options?.token && Deno.env.get(options?.token);
const rawToken = !envToken ? options?.token : undefined;
return (envToken || rawToken) ?? env.GITHUB_TOKEN;
};
const getUserToken = (options?: GlobalOptions) => {
const envUserToken = options?.userToken && Deno.env.get(options?.userToken);
const rawUserToken = !envUserToken ? options?.userToken : undefined;
return envUserToken ?? rawUserToken;
};
const defaultExcludePaths = [
"deno.lock",
] as const;
interface UpdateOptions {
include?: string[];
exclude?: string[];
root?: string; // not prefixed with "./"
}
export async function getUpdates(
repository: string,
options?: GlobalOptions & UpdateOptions,
) {
const github = new GitHubClient({
repository,
octokit: options?.octokit,
token: getActionToken(options) ?? getUserToken(options),
});
const base = options?.baseBranch ?? await github.defaultBranch();
const baseTree = await github.getTree(base, options?.root);
const paths = baseTree.map((blob) => blob.path!);
const pathsToInclude = options?.include || paths;
const pathsToExclude = options?.exclude || defaultExcludePaths;
const pathsToUpdate = withoutAll(
intersect(paths, pathsToInclude),
pathsToExclude,
);
const blobs = baseTree.filter((blob) => pathsToUpdate.includes(blob.path!));
const updates: Update[] = [];
for (const blob of blobs) {
console.debug(`🔍 ${blob.path}`);
const content = await github.getBlobContent(blob.sha!);
const contentToUpdate = removeIgnore(content);
// TS/JS modules
const moduleName = repository.split("/")[1].replaceAll("-", "_");
const moduleReleaseSpec = options?.release
? { name: moduleName, target: options.release }
: undefined;
const moduleSpecs = await getModuleUpdateSpecs(
contentToUpdate,
moduleReleaseSpec,
);
moduleSpecs.forEach((spec) =>
updates.push(new ModuleUpdate(blob.path!, spec))
);
// other repositories
const repoReleaseSpec = options?.release
? { name: repository, target: options.release }
: undefined;
const repoSpecs = await getRepoUpdateSpecs(
contentToUpdate,
repoReleaseSpec,
github,
);
repoSpecs.forEach((spec) => updates.push(new RepoUpdate(blob.path!, spec)));
}
return updates;
}
export async function createCommits(
repository: string,
updates: Update[],
options: GlobalOptions,
) {
if (updates.length === 0) {
throw new Error("❗ No updates available");
}
const actionToken = getActionToken(options);
const userToken = getUserToken(options);
if (!options?.octokit && !actionToken && !userToken) {
throw new Error("❗ Access token is not provided");
}
const github = new GitHubClient({
repository,
octokit: options?.octokit,
token: userToken ?? actionToken,
});
const authorized = options?.octokit || userToken;
// filter out workflows if we are not authorized to update them
const updatables = !authorized
? updates.filter((update) => !update.isWorkflow())
: updates;
const branch = options?.workingBranch ?? "denopendabot";
const latest = await github.getLatestCommit(options?.baseBranch);
const groupsByDep = groupBy(updatables, (it) => it.spec.name);
const deps = Object.keys(groupsByDep);
// create commits for each updated dependency
let sha = latest.sha;
for (const dep of deps) {
const updates = groupsByDep[dep]!;
const message = updates[0].message();
const commit = await github.createCommit(sha, message, updates);
sha = commit.sha;
}
await github.createBranch(branch, options?.baseBranch);
await github.updateBranch(branch, sha);
if (!authorized) {
console.info(
"📣 Skipped the workflow files since we are not authorized to update them.",
);
}
}
interface PullRequestOptions {
labels?: string[];
}
export async function createPullRequest(
repository: string,
options?: GlobalOptions & PullRequestOptions,
) {
const actionToken = getActionToken(options);
const userToken = getUserToken(options);
if (!options?.octokit && !actionToken && !userToken) {
throw new Error("❗ Access token is not provided");
}
const github = new GitHubClient({
repository,
octokit: options?.octokit,
token: actionToken ?? userToken,
});
const base = options?.baseBranch ?? await github.defaultBranch();
const head = options?.workingBranch ?? "denopendabot";
const { commits } = await gh.neting.ccpareBranches({ base, head });
if (!commits.length) {
console.info(`📣 ${base} and ${head} are identical`);
return null;
}
const messages = commits.map((commit) => commit.commit.message);
const types = intersect(
messages.map((message) => {
if (!message.includes(":")) {
return null;
}
return message.split(":")[0].split("(")[0];
}),
CommitType,
) as CommitType[];
const version = await github.getLatestRelease(repository);
const type = pullRequestType(types);
const scope = options?.release ? "version" : "deps";
const body = options?.release
? `bump the version from ${version} to ${options.release}`
: "update dependencies";
const title = `${type}(${scope}): ${body}`;
return await github.createPullRequest({
base,
head,
title,
modifiable: true,
labels: options?.labels,
});
}