Skip to content

Commit cf10d77

Browse files
committed
add new endpoints for push mirrors management:
* [x] add a new push mirror to specific repository * [x] sync now ( send all the changes to the configured push mirrors ) * [x] Get list of all push mirrors of a repository * [x] get a push mirror by ID * [x] delete push mirror by ID Signed-off-by: Mohamed Sekour <[email protected]>
1 parent d2a91e5 commit cf10d77

File tree

17 files changed

+839
-10
lines changed

17 files changed

+839
-10
lines changed

.drone.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,67 @@ steps:
10611061
exclude:
10621062
- pull_request
10631063

1064+
---
1065+
kind: pipeline
1066+
name: docker-linux-amd64-release-branch
1067+
1068+
platform:
1069+
os: linux
1070+
arch: amd64
1071+
1072+
depends_on:
1073+
- testing-amd64
1074+
- testing-arm64
1075+
1076+
trigger:
1077+
ref:
1078+
- "refs/heads/release/v*"
1079+
event:
1080+
exclude:
1081+
- cron
1082+
1083+
steps:
1084+
- name: fetch-tags
1085+
image: docker:git
1086+
commands:
1087+
- git fetch --tags --force
1088+
1089+
- name: publish
1090+
pull: always
1091+
image: techknowlogick/drone-docker:latest
1092+
settings:
1093+
auto_tag: false
1094+
tags: ${DRONE_BRANCH##release/v}-dev-linux-amd64
1095+
repo: gitea/gitea
1096+
build_args:
1097+
- GOPROXY=https://goproxy.cn
1098+
password:
1099+
from_secret: docker_password
1100+
username:
1101+
from_secret: docker_username
1102+
when:
1103+
event:
1104+
exclude:
1105+
- pull_request
1106+
1107+
- name: publish-rootless
1108+
image: techknowlogick/drone-docker:latest
1109+
settings:
1110+
dockerfile: Dockerfile.rootless
1111+
auto_tag: false
1112+
tags: ${DRONE_BRANCH##release/v}-dev-linux-amd64-rootless
1113+
repo: gitea/gitea
1114+
build_args:
1115+
- GOPROXY=https://goproxy.cn
1116+
password:
1117+
from_secret: docker_password
1118+
username:
1119+
from_secret: docker_username
1120+
when:
1121+
event:
1122+
exclude:
1123+
- pull_request
1124+
10641125
---
10651126
kind: pipeline
10661127
type: docker

integrations/git_helper_for_declarative_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,17 @@ func doPartialGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
117117
}
118118
}
119119

120+
func doPartialGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
121+
return func(t *testing.T) {
122+
assert.NoError(t, git.CloneWithArgs(context.Background(), u.String(), dstLocalPath, allowLFSFilters(), git.CloneRepoOptions{
123+
Filter: "blob:none",
124+
}))
125+
exist, err := util.IsExist(filepath.Join(dstLocalPath, "README.md"))
126+
assert.NoError(t, err)
127+
assert.True(t, exist)
128+
}
129+
}
130+
120131
func doGitCloneFail(u *url.URL) func(*testing.T) {
121132
return func(t *testing.T) {
122133
tmpDir, err := os.MkdirTemp("", "doGitCloneFail")

integrations/mirror_push_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"testing"
1414

1515
"code.gitea.io/gitea/models"
16+
"code.gitea.io/gitea/models/db"
1617
repo_model "code.gitea.io/gitea/models/repo"
1718
"code.gitea.io/gitea/models/unittest"
1819
user_model "code.gitea.io/gitea/models/user"
@@ -47,7 +48,7 @@ func testMirrorPush(t *testing.T, u *url.URL) {
4748

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

50-
mirrors, err := repo_model.GetPushMirrorsByRepoID(srcRepo.ID)
51+
mirrors, err := repo_model.GetPushMirrorsByRepoID(srcRepo.ID, db.ListOptions{})
5152
assert.NoError(t, err)
5253
assert.Len(t, mirrors, 1)
5354

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

7374
// Cleanup
7475
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)
75-
mirrors, err = repo_model.GetPushMirrorsByRepoID(srcRepo.ID)
76+
mirrors, err = repo_model.GetPushMirrorsByRepoID(srcRepo.ID, db.ListOptions{})
7677
assert.NoError(t, err)
7778
assert.Len(t, mirrors, 0)
7879
}

models/repo/pushmirror.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,13 @@ func GetPushMirrorByID(ID int64) (*PushMirror, error) {
8888
}
8989

9090
// GetPushMirrorsByRepoID returns push-mirror information of a repository.
91-
func GetPushMirrorsByRepoID(repoID int64) ([]*PushMirror, error) {
91+
func GetPushMirrorsByRepoID(repoID int64, listOptions db.ListOptions) ([]*PushMirror, error) {
9292
mirrors := make([]*PushMirror, 0, 10)
93-
return mirrors, db.GetEngine(db.DefaultContext).Where("repo_id=?", repoID).Find(&mirrors)
93+
sess := db.GetEngine(db.DefaultContext).Where("repo_id = ?", repoID)
94+
if listOptions.Page != 0 {
95+
sess = db.SetSessionPagination(sess, &listOptions)
96+
}
97+
return mirrors, sess.Find(&mirrors)
9498
}
9599

96100
// PushMirrorsIterate iterates all push-mirror repositories.

modules/context/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ func repoAssignment(ctx *Context, repo *repo_model.Repository) {
398398
}
399399
}
400400

401-
pushMirrors, err := repo_model.GetPushMirrorsByRepoID(repo.ID)
401+
pushMirrors, err := repo_model.GetPushMirrorsByRepoID(repo.ID, db.ListOptions{})
402402
if err != nil {
403403
ctx.ServerError("GetPushMirrorsByRepoID", err)
404404
return

modules/convert/convert.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,4 @@ func ToLFSLock(l *models.LFSLock) *api.LFSLock {
413413
},
414414
}
415415
}
416+

modules/convert/mirror.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package convert
6+
7+
import (
8+
repo_model "code.gitea.io/gitea/models/repo"
9+
"code.gitea.io/gitea/modules/git"
10+
api "code.gitea.io/gitea/modules/structs"
11+
)
12+
13+
// ToPushMirror convert from repo_model.PushMirror and remoteAddress to api.TopicResponse
14+
func ToPushMirror(pm *repo_model.PushMirror, repo *repo_model.Repository) *api.PushMirror {
15+
remoteAddress, _ := getMirrorRemoteAddress(repo, pm.RemoteName)
16+
return &api.PushMirror{
17+
ID: pm.ID,
18+
RepoName: repo.Name,
19+
RemoteName: pm.RemoteName,
20+
RemoteAddress: remoteAddress,
21+
CreatedUnix: pm.CreatedUnix.FormatLong(),
22+
LastUpdateUnix: pm.LastUpdateUnix.FormatLong(),
23+
LastError: pm.LastError,
24+
Interval: pm.Interval.String(),
25+
}
26+
}
27+
28+
func getMirrorRemoteAddress(repo *repo_model.Repository, remoteName string) (string, error) {
29+
u, err := git.GetRemoteAddress(git.DefaultContext, repo.RepoPath(), remoteName)
30+
if err != nil {
31+
return "", err
32+
}
33+
// remove confidential information
34+
u.User = nil
35+
return u.String(), nil
36+
}

modules/structs/mirror.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package structs
6+
7+
// CreatePushMirrorOption represents need information to create a push mirror of a repository.
8+
type CreatePushMirrorOption struct {
9+
RemoteAddress string `json:"remoteAddress"`
10+
RemoteUsername string `json:"remoteUsername"`
11+
RemotePassword string `json:"remotePassword"`
12+
Interval string `json:"interval"`
13+
}
14+
15+
// PushMirror represents information of a push mirror
16+
// swagger:model
17+
type PushMirror struct {
18+
ID int64 `json:"id"`
19+
RepoName string `json:"repoName"`
20+
RemoteName string `json:"remoteName"`
21+
RemoteAddress string `json:"remoteAddress"`
22+
CreatedUnix string `json:"created"`
23+
LastUpdateUnix string `json:"lastUpdate"`
24+
LastError string `json:"lastError"`
25+
Interval string `json:"interval"`
26+
}

package-lock.json

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

routers/api/v1/api.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,15 @@ func Routes() *web.Route {
973973
})
974974
}, reqRepoReader(unit.TypeReleases))
975975
m.Post("/mirror-sync", reqToken(), reqRepoWriter(unit.TypeCode), repo.MirrorSync)
976+
m.Post("/push-mirror-sync", reqAdmin(), repo.PushMirrorSync)
977+
m.Group("/push-mirror", func() {
978+
m.Combo("").Get(reqAdmin(), repo.ListPushMirrors).
979+
Post(reqAdmin(), bind(api.CreatePushMirrorOption{}), repo.AddPushMirror)
980+
m.Combo("/{id}").
981+
Delete(reqAdmin(), repo.DeletePushMirrorByID).
982+
Get(reqAdmin(), repo.GetPushMirrorByID)
983+
})
984+
976985
m.Get("/editorconfig/{filename}", context.ReferencesGitRepo(), context.RepoRefForAPI, reqRepoReader(unit.TypeCode), repo.GetEditorconfig)
977986
m.Group("/pulls", func() {
978987
m.Combo("").Get(repo.ListPullRequests).

0 commit comments

Comments
 (0)