Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API] add new endpoints for push mirrors management #19841

Merged
merged 46 commits into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
cf10d77
add new endpoints for push mirrors management:
mohsek May 30, 2022
015a57a
Merge branch 'main' into main
mohsek May 31, 2022
fb9ea68
Merge branch 'main' into main
mohsek Jun 2, 2022
a4b40bd
remove changes added to some files by mistake
mohsek Jun 2, 2022
4a1fb91
fix lint errors
mohsek Jun 2, 2022
5c5eef3
Update .drone.yml
zeripath Jun 2, 2022
a79cd85
Remove unnecessary package change
zeripath Jun 2, 2022
99f6a77
Protect against reading other repo push mirrors
zeripath Jun 2, 2022
4141460
Update services/mailer/mail_test.go
zeripath Jun 2, 2022
0742732
Apply suggestions from code review
zeripath Jun 2, 2022
4fa4660
And Delete too
zeripath Jun 2, 2022
97204df
Merge branch 'mohsek/main-upstream' into mohsek/main
zeripath Jun 2, 2022
633095d
fix lint
zeripath Jun 2, 2022
d507d47
Use remote name not id
zeripath Jun 2, 2022
179e935
oops swagger
zeripath Jun 2, 2022
37ba6cf
rename push-mirror endpoints to push_mirrors
mohsek Jun 3, 2022
4560d3b
apply the suggested changes
mohsek Jun 7, 2022
b4e0977
Merge branch 'main' into main
mohsek Jun 7, 2022
addd7f1
handle error on failed sync mirror
mohsek Jun 8, 2022
4071774
fix typo
mohsek Jun 8, 2022
9206fc5
apply PR suggestions
mohsek Jun 9, 2022
6950ff1
some code refactor to use remoteName instead of ID
mohsek Jun 12, 2022
3e90998
Merge branch 'main' into main
mohsek Jun 12, 2022
0d39152
fix lint-backend error
mohsek Jun 12, 2022
88f620c
Merge branch 'main' into main
mohsek Jun 13, 2022
b23979a
code refactoring
mohsek Jun 17, 2022
c775d3c
fix lint
mohsek Jun 17, 2022
614d724
handle error getMirrorRemoteAddress
mohsek Jun 17, 2022
85eecf5
handle errors on convert.ToPushMirror
mohsek Jun 17, 2022
aabe5e9
use builder.Cond function to simplify code
mohsek Jun 18, 2022
4a54502
restore changes done to helper.go
mohsek Jun 18, 2022
639268a
Merge branch 'main' into main
mohsek Jun 24, 2022
2645df8
add context parameter to added functions
mohsek Jun 30, 2022
61e493e
apply PR suggestions
mohsek Jul 1, 2022
da57680
apply PR suggestion
mohsek Jul 2, 2022
e50174c
some minor refactoring
mohsek Jul 3, 2022
d59a235
Merge branch 'main' into main
mohsek Jul 3, 2022
5cff07e
Merge remote-tracking branch 'origin/main' into mohsek/main
zeripath Jul 15, 2022
0884346
Merge branch 'main' into main
zeripath Jul 16, 2022
1886253
Merge branch 'main' into main
zeripath Jul 24, 2022
cadcfb7
Merge branch 'main' into main
mohsek Jul 26, 2022
c535692
Merge branch 'main' into main
lunny Jul 28, 2022
8578582
Merge branch 'main' into main
zeripath Jul 28, 2022
1309dec
Merge branch 'main' into main
mohsek Jul 29, 2022
d9ddbcf
fix wrong count calculation
mohsek Jul 30, 2022
d1f2993
Merge branch 'main' into main
6543 Jul 30, 2022
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
5 changes: 3 additions & 2 deletions integrations/mirror_push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"testing"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
Expand Down Expand Up @@ -47,7 +48,7 @@ func testMirrorPush(t *testing.T, u *url.URL) {

doCreatePushMirror(ctx, fmt.Sprintf("%s%s/%s", u.String(), url.PathEscape(ctx.Username), url.PathEscape(mirrorRepo.Name)), user.LowerName, userPassword)(t)

mirrors, err := repo_model.GetPushMirrorsByRepoID(srcRepo.ID)
mirrors, err := repo_model.GetPushMirrorsByRepoID(srcRepo.ID, db.ListOptions{})
assert.NoError(t, err)
assert.Len(t, mirrors, 1)

Expand All @@ -72,7 +73,7 @@ func testMirrorPush(t *testing.T, u *url.URL) {

// Cleanup
doRemovePushMirror(ctx, fmt.Sprintf("%s%s/%s", u.String(), url.PathEscape(ctx.Username), url.PathEscape(mirrorRepo.Name)), user.LowerName, userPassword, int(mirrors[0].ID))(t)
mirrors, err = repo_model.GetPushMirrorsByRepoID(srcRepo.ID)
mirrors, err = repo_model.GetPushMirrorsByRepoID(srcRepo.ID, db.ListOptions{})
assert.NoError(t, err)
assert.Len(t, mirrors, 0)
}
Expand Down
21 changes: 19 additions & 2 deletions models/repo/pushmirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func DeletePushMirrorsByRepoID(repoID int64) error {
}

// GetPushMirrorByID returns push-mirror information.
// WARNING: You should ensure that this PushMirror belongs to the repository you are intending to use it with
func GetPushMirrorByID(ID int64) (*PushMirror, error) {
m := &PushMirror{}
has, err := db.GetEngine(db.DefaultContext).ID(ID).Get(m)
Expand All @@ -87,10 +88,26 @@ func GetPushMirrorByID(ID int64) (*PushMirror, error) {
return m, nil
}

// GetPushMirrorByRepoIDAndID returns push-mirror information.
func GetPushMirrorByRepoIDAndID(repoID, mirrorID int64) (*PushMirror, error) {
m := &PushMirror{}
has, err := db.GetEngine(db.DefaultContext).ID(mirrorID).Where("repo_id = ?", repoID).Get(m)
if err != nil {
return nil, err
} else if !has {
return nil, ErrPushMirrorNotExist
}
return m, nil
}

// GetPushMirrorsByRepoID returns push-mirror information of a repository.
func GetPushMirrorsByRepoID(repoID int64) ([]*PushMirror, error) {
func GetPushMirrorsByRepoID(repoID int64, listOptions db.ListOptions) ([]*PushMirror, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the other functions now.

Copy link
Contributor Author

@mohsek mohsek Jun 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To summarize, currently we have 2 functions :

  • GetPushMirrors : to get a push mirror using a repoID and (pushMirrror ID or remoteName)
  • GetPushMirrorsByRepoID : this returns a list of push mirrors belonging to the repo, I kept this function cause I think it's better to do not mix between get a list of push mirrors and get only one push mirror.

Should we merge also this one ? if soo all the calling functions should parse the response as a list and I don't think it's a good idea

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops I'd missed that.

mirrors := make([]*PushMirror, 0, 10)
return mirrors, db.GetEngine(db.DefaultContext).Where("repo_id=?", repoID).Find(&mirrors)
sess := db.GetEngine(db.DefaultContext).Where("repo_id = ?", repoID)
if listOptions.Page != 0 {
sess = db.SetSessionPagination(sess, &listOptions)
}
return mirrors, sess.Find(&mirrors)
}

// PushMirrorsIterate iterates all push-mirror repositories.
Expand Down
2 changes: 1 addition & 1 deletion modules/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func repoAssignment(ctx *Context, repo *repo_model.Repository) {
}
}

pushMirrors, err := repo_model.GetPushMirrorsByRepoID(repo.ID)
pushMirrors, err := repo_model.GetPushMirrorsByRepoID(repo.ID, db.ListOptions{})
if err != nil {
ctx.ServerError("GetPushMirrorsByRepoID", err)
return
Expand Down
36 changes: 36 additions & 0 deletions modules/convert/mirror.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package convert

import (
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/git"
api "code.gitea.io/gitea/modules/structs"
)

// ToPushMirror convert from repo_model.PushMirror and remoteAddress to api.TopicResponse
func ToPushMirror(pm *repo_model.PushMirror, repo *repo_model.Repository) *api.PushMirror {
remoteAddress, _ := getMirrorRemoteAddress(repo, pm.RemoteName)
6543 marked this conversation as resolved.
Show resolved Hide resolved
return &api.PushMirror{
ID: pm.ID,
RepoName: repo.Name,
RemoteName: pm.RemoteName,
RemoteAddress: remoteAddress,
CreatedUnix: pm.CreatedUnix.FormatLong(),
LastUpdateUnix: pm.LastUpdateUnix.FormatLong(),
LastError: pm.LastError,
Interval: pm.Interval.String(),
}
}

func getMirrorRemoteAddress(repo *repo_model.Repository, remoteName string) (string, error) {
lunny marked this conversation as resolved.
Show resolved Hide resolved
u, err := git.GetRemoteAddress(git.DefaultContext, repo.RepoPath(), remoteName)
if err != nil {
return "", err
}
// remove confidential information
u.User = nil
return u.String(), nil
}
26 changes: 26 additions & 0 deletions modules/structs/mirror.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package structs

// CreatePushMirrorOption represents need information to create a push mirror of a repository.
type CreatePushMirrorOption struct {
RemoteAddress string `json:"remoteAddress"`
RemoteUsername string `json:"remoteUsername"`
RemotePassword string `json:"remotePassword"`
Interval string `json:"interval"`
}

// PushMirror represents information of a push mirror
// swagger:model
type PushMirror struct {
ID int64 `json:"id"`
RepoName string `json:"repoName"`
RemoteName string `json:"remoteName"`
RemoteAddress string `json:"remoteAddress"`
CreatedUnix string `json:"created"`
LastUpdateUnix string `json:"lastUpdate"`
LastError string `json:"lastError"`
Interval string `json:"interval"`
}
9 changes: 9 additions & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,15 @@ func Routes() *web.Route {
})
}, reqRepoReader(unit.TypeReleases))
m.Post("/mirror-sync", reqToken(), reqRepoWriter(unit.TypeCode), repo.MirrorSync)
m.Post("/push-mirror-sync", reqAdmin(), repo.PushMirrorSync)
m.Group("/push-mirror", func() {
m.Combo("").Get(reqAdmin(), repo.ListPushMirrors).
Post(reqAdmin(), bind(api.CreatePushMirrorOption{}), repo.AddPushMirror)
m.Combo("/{id}").
Delete(reqAdmin(), repo.DeletePushMirrorByID).
Get(reqAdmin(), repo.GetPushMirrorByID)
})

m.Get("/editorconfig/{filename}", context.ReferencesGitRepo(), context.RepoRefForAPI, reqRepoReader(unit.TypeCode), repo.GetEditorconfig)
m.Group("/pulls", func() {
m.Combo("").Get(repo.ListPullRequests).
Expand Down
Loading