Skip to content

Commit

Permalink
add new endpoints for push mirrors management:
Browse files Browse the repository at this point in the history
* [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 <mohamed.sekour@exfo.com>
  • Loading branch information
mohsek committed May 31, 2022
1 parent d2a91e5 commit cf10d77
Show file tree
Hide file tree
Showing 17 changed files with 839 additions and 10 deletions.
61 changes: 61 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,67 @@ steps:
exclude:
- pull_request

---
kind: pipeline
name: docker-linux-amd64-release-branch

platform:
os: linux
arch: amd64

depends_on:
- testing-amd64
- testing-arm64

trigger:
ref:
- "refs/heads/release/v*"
event:
exclude:
- cron

steps:
- name: fetch-tags
image: docker:git
commands:
- git fetch --tags --force

- name: publish
pull: always
image: techknowlogick/drone-docker:latest
settings:
auto_tag: false
tags: ${DRONE_BRANCH##release/v}-dev-linux-amd64
repo: gitea/gitea
build_args:
- GOPROXY=https://goproxy.cn
password:
from_secret: docker_password
username:
from_secret: docker_username
when:
event:
exclude:
- pull_request

- name: publish-rootless
image: techknowlogick/drone-docker:latest
settings:
dockerfile: Dockerfile.rootless
auto_tag: false
tags: ${DRONE_BRANCH##release/v}-dev-linux-amd64-rootless
repo: gitea/gitea
build_args:
- GOPROXY=https://goproxy.cn
password:
from_secret: docker_password
username:
from_secret: docker_username
when:
event:
exclude:
- pull_request

---
kind: pipeline
type: docker
Expand Down
11 changes: 11 additions & 0 deletions integrations/git_helper_for_declarative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ func doPartialGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
}
}

func doPartialGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
return func(t *testing.T) {
assert.NoError(t, git.CloneWithArgs(context.Background(), u.String(), dstLocalPath, allowLFSFilters(), git.CloneRepoOptions{
Filter: "blob:none",
}))
exist, err := util.IsExist(filepath.Join(dstLocalPath, "README.md"))
assert.NoError(t, err)
assert.True(t, exist)
}
}

func doGitCloneFail(u *url.URL) func(*testing.T) {
return func(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "doGitCloneFail")
Expand Down
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
8 changes: 6 additions & 2 deletions models/repo/pushmirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,13 @@ func GetPushMirrorByID(ID int64) (*PushMirror, error) {
}

// GetPushMirrorsByRepoID returns push-mirror information of a repository.
func GetPushMirrorsByRepoID(repoID int64) ([]*PushMirror, error) {
func GetPushMirrorsByRepoID(repoID int64, listOptions db.ListOptions) ([]*PushMirror, error) {
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
1 change: 1 addition & 0 deletions modules/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,4 @@ func ToLFSLock(l *models.LFSLock) *api.LFSLock {
},
}
}

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)
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) {
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"`
}
53 changes: 53 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

0 comments on commit cf10d77

Please sign in to comment.