Skip to content

Commit

Permalink
Make assigned team member check case-insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akhmetov committed Nov 21, 2024
1 parent 647e7ca commit 9bcc29c
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 7 deletions.
22 changes: 15 additions & 7 deletions pkg/icassigner/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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, ""
}
68 changes: 68 additions & 0 deletions pkg/icassigner/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package icassigner
import (
"fmt"
"testing"

"github.com/google/go-github/github"
)

func TestFindTeam(t *testing.T) {
Expand Down Expand Up @@ -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)
}
})
}
}

0 comments on commit 9bcc29c

Please sign in to comment.