Skip to content

Commit

Permalink
fix: don't panic when a PR branch is deleted (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
devcharmander authored Sep 12, 2023
1 parent 7820c22 commit fca98e1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
7 changes: 6 additions & 1 deletion internal/scm/github/pullrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ func convertPullRequest(pr *github.PullRequest) pullRequest {
}

func convertGraphQLPullRequest(pr graphqlPR) pullRequest {
combinedStatus := pr.Commits.Nodes[0].Commit.StatusCheckRollup.State
var combinedStatus *graphqlPullRequestState
nodes := pr.Commits.Nodes
if len(nodes) > 0 {
combinedStatus = nodes[0].Commit.StatusCheckRollup.State
}

status := scm.PullRequestStatusUnknown

if pr.Merged {
Expand Down
66 changes: 66 additions & 0 deletions internal/scm/github/pullrequest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package github

import (
"testing"

"github.com/lindell/multi-gitter/internal/scm"
"github.com/stretchr/testify/assert"
)

func Test_convertGraphQLPullRequest(t *testing.T) {
scenarios := []struct {
name string
pr graphqlPR
expected pullRequest
}{{
name: "should return status 'closed' when the PR branch is deleted",
pr: graphqlPR{
Number: 1,
HeadRefName: "dummy_branch",
Closed: true,
URL: "http://dummy.url",
Merged: false,
BaseRepository: struct {
Name string "json:\"name\""
Owner struct {
Login string "json:\"login\""
} "json:\"owner\""
}{
Name: "base_repo",
Owner: struct {
Login string "json:\"login\""
}{Login: "dummy_user"},
},
HeadRepository: struct {
Name string "json:\"name\""
Owner struct {
Login string "json:\"login\""
} "json:\"owner\""
}{
Name: "pr_owner",
Owner: struct {
Login string "json:\"login\""
}{Login: "dummy_owner"},
},
},
expected: pullRequest{
status: scm.PullRequestStatusClosed,
ownerName: "dummy_user",
repoName: "base_repo",
branchName: "dummy_branch",
prOwnerName: "dummy_owner",
prRepoName: "pr_owner",
number: 1,
guiURL: "http://dummy.url",
},
}}

for _, scenario := range scenarios {
t.Run(scenario.name, func(t *testing.T) {
got := convertGraphQLPullRequest(scenario.pr)
if got != scenario.expected {
assert.Equal(t, scenario.expected, got)
}
})
}
}

0 comments on commit fca98e1

Please sign in to comment.