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

Add MirrorInterval to the API #14163

Merged
merged 14 commits into from
Jan 2, 2021
7 changes: 7 additions & 0 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,12 @@ func (repo *Repository) GetMirror() (err error) {
return err
}

// UpdateMirror updates the repository mirror, returns an error upon failure
func (repo *Repository) UpdateMirror(m *Mirror) (err error) {
err = UpdateMirror(m)
return err
}

// GetBaseRepo populates repo.BaseRepo for a fork repository and
// returns an error on failure (NOTE: no error is returned for
// non-fork repositories, and BaseRepo will be left untouched)
Expand Down Expand Up @@ -979,6 +985,7 @@ type CreateRepoOptions struct {
AutoInit bool
Status RepositoryStatus
TrustModel TrustModelType
MirrorInterval string
}

// GetRepoInitFile returns repository init files
Expand Down
21 changes: 11 additions & 10 deletions modules/auth/repo_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,17 @@ type MigrateRepoForm struct {
// required: true
UID int64 `json:"uid" binding:"Required"`
// required: true
RepoName string `json:"repo_name" binding:"Required;AlphaDashDot;MaxSize(100)"`
Mirror bool `json:"mirror"`
Private bool `json:"private"`
Description string `json:"description" binding:"MaxSize(255)"`
Wiki bool `json:"wiki"`
Milestones bool `json:"milestones"`
Labels bool `json:"labels"`
Issues bool `json:"issues"`
PullRequests bool `json:"pull_requests"`
Releases bool `json:"releases"`
RepoName string `json:"repo_name" binding:"Required;AlphaDashDot;MaxSize(100)"`
Mirror bool `json:"mirror"`
Private bool `json:"private"`
Description string `json:"description" binding:"MaxSize(255)"`
Wiki bool `json:"wiki"`
Milestones bool `json:"milestones"`
Labels bool `json:"labels"`
Issues bool `json:"issues"`
PullRequests bool `json:"pull_requests"`
Releases bool `json:"releases"`
MirrorInterval string `json:"mirror_interval"`
}

// Validate validates the fields
Expand Down
1 change: 1 addition & 0 deletions modules/migrations/base/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ type MigrateOptions struct {
PullRequests bool
ReleaseAssets bool
MigrateToRepoID int64
MirrorInterval string `json:"mirror_interval"`
}
1 change: 1 addition & 0 deletions modules/migrations/gitea_uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.Migrate
Private: repo.IsPrivate,
Wiki: opts.Wiki,
Releases: opts.Releases, // if didn't get releases, then sync them from tags
MirrorInterval: opts.MirrorInterval,
})

g.repo = r
Expand Down
13 changes: 11 additions & 2 deletions modules/repository/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,20 @@ func MigrateRepositoryGitData(ctx context.Context, u *models.User, repo *models.
}

if opts.Mirror {
var interval = setting.Mirror.DefaultInterval
if opts.MirrorInterval != "" {
if interval, err = time.ParseDuration(opts.MirrorInterval); err != nil {
// Reset to Default or Raise Error...
// interval = setting.Mirror.DefaultInterval
log.Error("Failed to set Interval: %v", err)
return repo, err
}
}
if err = models.InsertMirror(&models.Mirror{
RepoID: repo.ID,
Interval: setting.Mirror.DefaultInterval,
Interval: interval,
EnablePrune: true,
NextUpdateUnix: timeutil.TimeStampNow().AddDuration(setting.Mirror.DefaultInterval),
NextUpdateUnix: timeutil.TimeStampNow().AddDuration(interval),
}); err != nil {
return repo, fmt.Errorf("InsertOne: %v", err)
}
Expand Down
24 changes: 15 additions & 9 deletions modules/structs/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type Repository struct {
AllowSquash bool `json:"allow_squash_merge"`
AvatarURL string `json:"avatar_url"`
Internal bool `json:"internal"`
MirrorInterval string `json:"mirror_interval"`
}

// CreateRepoOption options when creating repository
Expand Down Expand Up @@ -122,6 +123,8 @@ type CreateRepoOption struct {
// TrustModel of the repository
// enum: default,collaborator,committer,collaboratorcommitter
TrustModel string `json:"trust_model"`
// MirrorInterval time when creating a mirror (used with mirrors)
MirrorInterval string `json:"mirror_interval"`
}

// EditRepoOption options when editing a repository's properties
Expand Down Expand Up @@ -168,6 +171,8 @@ type EditRepoOption struct {
AllowSquash *bool `json:"allow_squash_merge,omitempty"`
// set to `true` to archive this repository.
Archived *bool `json:"archived,omitempty"`
// set to a string like `8h30m0s` to set the mirror interval time
MirrorInterval *string `json:"mirror_interval,omitempty"`
}

// CreateBranchRepoOption options when creating a branch in a repository
Expand Down Expand Up @@ -249,15 +254,16 @@ type MigrateRepoOptions struct {
AuthPassword string `json:"auth_password"`
AuthToken string `json:"auth_token"`

Mirror bool `json:"mirror"`
Private bool `json:"private"`
Description string `json:"description" binding:"MaxSize(255)"`
Wiki bool `json:"wiki"`
Milestones bool `json:"milestones"`
Labels bool `json:"labels"`
Issues bool `json:"issues"`
PullRequests bool `json:"pull_requests"`
Releases bool `json:"releases"`
Mirror bool `json:"mirror"`
Private bool `json:"private"`
Description string `json:"description" binding:"MaxSize(255)"`
Wiki bool `json:"wiki"`
Milestones bool `json:"milestones"`
Labels bool `json:"labels"`
Issues bool `json:"issues"`
PullRequests bool `json:"pull_requests"`
Releases bool `json:"releases"`
MirrorInterval string `json:"mirror_interval"`
}

// TokenAuth represents whether a service type supports token-based auth
Expand Down
3 changes: 3 additions & 0 deletions routers/api/v1/repo/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ func Migrate(ctx *context.APIContext, form api.MigrateRepoOptions) {
return
}

log.Info("Mirror Interval Party:", form.MirrorInterval)

var opts = migrations.MigrateOptions{
CloneAddr: remoteAddr,
RepoName: form.RepoName,
Expand All @@ -141,6 +143,7 @@ func Migrate(ctx *context.APIContext, form api.MigrateRepoOptions) {
PullRequests: form.PullRequests,
Releases: form.Releases,
GitServiceType: gitServiceType,
MirrorInterval: form.MirrorInterval,
}
if opts.Mirror {
opts.Issues = false
Expand Down
53 changes: 42 additions & 11 deletions routers/api/v1/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"net/http"
"strings"
"time"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
Expand Down Expand Up @@ -242,17 +243,18 @@ func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateR
opt.Readme = "Default"
}
repo, err := repo_service.CreateRepository(ctx.User, owner, models.CreateRepoOptions{
Name: opt.Name,
Description: opt.Description,
IssueLabels: opt.IssueLabels,
Gitignores: opt.Gitignores,
License: opt.License,
Readme: opt.Readme,
IsPrivate: opt.Private,
AutoInit: opt.AutoInit,
DefaultBranch: opt.DefaultBranch,
TrustModel: models.ToTrustModel(opt.TrustModel),
IsTemplate: opt.Template,
Name: opt.Name,
Description: opt.Description,
IssueLabels: opt.IssueLabels,
Gitignores: opt.Gitignores,
License: opt.License,
Readme: opt.Readme,
IsPrivate: opt.Private,
AutoInit: opt.AutoInit,
DefaultBranch: opt.DefaultBranch,
TrustModel: models.ToTrustModel(opt.TrustModel),
IsTemplate: opt.Template,
MirrorInterval: opt.MirrorInterval,
})
if err != nil {
if models.IsErrRepoAlreadyExist(err) {
Expand Down Expand Up @@ -501,6 +503,12 @@ func Edit(ctx *context.APIContext, opts api.EditRepoOption) {
}
}

if opts.MirrorInterval != nil {
if err := updateMirrorInterval(ctx, opts); err != nil {
return
}
}

ctx.JSON(http.StatusOK, convert.ToRepo(ctx.Repo.Repository, ctx.Repo.AccessMode))
}

Expand Down Expand Up @@ -783,6 +791,29 @@ func updateRepoArchivedState(ctx *context.APIContext, opts api.EditRepoOption) e
return nil
}

// updateMirrorInterval updates the repo's mirror Interval
func updateMirrorInterval(ctx *context.APIContext, opts api.EditRepoOption) error {
repo := ctx.Repo.Repository

if opts.MirrorInterval != nil {
if err := repo.GetMirror(); err != nil {
return err
}
if interval, err := time.ParseDuration(*opts.MirrorInterval); err == nil {
repo.Mirror.Interval = interval
if err := repo.UpdateMirror(repo.Mirror); err != nil {
log.Error("Failed to Set Mirror Interval: %s", err)
ctx.Error(http.StatusInternalServerError, "MirrorInterval", err)
return err
}
} else {
log.Error("Wrong format for MirrorInternal Sent: %s", err)
ctx.Error(http.StatusInternalServerError, "MirrorInterval", err)
}
}
return nil
}

// Delete one repository
func Delete(ctx *context.APIContext) {
// swagger:operation DELETE /repos/{owner}/{repo} repository repoDelete
Expand Down
22 changes: 22 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12491,6 +12491,11 @@
"type": "string",
"x-go-name": "License"
},
"mirror_interval": {
"description": "MirrorInterval time when creating a mirror (used with mirrors)",
"type": "string",
"x-go-name": "MirrorInterval"
},
"name": {
"description": "Name of the repository to create",
"type": "string",
Expand Down Expand Up @@ -13263,6 +13268,11 @@
"internal_tracker": {
"$ref": "#/definitions/InternalTracker"
},
"mirror_interval": {
"description": "set to a string like `8h30m0s` to set the mirror interval time",
"type": "string",
"x-go-name": "MirrorInterval"
},
"name": {
"description": "name of the repository",
"type": "string",
Expand Down Expand Up @@ -14248,6 +14258,10 @@
"type": "boolean",
"x-go-name": "Mirror"
},
"mirror_interval": {
"type": "string",
"x-go-name": "MirrorInterval"
},
"private": {
"type": "boolean",
"x-go-name": "Private"
Expand Down Expand Up @@ -14323,6 +14337,10 @@
"type": "boolean",
"x-go-name": "Mirror"
},
"mirror_interval": {
"type": "string",
"x-go-name": "MirrorInterval"
},
"private": {
"type": "boolean",
"x-go-name": "Private"
Expand Down Expand Up @@ -15307,6 +15325,10 @@
"type": "boolean",
"x-go-name": "Mirror"
},
"mirror_interval": {
"type": "string",
"x-go-name": "MirrorInterval"
},
"name": {
"type": "string",
"x-go-name": "Name"
Expand Down