Skip to content

Commit 2e3d9aa

Browse files
authored
feat: Add configuration to exclude repositories from being fetched. (#22)
1 parent 375386a commit 2e3d9aa

File tree

5 files changed

+52
-24
lines changed

5 files changed

+52
-24
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
- uses: actions/checkout@v2
4242
- uses: actions/setup-go@v2
4343
with:
44-
go-version: '^1.17.8'
44+
go-version: '^1.18.10'
4545
- run: ${{ matrix.goopts }} go build -o ${{ matrix.filename }} -ldflags="-X 'main.Version=${GITHUB_REF##*/}' -X 'main.CommitHash=${GITHUB_SHA}' -X 'main.BuildTimestamp=$(date)'" ./cmd/git-backup
4646
env:
4747
GOOS: ${{ matrix.goos }}

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ github:
3939
# your self-hosted github install.
4040
# (default: https://api.github.com)
4141
url: https://github.mydomain.com
42+
# (optional) Exclude this list of repos
43+
excluded:
44+
- my-namespace/excluded-repository-name
4245
# The gitlab section contains backup jobs for
4346
# GitLab.com and GitLab on premise
4447
gitlab:
@@ -64,6 +67,9 @@ gitlab:
6467
# your self-hosted gitlab install.
6568
# (default: https://gitlab.com/)
6669
url: https://gitlab.mydomain.com
70+
# (optional) Exclude this list of repos
71+
excluded:
72+
- my-namespace/excluded-repository-name
6773
```
6874
6975
## Usage: CLI

github.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,24 @@ package git_backup
33
import (
44
"context"
55
"fmt"
6-
"github.com/google/go-github/v43/github"
7-
"golang.org/x/oauth2"
86
"log"
97
"net/url"
8+
"slices"
109
"strings"
10+
11+
"github.com/google/go-github/v43/github"
12+
"golang.org/x/oauth2"
1113
)
1214

1315
type GithubConfig struct {
14-
JobName string `yaml:"job_name"`
15-
AccessToken string `yaml:"access_token"`
16-
URL string `yaml:"url,omitempty"`
17-
Starred *bool `yaml:"starred,omitempty"`
18-
OrgMember *bool `yaml:"org_member,omitempty"`
19-
Collaborator *bool `yaml:"collaborator,omitempty"`
20-
Owned *bool `yaml:"owned,omitempty"`
16+
JobName string `yaml:"job_name"`
17+
AccessToken string `yaml:"access_token"`
18+
URL string `yaml:"url,omitempty"`
19+
Starred *bool `yaml:"starred,omitempty"`
20+
OrgMember *bool `yaml:"org_member,omitempty"`
21+
Collaborator *bool `yaml:"collaborator,omitempty"`
22+
Owned *bool `yaml:"owned,omitempty"`
23+
Exclude []string `yaml:"exclude,omitempty"`
2124
client *github.Client
2225
}
2326

@@ -39,16 +42,24 @@ func (c *GithubConfig) ListRepositories() ([]*Repository, error) {
3942
if err != nil {
4043
return nil, err
4144
}
42-
out := make([]*Repository, len(repos))
43-
for i, repo := range repos {
45+
out := make([]*Repository, 0, len(repos))
46+
for _, repo := range repos {
4447
gitUrl, err := url.Parse(*repo.CloneURL)
4548
if err != nil {
4649
return out, err
4750
}
4851
gitUrl.User = url.UserPassword("github", c.AccessToken)
49-
out[i] = &Repository{
50-
FullName: *repo.FullName,
51-
GitURL: *gitUrl,
52+
53+
isExcluded := slices.ContainsFunc(c.Exclude, func(s string) bool {
54+
return strings.EqualFold(s, *repo.FullName)
55+
})
56+
if isExcluded {
57+
log.Printf("Skipping excluded repository: %s", *repo.FullName)
58+
} else {
59+
out = append(out, &Repository{
60+
FullName: *repo.FullName,
61+
GitURL: *gitUrl,
62+
})
5263
}
5364
}
5465
return out, nil

gitlab.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
package git_backup
22

33
import (
4-
"github.com/xanzy/go-gitlab"
54
"log"
65
"net/url"
6+
"slices"
7+
"strings"
8+
9+
"github.com/xanzy/go-gitlab"
710
)
811

912
type GitLabConfig struct {
10-
URL string `yaml:"url,omitempty"`
11-
JobName string `yaml:"job_name"`
12-
AccessToken string `yaml:"access_token"`
13-
Starred *bool `yaml:"starred,omitempty"`
14-
Member *bool `yaml:"member,omitempty"`
15-
Owned *bool `yaml:"owned,omitempty"`
13+
URL string `yaml:"url,omitempty"`
14+
JobName string `yaml:"job_name"`
15+
AccessToken string `yaml:"access_token"`
16+
Starred *bool `yaml:"starred,omitempty"`
17+
Member *bool `yaml:"member,omitempty"`
18+
Owned *bool `yaml:"owned,omitempty"`
19+
Exclude []string `yaml:"exclude,omitempty"`
1620
client *gitlab.Client
1721
}
1822

@@ -64,7 +68,14 @@ func (g *GitLabConfig) ListRepositories() ([]*Repository, error) {
6468

6569
outSlice := make([]*Repository, 0, len(out))
6670
for _, repository := range out {
67-
outSlice = append(outSlice, repository)
71+
isExcluded := slices.ContainsFunc(g.Exclude, func(s string) bool {
72+
return strings.EqualFold(s, repository.FullName)
73+
})
74+
if isExcluded {
75+
log.Printf("Skipping excluded repository: %s", repository.FullName)
76+
} else {
77+
outSlice = append(outSlice, repository)
78+
}
6879
}
6980

7081
return outSlice, nil

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module git-backup
22

3-
go 1.17
3+
go 1.18
44

55
require (
66
github.com/go-git/go-git/v5 v5.4.2

0 commit comments

Comments
 (0)