@theholocron/github-client 0.1.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +53 -0
- package/dist/index.d.mts +273 -0
- package/dist/index.mjs +311 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# @theholocron/github-client
|
|
2
|
+
|
|
3
|
+
TypeScript client for the GitHub REST API, built on `@theholocron/http-client`.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm i @theholocron/github-client
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { createGitHubClient } from "@theholocron/github-client";
|
|
15
|
+
|
|
16
|
+
const client = createGitHubClient({ token: process.env.GITHUB_TOKEN! });
|
|
17
|
+
|
|
18
|
+
// Repos
|
|
19
|
+
const repo = await client.repos.getRepo("owner/name");
|
|
20
|
+
|
|
21
|
+
// Labels
|
|
22
|
+
const labels = await client.labels.listLabels("owner/name");
|
|
23
|
+
await client.labels.createLabel("owner/name", {
|
|
24
|
+
name: "bug",
|
|
25
|
+
color: "d73a4a",
|
|
26
|
+
description: "Something isn't working",
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// Topics
|
|
30
|
+
await client.topics.setTopics("owner/name", ["cli", "nodejs"]);
|
|
31
|
+
|
|
32
|
+
// Git plumbing (blobs, trees, commits, refs, PRs)
|
|
33
|
+
const ref = await client.git.getRef("owner/name", "main");
|
|
34
|
+
const blob = await client.git.createBlob("owner/name", "file contents");
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Namespaces
|
|
38
|
+
|
|
39
|
+
| Namespace | Methods |
|
|
40
|
+
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
41
|
+
| `user` | `getCurrentUser` |
|
|
42
|
+
| `repos` | `getRepo`, `updateRepo`, `getContents` |
|
|
43
|
+
| `security` | `enableVulnerabilityAlerts`, `enableAutomatedSecurityFixes`, `enableSecretScanning`, `enablePrivateVulnerabilityReporting`, `enableDependencyGraph`, `enableCodeScanning`, `disableDefaultCodeScanning` |
|
|
44
|
+
| `rulesets` | `listRulesets`, `createRuleset`, `updateRuleset` |
|
|
45
|
+
| `branches` | `protectBranch` |
|
|
46
|
+
| `workflows` | `listRuns`, `getRun` |
|
|
47
|
+
| `secrets` | `listSecrets`, `getPublicKey`, `putSecret`, `deleteSecret` |
|
|
48
|
+
| `environments` | `listEnvironments`, `upsertEnvironment`, `deleteEnvironment` |
|
|
49
|
+
| `issues` | `listIssues`, `getIssue`, `createIssue`, `updateIssue`, `addLabels`, `removeLabel`, `createComment`, `listMilestones` |
|
|
50
|
+
| `labels` | `listLabels`, `createLabel`, `updateLabel`, `deleteLabel` |
|
|
51
|
+
| `topics` | `setTopics` |
|
|
52
|
+
| `properties` | `setProperties` |
|
|
53
|
+
| `git` | `getRef`, `getCommit`, `getTree`, `getContents`, `createBlob`, `createTree`, `createCommit`, `createRef`, `updateRef`, `createPull` |
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import "@theholocron/http-client";
|
|
2
|
+
//#region src/utils.d.ts
|
|
3
|
+
interface GitHubClientOptions {
|
|
4
|
+
token: string;
|
|
5
|
+
baseUrl?: string;
|
|
6
|
+
fetch?: typeof fetch;
|
|
7
|
+
}
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/environments/environments.d.ts
|
|
10
|
+
interface GitHubEnvironment {
|
|
11
|
+
name: string;
|
|
12
|
+
wait_timer?: number;
|
|
13
|
+
prevent_self_review?: boolean;
|
|
14
|
+
protection_rules?: Array<{
|
|
15
|
+
type: string;
|
|
16
|
+
reviewers?: Array<{
|
|
17
|
+
type: "User" | "Team";
|
|
18
|
+
reviewer: {
|
|
19
|
+
id: number;
|
|
20
|
+
};
|
|
21
|
+
}>;
|
|
22
|
+
}>;
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/git/git.d.ts
|
|
26
|
+
interface GitRef {
|
|
27
|
+
object: {
|
|
28
|
+
sha: string;
|
|
29
|
+
type: string;
|
|
30
|
+
url: string;
|
|
31
|
+
};
|
|
32
|
+
ref: string;
|
|
33
|
+
url: string;
|
|
34
|
+
}
|
|
35
|
+
interface GitCommit {
|
|
36
|
+
sha: string;
|
|
37
|
+
tree: {
|
|
38
|
+
sha: string;
|
|
39
|
+
url: string;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
interface GitTreeItem {
|
|
43
|
+
path: string;
|
|
44
|
+
sha: string;
|
|
45
|
+
type: string;
|
|
46
|
+
mode?: string;
|
|
47
|
+
size?: number;
|
|
48
|
+
}
|
|
49
|
+
interface GitTree {
|
|
50
|
+
sha: string;
|
|
51
|
+
tree: GitTreeItem[];
|
|
52
|
+
truncated: boolean;
|
|
53
|
+
}
|
|
54
|
+
interface GitContents {
|
|
55
|
+
content: string;
|
|
56
|
+
encoding: string;
|
|
57
|
+
sha: string;
|
|
58
|
+
name: string;
|
|
59
|
+
path: string;
|
|
60
|
+
}
|
|
61
|
+
interface GitBlob {
|
|
62
|
+
sha: string;
|
|
63
|
+
url: string;
|
|
64
|
+
}
|
|
65
|
+
interface GitPull {
|
|
66
|
+
number: number;
|
|
67
|
+
html_url: string;
|
|
68
|
+
title: string;
|
|
69
|
+
}
|
|
70
|
+
interface CreatePullInput {
|
|
71
|
+
title: string;
|
|
72
|
+
head: string;
|
|
73
|
+
base: string;
|
|
74
|
+
body?: string;
|
|
75
|
+
}
|
|
76
|
+
//#endregion
|
|
77
|
+
//#region src/issues/issues.d.ts
|
|
78
|
+
interface GitHubIssue {
|
|
79
|
+
id: number;
|
|
80
|
+
number: number;
|
|
81
|
+
title: string;
|
|
82
|
+
body?: string | null;
|
|
83
|
+
state: "open" | "closed";
|
|
84
|
+
labels: Array<{
|
|
85
|
+
name: string;
|
|
86
|
+
}>;
|
|
87
|
+
assignee: {
|
|
88
|
+
login: string;
|
|
89
|
+
name?: string | null;
|
|
90
|
+
email?: string | null;
|
|
91
|
+
} | null;
|
|
92
|
+
updated_at: string;
|
|
93
|
+
html_url: string;
|
|
94
|
+
pull_request?: unknown;
|
|
95
|
+
}
|
|
96
|
+
interface GitHubMilestone {
|
|
97
|
+
number: number;
|
|
98
|
+
title: string;
|
|
99
|
+
}
|
|
100
|
+
interface IssueSearchParams {
|
|
101
|
+
state?: "open" | "closed" | "all";
|
|
102
|
+
sort?: string;
|
|
103
|
+
direction?: string;
|
|
104
|
+
per_page?: number;
|
|
105
|
+
filter?: string;
|
|
106
|
+
assignee?: string;
|
|
107
|
+
}
|
|
108
|
+
//#endregion
|
|
109
|
+
//#region src/labels/labels.d.ts
|
|
110
|
+
interface GitHubLabel {
|
|
111
|
+
name: string;
|
|
112
|
+
color: string;
|
|
113
|
+
description: string | null;
|
|
114
|
+
}
|
|
115
|
+
//#endregion
|
|
116
|
+
//#region src/repos/repos.d.ts
|
|
117
|
+
interface GitHubRepo {
|
|
118
|
+
default_branch: string;
|
|
119
|
+
full_name: string;
|
|
120
|
+
}
|
|
121
|
+
interface GitHubContents {
|
|
122
|
+
content: string;
|
|
123
|
+
encoding: string;
|
|
124
|
+
}
|
|
125
|
+
//#endregion
|
|
126
|
+
//#region src/rulesets/rulesets.d.ts
|
|
127
|
+
interface GitHubRuleset {
|
|
128
|
+
id: number;
|
|
129
|
+
name: string;
|
|
130
|
+
enforcement: string;
|
|
131
|
+
}
|
|
132
|
+
//#endregion
|
|
133
|
+
//#region src/secrets/secrets.d.ts
|
|
134
|
+
type SecretScope = {
|
|
135
|
+
kind: "repo";
|
|
136
|
+
} | {
|
|
137
|
+
kind: "environment";
|
|
138
|
+
name: string;
|
|
139
|
+
} | {
|
|
140
|
+
kind: "organization";
|
|
141
|
+
org: string;
|
|
142
|
+
};
|
|
143
|
+
interface GitHubPublicKey {
|
|
144
|
+
key_id: string;
|
|
145
|
+
key: string;
|
|
146
|
+
}
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region src/security/security.d.ts
|
|
149
|
+
interface CodeScanningSetupResult {
|
|
150
|
+
run_id: number;
|
|
151
|
+
run_url: string;
|
|
152
|
+
}
|
|
153
|
+
//#endregion
|
|
154
|
+
//#region src/user/user.d.ts
|
|
155
|
+
interface GitHubUser {
|
|
156
|
+
login: string;
|
|
157
|
+
name: string | null;
|
|
158
|
+
email: string | null;
|
|
159
|
+
}
|
|
160
|
+
//#endregion
|
|
161
|
+
//#region src/workflows/workflows.d.ts
|
|
162
|
+
interface GitHubWorkflowRun {
|
|
163
|
+
id: number;
|
|
164
|
+
name: string | null;
|
|
165
|
+
display_title: string;
|
|
166
|
+
head_branch: string;
|
|
167
|
+
head_sha: string;
|
|
168
|
+
status: string;
|
|
169
|
+
conclusion: string | null;
|
|
170
|
+
html_url: string;
|
|
171
|
+
created_at: string;
|
|
172
|
+
updated_at: string;
|
|
173
|
+
}
|
|
174
|
+
interface WorkflowRunFilter {
|
|
175
|
+
branch?: string;
|
|
176
|
+
limit?: number;
|
|
177
|
+
status?: string;
|
|
178
|
+
}
|
|
179
|
+
//#endregion
|
|
180
|
+
//#region src/index.d.ts
|
|
181
|
+
declare function createGitHubClient(opts: GitHubClientOptions): {
|
|
182
|
+
branches: {
|
|
183
|
+
protectBranch: (repo: string, branch: string, payload: Record<string, unknown>) => Promise<void>;
|
|
184
|
+
};
|
|
185
|
+
environments: {
|
|
186
|
+
listEnvironments: (repo: string) => Promise<GitHubEnvironment[]>;
|
|
187
|
+
upsertEnvironment: (repo: string, name: string, body: Record<string, unknown>) => Promise<void>;
|
|
188
|
+
deleteEnvironment: (repo: string, name: string) => Promise<void>;
|
|
189
|
+
};
|
|
190
|
+
git: {
|
|
191
|
+
getRef: (repo: string, branch: string) => Promise<GitRef>;
|
|
192
|
+
getCommit: (repo: string, sha: string) => Promise<GitCommit>;
|
|
193
|
+
getTree: (repo: string, sha: string, recursive?: boolean) => Promise<GitTree>;
|
|
194
|
+
getContents: (repo: string, path: string) => Promise<GitContents>;
|
|
195
|
+
createBlob: (repo: string, content: string, encoding?: string) => Promise<GitBlob>;
|
|
196
|
+
createTree: (repo: string, tree: Array<{
|
|
197
|
+
path: string;
|
|
198
|
+
mode: string;
|
|
199
|
+
type: string;
|
|
200
|
+
sha: string;
|
|
201
|
+
}>, baseTree?: string) => Promise<GitTree>;
|
|
202
|
+
createCommit: (repo: string, message: string, tree: string, parents: string[]) => Promise<GitCommit>;
|
|
203
|
+
createRef: (repo: string, ref: string, sha: string) => Promise<GitRef>;
|
|
204
|
+
updateRef: (repo: string, ref: string, sha: string, force?: boolean) => Promise<GitRef>;
|
|
205
|
+
createPull: (repo: string, input: CreatePullInput) => Promise<GitPull>;
|
|
206
|
+
};
|
|
207
|
+
issues: {
|
|
208
|
+
listIssues: (repo: string, params?: IssueSearchParams) => Promise<GitHubIssue[]>;
|
|
209
|
+
getIssue: (repo: string, number: number) => Promise<GitHubIssue>;
|
|
210
|
+
createIssue: (repo: string, body: Record<string, unknown>) => Promise<GitHubIssue>;
|
|
211
|
+
updateIssue: (repo: string, number: number, body: Record<string, unknown>) => Promise<GitHubIssue>;
|
|
212
|
+
addLabels: (repo: string, number: number, labels: string[]) => Promise<void>;
|
|
213
|
+
removeLabel: (repo: string, number: number, label: string) => Promise<void>;
|
|
214
|
+
createComment: (repo: string, number: number, body: string) => Promise<void>;
|
|
215
|
+
listMilestones: (repo: string, params?: {
|
|
216
|
+
state?: "open" | "closed" | "all";
|
|
217
|
+
}) => Promise<GitHubMilestone[]>;
|
|
218
|
+
};
|
|
219
|
+
labels: {
|
|
220
|
+
listLabels: (repo: string) => Promise<GitHubLabel[]>;
|
|
221
|
+
createLabel: (repo: string, body: {
|
|
222
|
+
name: string;
|
|
223
|
+
color: string;
|
|
224
|
+
description: string;
|
|
225
|
+
}) => Promise<GitHubLabel>;
|
|
226
|
+
updateLabel: (repo: string, name: string, body: {
|
|
227
|
+
color?: string;
|
|
228
|
+
description?: string;
|
|
229
|
+
}) => Promise<GitHubLabel>;
|
|
230
|
+
deleteLabel: (repo: string, name: string) => Promise<void>;
|
|
231
|
+
};
|
|
232
|
+
properties: {
|
|
233
|
+
setProperties: (repo: string, values: Record<string, string>) => Promise<void>;
|
|
234
|
+
};
|
|
235
|
+
repos: {
|
|
236
|
+
getRepo: (repo: string) => Promise<GitHubRepo>;
|
|
237
|
+
updateRepo: (repo: string, settings: Record<string, unknown>) => Promise<void>;
|
|
238
|
+
getContents: (repo: string, path: string) => Promise<GitHubContents>;
|
|
239
|
+
};
|
|
240
|
+
rulesets: {
|
|
241
|
+
listRulesets: (repo: string) => Promise<GitHubRuleset[]>;
|
|
242
|
+
createRuleset: (repo: string, payload: Record<string, unknown>) => Promise<GitHubRuleset>;
|
|
243
|
+
updateRuleset: (repo: string, id: number, payload: Record<string, unknown>) => Promise<GitHubRuleset>;
|
|
244
|
+
};
|
|
245
|
+
secrets: {
|
|
246
|
+
listSecrets: (repo: string, scope: SecretScope) => Promise<string[]>;
|
|
247
|
+
getPublicKey: (repo: string, scope: SecretScope) => Promise<GitHubPublicKey>;
|
|
248
|
+
putSecret: (repo: string, scope: SecretScope, name: string, body: Record<string, unknown>) => Promise<void>;
|
|
249
|
+
deleteSecret: (repo: string, scope: SecretScope, name: string) => Promise<void>;
|
|
250
|
+
};
|
|
251
|
+
security: {
|
|
252
|
+
enableVulnerabilityAlerts: (repo: string) => Promise<void>;
|
|
253
|
+
enableAutomatedSecurityFixes: (repo: string) => Promise<void>;
|
|
254
|
+
enableSecretScanning: (repo: string) => Promise<void>;
|
|
255
|
+
enablePrivateVulnerabilityReporting: (repo: string) => Promise<void>;
|
|
256
|
+
enableDependencyGraph: (repo: string) => Promise<void>;
|
|
257
|
+
enableCodeScanning: (repo: string) => Promise<CodeScanningSetupResult>;
|
|
258
|
+
disableDefaultCodeScanning: (repo: string) => Promise<void>;
|
|
259
|
+
};
|
|
260
|
+
topics: {
|
|
261
|
+
setTopics: (repo: string, names: string[]) => Promise<void>;
|
|
262
|
+
};
|
|
263
|
+
user: {
|
|
264
|
+
getCurrentUser: () => Promise<GitHubUser>;
|
|
265
|
+
};
|
|
266
|
+
workflows: {
|
|
267
|
+
listRuns: (repo: string, filter?: WorkflowRunFilter) => Promise<GitHubWorkflowRun[]>;
|
|
268
|
+
getRun: (repo: string, id: string | number) => Promise<GitHubWorkflowRun>;
|
|
269
|
+
};
|
|
270
|
+
};
|
|
271
|
+
type GitHubClient = ReturnType<typeof createGitHubClient>;
|
|
272
|
+
//#endregion
|
|
273
|
+
export { type CodeScanningSetupResult, type CreatePullInput, type GitBlob, type GitCommit, type GitContents, GitHubClient, type GitHubClientOptions, type GitHubContents, type GitHubEnvironment, type GitHubIssue, type GitHubLabel, type GitHubMilestone, type GitHubPublicKey, type GitHubRepo, type GitHubRuleset, type GitHubUser, type GitHubWorkflowRun, type GitPull, type GitRef, type GitTree, type GitTreeItem, type IssueSearchParams, type SecretScope, type WorkflowRunFilter, createGitHubClient };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import { createRestClient } from "@theholocron/http-client";
|
|
2
|
+
//#region src/utils.ts
|
|
3
|
+
function createGitHubRestClient(opts) {
|
|
4
|
+
return createRestClient({
|
|
5
|
+
baseUrl: opts.baseUrl ?? "https://api.github.com",
|
|
6
|
+
token: opts.token,
|
|
7
|
+
extraHeaders: {
|
|
8
|
+
accept: "application/vnd.github+json",
|
|
9
|
+
"x-github-api-version": "2022-11-28"
|
|
10
|
+
},
|
|
11
|
+
vendor: "GitHub",
|
|
12
|
+
fetch: opts.fetch
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
/** `/repos/owner/name` prefix from a `"owner/name"` repo string. */
|
|
16
|
+
function repoBase(repo) {
|
|
17
|
+
const [owner, name] = repo.split("/", 2);
|
|
18
|
+
return `/repos/${owner}/${name}`;
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/branches/branches.ts
|
|
22
|
+
function branches(rest) {
|
|
23
|
+
return { protectBranch: (repo, branch, payload) => rest.request(`${repoBase(repo)}/branches/${encodeURIComponent(branch)}/protection`, {
|
|
24
|
+
method: "PUT",
|
|
25
|
+
body: payload
|
|
26
|
+
}) };
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/environments/environments.ts
|
|
30
|
+
function environments(rest) {
|
|
31
|
+
return {
|
|
32
|
+
listEnvironments: (repo) => rest.request(`${repoBase(repo)}/environments`).then((r) => r.environments),
|
|
33
|
+
upsertEnvironment: (repo, name, body) => rest.request(`${repoBase(repo)}/environments/${encodeURIComponent(name)}`, {
|
|
34
|
+
method: "PUT",
|
|
35
|
+
body
|
|
36
|
+
}),
|
|
37
|
+
deleteEnvironment: (repo, name) => rest.request(`${repoBase(repo)}/environments/${encodeURIComponent(name)}`, {
|
|
38
|
+
method: "DELETE",
|
|
39
|
+
expectNoContent: true
|
|
40
|
+
})
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/git/git.ts
|
|
45
|
+
function git(rest) {
|
|
46
|
+
return {
|
|
47
|
+
getRef: (repo, branch) => rest.request(`${repoBase(repo)}/git/ref/heads/${branch}`),
|
|
48
|
+
getCommit: (repo, sha) => rest.request(`${repoBase(repo)}/git/commits/${sha}`),
|
|
49
|
+
getTree: (repo, sha, recursive = false) => rest.request(`${repoBase(repo)}/git/trees/${sha}${recursive ? "?recursive=1" : ""}`),
|
|
50
|
+
getContents: (repo, path) => rest.request(`${repoBase(repo)}/contents/${path}`),
|
|
51
|
+
createBlob: (repo, content, encoding = "utf-8") => rest.request(`${repoBase(repo)}/git/blobs`, {
|
|
52
|
+
method: "POST",
|
|
53
|
+
body: {
|
|
54
|
+
content,
|
|
55
|
+
encoding
|
|
56
|
+
}
|
|
57
|
+
}),
|
|
58
|
+
createTree: (repo, tree, baseTree) => rest.request(`${repoBase(repo)}/git/trees`, {
|
|
59
|
+
method: "POST",
|
|
60
|
+
body: {
|
|
61
|
+
base_tree: baseTree,
|
|
62
|
+
tree
|
|
63
|
+
}
|
|
64
|
+
}),
|
|
65
|
+
createCommit: (repo, message, tree, parents) => rest.request(`${repoBase(repo)}/git/commits`, {
|
|
66
|
+
method: "POST",
|
|
67
|
+
body: {
|
|
68
|
+
message,
|
|
69
|
+
tree,
|
|
70
|
+
parents
|
|
71
|
+
}
|
|
72
|
+
}),
|
|
73
|
+
createRef: (repo, ref, sha) => rest.request(`${repoBase(repo)}/git/refs`, {
|
|
74
|
+
method: "POST",
|
|
75
|
+
body: {
|
|
76
|
+
ref,
|
|
77
|
+
sha
|
|
78
|
+
}
|
|
79
|
+
}),
|
|
80
|
+
updateRef: (repo, ref, sha, force = false) => rest.request(`${repoBase(repo)}/git/refs/${ref}`, {
|
|
81
|
+
method: "PATCH",
|
|
82
|
+
body: {
|
|
83
|
+
sha,
|
|
84
|
+
force
|
|
85
|
+
}
|
|
86
|
+
}),
|
|
87
|
+
createPull: (repo, input) => rest.request(`${repoBase(repo)}/pulls`, {
|
|
88
|
+
method: "POST",
|
|
89
|
+
body: input
|
|
90
|
+
})
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
//#endregion
|
|
94
|
+
//#region src/issues/issues.ts
|
|
95
|
+
function issues(rest) {
|
|
96
|
+
return {
|
|
97
|
+
listIssues: (repo, params = {}) => {
|
|
98
|
+
const qs = new URLSearchParams();
|
|
99
|
+
if (params.state) qs.set("state", params.state);
|
|
100
|
+
if (params.sort) qs.set("sort", params.sort);
|
|
101
|
+
if (params.direction) qs.set("direction", params.direction);
|
|
102
|
+
if (params.per_page) qs.set("per_page", String(params.per_page));
|
|
103
|
+
if (params.filter) qs.set("filter", params.filter);
|
|
104
|
+
if (params.assignee) qs.set("assignee", params.assignee);
|
|
105
|
+
const q = qs.toString();
|
|
106
|
+
const path = q ? `${repoBase(repo)}/issues?${q}` : `${repoBase(repo)}/issues`;
|
|
107
|
+
return rest.request(path);
|
|
108
|
+
},
|
|
109
|
+
getIssue: (repo, number) => rest.request(`${repoBase(repo)}/issues/${number}`),
|
|
110
|
+
createIssue: (repo, body) => rest.request(`${repoBase(repo)}/issues`, {
|
|
111
|
+
method: "POST",
|
|
112
|
+
body
|
|
113
|
+
}),
|
|
114
|
+
updateIssue: (repo, number, body) => rest.request(`${repoBase(repo)}/issues/${number}`, {
|
|
115
|
+
method: "PATCH",
|
|
116
|
+
body
|
|
117
|
+
}),
|
|
118
|
+
addLabels: (repo, number, labels) => rest.request(`${repoBase(repo)}/issues/${number}/labels`, {
|
|
119
|
+
method: "POST",
|
|
120
|
+
body: { labels }
|
|
121
|
+
}),
|
|
122
|
+
removeLabel: (repo, number, label) => rest.request(`${repoBase(repo)}/issues/${number}/labels/${encodeURIComponent(label)}`, { method: "DELETE" }),
|
|
123
|
+
createComment: (repo, number, body) => rest.request(`${repoBase(repo)}/issues/${number}/comments`, {
|
|
124
|
+
method: "POST",
|
|
125
|
+
body: { body }
|
|
126
|
+
}),
|
|
127
|
+
listMilestones: (repo, params = {}) => {
|
|
128
|
+
const qs = new URLSearchParams({ per_page: "100" });
|
|
129
|
+
if (params.state) qs.set("state", params.state);
|
|
130
|
+
return rest.request(`${repoBase(repo)}/milestones?${qs.toString()}`);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
//#endregion
|
|
135
|
+
//#region src/labels/labels.ts
|
|
136
|
+
function labels(rest) {
|
|
137
|
+
return {
|
|
138
|
+
listLabels: (repo) => rest.request(`${repoBase(repo)}/labels?per_page=100`),
|
|
139
|
+
createLabel: (repo, body) => rest.request(`${repoBase(repo)}/labels`, {
|
|
140
|
+
method: "POST",
|
|
141
|
+
body
|
|
142
|
+
}),
|
|
143
|
+
updateLabel: (repo, name, body) => rest.request(`${repoBase(repo)}/labels/${encodeURIComponent(name)}`, {
|
|
144
|
+
method: "PATCH",
|
|
145
|
+
body
|
|
146
|
+
}),
|
|
147
|
+
deleteLabel: (repo, name) => rest.request(`${repoBase(repo)}/labels/${encodeURIComponent(name)}`, { method: "DELETE" })
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
//#endregion
|
|
151
|
+
//#region src/properties/properties.ts
|
|
152
|
+
function properties(rest) {
|
|
153
|
+
return { setProperties: (repo, values) => {
|
|
154
|
+
const propertyList = Object.entries(values).map(([property_name, value]) => ({
|
|
155
|
+
property_name,
|
|
156
|
+
value
|
|
157
|
+
}));
|
|
158
|
+
return rest.request(`${repoBase(repo)}/properties/values`, {
|
|
159
|
+
method: "PATCH",
|
|
160
|
+
body: { properties: propertyList }
|
|
161
|
+
});
|
|
162
|
+
} };
|
|
163
|
+
}
|
|
164
|
+
//#endregion
|
|
165
|
+
//#region src/repos/repos.ts
|
|
166
|
+
function repos(rest) {
|
|
167
|
+
return {
|
|
168
|
+
getRepo: (repo) => rest.request(repoBase(repo)),
|
|
169
|
+
updateRepo: (repo, settings) => rest.request(repoBase(repo), {
|
|
170
|
+
method: "PATCH",
|
|
171
|
+
body: settings
|
|
172
|
+
}),
|
|
173
|
+
getContents: (repo, path) => rest.request(`${repoBase(repo)}/contents/${path}`)
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
//#endregion
|
|
177
|
+
//#region src/rulesets/rulesets.ts
|
|
178
|
+
function rulesets(rest) {
|
|
179
|
+
return {
|
|
180
|
+
listRulesets: (repo) => rest.request(`${repoBase(repo)}/rulesets`),
|
|
181
|
+
createRuleset: (repo, payload) => rest.request(`${repoBase(repo)}/rulesets`, {
|
|
182
|
+
method: "POST",
|
|
183
|
+
body: payload
|
|
184
|
+
}),
|
|
185
|
+
updateRuleset: (repo, id, payload) => rest.request(`${repoBase(repo)}/rulesets/${id}`, {
|
|
186
|
+
method: "PUT",
|
|
187
|
+
body: payload
|
|
188
|
+
})
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
//#endregion
|
|
192
|
+
//#region src/secrets/secrets.ts
|
|
193
|
+
function scopeBase(repo, scope) {
|
|
194
|
+
switch (scope.kind) {
|
|
195
|
+
case "repo": return `${repoBase(repo)}/actions`;
|
|
196
|
+
case "environment": return `${repoBase(repo)}/environments/${scope.name}`;
|
|
197
|
+
case "organization": return `/orgs/${scope.org}/actions`;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
function secrets(rest) {
|
|
201
|
+
return {
|
|
202
|
+
listSecrets: (repo, scope) => rest.request(`${scopeBase(repo, scope)}/secrets`).then((r) => r.secrets.map((s) => s.name)),
|
|
203
|
+
getPublicKey: (repo, scope) => rest.request(`${scopeBase(repo, scope)}/secrets/public-key`),
|
|
204
|
+
putSecret: (repo, scope, name, body) => rest.request(`${scopeBase(repo, scope)}/secrets/${name}`, {
|
|
205
|
+
method: "PUT",
|
|
206
|
+
body,
|
|
207
|
+
expectNoContent: true
|
|
208
|
+
}),
|
|
209
|
+
deleteSecret: (repo, scope, name) => rest.request(`${scopeBase(repo, scope)}/secrets/${name}`, {
|
|
210
|
+
method: "DELETE",
|
|
211
|
+
expectNoContent: true
|
|
212
|
+
})
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
//#endregion
|
|
216
|
+
//#region src/security/security.ts
|
|
217
|
+
function security(rest) {
|
|
218
|
+
return {
|
|
219
|
+
enableVulnerabilityAlerts: (repo) => rest.request(`${repoBase(repo)}/vulnerability-alerts`, {
|
|
220
|
+
method: "PUT",
|
|
221
|
+
expectNoContent: true
|
|
222
|
+
}),
|
|
223
|
+
enableAutomatedSecurityFixes: (repo) => rest.request(`${repoBase(repo)}/automated-security-fixes`, {
|
|
224
|
+
method: "PUT",
|
|
225
|
+
expectNoContent: true
|
|
226
|
+
}),
|
|
227
|
+
enableSecretScanning: (repo) => rest.request(repoBase(repo), {
|
|
228
|
+
method: "PATCH",
|
|
229
|
+
body: { security_and_analysis: {
|
|
230
|
+
secret_scanning: { status: "enabled" },
|
|
231
|
+
secret_scanning_push_protection: { status: "enabled" },
|
|
232
|
+
secret_scanning_validity_checks: { status: "enabled" },
|
|
233
|
+
secret_scanning_non_provider_patterns: { status: "enabled" }
|
|
234
|
+
} }
|
|
235
|
+
}),
|
|
236
|
+
enablePrivateVulnerabilityReporting: (repo) => rest.request(`${repoBase(repo)}/private-vulnerability-reporting`, {
|
|
237
|
+
method: "PUT",
|
|
238
|
+
expectNoContent: true
|
|
239
|
+
}),
|
|
240
|
+
enableDependencyGraph: (repo) => rest.request(repoBase(repo), {
|
|
241
|
+
method: "PATCH",
|
|
242
|
+
body: { security_and_analysis: {
|
|
243
|
+
dependency_graph: { status: "enabled" },
|
|
244
|
+
dependency_graph_autosubmit_action: { status: "enabled" }
|
|
245
|
+
} }
|
|
246
|
+
}),
|
|
247
|
+
enableCodeScanning: (repo) => rest.request(`${repoBase(repo)}/code-scanning/default-setup`, {
|
|
248
|
+
method: "PATCH",
|
|
249
|
+
body: {
|
|
250
|
+
state: "configured",
|
|
251
|
+
query_suite: "extended",
|
|
252
|
+
threat_model: "remote_and_local"
|
|
253
|
+
}
|
|
254
|
+
}),
|
|
255
|
+
disableDefaultCodeScanning: (repo) => rest.request(`${repoBase(repo)}/code-scanning/default-setup`, {
|
|
256
|
+
method: "PATCH",
|
|
257
|
+
body: { state: "not-configured" }
|
|
258
|
+
})
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
//#endregion
|
|
262
|
+
//#region src/topics/topics.ts
|
|
263
|
+
function topics(rest) {
|
|
264
|
+
return { setTopics: (repo, names) => rest.request(`${repoBase(repo)}/topics`, {
|
|
265
|
+
method: "PUT",
|
|
266
|
+
body: { names }
|
|
267
|
+
}) };
|
|
268
|
+
}
|
|
269
|
+
//#endregion
|
|
270
|
+
//#region src/user/user.ts
|
|
271
|
+
function user(rest) {
|
|
272
|
+
return { getCurrentUser: () => rest.request("/user") };
|
|
273
|
+
}
|
|
274
|
+
//#endregion
|
|
275
|
+
//#region src/workflows/workflows.ts
|
|
276
|
+
function workflows(rest) {
|
|
277
|
+
return {
|
|
278
|
+
listRuns: (repo, filter) => {
|
|
279
|
+
const params = new URLSearchParams();
|
|
280
|
+
if (filter?.branch) params.set("branch", filter.branch);
|
|
281
|
+
if (filter?.limit) params.set("per_page", String(filter.limit));
|
|
282
|
+
if (filter?.status) params.set("status", filter.status);
|
|
283
|
+
const qs = params.toString();
|
|
284
|
+
const path = qs ? `${repoBase(repo)}/actions/runs?${qs}` : `${repoBase(repo)}/actions/runs`;
|
|
285
|
+
return rest.request(path).then((r) => r.workflow_runs);
|
|
286
|
+
},
|
|
287
|
+
getRun: (repo, id) => rest.request(`${repoBase(repo)}/actions/runs/${id}`)
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
//#endregion
|
|
291
|
+
//#region src/index.ts
|
|
292
|
+
function createGitHubClient(opts) {
|
|
293
|
+
const rest = createGitHubRestClient(opts);
|
|
294
|
+
return {
|
|
295
|
+
branches: branches(rest),
|
|
296
|
+
environments: environments(rest),
|
|
297
|
+
git: git(rest),
|
|
298
|
+
issues: issues(rest),
|
|
299
|
+
labels: labels(rest),
|
|
300
|
+
properties: properties(rest),
|
|
301
|
+
repos: repos(rest),
|
|
302
|
+
rulesets: rulesets(rest),
|
|
303
|
+
secrets: secrets(rest),
|
|
304
|
+
security: security(rest),
|
|
305
|
+
topics: topics(rest),
|
|
306
|
+
user: user(rest),
|
|
307
|
+
workflows: workflows(rest)
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
//#endregion
|
|
311
|
+
export { createGitHubClient };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theholocron/github-client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "A TypeScript client for the GitHub REST API",
|
|
5
5
|
"homepage": "https://github.com/theholocron/clients/tree/main/packages/github-client#readme",
|
|
6
6
|
"bugs": "https://github.com/theholocron/clients/issues",
|