1+ import { Inject , Injectable } from "@nestjs/common" ;
2+ import { Octokit } from "@octokit/rest" ;
13import { ReleasePublishedEvent } from "@octokit/webhooks-types" ;
24import { HandlerFunction } from "@octokit/webhooks/dist-types/types" ;
35import { CreateEntryService } from "../domain/create-entry.js" ;
@@ -10,24 +12,26 @@ import {
1012 RulesetRepository ,
1113} from "../domain/ruleset-repository.js" ;
1214import { User } from "../domain/user.js" ;
13- import { EmailClient } from "../infrastructure/email.js" ;
14- import { GitClient } from "../infrastructure/git.js" ;
1515import { GitHubClient } from "../infrastructure/github.js" ;
16- import { SecretsClient } from "../infrastructure/secrets.js" ;
1716import { NotificationsService } from "./notifications.js" ;
18- import {
19- createAppAuthorizedOctokit ,
20- createBotAppAuthorizedOctokit ,
21- } from "./octokit.js" ;
2217
2318interface PublishAttempt {
2419 readonly successful : boolean ;
2520 readonly bcrFork : Repository ;
2621 readonly error ?: Error ;
2722}
2823
24+ @Injectable ( )
2925export class ReleaseEventHandler {
30- constructor ( private readonly secretsClient : SecretsClient ) { }
26+ constructor (
27+ @Inject ( "rulesetRepoGitHubClient" )
28+ private rulesetRepoGitHubClient : GitHubClient ,
29+ @Inject ( "appOctokit" ) private appOctokit : Octokit ,
30+ private readonly findRegistryForkService : FindRegistryForkService ,
31+ private readonly createEntryService : CreateEntryService ,
32+ private readonly publishEntryService : PublishEntryService ,
33+ private readonly notificationsService : NotificationsService
34+ ) { }
3135
3236 public readonly handle : HandlerFunction < "release.published" , unknown > =
3337 async ( event ) => {
@@ -36,41 +40,8 @@ export class ReleaseEventHandler {
3640 process . env . BAZEL_CENTRAL_REGISTRY
3741 ) ;
3842
39- // The "app" refers to the public facing GitHub app installed to users'
40- // ruleset repos and BCR Forks that creates and pushes the entry to the
41- // fork. The "bot app" refers to the private app only installed to the
42- // canonical BCR which has reduced permissions and only opens PRs.
43- const appOctokit = await createAppAuthorizedOctokit ( this . secretsClient ) ;
44- const rulesetGitHubClient = await GitHubClient . forRepoInstallation (
45- appOctokit ,
46- repository ,
47- event . payload . installation . id
48- ) ;
49-
50- const botAppOctokit = await createBotAppAuthorizedOctokit (
51- this . secretsClient
52- ) ;
53- const bcrGitHubClient = await GitHubClient . forRepoInstallation (
54- botAppOctokit ,
55- bcr
56- ) ;
57-
58- const gitClient = new GitClient ( ) ;
59- Repository . gitClient = gitClient ;
60-
61- const emailClient = new EmailClient ( ) ;
62- const findRegistryForkService = new FindRegistryForkService (
63- rulesetGitHubClient
64- ) ;
65- const publishEntryService = new PublishEntryService ( bcrGitHubClient ) ;
66- const notificationsService = new NotificationsService (
67- emailClient ,
68- this . secretsClient ,
69- rulesetGitHubClient
70- ) ;
71-
7243 const repoCanonicalName = `${ event . payload . repository . owner . login } /${ event . payload . repository . name } ` ;
73- let releaser = await rulesetGitHubClient . getRepoUser (
44+ let releaser = await this . rulesetRepoGitHubClient . getRepoUser (
7445 event . payload . sender . login ,
7546 repository
7647 ) ;
@@ -82,8 +53,7 @@ export class ReleaseEventHandler {
8253 const createRepoResult = await this . validateRulesetRepoOrNotifyFailure (
8354 repository ,
8455 tag ,
85- releaser ,
86- notificationsService
56+ releaser
8757 ) ;
8858 if ( ! createRepoResult . successful ) {
8959 return ;
@@ -93,18 +63,14 @@ export class ReleaseEventHandler {
9363
9464 console . log ( `Release author: ${ releaser . username } ` ) ;
9565
96- releaser = await this . overrideReleaser (
97- releaser ,
98- rulesetRepo ,
99- rulesetGitHubClient
100- ) ;
66+ releaser = await this . overrideReleaser ( releaser , rulesetRepo ) ;
10167
10268 console . log (
10369 `Release published: ${ rulesetRepo . canonicalName } @${ tag } by @${ releaser . username } `
10470 ) ;
10571
10672 const candidateBcrForks =
107- await findRegistryForkService . findCandidateForks (
73+ await this . findRegistryForkService . findCandidateForks (
10874 rulesetRepo ,
10975 releaser
11076 ) ;
@@ -122,14 +88,9 @@ export class ReleaseEventHandler {
12288
12389 for ( let bcrFork of candidateBcrForks ) {
12490 const forkGitHubClient = await GitHubClient . forRepoInstallation (
125- appOctokit ,
91+ this . appOctokit ,
12692 bcrFork
12793 ) ;
128- const createEntryService = new CreateEntryService (
129- gitClient ,
130- forkGitHubClient ,
131- bcrGitHubClient
132- ) ;
13394
13495 const attempt = await this . attemptPublish (
13596 rulesetRepo ,
@@ -139,8 +100,7 @@ export class ReleaseEventHandler {
139100 moduleRoot ,
140101 releaser ,
141102 releaseUrl ,
142- createEntryService ,
143- publishEntryService
103+ forkGitHubClient
144104 ) ;
145105 attempts . push ( attempt ) ;
146106
@@ -152,7 +112,7 @@ export class ReleaseEventHandler {
152112
153113 // Send out error notifications if none of the attempts succeeded
154114 if ( ! attempts . some ( ( a ) => a . successful ) ) {
155- await notificationsService . notifyError (
115+ await this . notificationsService . notifyError (
156116 releaser ,
157117 rulesetRepo . metadataTemplate ( moduleRoot ) . maintainers ,
158118 rulesetRepo ,
@@ -165,7 +125,7 @@ export class ReleaseEventHandler {
165125 // Handle any other unexpected errors
166126 console . log ( error ) ;
167127
168- await notificationsService . notifyError (
128+ await this . notificationsService . notifyError (
169129 releaser ,
170130 [ ] ,
171131 Repository . fromCanonicalName ( repoCanonicalName ) ,
@@ -180,8 +140,7 @@ export class ReleaseEventHandler {
180140 private async validateRulesetRepoOrNotifyFailure (
181141 repository : Repository ,
182142 tag : string ,
183- releaser : User ,
184- notificationsService : NotificationsService
143+ releaser : User
185144 ) : Promise < { rulesetRepo ?: RulesetRepository ; successful : boolean } > {
186145 try {
187146 const rulesetRepo = await RulesetRepository . create (
@@ -217,7 +176,7 @@ export class ReleaseEventHandler {
217176 ) ;
218177 }
219178
220- await notificationsService . notifyError (
179+ await this . notificationsService . notifyError (
221180 releaser ,
222181 maintainers ,
223182 repository ,
@@ -240,32 +199,36 @@ export class ReleaseEventHandler {
240199 moduleRoot : string ,
241200 releaser : User ,
242201 releaseUrl : string ,
243- createEntryService : CreateEntryService ,
244- publishEntryService : PublishEntryService
202+ bcrForkGitHubClient : GitHubClient
245203 ) : Promise < PublishAttempt > {
246204 console . log ( `Attempting publish to fork ${ bcrFork . canonicalName } .` ) ;
247205
248206 try {
249- const { moduleName} = await createEntryService . createEntryFiles (
207+ const { moduleName } = await this . createEntryService . createEntryFiles (
250208 rulesetRepo ,
251209 bcr ,
252210 tag ,
253211 moduleRoot
254212 ) ;
255213
256- const branch = await createEntryService . commitEntryToNewBranch (
214+ const branch = await this . createEntryService . commitEntryToNewBranch (
257215 rulesetRepo ,
258216 bcr ,
259217 tag ,
260218 releaser
261219 ) ;
262- await createEntryService . pushEntryToFork ( bcrFork , bcr , branch ) ;
220+ await this . createEntryService . pushEntryToFork (
221+ bcrFork ,
222+ bcr ,
223+ branch ,
224+ bcrForkGitHubClient
225+ ) ;
263226
264227 console . log (
265228 `Pushed bcr entry for module '${ moduleRoot } ' to fork ${ bcrFork . canonicalName } on branch ${ branch } `
266229 ) ;
267230
268- await publishEntryService . sendRequest (
231+ await this . publishEntryService . sendRequest (
269232 tag ,
270233 bcrFork ,
271234 bcr ,
@@ -297,8 +260,7 @@ export class ReleaseEventHandler {
297260
298261 private async overrideReleaser (
299262 releaser : User ,
300- rulesetRepo : RulesetRepository ,
301- githubClient : GitHubClient
263+ rulesetRepo : RulesetRepository
302264 ) : Promise < User > {
303265 // Use the release author unless a fixedReleaser is configured
304266 if ( rulesetRepo . config . fixedReleaser ) {
@@ -307,7 +269,7 @@ export class ReleaseEventHandler {
307269 ) ;
308270
309271 // Fetch the releaser to get their name
310- const fixedReleaser = await githubClient . getRepoUser (
272+ const fixedReleaser = await this . rulesetRepoGitHubClient . getRepoUser (
311273 rulesetRepo . config . fixedReleaser . login ,
312274 rulesetRepo
313275 ) ;
0 commit comments