Repository: srvaroa/labeler (go)

Issue:
[Bug] author-in-team isn't labeling when author is a member of the specified team
I have tested this in our repo, and believe I have an explanation of why this isn't working:

The [go github function](https://github.com/srvaroa/labeler/pull/150/files#diff-329f84be6a6b359043a3791d6d77cba74f98a673ff4ce35421b8234d90ed00e0R248) that is called, [GetOrgMembership](https://pkg.go.dev/github.com/google/go-github/v50/github#OrganizationsService.GetOrgMembership), uses the ["Get organization membership for a user"](https://docs.github.com/en/rest/orgs/members?apiVersion=2022-11-28#get-organization-membership-for-a-user) endpoint, which doesn't seem to contain any info about the teams within that org that the user is a member of. Also, we are passing a `team` string to the second argument, which expects an `org` string instead, leading to 404s always being returned in the logs from the action:

```
[Author is member of team] skip, GET https://api.github.com/orgs/{team}/memberships/{user}: 404 Not Found []
```

I think instead, you may need to use the [GetTeamMembershipBySlug](https://pkg.go.dev/github.com/google/go-github/v50/github#TeamsService.GetTeamMembershipBySlug) function, which requires:
1. org identifier (perhaps this is already available somewhere for other API calls that are already being made by labeler? Otherwise, it will probably have to be added to config)
2. team _slug_, as opposed to the team name (as [currently documented](https://github.com/srvaroa/labeler/pull/150/files#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5R351)), which can contain spaces
3. user (we already have this)

Pull request description:
Fix author-in-team
Use the right GH API to resolve team membership

Fixes #153 

Signed-off-by: Galo Navarro <anglorvaroa@gmail.com>


Patch:
diff --git a/cmd/action.go b/cmd/action.go
index 137be2d..01b5f81 100644
--- a/cmd/action.go
+++ b/cmd/action.go
@@ -231,7 +231,7 @@ func newLabeler(gh *github.Client, config *labeler.LabelerConfigV1) *labeler.Lab
 			GetRawDiff: func(owner, repo string, prNumber int) (string, error) {
 				diff, _, err := gh.PullRequests.GetRaw(ctx,
 					owner, repo, prNumber,
-					github.RawOptions{github.Diff})
+					github.RawOptions{Type: github.Diff})
 				return diff, err
 			},
 			ListIssuesByRepo: func(owner, repo string) ([]*github.Issue, error) {
@@ -244,8 +244,8 @@ func newLabeler(gh *github.Client, config *labeler.LabelerConfigV1) *labeler.Lab
 					owner, repo, &github.PullRequestListOptions{})
 				return prs, err
 			},
-			IsUserMemberOfTeam: func(user, team string) (bool, error) {
-				membership, _, err := gh.Organizations.GetOrgMembership(ctx, user, team)
+			IsUserMemberOfTeam: func(org, user, team string) (bool, error) {
+				membership, _, err := gh.Teams.GetTeamMembershipBySlug(ctx, org, team, user)
 				if err != nil {
 					return false, err
 				}
diff --git a/pkg/condition_author_in_team.go b/pkg/condition_author_in_team.go
index 96c53d9..ecd7e12 100644
--- a/pkg/condition_author_in_team.go
+++ b/pkg/condition_author_in_team.go
@@ -17,7 +17,11 @@ func AuthorInTeamCondition(l *Labeler) Condition {
 				return false, fmt.Errorf("author-in-team is not set in config")
 			}
 			// check if author is a member of team
-			return l.GitHubFacade.IsUserMemberOfTeam(target.Author, matcher.AuthorInTeam)
+			return l.GitHubFacade.IsUserMemberOfTeam(
+				target.Owner,
+				target.Author,
+				matcher.AuthorInTeam, // this is the team slug
+			)
 		},
 	}
 }
diff --git a/pkg/labeler.go b/pkg/labeler.go
index 1f61624..a64016c 100644
--- a/pkg/labeler.go
+++ b/pkg/labeler.go
@@ -69,7 +69,7 @@ type GitHubFacade struct {
 	GetRawDiff         func(owner, repo string, prNumber int) (string, error)
 	ListIssuesByRepo   func(owner, repo string) ([]*gh.Issue, error)
 	ListPRs            func(owner, repo string) ([]*gh.PullRequest, error)
-	IsUserMemberOfTeam func(user, team string) (bool, error)
+	IsUserMemberOfTeam func(org, user, team string) (bool, error)
 }
 
 type Labeler struct {


Test patch:
diff --git a/pkg/labeler_test.go b/pkg/labeler_test.go
index 50316b6..b0aade9 100644
--- a/pkg/labeler_test.go
+++ b/pkg/labeler_test.go
@@ -1075,7 +1075,7 @@ func NewTestLabeler(t *testing.T, tc TestCase) Labeler {
 				return string(data), nil
 			},
 			// Will return true whenever team contains the given user name
-			IsUserMemberOfTeam: func(user, team string) (bool, error) {
+			IsUserMemberOfTeam: func(org, user, team string) (bool, error) {
 				return strings.Contains(team, user), nil
 			},
 		},