-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
[API] add new endpoints for push mirrors management #19841
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
Changes from 15 commits
cf10d77
015a57a
fb9ea68
a4b40bd
4a1fb91
5c5eef3
a79cd85
99f6a77
4141460
0742732
4fa4660
97204df
633095d
d507d47
179e935
37ba6cf
4560d3b
b4e0977
addd7f1
4071774
9206fc5
6950ff1
3e90998
0d39152
88f620c
b23979a
c775d3c
614d724
85eecf5
aabe5e9
4a54502
639268a
2645df8
61e493e
da57680
e50174c
d59a235
5cff07e
0884346
1886253
cadcfb7
c535692
8578582
1309dec
d9ddbcf
d1f2993
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,18 +64,26 @@ func UpdatePushMirror(m *PushMirror) error { | |
} | ||
|
||
// DeletePushMirrorByID deletes a push-mirrors by ID | ||
// WARNING: This does not check if this PushMirror belongs to a RepoID | ||
func DeletePushMirrorByID(ID int64) error { | ||
_, err := db.GetEngine(db.DefaultContext).ID(ID).Delete(&PushMirror{}) | ||
return err | ||
} | ||
|
||
// DeletePushMirrorByRepoIDAndName deletes a push-mirrors by remote name | ||
func DeletePushMirrorByRepoIDAndName(repoID int64, remoteName string) error { | ||
_, err := db.GetEngine(db.DefaultContext).Where("repo_id = ? AND remote_name = ?", repoID, remoteName).Delete(&PushMirror{}) | ||
return err | ||
} | ||
|
||
// DeletePushMirrorsByRepoID deletes all push-mirrors by repoID | ||
func DeletePushMirrorsByRepoID(repoID int64) error { | ||
_, err := db.GetEngine(db.DefaultContext).Delete(&PushMirror{RepoID: repoID}) | ||
return err | ||
} | ||
|
||
// GetPushMirrorByID returns push-mirror information. | ||
// WARNING: You must 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) | ||
|
@@ -87,10 +95,26 @@ func GetPushMirrorByID(ID int64) (*PushMirror, error) { | |
return m, nil | ||
} | ||
|
||
// GetPushMirrorByRepoIDAndName returns push-mirror information. | ||
func GetPushMirrorByRepoIDAndName(repoID int64, remoteName string) (*PushMirror, error) { | ||
m := &PushMirror{} | ||
has, err := db.GetEngine(db.DefaultContext).Where("repo_id = ? AND remote_name = ?", repoID, remoteName).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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove the other functions now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To summarize, currently we have 2 functions :
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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// 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{ | ||
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// 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 { | ||
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"` | ||
mohsek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a chance to merge the 3 functions as 1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from my point of view, these are three distinct functions. I don't know if there is any benefit in merging them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we can merge the two functions
DeletePushMirrorByID(ID int64)
andDeletePushMirrorByRepoIDAndName(repoID int64, remoteName string)
into
but for
DeletePushMirrorsByRepoID(repoID int64)
for safety it will be better to keep it as a separate functionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to have a function named
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case RemoteName and the ID are empty, should we delete all the pushMirrors of the repoID ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or return an error like
you needs at least one condition
.