diff --git a/pkg/icassigner/action.go b/pkg/icassigner/action.go index 2892563..7ca0cdd 100644 --- a/pkg/icassigner/action.go +++ b/pkg/icassigner/action.go @@ -60,13 +60,9 @@ func (a *Action) Run(ctx context.Context, event *github.IssuesEvent, labelsInput } // check if someone from the team is already assigned (skip in this case) - for _, m := range teamMembers { - for _, a := range event.Issue.Assignees { - if a.GetLogin() == m.Name { - log.Printf("Found assignee %q which is member of the matched team %q. Stopping\n", m.Name, teamName) - return nil - } - } + if assigned, teamMember := isTeamMemberAssigned(teamMembers, event.Issue.Assignees); assigned { + log.Printf("Found assignee %q which is member of the matched team %q. Stopping\n", teamMember, teamName) + return nil } // Log the known team member names. @@ -255,3 +251,15 @@ func convertLabels(labels []github.Label) []string { return labelStrings } + +func isTeamMemberAssigned(teamMembers []MemberConfig, assignees []*github.User) (bool, string) { + for _, m := range teamMembers { + for _, a := range assignees { + if strings.ToLower(a.GetLogin()) == strings.ToLower(m.Name) { + return true, m.Name + } + } + } + + return false, "" +} diff --git a/pkg/icassigner/action_test.go b/pkg/icassigner/action_test.go index 4707c77..4b6895a 100644 --- a/pkg/icassigner/action_test.go +++ b/pkg/icassigner/action_test.go @@ -19,6 +19,8 @@ package icassigner import ( "fmt" "testing" + + "github.com/google/go-github/github" ) func TestFindTeam(t *testing.T) { @@ -93,3 +95,69 @@ func TestFindTeam(t *testing.T) { }) } } + +func TestIsTeamMemberAssigned(t *testing.T) { + teamMembers := []MemberConfig{ + {Name: "Alice"}, + {Name: "Bob"}, + {Name: "Charlie"}, + } + + testCases := []struct { + name string + teamMembers []MemberConfig + assignees []*github.User + expectedResult bool + expectedName string + }{ + { + name: "No assignees", + teamMembers: teamMembers, + assignees: []*github.User{}, + expectedResult: false, + expectedName: "", + }, + { + name: "Assignee matches team member", + teamMembers: teamMembers, + assignees: []*github.User{{Login: github.String("Alice")}}, + expectedResult: true, + expectedName: "Alice", + }, + { + name: "Case-insensitive match", + teamMembers: teamMembers, + assignees: []*github.User{{Login: github.String("bob")}}, + expectedResult: true, + expectedName: "Bob", + }, + { + name: "No matching assignees", + teamMembers: teamMembers, + assignees: []*github.User{{Login: github.String("Unknown")}}, + expectedResult: false, + expectedName: "", + }, + { + name: "Multiple assignees, one matches", + teamMembers: teamMembers, + assignees: []*github.User{{Login: github.String("Unknown")}, {Login: github.String("Charlie")}}, + expectedResult: true, + expectedName: "Charlie", + }, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + result, name := isTeamMemberAssigned(testCase.teamMembers, testCase.assignees) + + if result != testCase.expectedResult { + t.Errorf("Expected result to be %v, but got %v", testCase.expectedResult, result) + } + + if name != testCase.expectedName { + t.Errorf("Expected name to be %q, but got %q", testCase.expectedName, name) + } + }) + } +}