From 8c66e2fa29323a3fa87a65c7a7c9ebfae6aa8483 Mon Sep 17 00:00:00 2001 From: Thomas Biesaart Date: Thu, 17 Mar 2022 10:21:02 +0100 Subject: [PATCH 1/2] Add implementation --- github.go | 50 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/github.go b/github.go index 8a9892b..704c92b 100644 --- a/github.go +++ b/github.go @@ -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 { @@ -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) @@ -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, ","), }) } @@ -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 -} +} \ No newline at end of file From 20f698396483cb523914768932b1ebe64d7fae84 Mon Sep 17 00:00:00 2001 From: Thomas Biesaart Date: Thu, 17 Mar 2022 10:24:42 +0100 Subject: [PATCH 2/2] Add docs --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index a9e1827..38892bf 100644 --- a/README.md +++ b/README.md @@ -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