Skip to content

Commit

Permalink
✨ ユーザーがOrgに所属しているか調べる
Browse files Browse the repository at this point in the history
  • Loading branch information
ikura-hamu committed Aug 23, 2024
1 parent addfeca commit 367469a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
16 changes: 16 additions & 0 deletions handler/message_created.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,22 @@ func (h *BotHandler) invite(p *payload.MessageCreated) {
return
}

inOrg, err := h.githubClient.CheckUserInOrg(ctx, gitHubID)
if err != nil {
log.Println("failed to check user in org: ", err)
return
}

if inOrg {
_, err := h.traqClient.PostMessage(ctx, p.Message.ChannelID,
fmt.Sprintf("GitHubユーザー %s は既に %s に所属しています", gitHubID, h.githubClient.OrgName()))
if err != nil {
log.Println("failed to post message: ", err)
}

return
}

traQIDs = append(traQIDs, traQID)
gitHubIDs = append(gitHubIDs, gitHubID)
}
Expand Down
19 changes: 19 additions & 0 deletions handler/message_created_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func TestInvite(t *testing.T) {
messageID string
embedded []payload.EmbeddedInfo
gitHubUserExist bool
belongToOrg bool
postTextFunc func(test) string
postToBotChannel bool
invitations []*model.Invitation
Expand Down Expand Up @@ -122,6 +123,18 @@ https://q.trap.jp/messages/%s`, t.messageID)
return "GitHubユーザー no-user は存在しません"
},
},
"すでに所属している": {
plainText: "@BOT_traP-jp /invite @ikura-hamu ikura-hamu",
messageID: messageID,
embedded: []payload.EmbeddedInfo{
{Type: "user", Raw: "@BOT_traP-jp", ID: botUserID},
},
gitHubUserExist: true,
belongToOrg: true,
postTextFunc: func(test) string {
return fmt.Sprintf("GitHubユーザー ikura-hamu は既に traP-jp に所属しています")
},
},
}

for name, test := range testCases {
Expand All @@ -148,6 +161,12 @@ https://q.trap.jp/messages/%s`, t.messageID)
CheckUserExistFunc: func(ctx context.Context, userID string) (bool, error) {
return test.gitHubUserExist, nil
},
CheckUserInOrgFunc: func(ctx context.Context, userID string) (bool, error) {
return test.belongToOrg, nil
},
OrgNameFunc: func() string {
return "traP-jp"
},
}

bh := &BotHandler{
Expand Down
1 change: 1 addition & 0 deletions service/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ import (
type GitHub interface {
SendInvitations(ctx context.Context, invitations []*model.Invitation) error
CheckUserExist(ctx context.Context, userID string) (bool, error)
CheckUserInOrg(ctx context.Context, userID string) (bool, error)
OrgName() string
}
13 changes: 13 additions & 0 deletions service/impl/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ func (g *GitHub) CheckUserExist(ctx context.Context, userID string) (bool, error
return true, nil
}

func (g *GitHub) CheckUserInOrg(ctx context.Context, userID string) (bool, error) {
_, _, err := g.cl.Organizations.GetOrgMembership(ctx, userID, g.orgName)
var gitHubErr *github.ErrorResponse
if errors.As(err, &gitHubErr) && gitHubErr.Response.StatusCode == 404 {
return false, nil
}
if err != nil {
return false, fmt.Errorf("failed to get GitHub org membership: %w", err)
}

return true, nil
}

func (g *GitHub) OrgName() string {
return g.orgName
}

0 comments on commit 367469a

Please sign in to comment.