Show conflicts to parent branch

This commit is contained in:
2026-07-28 11:46:43 +02:00
parent 0e880fa9b0
commit f93c2c517a
8 changed files with 509 additions and 41 deletions

View File

@@ -26,16 +26,21 @@ type GitHubWriteService interface {
}
type GitHubClient struct {
endpoint string
token string
http *http.Client
endpoint string
token string
http *http.Client
conflicts conflictFileLoader
conflictMu sync.Mutex
conflictCache map[string]conflictFileResult
}
func NewGitHubClient(endpoint, token string) *GitHubClient {
return &GitHubClient{
endpoint: endpoint,
token: token,
http: &http.Client{Timeout: 20 * time.Second},
endpoint: endpoint,
token: token,
http: &http.Client{Timeout: 20 * time.Second},
conflicts: analyzeConflictFiles,
conflictCache: make(map[string]conflictFileResult),
}
}
@@ -272,6 +277,7 @@ func nullableCursor(cursor string) any {
const detailsQuery = `
query PullRequestDetails($owner: String!, $name: String!, $number: Int!) {
repository(owner: $owner, name: $name) {
url
viewerPermission
defaultBranchRef { name }
rulesets(first: 100, includeParents: true, targets: [BRANCH]) {
@@ -288,6 +294,7 @@ query PullRequestDetails($owner: String!, $name: String!, $number: Int!) {
baseRefName headRefName headRefOid
viewerCanUpdate viewerCanReact viewerCanSubscribe viewerCanEnableAutoMerge
baseRef {
target { ... on Commit { oid } }
branchProtectionRule {
requiresApprovingReviews requiredApprovingReviewCount
requiresStatusChecks requiresConversationResolution
@@ -586,6 +593,7 @@ type githubPullRequestDetails struct {
Author *githubActor
ViewerCanUpdate, ViewerCanReact, ViewerCanSubscribe, ViewerCanEnableAutoMerge bool
BaseRef *struct {
Target *struct{ OID string }
BranchProtectionRule *struct {
RequiresApprovingReviews, RequiresStatusChecks bool
RequiresConversationResolution, RequiresCodeOwnerReviews bool
@@ -885,6 +893,7 @@ func (c *GitHubClient) allCheckAnnotations(
func (c *GitHubClient) GetPullRequest(ctx context.Context, owner, name string, number int) (PRDetails, error) {
var data struct {
Repository *struct {
URL string
ViewerPermission string `json:"viewerPermission"`
DefaultBranchRef *struct{ Name string }
Rulesets struct{ Nodes []githubRuleset }
@@ -909,6 +918,8 @@ func (c *GitHubClient) GetPullRequest(ctx context.Context, owner, name string, n
reviewErr error
timelineErr error
checkErr error
conflictFiles []string
conflictFileErr error
wait sync.WaitGroup
)
wait.Add(4)
@@ -936,6 +947,19 @@ func (c *GitHubClient) GetPullRequest(ctx context.Context, owner, name string, n
checkNodes, checkErr = c.allCheckContexts(ctx, rollup.Contexts, rollup.ID)
}()
}
if node.Mergeable == "CONFLICTING" && c.conflicts != nil {
wait.Add(1)
go func() {
defer wait.Done()
baseOID := ""
if node.BaseRef != nil && node.BaseRef.Target != nil {
baseOID = node.BaseRef.Target.OID
}
conflictFiles, conflictFileErr = c.loadConflictFiles(
ctx, data.Repository.URL, number, node.BaseRefName, baseOID, node.HeadRefOID,
)
}()
}
wait.Wait()
for _, err := range []error{threadErr, conversationErr, reviewErr, timelineErr, checkErr} {
if err != nil {
@@ -951,7 +975,8 @@ func (c *GitHubClient) GetPullRequest(ctx context.Context, owner, name string, n
},
Body: node.Body, CreatedAt: node.CreatedAt, BaseRef: node.BaseRefName, HeadRef: node.HeadRefName,
HeadOID: node.HeadRefOID, Mergeable: node.Mergeable, MergeState: node.MergeStateStatus,
Additions: node.Additions, Deletions: node.Deletions, ChangedFiles: node.ChangedFiles,
ConflictFiles: conflictFiles,
Additions: node.Additions, Deletions: node.Deletions, ChangedFiles: node.ChangedFiles,
CommitCount: node.Commits.TotalCount, CommentCount: node.Comments.TotalCount,
CheckState: "NONE", ReviewDecision: node.ReviewDecision,
Permissions: ViewerPermissions{
@@ -960,6 +985,9 @@ func (c *GitHubClient) GetPullRequest(ctx context.Context, owner, name string, n
CanSubscribe: node.ViewerCanSubscribe, CanEnableMerge: node.ViewerCanEnableAutoMerge,
},
}
if conflictFileErr != nil {
details.ConflictFileError = conflictFileErr.Error()
}
if node.BaseRef != nil && node.BaseRef.BranchProtectionRule != nil {
rule := node.BaseRef.BranchProtectionRule
details.Requirements = MergeRequirements{