@@ -31,15 +31,15 @@ const CAPABILITIES: PullRequestCapabilities = {
3131 stacks : true ,
3232 stackActions : true ,
3333 reactions : true ,
34- labels : false ,
34+ labels : true ,
3535 // Line comments go through GitCafe's review drafts, which arrive with revision fencing.
3636 review : {
3737 inlineComment : false ,
3838 reply : true ,
3939 resolve : true ,
4040 verdicts : [ "approve" , "request-changes" , "comment" ] ,
4141 } ,
42- reviewers : { request : false , listCandidates : false } ,
42+ reviewers : { request : true , listCandidates : true } ,
4343 edit : { changeRequest : true , comment : true } ,
4444} ;
4545
@@ -93,6 +93,10 @@ const Status = Schema.Struct({
9393 total : NonNegativeInt ,
9494 } ) ,
9595} ) ;
96+ const Listed = Schema . Struct ( {
97+ items : Schema . Array ( Schema . Struct ( { id : TrimmedNonEmptyString } ) ) ,
98+ next : Schema . NullOr ( Schema . String ) ,
99+ } ) ;
96100const FilterOptions = Schema . Struct ( {
97101 actors : Schema . Array (
98102 Schema . Struct ( { actorId : TrimmedNonEmptyString , handle : TrimmedNonEmptyString } ) ,
@@ -254,6 +258,39 @@ export const make = Effect.gen(function* () {
254258 ) ,
255259 ) ;
256260
261+ /**
262+ * GitCafe replaces a pull request's whole reviewer or label list, so one toggle reads the
263+ * list, changes it, and writes it back fenced on the pull request's version.
264+ */
265+ const toggleListed = Effect . fn ( "GitCafePullRequestProvider.toggleListed" ) ( function * (
266+ operation : string ,
267+ input : PullRef ,
268+ list : { readonly name : "reviewers" | "labels" ; readonly key : "actorIds" | "labelIds" } ,
269+ ids : ReadonlyArray < string > ,
270+ on : boolean ,
271+ ) {
272+ const path = yield * pullPath ( operation , input ) ;
273+ const [ pull , listed ] = yield * Effect . all (
274+ [ readPull ( operation , input ) , readPages ( operation , input , `${ path } /${ list . name } ` , Listed ) ] ,
275+ { concurrency : 2 } ,
276+ ) ;
277+ const current = listed . items . map ( ( item ) => item . id ) ;
278+ const next = on
279+ ? [ ...new Set ( [ ...current , ...ids ] ) ]
280+ : current . filter ( ( id ) => ! ids . includes ( id ) ) ;
281+ if ( next . length === current . length ) return ;
282+ yield * read (
283+ operation ,
284+ input ,
285+ {
286+ method : "PUT" ,
287+ path : `${ path } /${ list . name } ` ,
288+ body : { expectedVersion : pull . version , [ list . key ] : next } ,
289+ } ,
290+ Versioned ,
291+ ) ;
292+ } ) ;
293+
257294 /**
258295 * GitCafe's stack lists no heads, and they are what a land or restack is fenced on, so each
259296 * open layer's pull is read.
@@ -537,7 +574,8 @@ export const make = Effect.gen(function* () {
537574 const pull = yield * readPull ( "getChangeRequest" , input ) ;
538575 const path = yield * pullPath ( "getChangeRequest" , input ) ;
539576 const base = yield * repositoryPath ( "getChangeRequest" , input ) ;
540- const [ status , checks , changes ] = yield * Effect . all (
577+ // A single pull request read carries neither its reviewers nor its labels.
578+ const [ status , checks , changes , reviewers , labels ] = yield * Effect . all (
541579 [
542580 read ( "getChangeRequest" , input , { path : `${ path } /status` } , Status ) ,
543581 pull . headOid === null
@@ -551,12 +589,15 @@ export const make = Effect.gen(function* () {
551589 pull . headOid === null
552590 ? Effect . succeed ( null )
553591 : readLineStats ( input , pull . version ) . pipe ( Effect . orElseSucceed ( ( ) => null ) ) ,
592+ readPages ( "getChangeRequest" , input , `${ path } /reviewers` , Json . GitCafeActorEntries ) ,
593+ readPages ( "getChangeRequest" , input , `${ path } /labels` , Json . GitCafeLabels ) ,
554594 ] ,
555- { concurrency : 3 } ,
595+ { concurrency : 5 } ,
556596 ) ;
597+ const listed = { ...pull , reviewers : reviewers . items , labels : labels . items } ;
557598 const permissions = Json . toViewerPermissions ( pull ) ;
558599 return {
559- ...Json . toChangeRequest ( pull , input ) ,
600+ ...Json . toChangeRequest ( listed , input ) ,
560601 headSha : pull . headOid ,
561602 body : pull . description ?? "" ,
562603 ...( pull . sourceRepo === null
@@ -567,7 +608,7 @@ export const make = Effect.gen(function* () {
567608 changedFiles : changes ?. changedFiles ?? 0 ,
568609 closedAt : pull . closedAt ,
569610 mergedAt : pull . mergedAt ,
570- reviewers : Json . toReviewers ( pull , input . host ) ,
611+ reviewers : Json . toReviewers ( listed , input . host ) ,
571612 checks : checks === null ? [ ] : Json . toChecks ( checks ) ,
572613 ...( status . checks . total === 0
573614 ? { }
@@ -758,12 +799,73 @@ export const make = Effect.gen(function* () {
758799 Written ,
759800 ) ;
760801 } ) ,
761- listReviewerCandidates : ( ) =>
762- Effect . fail (
763- failure ( "listReviewerCandidates" , "GitCafe reviewer requests are not supported." ) ,
802+ listReviewerCandidates : Effect . fn ( "GitCafePullRequestProvider.listReviewerCandidates" ) (
803+ function * ( input ) {
804+ const operation = "listReviewerCandidates" ;
805+ const path = yield * pullPath ( operation , input ) ;
806+ const [ pull , candidates , reviewers ] = yield * Effect . all (
807+ [
808+ readPull ( operation , input ) ,
809+ readPages ( operation , input , `${ path } /reviewer-candidates` , Json . GitCafeActorEntries ) ,
810+ readPages ( operation , input , `${ path } /reviewers` , Listed ) ,
811+ ] ,
812+ { concurrency : 3 } ,
813+ ) ;
814+ return {
815+ candidates : Json . toReviewerCandidates ( candidates , {
816+ requested : new Set ( reviewers . items . map ( ( reviewer ) => reviewer . id ) ) ,
817+ authorActorId : pull . author . actorId ,
818+ host : input . host ,
819+ } ) ,
820+ truncated : candidates . next !== null ,
821+ } ;
822+ } ,
823+ ) ,
824+ setReviewerRequest : ( input ) =>
825+ toggleListed (
826+ "setReviewerRequest" ,
827+ input ,
828+ { name : "reviewers" , key : "actorIds" } ,
829+ input . reviewers . map ( ( reviewer ) => reviewer . id ) ,
830+ input . requested ,
764831 ) ,
765- setReviewerRequest : ( ) =>
766- Effect . fail ( failure ( "setReviewerRequest" , "GitCafe reviewer requests are not supported." ) ) ,
832+ listLabelCandidates : Effect . fn ( "GitCafePullRequestProvider.listLabelCandidates" ) (
833+ function * ( input ) {
834+ const operation = "listLabelCandidates" ;
835+ const base = yield * repositoryPath ( operation , input ) ;
836+ const [ labels , applied ] = yield * Effect . all (
837+ [
838+ readPages ( operation , input , `${ base } /labels` , Json . GitCafeLabels ) ,
839+ readPages ( operation , input , `${ base } /pulls/${ input . number } /labels` , Listed ) ,
840+ ] ,
841+ { concurrency : 2 } ,
842+ ) ;
843+ const appliedIds = new Set ( applied . items . map ( ( label ) => label . id ) ) ;
844+ return {
845+ candidates : labels . items . map ( ( label ) => ( {
846+ name : label . name ,
847+ color : label . color ,
848+ description : label . description ?? null ,
849+ isApplied : appliedIds . has ( label . id ) ,
850+ } ) ) ,
851+ truncated : labels . next !== null ,
852+ } ;
853+ } ,
854+ ) ,
855+ setLabels : Effect . fn ( "GitCafePullRequestProvider.setLabels" ) ( function * ( input ) {
856+ const base = yield * repositoryPath ( "setLabels" , input ) ;
857+ const labels = yield * readPages ( "setLabels" , input , `${ base } /labels` , Json . GitCafeLabels ) ;
858+ const selected = labels . items . filter ( ( label ) => input . labels . includes ( label . name ) ) ;
859+ if ( selected . length !== input . labels . length )
860+ return yield * failure ( "setLabels" , "One or more requested labels could not be found." ) ;
861+ yield * toggleListed (
862+ "setLabels" ,
863+ input ,
864+ { name : "labels" , key : "labelIds" } ,
865+ selected . map ( ( label ) => label . id ) ,
866+ input . applied ,
867+ ) ;
868+ } ) ,
767869 replyToThread : Effect . fn ( "GitCafePullRequestProvider.replyToThread" ) ( function * ( input ) {
768870 if ( ! isId ( input . threadId ) )
769871 return yield * failure ( "replyToThread" , "GitCafe thread ids are opaque slugs." ) ;
0 commit comments