Skip to content

Commit d361bd5

Browse files
authored
GitHub repo select (#1)
* Add implementation * Add docs Co-authored-by: Thomas Biesaart <[email protected]>
1 parent d427f3d commit d361bd5

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ github:
2222
# "read:org, repo"
2323
# https://github.com/settings/tokens/new?scopes=repo,read:org
2424
access_token: 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
25+
# (optional) Back up repos you own.
26+
# (default: true)
27+
owned: true
28+
# (optional) Back up repos you starred.
29+
# (default: true)
30+
starred: true
31+
# (optional) Back up repos on which you
32+
# are a collaborator. (default: true)
33+
collaborator: true
34+
# (optional) Back up repos owned by
35+
# organisations of which you are a member.
36+
# (default: true)
37+
org_member: true
2538
```
2639
2740
## Usage

github.go

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,22 @@ import (
77
"golang.org/x/oauth2"
88
"log"
99
"net/url"
10+
"strings"
1011
)
1112

13+
func boolPointer(b bool) *bool {
14+
return &b
15+
}
16+
1217
type GithubConfig struct {
13-
JobName string `yaml:"job_name"`
14-
AccessToken string `yaml:"access_token"`
15-
URL string `yaml:"url,omitempty"`
16-
client *github.Client
18+
JobName string `yaml:"job_name"`
19+
AccessToken string `yaml:"access_token"`
20+
URL string `yaml:"url,omitempty"`
21+
Starred *bool `yaml:"starred,omitempty"`
22+
OrgMember *bool `yaml:"org_member,omitempty"`
23+
Collaborator *bool `yaml:"collaborator,omitempty"`
24+
Owned *bool `yaml:"owned,omitempty"`
25+
client *github.Client
1726
}
1827

1928
func (c *GithubConfig) Test() error {
@@ -53,6 +62,18 @@ func (c *GithubConfig) setDefaults() {
5362
if c.JobName == "" {
5463
c.JobName = "GitHub"
5564
}
65+
if c.Collaborator == nil {
66+
c.Collaborator = boolPointer(true)
67+
}
68+
if c.OrgMember == nil {
69+
c.OrgMember = boolPointer(true)
70+
}
71+
if c.Owned == nil {
72+
c.Owned = boolPointer(true)
73+
}
74+
if c.Starred == nil {
75+
c.Starred = boolPointer(true)
76+
}
5677
httpClient := oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(&oauth2.Token{AccessToken: c.AccessToken}))
5778
if c.URL == "" {
5879
c.client = github.NewClient(httpClient)
@@ -106,11 +127,28 @@ func (c *GithubConfig) getAllRepos() ([]*github.Repository, error) {
106127
}
107128

108129
func (c *GithubConfig) getRepos(page int) ([]*github.Repository, *github.Response, error) {
130+
affiliations := make([]string, 0)
131+
132+
if *c.Owned {
133+
affiliations = append(affiliations, "owner")
134+
}
135+
if *c.Collaborator {
136+
affiliations = append(affiliations, "collaborator")
137+
}
138+
if *c.OrgMember {
139+
affiliations = append(affiliations, "organization_member")
140+
}
141+
142+
if len(affiliations) == 0 {
143+
return make([]*github.Repository, 0), &github.Response{}, nil
144+
}
145+
109146
return c.client.Repositories.List(context.Background(), "", &github.RepositoryListOptions{
110147
ListOptions: github.ListOptions{
111148
Page: page,
112149
PerPage: 100,
113150
},
151+
Affiliation: strings.Join(affiliations, ","),
114152
})
115153
}
116154

@@ -125,8 +163,8 @@ func (c *GithubConfig) getStarredRepos(page int) ([]*github.Repository, *github.
125163
return nil, response, err
126164
}
127165
repos := make([]*github.Repository, len(starred))
128-
for i, _ := range repos {
166+
for i := range repos {
129167
repos[i] = starred[i].Repository
130168
}
131169
return repos, response, err
132-
}
170+
}

0 commit comments

Comments
 (0)