Skip to content

Commit

Permalink
Use MergedEvent to improve the merge commit detection (#503)
Browse files Browse the repository at this point in the history
Even though PR is in "MERGED" state, the mergeCommit field can be empty
in the GitHub API response. Use the MergedEvent to get the merge commit
hash to complement the detection.
  • Loading branch information
draftcode authored Jan 3, 2025
1 parent 97e844a commit 4a35b85
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion e2e_tests/mock_ghgql_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

const (
// These are ugly, but this is easy way to tell which query is being used.
prQuery = "query($after:String$baseRefName:String$first:Int!$headRefName:String$owner:String!$repo:String!$states:[PullRequestState!]){repository(owner: $owner, name: $repo){pullRequests(states: $states, headRefName: $headRefName, baseRefName: $baseRefName, first: $first, after: $after){nodes{id,number,headRefName,baseRefName,isDraft,permalink,state,title,body,mergeCommit{oid},timelineItems(last: 10, itemTypes: CLOSED_EVENT){nodes{... on ClosedEvent{closer{... on Commit{oid}}}}}},pageInfo{endCursor,hasNextPage,hasPreviousPage,startCursor}}}}"
prQuery = "query($after:String$baseRefName:String$first:Int!$headRefName:String$owner:String!$repo:String!$states:[PullRequestState!]){repository(owner: $owner, name: $repo){pullRequests(states: $states, headRefName: $headRefName, baseRefName: $baseRefName, first: $first, after: $after){nodes{id,number,headRefName,baseRefName,isDraft,permalink,state,title,body,mergeCommit{oid},timelineItems(last: 10, itemTypes: [CLOSED_EVENT, MERGED_EVENT]){nodes{... on ClosedEvent{closer{... on Commit{oid}}},... on MergedEvent{commit{oid}}}}},pageInfo{endCursor,hasNextPage,hasPreviousPage,startCursor}}}}"
)

func RunMockGitHubServer(t *testing.T) *mockGitHubServer {
Expand Down
22 changes: 18 additions & 4 deletions internal/gh/pullrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ type PullRequest struct {
} `graphql:"... on Commit"`
}
} `graphql:"... on ClosedEvent"`
MergedEvent struct {
Commit struct {
Oid string
}
} `graphql:"... on MergedEvent"`
}
} `graphql:"timelineItems(last: 10, itemTypes: CLOSED_EVENT)"`
} `graphql:"timelineItems(last: 10, itemTypes: [CLOSED_EVENT, MERGED_EVENT])"`
}

func (p *PullRequest) HeadBranchName() string {
Expand All @@ -49,10 +54,19 @@ func (p *PullRequest) BaseBranchName() string {
func (p *PullRequest) GetMergeCommit() string {
if p.State == githubv4.PullRequestStateOpen {
return ""
} else if p.State == githubv4.PullRequestStateMerged {
} else if p.State == githubv4.PullRequestStateMerged && p.PRIVATE_MergeCommit.Oid != "" {
return p.PRIVATE_MergeCommit.Oid
} else if p.State == githubv4.PullRequestStateClosed && len(p.PRIVATE_TimelineItems.Nodes) != 0 {
return p.PRIVATE_TimelineItems.Nodes[0].ClosedEvent.Closer.Commit.Oid
}
// The timeline is in chronological order, so we can iterate backwards to find the latest
// one.
for i := len(p.PRIVATE_TimelineItems.Nodes) - 1; i >= 0; i-- {
item := p.PRIVATE_TimelineItems.Nodes[i]
if item.ClosedEvent.Closer.Commit.Oid != "" {
return item.ClosedEvent.Closer.Commit.Oid
}
if item.MergedEvent.Commit.Oid != "" {
return item.MergedEvent.Commit.Oid
}
}
return ""
}
Expand Down

0 comments on commit 4a35b85

Please sign in to comment.