@@ -136,7 +136,8 @@ it.effect("reads the newest Forgejo checks without repository or viewer requests
136136 } ) ;
137137 assert . strictEqual ( result . state , "open" ) ;
138138 assert . strictEqual ( result . checks [ 0 ] ?. status , "failure" ) ;
139- assert . deepStrictEqual ( paths , [
139+ assert . deepStrictEqual ( paths . toSorted ( ) , [
140+ "repos/acme/web/branches/main" ,
140141 "repos/acme/web/pulls/1" ,
141142 "repos/acme/web/statuses/head?sort=recentupdate&limit=50&page=1" ,
142143 "repos/acme/web/statuses/head?sort=recentupdate&limit=50&page=2" ,
@@ -146,16 +147,18 @@ it.effect("reads the newest Forgejo checks without repository or viewer requests
146147 Layer . mock ( ForgejoCli . ForgejoCli ) ( {
147148 api : ( input ) => {
148149 paths . push ( input . path ) ;
149- assert . match ( input . path , / ^ r e p o s \/ a c m e \/ w e b \/ ( p u l l s \/ 1 | s t a t u s e s \/ h e a d ) / ) ;
150+ assert . match ( input . path , / ^ r e p o s \/ a c m e \/ w e b \/ ( p u l l s \/ 1 | s t a t u s e s \/ h e a d | b r a n c h e s \/ m a i n ) / ) ;
150151 return Effect . succeed (
151152 processOutput (
152- input . path . endsWith ( "pulls/1" )
153- ? `{"number":1,"title":"Checks","body":"","html_url":"https://forgejo.test/acme/web/pulls/1", "user":null,"state":"open","merged":false,
153+ input . path . endsWith ( "branches/main" )
154+ ? `{"name":"main","protected":false,"enable_status_check":false,"status_check_contexts":[]}`
155+ : input . path . endsWith ( "pulls/1" )
156+ ? `{"number":1,"title":"Checks","body":"","html_url":"https://forgejo.test/acme/web/pulls/1", "user":null,"state":"open","merged":false,
154157 "head":{"ref":"feature","sha":"head","repo":null},"base":{"ref":"main","sha":"base","repo":null},
155158 "created_at":"2026-09-16T00:00:00Z","updated_at":"2026-09-16T00:00:00Z","closed_at":null,"merged_at":null,"labels":[]}`
156- : input . path . endsWith ( "page=1" )
157- ? `[{"context":"build","status":"failure","description":null,"target_url":null,"updated_at":"2026-09-16T00:00:00.500Z"},{"context":"build","status":"success","description":null,"target_url":null,"updated_at":"2026-09-16T00:00:00Z"}]`
158- : "[]" ,
159+ : input . path . endsWith ( "page=1" )
160+ ? `[{"context":"build","status":"failure","description":null,"target_url":null,"updated_at":"2026-09-16T00:00:00.500Z"},{"context":"build","status":"success","description":null,"target_url":null,"updated_at":"2026-09-16T00:00:00Z"}]`
161+ : "[]" ,
159162 ) ,
160163 ) ;
161164 } ,
@@ -164,6 +167,164 @@ it.effect("reads the newest Forgejo checks without repository or viewer requests
164167 ) ;
165168} ) ;
166169
170+ const requiredChecksPullRequest = {
171+ number : 7 ,
172+ title : "Required checks" ,
173+ body : "" ,
174+ html_url : "https://forgejo.test/acme/web/pulls/7" ,
175+ user : { login : "maria" } ,
176+ state : "open" ,
177+ merged : false ,
178+ head : { ref : "feature" , sha : "head" , repo : null } ,
179+ base : { ref : "release/1.x" , sha : "base" , repo : null } ,
180+ created_at : "2026-10-12T00:00:00Z" ,
181+ updated_at : "2026-10-12T00:00:00Z" ,
182+ closed_at : null ,
183+ merged_at : null ,
184+ labels : [ ] ,
185+ } ;
186+ const requiredChecksStatuses = [
187+ [ "ci/required-build" , "success" ] ,
188+ [ "ci/optional-lint" , "failure" ] ,
189+ ] . map ( ( [ context , status ] ) => ( {
190+ context,
191+ status,
192+ description : null ,
193+ target_url : null ,
194+ updated_at : "2026-10-12T00:00:00Z" ,
195+ } ) ) ;
196+
197+ const requiredChecksInput = {
198+ cwd : "/repo" ,
199+ repository : "acme/web" ,
200+ host : "forgejo.test" ,
201+ number : 7 ,
202+ } ;
203+ const requiredChecksLayer = (
204+ branch : Effect . Effect < unknown , ForgejoCli . ForgejoCliError > ,
205+ statuses = requiredChecksStatuses ,
206+ ) =>
207+ Layer . mock ( ForgejoCli . ForgejoCli ) ( {
208+ api : ( input ) => {
209+ const [ path , query ] = input . path . split ( "?" ) ;
210+ const respond = ( response : unknown ) =>
211+ encodeJsonEffect ( response ) . pipe ( Effect . orDie , Effect . map ( processOutput ) ) ;
212+ switch ( path ) {
213+ case "repos/acme/web/pulls/7" :
214+ return respond ( requiredChecksPullRequest ) ;
215+ case "repos/acme/web/statuses/head" :
216+ return respond ( new URLSearchParams ( query ) . get ( "page" ) === "1" ? statuses : [ ] ) ;
217+ case "repos/acme/web/branches/release/1.x" :
218+ return Effect . flatMap ( branch , respond ) ;
219+ case "repos/acme/web" :
220+ return respond ( { full_name : "acme/web" , permissions : { push : true , admin : false } } ) ;
221+ case "user" :
222+ return respond ( { login : "maria" } ) ;
223+ default :
224+ return Effect . die ( `Unexpected Forgejo request: ${ input . path } ` ) ;
225+ }
226+ } ,
227+ } ) ;
228+
229+ it . effect . each ( [
230+ [ "ci/required-deploy" , true ] ,
231+ [ "test-*" , true ] ,
232+ [ "ci/[ab]" , false ] ,
233+ ] as const ) ( "keeps missing required pattern %s pending (supported: %s)" , ( [ missing , supported ] ) =>
234+ Effect . gen ( function * ( ) {
235+ const provider = yield * ForgejoPullRequestProvider . make ;
236+ const readChecks = provider . getChangeRequestChecks ;
237+ if ( readChecks === undefined ) return yield * Effect . die ( "checks read missing" ) ;
238+ const summary = yield * readChecks ( requiredChecksInput ) ;
239+ const detail = yield * provider . getChangeRequest ( requiredChecksInput ) ;
240+ for ( const checks of [ summary . checks , detail . checks ] ) {
241+ assert . deepStrictEqual (
242+ checks . map ( ( check ) => [ check . name , check . status , check . required ] ) ,
243+ [
244+ [ "ci/required-build" , "success" , supported ? true : undefined ] ,
245+ [ "ci/optional-lint" , supported ? "failure" : "success" , undefined ] ,
246+ [ missing , "pending" , supported ? true : undefined ] ,
247+ ] ,
248+ ) ;
249+ if ( ! supported ) {
250+ assert . strictEqual (
251+ checks . at ( - 1 ) ?. description ,
252+ "Required check pattern cannot be evaluated." ,
253+ ) ;
254+ }
255+ }
256+ } ) . pipe (
257+ Effect . provide (
258+ requiredChecksLayer (
259+ Effect . succeed ( {
260+ name : "release/1.x" ,
261+ protected : true ,
262+ enable_status_check : true ,
263+ status_check_contexts : [ "ci/required-*" , missing , missing ] ,
264+ } ) ,
265+ supported
266+ ? requiredChecksStatuses
267+ : requiredChecksStatuses . map ( ( status ) => ( { ...status , status : "success" } ) ) ,
268+ ) ,
269+ ) ,
270+ ) ,
271+ ) ;
272+
273+ it . effect . each ( [
274+ [ "older branch metadata" , Effect . succeed ( { name : "release/1.x" , protected : true } ) ] ,
275+ ...( [ "not-found" , "forbidden" ] as const ) . map (
276+ ( reason ) =>
277+ [
278+ reason ,
279+ Effect . fail (
280+ new ForgejoCli . ForgejoCliError ( {
281+ command : "fj" ,
282+ cwd : "/repo" ,
283+ reason,
284+ httpStatus : reason === "not-found" ? 404 : 403 ,
285+ detail : "Branch metadata unavailable." ,
286+ } ) ,
287+ ) ,
288+ ] as const ,
289+ ) ,
290+ ] as const ) ( "keeps all statuses gating with %s" , ( [ , branch ] ) =>
291+ Effect . gen ( function * ( ) {
292+ const provider = yield * ForgejoPullRequestProvider . make ;
293+ const readChecks = provider . getChangeRequestChecks ;
294+ if ( readChecks === undefined ) return yield * Effect . die ( "checks read missing" ) ;
295+ const summary = yield * readChecks ( requiredChecksInput ) ;
296+ const detail = yield * provider . getChangeRequest ( requiredChecksInput ) ;
297+ for ( const checks of [ summary . checks , detail . checks ] ) {
298+ assert . deepStrictEqual (
299+ checks . map ( ( check ) => [ check . name , check . status , check . required ] ) ,
300+ [
301+ [ "ci/required-build" , "success" , undefined ] ,
302+ [ "ci/optional-lint" , "failure" , undefined ] ,
303+ ] ,
304+ ) ;
305+ }
306+ } ) . pipe ( Effect . provide ( requiredChecksLayer ( branch ) ) ) ,
307+ ) ;
308+
309+ it . effect . each ( [
310+ [ "rate limit" , { reason : "rate-limit" , httpStatus : 429 } , "rate-limited" ] ,
311+ [ "authentication failure" , { reason : "authentication" , httpStatus : 401 } , "unauthenticated" ] ,
312+ [ "server failure" , { httpStatus : 503 } , "failed" ] ,
313+ [ "network failure" , { } , "failed" ] ,
314+ ] as const ) ( "propagates branch metadata %s" , ( [ detail , fields , reason ] ) => {
315+ const cause = new ForgejoCli . ForgejoCliError ( { command : "fj" , cwd : "/repo" , detail, ...fields } ) ;
316+ return Effect . gen ( function * ( ) {
317+ const provider = yield * ForgejoPullRequestProvider . make ;
318+ const readChecks = provider . getChangeRequestChecks ;
319+ if ( readChecks === undefined ) return yield * Effect . die ( "checks read missing" ) ;
320+ for ( const read of [ readChecks , provider . getChangeRequest ] ) {
321+ const error = yield * read ( requiredChecksInput ) . pipe ( Effect . flip ) ;
322+ assert . strictEqual ( error . reason , reason ) ;
323+ assert . strictEqual ( error . cause , cause ) ;
324+ }
325+ } ) . pipe ( Effect . provide ( requiredChecksLayer ( Effect . fail ( cause ) ) ) ) ;
326+ } ) ;
327+
167328it . effect ( "opens Forgejo Actions check links on the pull request's server" , ( ) =>
168329 Effect . gen ( function * ( ) {
169330 const provider = yield * ForgejoPullRequestProvider . make ;
@@ -194,31 +355,33 @@ it.effect("opens Forgejo Actions check links on the pull request's server", () =
194355 target_url,
195356 updated_at : "2026-10-11T00:00:00Z" ,
196357 } ) ;
197- const body = input . path . endsWith ( "pulls/1" )
198- ? {
199- number : 1 ,
200- title : "Checks" ,
201- body : "" ,
202- html_url : "https://forgejo.test/git/acme/web/pulls/1" ,
203- user : null ,
204- state : "open" ,
205- merged : false ,
206- head : { ref : "feature" , sha : "head" , repo : null } ,
207- base : { ref : "main" , sha : "base" , repo : null } ,
208- created_at : "2026-10-11T00:00:00Z" ,
209- updated_at : "2026-10-11T00:00:00Z" ,
210- closed_at : null ,
211- merged_at : null ,
212- labels : [ ] ,
213- }
214- : input . path . endsWith ( "page=1" )
215- ? [
216- // Forgejo includes any sub-path mount in the relative links it reports.
217- status ( "actions" , "/git/acme/web/actions/runs/7/jobs/2" ) ,
218- status ( "external" , "https://ci.example/build/9" ) ,
219- status ( "none" , null ) ,
220- ]
221- : [ ] ;
358+ const body = input . path . endsWith ( "branches/main" )
359+ ? { }
360+ : input . path . endsWith ( "pulls/1" )
361+ ? {
362+ number : 1 ,
363+ title : "Checks" ,
364+ body : "" ,
365+ html_url : "https://forgejo.test/git/acme/web/pulls/1" ,
366+ user : null ,
367+ state : "open" ,
368+ merged : false ,
369+ head : { ref : "feature" , sha : "head" , repo : null } ,
370+ base : { ref : "main" , sha : "base" , repo : null } ,
371+ created_at : "2026-10-11T00:00:00Z" ,
372+ updated_at : "2026-10-11T00:00:00Z" ,
373+ closed_at : null ,
374+ merged_at : null ,
375+ labels : [ ] ,
376+ }
377+ : input . path . endsWith ( "page=1" )
378+ ? [
379+ // Forgejo includes any sub-path mount in the relative links it reports.
380+ status ( "actions" , "/git/acme/web/actions/runs/7/jobs/2" ) ,
381+ status ( "external" , "https://ci.example/build/9" ) ,
382+ status ( "none" , null ) ,
383+ ]
384+ : [ ] ;
222385 return encodeJsonEffect ( body ) . pipe ( Effect . orDie , Effect . map ( processOutput ) ) ;
223386 } ,
224387 } ) ,
@@ -297,6 +460,9 @@ it.effect.each([
297460 case "repos/maria/project/statuses/head" :
298461 response = [ ] ;
299462 break ;
463+ case "repos/maria/project/branches/main" :
464+ response = { name : "main" , protected : false } ;
465+ break ;
300466 default :
301467 return Effect . die ( `Unexpected Forgejo request: ${ input . path } ` ) ;
302468 }
0 commit comments