Skip to content

Commit db903d8

Browse files
authored
fix private repo sync bug (#4)
* fix private repo mirror bug * update Dockerfile * update for IMirror interface
1 parent 8137fb3 commit db903d8

File tree

5 files changed

+44
-14
lines changed

5 files changed

+44
-14
lines changed

Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ LABEL "homepage"="http://github.com/x-actions/git-mirrors"
1111
LABEL "maintainer"="xiexianbin<[email protected]>"
1212

1313
LABEL "Name"="git-mirrors"
14-
LABEL "Version"="v0.1.0"
1514

1615
ENV LC_ALL C.UTF-8
1716
ENV LANG en_US.UTF-8

mirrors/base.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ package mirrors
1717
type IMirror interface {
1818
Do() error
1919
prepare() error
20-
mirrorRepoInfo() error
21-
mirrorGit() error
20+
mirrorRepoInfo(srcRepo *Repository, dstRepoName string) (*Repository, error)
21+
mirrorGit(srcRepo, dstRepo *Repository) error
2222
}
2323

2424
type IGitAPI interface {
25+
IsAPIAuthed() bool
2526
Organizations(user string) ([]*Organization, error)
2627
GetOrganization(orgName string) (*Organization, error)
2728
Repositories(user string) ([]*Repository, error)

mirrors/do.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,14 @@ func (m *Mirror) prepare() error {
116116
switch t {
117117
// init User Repos
118118
case constants.AccountTypeUser:
119-
repos, err := client.Repositories(orgName)
119+
// client.
120+
var repos []*Repository
121+
var err error
122+
if client.IsAPIAuthed() {
123+
repos, err = client.Repositories("")
124+
} else {
125+
repos, err = client.Repositories(orgName)
126+
}
120127
if err != nil {
121128
return nil, err
122129
}
@@ -227,7 +234,14 @@ func (m *Mirror) mirrorRepoInfo(srcRepo *Repository, dstRepoName string) (*Repos
227234
dstRepo.Description = srcRepo.Description
228235
dstRepo.Topics = srcRepo.Topics
229236
dstRepo.Private = srcRepo.Private
230-
_, err := client.UpdateRepository(*dstRepo.Organization.Name, *dstRepo.Name, dstRepo)
237+
238+
var orgName string
239+
if dstRepo.Organization == nil {
240+
orgName = *dstRepo.Owner.Name
241+
} else {
242+
orgName = *dstRepo.Organization.Name
243+
}
244+
_, err := client.UpdateRepository(orgName, *dstRepo.Name, dstRepo)
231245
if err != nil {
232246
logger.Warnf("update repo %s/%s err: %s", *dstRepo.Owner.Name, *dstRepo.Name, err.Error())
233247
return dstRepo, nil
@@ -258,7 +272,8 @@ func (m *Mirror) mirrorRepoInfo(srcRepo *Repository, dstRepoName string) (*Repos
258272
// mirrorGit clone/pull from src repo and push to dst repo
259273
func (m *Mirror) mirrorGit(srcRepo, dstRepo *Repository) error {
260274
var err error
261-
cachePath := path.Join(m.CachePath, *srcRepo.Name)
275+
// cachePath format: m.CachePath + "/" + m.SrcOrg + "/" + *srcRepo.Name
276+
cachePath := path.Join(m.CachePath, m.SrcOrg, *srcRepo.Name)
262277
// clone or fetch from origin
263278
_, err = m.srcGitClient.CloneOrFetch(GitURL(srcRepo, m.srcGitClient.GitAuthType), "origin", cachePath)
264279
if err != nil {

mirrors/gitee.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,33 @@ type GiteeAPI struct {
3131
Client *gitee.APIClient
3232
Context context.Context
3333
accessToken string
34+
IsAuthed bool
3435
}
3536

3637
// NewGiteeAPI return new Gitee API
3738
func NewGiteeAPI(accessToken string) (*GiteeAPI, error) {
3839
ctx := context.Background()
3940
// configuration
4041
conf := gitee.NewConfiguration()
42+
isAuthed := false
4143
if accessToken != "" {
4244
// oauth
4345
ts := oauth2.StaticTokenSource(
4446
&oauth2.Token{AccessToken: accessToken},
4547
)
4648
conf.HTTPClient = oauth2.NewClient(ctx, ts)
49+
isAuthed = true
4750
}
4851

4952
// git client
5053
client := gitee.NewAPIClient(conf)
5154

52-
return &GiteeAPI{
53-
Client: client,
54-
Context: ctx,
55-
accessToken: accessToken,
56-
}, nil
55+
return &GiteeAPI{Client: client, Context: ctx, accessToken: accessToken, IsAuthed: isAuthed}, nil
56+
}
57+
58+
// IsAPIAuthed return is the API auth, true or false
59+
func (g *GiteeAPI) IsAPIAuthed() bool {
60+
return g.IsAuthed
5761
}
5862

5963
// Organizations list all Organizations

mirrors/github.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,29 @@ type GithubAPI struct {
3030
Client *github.Client
3131
Context context.Context
3232
accessToken string
33+
IsAuthed bool
3334
}
3435

3536
// NewGithubAPI return new Github API
3637
func NewGithubAPI(accessToken string) (*GithubAPI, error) {
3738
ctx := context.Background()
3839
client := github.NewClient(nil)
40+
isAuthed := false
3941
if accessToken != "" {
4042
ts := oauth2.StaticTokenSource(
4143
&oauth2.Token{AccessToken: accessToken},
4244
)
4345
tc := oauth2.NewClient(ctx, ts)
4446
client = github.NewClient(tc)
47+
isAuthed = true
4548
}
4649

47-
return &GithubAPI{Client: client, Context: ctx, accessToken: accessToken}, nil
50+
return &GithubAPI{Client: client, Context: ctx, accessToken: accessToken, IsAuthed: isAuthed}, nil
51+
}
52+
53+
// IsAPIAuthed return is the API auth, true or false
54+
func (g *GithubAPI) IsAPIAuthed() bool {
55+
return g.IsAuthed
4856
}
4957

5058
// Organizations list Organizations
@@ -81,11 +89,14 @@ func (g *GithubAPI) GetOrganization(orgName string) (*Organization, error) {
8189
}
8290

8391
// Repositories list all repositories for the authenticated user, if user is empty show all repos
84-
// https://docs.github.com/en/rest/repos/repos#list-repositories-for-the-authenticated-user
92+
// support two method:
93+
// https://docs.github.com/en/rest/repos/repos#list-repositories-for-the-authenticated-user if user is empty
94+
// https://docs.github.com/en/rest/repos/repos#list-repositories-for-a-user if user is special
8595
func (g *GithubAPI) Repositories(user string) ([]*Repository, error) {
8696
opt := &github.RepositoryListOptions{
87-
//Type: "all",
97+
Visibility: "all",
8898
Affiliation: "owner",
99+
//Type: "all",
89100
ListOptions: github.ListOptions{
90101
Page: 1,
91102
PerPage: 1000,

0 commit comments

Comments
 (0)