@@ -73,7 +73,7 @@ const mergeStrategyOpen = (status: typeof Status.Type, strategy: string) =>
7373const Id = TrimmedNonEmptyString . check ( Schema . isPattern ( / ^ [ A - Z a - z 0 - 9 _ - ] + $ / u) ) ;
7474const isId = Schema . is ( Id ) ;
7575const isApiError = Schema . is ( GitCafeApi . GitCafeApiError ) ;
76- const Principal = Schema . Struct ( { handle : TrimmedNonEmptyString } ) ;
76+ const Principal = Schema . Struct ( { actorId : TrimmedNonEmptyString , handle : TrimmedNonEmptyString } ) ;
7777const Written = Schema . Struct ( { id : Id } ) ;
7878const Versioned = Schema . Struct ( { version : NonNegativeInt } ) ;
7979const CommentDetail = Schema . Struct ( { id : Id , version : NonNegativeInt } ) ;
@@ -85,6 +85,7 @@ const Status = Schema.Struct({
8585 fastForward : Schema . NullOr ( Schema . Boolean ) ,
8686 strategies : Schema . Array ( Schema . String ) ,
8787 permitted : Schema . optional ( Schema . Boolean ) ,
88+ reasons : Schema . optional ( Schema . Array ( Schema . String ) ) ,
8889 blockers : Schema . optional (
8990 Schema . Array (
9091 Schema . Struct ( { blockedStrategies : Schema . optional ( Schema . Array ( Schema . String ) ) } ) ,
@@ -97,15 +98,12 @@ const Status = Schema.Struct({
9798 total : NonNegativeInt ,
9899 } ) ,
99100} ) ;
101+ /** A merge GitCafe accepted runs on afterwards: on GitHub for a synced pull request, or natively. */
102+ const MERGE_IN_FLIGHT = new Set ( [ "provider-merge-pending" , "native-operation-pending" ] ) ;
100103const Listed = Schema . Struct ( {
101104 items : Schema . Array ( Schema . Struct ( { id : TrimmedNonEmptyString } ) ) ,
102105 next : Schema . NullOr ( Schema . String ) ,
103106} ) ;
104- const FilterOptions = Schema . Struct ( {
105- actors : Schema . Array (
106- Schema . Struct ( { actorId : TrimmedNonEmptyString , handle : TrimmedNonEmptyString } ) ,
107- ) ,
108- } ) ;
109107const encodeActorIds = Schema . encodeSync ( Schema . fromJsonString ( Schema . Array ( Schema . String ) ) ) ;
110108const BranchCommit = Schema . Struct ( { oid : TrimmedNonEmptyString } ) ;
111109const Snapshot = {
@@ -136,6 +134,12 @@ const StackOutcome = Schema.Struct({ id: Schema.String, state: Schema.String });
136134const FAILED_STACK_STATES = new Set ( [ "failed" , "stopped" , "cancelled" , "reconciliation_required" ] ) ;
137135
138136type PullRef = ProviderRepositoryRef & { readonly number : number } ;
137+
138+ /** GitHub's merge counts as soon as GitCafe records it, ahead of the sync that reports it. */
139+ const settledMerge = ( pull : Json . GitCafePullDetail ) =>
140+ pull . state === "open" && pull . providerMerge != null
141+ ? { state : "merged" as const , mergedAt : pull . providerMerge . mergedAt }
142+ : { } ;
139143type Paged < A > = { readonly items : ReadonlyArray < A > ; readonly next : string | null } ;
140144
141145function stackHeadsCurrent (
@@ -542,14 +546,15 @@ export const make = Effect.gen(function* () {
542546 const involved = input . involvement === "authored" || input . involvement === "reviewing" ;
543547 let actorId : string | undefined ;
544548 if ( involved ) {
545- // A host narrows as far as it can: when the actor list can't be read, the listing comes
546- // back unnarrowed and the page's own involvement filter does the rest. A refused token
547- // or a rate limit still fails, since the service acts on those for the whole host.
548- const options = yield * read (
549+ // The viewer is the token's principal, whose actor also matches the pull requests GitHub
550+ // sync authored for its linked profile. When it can't be read, the listing comes back
551+ // unnarrowed and the page's own involvement filter does the rest. A refused token or a
552+ // rate limit still fails, since the service acts on those for the whole host.
553+ const principal = yield * read (
549554 "listChangeRequests" ,
550555 input ,
551- { path : ` ${ base } /filter-options` } ,
552- FilterOptions ,
556+ { path : "/auth/principal" } ,
557+ Principal ,
553558 ) . pipe (
554559 Effect . map ( Option . some ) ,
555560 Effect . catchTags ( {
@@ -559,11 +564,10 @@ export const make = Effect.gen(function* () {
559564 : Effect . succeed ( Option . none ( ) ) ,
560565 } ) ,
561566 ) ;
562- if ( Option . isSome ( options ) ) {
563- actorId = options . value . actors . find (
564- ( actor ) => actor . handle . toLowerCase ( ) === input . viewer . toLowerCase ( ) ,
565- ) ?. actorId ;
566- if ( actorId === undefined ) return { items : [ ] , truncated : false , continues : false } ;
567+ if ( Option . isSome ( principal ) ) {
568+ if ( principal . value . handle . toLowerCase ( ) !== input . viewer . toLowerCase ( ) )
569+ return { items : [ ] , truncated : false , continues : false } ;
570+ actorId = principal . value . actorId ;
567571 }
568572 }
569573 const limit = Math . max ( 1 , input . limit ) ;
@@ -624,6 +628,7 @@ export const make = Effect.gen(function* () {
624628 ...( changes === null ? { } : changes ) ,
625629 closedAt : pull . closedAt ,
626630 mergedAt : pull . mergedAt ,
631+ ...settledMerge ( pull ) ,
627632 } ;
628633 } ,
629634 ) ,
@@ -658,8 +663,10 @@ export const make = Effect.gen(function* () {
658663 ) ;
659664 const listed = { ...pull , reviewers : reviewers . items , labels : labels . items } ;
660665 const permissions = Json . toViewerPermissions ( pull ) ;
666+ const merging = status . merge . reasons ?. some ( ( reason ) => MERGE_IN_FLIGHT . has ( reason ) ) ?? false ;
661667 return {
662668 ...Json . toChangeRequest ( listed , input ) ,
669+ autoMergeEnabled : merging ,
663670 headSha : pull . headOid ,
664671 body : pull . description ?? "" ,
665672 ...( pull . sourceRepo === null
@@ -670,6 +677,7 @@ export const make = Effect.gen(function* () {
670677 changedFiles : changes ?. changedFiles ?? 0 ,
671678 closedAt : pull . closedAt ,
672679 mergedAt : pull . mergedAt ,
680+ ...settledMerge ( pull ) ,
673681 reviewers : Json . toReviewers ( listed , input . host ) ,
674682 checks : checks === null ? [ ] : Json . toChecks ( checks ) ,
675683 ...( status . checks . total === 0
@@ -700,7 +708,7 @@ export const make = Effect.gen(function* () {
700708 rebase : mergeStrategyOpen ( status , "rebase" ) ,
701709 } ,
702710 viewerPermissions :
703- status . merge . permitted === false
711+ status . merge . permitted === false || merging
704712 ? { ...permissions , actions : permissions . actions . filter ( ( a ) => a !== "merge" ) }
705713 : permissions ,
706714 } ;
@@ -720,7 +728,15 @@ export const make = Effect.gen(function* () {
720728 readPages ( operation , input , `${ path } /reviews` , Json . GitCafeReviews ) ,
721729 pull . headOid === null
722730 ? Effect . succeed ( { items : [ ] , next : null } )
723- : readPages ( operation , input , `${ path } /commits` , Json . GitCafeCommits ) ,
731+ : // A closed pull request whose branch is gone keeps its head, but git.cafe answers
732+ // 404 for commits it no longer has; the conversation still stands without them.
733+ readPages ( operation , input , `${ path } /commits` , Json . GitCafeCommits ) . pipe (
734+ Effect . catch ( ( error ) =>
735+ error . reason === "not-found"
736+ ? Effect . succeed ( { items : [ ] , next : null } )
737+ : Effect . fail ( error ) ,
738+ ) ,
739+ ) ,
724740 readPages ( operation , input , `${ path } /reactions/` , Json . GitCafeReactions ) . pipe (
725741 Effect . orElseSucceed ( ( ) => undefined ) ,
726742 ) ,
0 commit comments