forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteam.go
More file actions
66 lines (58 loc) · 1.82 KB
/
team.go
File metadata and controls
66 lines (58 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package codehost_testing
import (
"context"
"fmt"
"github.com/google/go-github/v55/github"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
// Team represents a GitHub team and provdes actions that operate on a GitHub team.
//
// All methods except Get, create actions which are added to the parent GitHubScenario this
// team belongs to.
type Team struct {
// s is the GithubScenario instance this team was created from
s *GitHubScenario
// org is the Org this team belongs to, and is ultimately the one who created this team
org *Org
// name is the name of the team
name string
}
// Get returns the corresponding GitHub Team object that was created by the `CreateTeam`
//
// This method will only return a Team if the Scenario that created it has been applied otherwise
// it will panic.
func (team *Team) Get(ctx context.Context) (*github.Team, error) {
if team.s.IsApplied() {
return team.get(ctx)
}
return nil, errors.New("cannot retrieve org before scenario is applied")
}
// get retrieves the GitHub team without panicking if not applied. It is meant as an
// internal helper method while actions are getting applied.
func (team *Team) get(ctx context.Context) (*github.Team, error) {
return team.s.client.GetTeam(ctx, team.org.name, team.name)
}
// AddUser adds an action that will add the given user to this team
func (tm *Team) AddUser(u *User) {
assignTeamMembership := &Action{
Name: fmt.Sprintf("team:membership:%s:%s", tm.name, u.name),
Apply: func(ctx context.Context) error {
org, err := tm.org.get(ctx)
if err != nil {
return err
}
team, err := tm.get(ctx)
if err != nil {
return err
}
user, err := u.get(ctx)
if err != nil {
return err
}
_, err = tm.s.client.AssignTeamMembership(ctx, org, team, user)
return err
},
Teardown: nil,
}
tm.s.Append(assignTeamMembership)
}