-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRepositoryService.ts
More file actions
712 lines (678 loc) · 20.9 KB
/
Copy pathRepositoryService.ts
File metadata and controls
712 lines (678 loc) · 20.9 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
import { GithubClient } from "../../client/GithubClient";
import {
mapCreateRepositoryFromTemplateParams,
mapCreateRepositoryParams,
mapImmutableReleasesStatus,
mapRepositories,
mapRepository,
mapRepositoryActivities,
mapRepositoryTags,
mapTeams,
mapTransferRepositoryParams,
mapUpdateRepositoryParams,
} from "./repository.mapper";
import { mapContributors } from "../users/user.mapper";
import {
Repository,
CreateRepositoryParams,
UpdateRepositoryParams,
RepositoryActivity,
DependabotSecurityUpdatesStatus,
CodeownersError,
CodeownersErrorsResponse,
ImmutableReleasesStatus,
RepositoryLanguages,
Status,
RepositoryTag,
Team,
RepositoryTopicsResponse,
TransferRepositoryParams,
CreateRepositoryFromTemplateParams,
} from "./repository.types";
import {
RepositoryDTO,
RepositoryActivityDTO,
ImmutableReleasesStatusDTO,
RepositoryTagDTO,
TeamDTO,
} from "./repository.dto";
import { Contributor } from "../users/user.types";
import { ContributorDTO } from "../users/user.dto";
import { assertConfig } from "../../shared/utils/config.utils";
export class RepositoryService {
private readonly path: string;
private readonly orgPath: string;
private readonly authPath: string;
constructor(private readonly client: GithubClient) {
this.path = `/repos/${client.config.owner}/${client.config.repo}`;
this.orgPath = `/orgs/${client.config.org}/repos`;
this.authPath = "/user/repos";
}
/**
* List repositories for the configured organisation
*
* @returns Array of repositories
*
* @example
* ```ts
* const repository = await github.repositories.listForOrg();
* ```
*/
public async listForOrg(): Promise<Repository[]> {
assertConfig(this.client, ["org"]);
const response = await this.client.request<RepositoryDTO[]>(
this.orgPath,
);
return mapRepositories(response.data);
}
/**
* Create new repository for the configured organisation
*
* @param params Configuration for the repository to create
* @returns Data of created repository
*
* @example
* ```ts
* await github.repositories.createForOrg({
* name: 'new-org-repo',
* description: 'this project is very cool'
* });
* ```
*/
public async createForOrg(
params: CreateRepositoryParams,
): Promise<Repository> {
assertConfig(this.client, ["org"]);
const body = mapCreateRepositoryParams(params);
const response = await this.client.request<RepositoryDTO>(
this.orgPath,
{
method: "POST",
body: JSON.stringify(body),
},
);
return mapRepository(response.data);
}
/**
* Get the configured repository
*
* @returns Data of the repository
*
* @example
* ```ts
* const repo = await github.repositories.get();
* ```
*/
public async get(): Promise<Repository> {
assertConfig(this.client, ["owner", "repo"]);
const response = await this.client.request<RepositoryDTO>(this.path);
return mapRepository(response.data);
}
/**
* Update the configured repository
*
* @param params Configuration for the repository to update
* @returns Data of the updated repository
*
* @example
* ```ts
* github.repositories.update({
* name: 'new-repo-name',
* description: 'this description is now better'
* });
* ```
*/
public async update(params: UpdateRepositoryParams): Promise<Repository> {
assertConfig(this.client, ["owner", "repo"]);
const body = mapUpdateRepositoryParams(params);
const response = await this.client.request<RepositoryDTO>(this.path, {
method: "PATCH",
body: JSON.stringify(body),
});
return mapRepository(response.data);
}
/**
* Delete the configured repository
*
* @returns Boolean value that determines if repo was deleted
*
* @example
* ```ts
* github.repositories.delete();
* ```
*/
public async delete(): Promise<boolean> {
assertConfig(this.client, ["owner", "repo"]);
const response = await this.client.request<null>(this.path, {
method: "DELETE",
});
return response.status === 204;
}
/**
* List activities of the configured repository
*
* @returns Array of repository activities
*
* @example
* ```ts
* const activities = await github.repositories.listActivities();
* ```
*/
public async listActivities(): Promise<RepositoryActivity[]> {
assertConfig(this.client, ["owner", "repo"]);
const response = await this.client.request<RepositoryActivityDTO[]>(
`${this.path}/activity`,
);
return mapRepositoryActivities(response.data);
}
/**
* Get the Dependabot security updates status for the configured repository
*
* @returns The current Dependabot security updates status
*
* @example
* ```ts
* const securityUpdatesEnabled = await github.repositories.getDependabotSecurityUpdatesStatus();
* ```
*/
public async getDependabotSecurityUpdatesStatus(): Promise<DependabotSecurityUpdatesStatus> {
assertConfig(this.client, ["owner", "repo"]);
const response =
await this.client.request<DependabotSecurityUpdatesStatus>(
`${this.path}/automated-security-fixes`,
);
return response.data;
}
/**
* Enable Dependabot security updates for the configured repository
*
* @returns Boolean value that determines if security updates were enabled
*
* @example
* ```ts
* github.repositories.enableDependabotSecurityUpdates();
* ```
*/
public async enableDependabotSecurityUpdates(): Promise<boolean> {
assertConfig(this.client, ["owner", "repo"]);
const response = await this.client.request<null>(
`${this.path}/automated-security-fixes`,
{
method: "PUT",
},
);
return response.status === 204;
}
/**
* Disable Dependabot security updates for the configured repository
*
* @returns Boolean value that determines if security updates were disabled
*
* @example
* ```ts
* github.repositories.disableDependabotSecurityUpdates();
* ```
*/
public async disableDependabotSecurityUpdates(): Promise<boolean> {
assertConfig(this.client, ["owner", "repo"]);
const response = await this.client.request<null>(
`${this.path}/automated-security-fixes`,
{
method: "DELETE",
},
);
return response.status === 204;
}
/**
* List any syntax errors that are detected in the CODEOWNERS file,
* within the configured repository
*
* @returns Array of CODEOWNERS errors
*
* @example
* ```ts
* const codeownersErrors = await github.repositories.getCodeownersErrors();
* ```
*/
public async getCodeownersErrors(): Promise<CodeownersError[]> {
assertConfig(this.client, ["owner", "repo"]);
const response = await this.client.request<CodeownersErrorsResponse>(
`${this.path}/codeowners/errors`,
);
return response.data.errors;
}
/**
* Get all contributors of the configured repository
*
* @returns Array of contributors
*
* @example
* ```ts
* const contributors = await github.repositories.getContributors();
* ```
*/
public async getContributors(): Promise<Contributor[]> {
assertConfig(this.client, ["owner", "repo"]);
const response = await this.client.request<ContributorDTO[]>(
`${this.path}/contributors`,
);
return mapContributors(response.data);
}
/**
* Create a dispatch event for the configured repository
*
* @param eventType A custom webhook event name. Must be 100 characters or less
* @returns Boolean value that determines if dispatch event was created
*
* @example
* ```ts
* github.repositories.createDispatchEvent('on-demand-test');
* ```
*/
public async createDispatchEvent(eventType: string): Promise<boolean> {
assertConfig(this.client, ["owner", "repo"]);
const response = await this.client.request<null>(
`${this.path}/dispatches`,
{
method: "POST",
body: JSON.stringify({ event_type: eventType }),
},
);
return response.status === 204;
}
/**
* Check if immutable releases are enabled for the configured repository
*
* @returns The current immutable releases status
*
* @example
* ```ts
* const immutableReleasesStatus = await github.repositories.getImmutableReleasesStatus();
* ```
*/
public async getImmutableReleasesStatus(): Promise<ImmutableReleasesStatus> {
assertConfig(this.client, ["owner", "repo"]);
const response = await this.client.request<ImmutableReleasesStatusDTO>(
`${this.path}/immutable-releases`,
);
return mapImmutableReleasesStatus(response.data);
}
/**
* Enable immutable releases for the configured repository
*
* @returns Boolean value that determines if immutable releases were enabled
*
* @example
* ```ts
* github.repositories.enableImmutableReleases();
* ```
*/
public async enableImmutableReleases(): Promise<boolean> {
assertConfig(this.client, ["owner", "repo"]);
const response = await this.client.request<null>(
`${this.path}/immutable-releases`,
{
method: "PUT",
},
);
return response.status === 204;
}
/**
* Disable immutable releases for the configured repository
*
* @returns Boolean value that determines if immutable releases were disabled
*
* @example
* ```ts
* github.repositories.disableImmutableReleases();
* ```
*/
public async disableImmutableReleases(): Promise<boolean> {
assertConfig(this.client, ["owner", "repo"]);
const response = await this.client.request<null>(
`${this.path}/immutable-releases`,
{
method: "DELETE",
},
);
return response.status === 204;
}
/**
* List languages for the configured repository
*
* @returns Object containing all languages
*
* @example
* ```ts
* const languages = await github.repositories.listLanguages();
* ```
*/
public async listLanguages(): Promise<RepositoryLanguages> {
assertConfig(this.client, ["owner", "repo"]);
const response = await this.client.request<RepositoryLanguages>(
`${this.path}/languages`,
);
return response.data;
}
/**
* Check if private vulnerability reporting is enabled
* for the configured repository
*
* @returns Boolean that determines if private vulnerability reporting is enabled
*
* @example
* ```ts
* const privateVulnerabilityReporting = await github.repositories.getPrivateVulnerabilityReportingStatus();
* ```
*/
public async getPrivateVulnerabilityReportingStatus(): Promise<boolean> {
assertConfig(this.client, ["owner", "repo"]);
const response = await this.client.request<Status>(
`${this.path}/private-vulnerability-reporting`,
);
return response.data.enabled === true;
}
/**
* Enable private vulnerability reporting for the configured repository
*
* @returns Boolean that determines if private vulnerability reporting was enabled
*
* @example
* ```ts
* github.repositories.enablePrivateVulnerabilityReporting();
* ```
*/
public async enablePrivateVulnerabilityReporting(): Promise<boolean> {
assertConfig(this.client, ["owner", "repo"]);
const response = await this.client.request<null>(
`${this.path}/private-vulnerability-reporting`,
{
method: "PUT",
},
);
return response.status === 204;
}
/**
* Disable private vulnerability reporting for the configured repository
*
* @returns Boolean that determines if private vulnerability reporting was disabled
*
* @example
* ```ts
* github.repositories.disablePrivateVulnerabilityReporting();
* ```
*/
public async disablePrivateVulnerabilityReporting(): Promise<boolean> {
assertConfig(this.client, ["owner", "repo"]);
const response = await this.client.request(
`${this.path}/private-vulnerability-reporting`,
{
method: "DELETE",
},
);
return response.status === 204;
}
/**
* List all tags of the configured repository
*
* @returns Array of all repository tags
*
* @example
* ```ts
* const repositoryTags = await github.repositories.listTags();
* ```
*/
public async listTags(): Promise<RepositoryTag[]> {
assertConfig(this.client, ["owner", "repo"]);
const response = await this.client.request<RepositoryTagDTO[]>(
`${this.path}/tags`,
);
return mapRepositoryTags(response.data);
}
/**
* List all teams with access to the configured repository
*
* @returns Array of teams
*
* @example
* ```ts
* const teams = await github.repositories.listTeams();
* ```
*/
public async listTeams(): Promise<Team[]> {
assertConfig(this.client, ["owner", "repo"]);
const response = await this.client.request<TeamDTO[]>(
`${this.path}/teams`,
);
return mapTeams(response.data);
}
/**
* Get all topics of the configured repository
*
* @returns Array of topics
*
* @example
* ```ts
* const topics = await github.repositories.getTopics();
* ```
*/
public async getTopics(): Promise<string[]> {
assertConfig(this.client, ["owner", "repo"]);
const response = await this.client.request<RepositoryTopicsResponse>(
`${this.path}/topics`,
);
return response.data.names;
}
/**
* Replace all topics of the configured repository
*
* @param topics An array of topics to add to the repository.
* An empty array will clear all topics
* @returns Array of new topics
*
* @example
* ```ts
* github.repositories.replaceTopics(['api', 'github']);
* ```
*/
public async replaceTopics(topics: string[]): Promise<string[]> {
assertConfig(this.client, ["owner", "repo"]);
const response = await this.client.request<RepositoryTopicsResponse>(
`${this.path}/topics`,
{
method: "PUT",
body: JSON.stringify({ names: topics }),
},
);
return response.data.names;
}
/**
* Transfer the configured repository to a new owner
*
* @param params Repository transfer options
* @returns Data of the transfered repository
*
* @example
* ```ts
* github.repositories.transfer({
* newOwner: 'Bob123'
* });
* ```
*/
public async transfer(
params: TransferRepositoryParams,
): Promise<Repository> {
assertConfig(this.client, ["owner", "repo"]);
const body = mapTransferRepositoryParams(params);
const response = await this.client.request<RepositoryDTO>(
`${this.path}/transfer`,
{
method: "POST",
body: JSON.stringify(body),
},
);
return mapRepository(response.data);
}
/**
* Check if vulnerability alerts are enabled for the configured repository
*
* @returns Boolean value that determines if vulnerability alerts are enabled
*
* @example
* ```ts
* const vulnerabilityAlerts = await github.repositories.getVulnerabilityAlertsStatus();
* ```
*/
public async getVulnerabilityAlertsStatus(): Promise<boolean> {
assertConfig(this.client, ["owner", "repo"]);
const response = await this.client.request<null>(
`${this.path}/vulnerability-alerts`,
);
return response.status === 204;
}
/**
* Enable vulnerability alerts for the configured repository
*
* @returns Boolean value that determines if vulnerability alerts were enabled
*
* @example
* ```ts
* github.repositories.enableVulnerabilityAlerts();
* ```
*/
public async enableVulnerabilityAlerts(): Promise<boolean> {
assertConfig(this.client, ["owner", "repo"]);
const response = await this.client.request<null>(
`${this.path}/vulnerability-alerts`,
{
method: "PUT",
},
);
return response.status === 204;
}
/**
* Disable vulnerability alerts for the configured repository
*
* @returns Boolean that determines if vulnerability alerts were disabled
*
* @example
* ```ts
* github.repositories.disableVulnerabilityAlerts();
* ```
*/
public async disableVulnerabilityAlerts(): Promise<boolean> {
assertConfig(this.client, ["owner", "repo"]);
const response = await this.client.request(
`${this.path}/vulnerability-alerts`,
{
method: "DELETE",
},
);
return response.status === 204;
}
/**
* Create a repository using a template
*
* @param params Configuration for the new repository
* @returns Data of the new repository
*
* @example
* ```ts
* github.repositories.createFromTemplate({
* templateOwner: TEMPLATE_OWNER,
* templateRepo: TEMPLATE_REPO,
* owner: 'lujax-dev',
* options: {
* name: 'new-repo',
* description: 'new repo using template'
* }
* })
* ```
*/
public async createFromTemplate(
params: CreateRepositoryFromTemplateParams,
): Promise<Repository> {
const body = mapCreateRepositoryFromTemplateParams(params);
const response = await this.client.request<RepositoryDTO>(
`/repos/${params.templateOwner}/${params.templateRepo}/generate`,
{
method: "POST",
body: JSON.stringify(body),
},
);
return mapRepository(response.data);
}
/**
* Lists all public repositories in the order that they were created
*
* @returns Array of public repositories
*
* @example
* ```ts
* const repositories = await github.repositories.listPublic();
* ```
*/
public async listPublic(): Promise<Repository[]> {
const response =
await this.client.request<RepositoryDTO[]>("/repositories");
return mapRepositories(response.data);
}
/**
* List repositories for the authenticated user
*
* @returns List of users repositories
*
* @example
* ```ts
* const repos = await github.repositories.list();
* ```
*/
public async list(): Promise<Repository[]> {
const repsonse = await this.client.request<RepositoryDTO[]>(
this.authPath,
);
return mapRepositories(repsonse.data);
}
/**
* Create a repository for the authenticated user
*
* @param params Configuration for the new repository
* @returns Data of created repository
*
* @example
* ```ts
* github.repositories.create({
* name: 'new-repo',
* description: 'this repo is cool'
* });
* ```
*/
public async create(params: CreateRepositoryParams): Promise<Repository> {
const body = mapCreateRepositoryParams(params);
const response = await this.client.request<RepositoryDTO>(
this.authPath,
{
method: "POST",
body: JSON.stringify(body),
},
);
return mapRepository(response.data);
}
/**
* List repositories for a user by their username
*
* @param username Username of the account to list repositories for
* @returns Array of repositories
*
* @example
* ```ts
* const repos = await github.repositories.listByUsername('LewieJ08');
* ```
*/
public async listByUsername(username: string): Promise<Repository[]> {
const response = await this.client.request<RepositoryDTO[]>(
`/users/${username}/repos`,
);
return mapRepositories(response.data);
}
}