-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathgithub.ts
160 lines (145 loc) Β· 3.89 KB
/
github.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
import { existsSync, promises as fsp } from "node:fs";
import { homedir } from "node:os";
import { $fetch, FetchOptions } from "ofetch";
import { join } from "pathe";
import { ResolvedChangelogConfig } from "./config";
export interface GithubOptions {
repo: string;
token: string;
}
export interface GithubRelease {
id?: string;
tag_name: string;
name?: string;
body?: string;
draft?: boolean;
prerelease?: boolean;
}
export async function listGithubReleases(
config: ResolvedChangelogConfig
): Promise<GithubRelease[]> {
return await githubFetch(config, `/repos/${config.repo.repo}/releases`, {
query: { per_page: 100 },
});
}
export async function getGithubReleaseByTag(
config: ResolvedChangelogConfig,
tag: string
): Promise<GithubRelease> {
return await githubFetch(
config,
`/repos/${config.repo.repo}/releases/tags/${tag}`
);
}
export async function getGithubChangelog(config: ResolvedChangelogConfig) {
return await githubFetch(
config,
`https://raw.githubusercontent.com/${config.repo.repo}/main/CHANGELOG.md`
);
}
export async function createGithubRelease(
config: ResolvedChangelogConfig,
body: GithubRelease
) {
return await githubFetch(config, `/repos/${config.repo.repo}/releases`, {
method: "POST",
body,
});
}
export async function updateGithubRelease(
config: ResolvedChangelogConfig,
id: string,
body: GithubRelease
) {
return await githubFetch(
config,
`/repos/${config.repo.repo}/releases/${id}`,
{
method: "PATCH",
body,
}
);
}
export async function syncGithubRelease(
config: ResolvedChangelogConfig,
release: { version: string; body: string }
) {
const currentGhRelease = await getGithubReleaseByTag(
config,
`v${release.version}`
).catch(() => {});
const ghRelease: GithubRelease = {
tag_name: `v${release.version}`,
name: `v${release.version}`,
body: release.body,
};
if (!config.tokens.github) {
return {
status: "manual",
url: githubNewReleaseURL(config, release),
};
}
try {
const newGhRelease = await (currentGhRelease
? updateGithubRelease(config, currentGhRelease.id, ghRelease)
: createGithubRelease(config, ghRelease));
return {
status: currentGhRelease ? "updated" : "created",
id: newGhRelease.id,
};
} catch (error) {
return {
status: "manual",
error,
url: githubNewReleaseURL(config, release),
};
}
}
export function githubNewReleaseURL(
config: ResolvedChangelogConfig,
release: { version: string; body: string }
) {
return `https://${config.repo.domain}/${config.repo.repo}/releases/new?tag=v${
release.version
}&title=v${release.version}&body=${encodeURIComponent(release.body)}`;
}
export async function resolveGithubToken(config: ResolvedChangelogConfig) {
const env =
process.env.CHANGELOGEN_TOKENS_GITHUB ||
process.env.GITHUB_TOKEN ||
process.env.GH_TOKEN;
if (env) {
return env;
}
const configHome = process.env.XDG_CONFIG_HOME || join(homedir(), ".config");
const ghCLIPath = join(configHome, "gh", "hosts.yml");
if (existsSync(ghCLIPath)) {
const yamlContents = await fsp.readFile(ghCLIPath, "utf8");
const { parseYAML } = await import("confbox/yaml");
const ghCLIConfig = parseYAML(yamlContents);
if (ghCLIConfig && ghCLIConfig[config.repo.domain]) {
return ghCLIConfig["github.com"].oauth_token;
}
}
}
// --- Internal utils ---
async function githubFetch(
config: ResolvedChangelogConfig,
url: string,
opts: FetchOptions = {}
) {
return await $fetch(url, {
...opts,
baseURL:
config.repo.domain === "github.com"
? "https://api.github.com"
: `https://${config.repo.domain}/api/v3`,
headers: {
"x-github-api-version": "2022-11-28",
...opts.headers,
authorization: config.tokens.github
? `Bearer ${config.tokens.github}`
: undefined,
},
});
}