Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ github:
# "read:org, repo"
# https://github.com/settings/tokens/new?scopes=repo,read:org
access_token: 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
# (optional) Back up repos you own.
# (default: true)
owned: true
# (optional) Back up repos you starred.
# (default: true)
starred: true
# (optional) Back up repos on which you
# are a collaborator. (default: true)
collaborator: true
# (optional) Back up repos owned by
# organisations of which you are a member.
# (default: true)
org_member: true
```

## Usage
Expand Down
50 changes: 44 additions & 6 deletions github.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@ import (
"golang.org/x/oauth2"
"log"
"net/url"
"strings"
)

func boolPointer(b bool) *bool {
return &b
}

type GithubConfig struct {
JobName string `yaml:"job_name"`
AccessToken string `yaml:"access_token"`
URL string `yaml:"url,omitempty"`
client *github.Client
JobName string `yaml:"job_name"`
AccessToken string `yaml:"access_token"`
URL string `yaml:"url,omitempty"`
Starred *bool `yaml:"starred,omitempty"`
OrgMember *bool `yaml:"org_member,omitempty"`
Collaborator *bool `yaml:"collaborator,omitempty"`
Owned *bool `yaml:"owned,omitempty"`
client *github.Client
}

func (c *GithubConfig) Test() error {
Expand Down Expand Up @@ -53,6 +62,18 @@ func (c *GithubConfig) setDefaults() {
if c.JobName == "" {
c.JobName = "GitHub"
}
if c.Collaborator == nil {
c.Collaborator = boolPointer(true)
}
if c.OrgMember == nil {
c.OrgMember = boolPointer(true)
}
if c.Owned == nil {
c.Owned = boolPointer(true)
}
if c.Starred == nil {
c.Starred = boolPointer(true)
}
httpClient := oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(&oauth2.Token{AccessToken: c.AccessToken}))
if c.URL == "" {
c.client = github.NewClient(httpClient)
Expand Down Expand Up @@ -106,11 +127,28 @@ func (c *GithubConfig) getAllRepos() ([]*github.Repository, error) {
}

func (c *GithubConfig) getRepos(page int) ([]*github.Repository, *github.Response, error) {
affiliations := make([]string, 0)

if *c.Owned {
affiliations = append(affiliations, "owner")
}
if *c.Collaborator {
affiliations = append(affiliations, "collaborator")
}
if *c.OrgMember {
affiliations = append(affiliations, "organization_member")
}

if len(affiliations) == 0 {
return make([]*github.Repository, 0), &github.Response{}, nil
}

return c.client.Repositories.List(context.Background(), "", &github.RepositoryListOptions{
ListOptions: github.ListOptions{
Page: page,
PerPage: 100,
},
Affiliation: strings.Join(affiliations, ","),
})
}

Expand All @@ -125,8 +163,8 @@ func (c *GithubConfig) getStarredRepos(page int) ([]*github.Repository, *github.
return nil, response, err
}
repos := make([]*github.Repository, len(starred))
for i, _ := range repos {
for i := range repos {
repos[i] = starred[i].Repository
}
return repos, response, err
}
}