forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepo.go
More file actions
127 lines (110 loc) · 3.08 KB
/
repo.go
File metadata and controls
127 lines (110 loc) · 3.08 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package codehost_testing
import (
"context"
"fmt"
"time"
"github.com/google/go-github/v55/github"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
// Repo represents a GitHub repository in the scenario
type Repo struct {
// s is the GithubScenario instance this repo is part of. Actions it creates will be added to this scenario
s *GitHubScenario
// team is the team this repo belongs to
team *Team
// org is the Org that owns this repo
org *Org
// name is the name of the repo
name string
}
// Get returns the corresponding GitHub Repository object that was created by the `CreateOrg`
//
// This method will only return a Repository if the Scenario that created it has been applied otherwise
// it will panic.
func (r *Repo) Get(ctx context.Context) (*github.Repository, error) {
if r.s.IsApplied() {
return r.get(ctx)
}
r.s.t.Fatal("cannot retrieve repo before scenario is applied")
return nil, nil
}
// get retrieves the GitHub repository without panicking if not applied. It is meant as an
// internal helper method while actions are getting applied.
func (r *Repo) get(ctx context.Context) (*github.Repository, error) {
return r.s.client.GetRepo(ctx, r.org.name, r.name)
}
// AddTeam creats an action that will update the repo permissions so that the given team
// has access to this repo.
func (r *Repo) AddTeam(team *Team) {
r.team = team
action := &Action{
Name: fmt.Sprintf("repo:team:%s:membership:%s", team.name, r.name),
Apply: func(ctx context.Context) error {
org, err := r.org.get(ctx)
if err != nil {
return err
}
repo, err := r.get(ctx)
if err != nil {
return err
}
team, err := r.team.get(ctx)
if err != nil {
return err
}
err = r.s.client.UpdateTeamRepoPermissions(ctx, org, team, repo)
if err != nil {
return err
}
return nil
},
Teardown: nil,
}
r.s.Append(action)
}
// SetPermissions adds an action that will set the permissions (public or private) for the repository
func (r *Repo) SetPermissions(private bool) {
permissionKey := "private"
if !private {
permissionKey = "public"
}
action := &Action{
Name: fmt.Sprintf("repo:permissions:%s:%s", r.name, permissionKey),
Apply: func(ctx context.Context) error {
repo, err := r.get(ctx)
if err != nil {
return err
}
repo.Private = &private
org, err := r.org.get(ctx)
if err != nil {
return err
}
_, err = r.s.client.UpdateRepo(ctx, org, repo)
if err != nil {
return err
}
return err
},
}
r.s.Append(action)
}
// WaitTillExists creates an action that waits for the repository to exist on GitHub. This action is especially
// useful for when a repo is forked since a forked repo doesn't immediately exist when requested on GitHub.
func (r *Repo) WaitTillExists() {
action := &Action{
Name: fmt.Sprintf("repo:exists:%s", r.name),
Apply: func(ctx context.Context) error {
var err error
for range 5 {
time.Sleep(1 * time.Second)
_, err = r.get(ctx)
if err == nil {
return nil
}
}
return errors.Newf("repo %q did not exist after waiting: %v", r.name, err)
},
}
r.s.Append(action)
}