Skip to content

Commit a3f9e2b

Browse files
committed
Added MirrorInterval to the API
1 parent f6ca44c commit a3f9e2b

File tree

9 files changed

+113
-32
lines changed

9 files changed

+113
-32
lines changed

models/repo.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,12 @@ func (repo *Repository) GetMirror() (err error) {
651651
return err
652652
}
653653

654+
// UpdateMirror updates the repository mirror, returns an error upon failure
655+
func (repo *Repository) UpdateMirror(m *Mirror) (err error) {
656+
err = UpdateMirror(m)
657+
return err
658+
}
659+
654660
// GetBaseRepo populates repo.BaseRepo for a fork repository and
655661
// returns an error on failure (NOTE: no error is returned for
656662
// non-fork repositories, and BaseRepo will be left untouched)
@@ -979,6 +985,7 @@ type CreateRepoOptions struct {
979985
AutoInit bool
980986
Status RepositoryStatus
981987
TrustModel TrustModelType
988+
MirrorInterval string
982989
}
983990

984991
// GetRepoInitFile returns repository init files

modules/auth/repo_form.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,17 @@ type MigrateRepoForm struct {
6868
// required: true
6969
UID int64 `json:"uid" binding:"Required"`
7070
// required: true
71-
RepoName string `json:"repo_name" binding:"Required;AlphaDashDot;MaxSize(100)"`
72-
Mirror bool `json:"mirror"`
73-
Private bool `json:"private"`
74-
Description string `json:"description" binding:"MaxSize(255)"`
75-
Uncyclo bool `json:"wiki"`
76-
Milestones bool `json:"milestones"`
77-
Labels bool `json:"labels"`
78-
Issues bool `json:"issues"`
79-
PullRequests bool `json:"pull_requests"`
80-
Releases bool `json:"releases"`
71+
RepoName string `json:"repo_name" binding:"Required;AlphaDashDot;MaxSize(100)"`
72+
Mirror bool `json:"mirror"`
73+
Private bool `json:"private"`
74+
Description string `json:"description" binding:"MaxSize(255)"`
75+
Uncyclo bool `json:"wiki"`
76+
Milestones bool `json:"milestones"`
77+
Labels bool `json:"labels"`
78+
Issues bool `json:"issues"`
79+
PullRequests bool `json:"pull_requests"`
80+
Releases bool `json:"releases"`
81+
MirrorInterval string `json:"mirror_interval"`
8182
}
8283

8384
// Validate validates the fields

modules/migrations/base/options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ type MigrateOptions struct {
3333
PullRequests bool
3434
ReleaseAssets bool
3535
MigrateToRepoID int64
36+
MirrorInterval string `json:"mirror_interval"`
3637
}

modules/migrations/gitea_uploader.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.Migrate
142142
Private: repo.IsPrivate,
143143
Uncyclo: opts.Uncyclo,
144144
Releases: opts.Releases, // if didn't get releases, then sync them from tags
145+
MirrorInterval: opts.MirrorInterval,
145146
})
146147

147148
g.repo = r

modules/repository/repo.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,20 @@ func MigrateRepositoryGitData(ctx context.Context, u *models.User, repo *models.
127127
}
128128

129129
if opts.Mirror {
130+
var interval = setting.Mirror.DefaultInterval
131+
if opts.MirrorInterval != "" {
132+
if interval, err = time.ParseDuration(opts.MirrorInterval); err != nil {
133+
// Reset to Default or Raise Error...
134+
// interval = setting.Mirror.DefaultInterval
135+
log.Error("Failed to set Interval: %v", err)
136+
return repo, err
137+
}
138+
}
130139
if err = models.InsertMirror(&models.Mirror{
131140
RepoID: repo.ID,
132-
Interval: setting.Mirror.DefaultInterval,
141+
Interval: interval,
133142
EnablePrune: true,
134-
NextUpdateUnix: timeutil.TimeStampNow().AddDuration(setting.Mirror.DefaultInterval),
143+
NextUpdateUnix: timeutil.TimeStampNow().AddDuration(interval),
135144
}); err != nil {
136145
return repo, fmt.Errorf("InsertOne: %v", err)
137146
}

modules/structs/repo.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ type Repository struct {
9191
AllowSquash bool `json:"allow_squash_merge"`
9292
AvatarURL string `json:"avatar_url"`
9393
Internal bool `json:"internal"`
94+
MirrorInterval string `json:"mirror_interval"`
9495
}
9596

9697
// CreateRepoOption options when creating repository
@@ -122,6 +123,8 @@ type CreateRepoOption struct {
122123
// TrustModel of the repository
123124
// enum: default,collaborator,committer,collaboratorcommitter
124125
TrustModel string `json:"trust_model"`
126+
// MirrorInterval time when creating a mirror (used with mirrors)
127+
MirrorInterval string `json:"mirror_interval"`
125128
}
126129

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

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

252-
Mirror bool `json:"mirror"`
253-
Private bool `json:"private"`
254-
Description string `json:"description" binding:"MaxSize(255)"`
255-
Uncyclo bool `json:"wiki"`
256-
Milestones bool `json:"milestones"`
257-
Labels bool `json:"labels"`
258-
Issues bool `json:"issues"`
259-
PullRequests bool `json:"pull_requests"`
260-
Releases bool `json:"releases"`
257+
Mirror bool `json:"mirror"`
258+
Private bool `json:"private"`
259+
Description string `json:"description" binding:"MaxSize(255)"`
260+
Uncyclo bool `json:"wiki"`
261+
Milestones bool `json:"milestones"`
262+
Labels bool `json:"labels"`
263+
Issues bool `json:"issues"`
264+
PullRequests bool `json:"pull_requests"`
265+
Releases bool `json:"releases"`
266+
MirrorInterval string `json:"mirror_interval"`
261267
}
262268

263269
// TokenAuth represents whether a service type supports token-based auth

routers/api/v1/repo/migrate.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ func Migrate(ctx *context.APIContext, form api.MigrateRepoOptions) {
124124
return
125125
}
126126

127+
log.Info("Mirror Interval Party:", form.MirrorInterval)
128+
127129
var opts = migrations.MigrateOptions{
128130
CloneAddr: remoteAddr,
129131
RepoName: form.RepoName,
@@ -141,6 +143,7 @@ func Migrate(ctx *context.APIContext, form api.MigrateRepoOptions) {
141143
PullRequests: form.PullRequests,
142144
Releases: form.Releases,
143145
GitServiceType: gitServiceType,
146+
MirrorInterval: form.MirrorInterval,
144147
}
145148
if opts.Mirror {
146149
opts.Issues = false

routers/api/v1/repo/repo.go

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"net/http"
1111
"strings"
12+
"time"
1213

1314
"code.gitea.io/gitea/models"
1415
"code.gitea.io/gitea/modules/context"
@@ -242,17 +243,18 @@ func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateR
242243
opt.Readme = "Default"
243244
}
244245
repo, err := repo_service.CreateRepository(ctx.User, owner, models.CreateRepoOptions{
245-
Name: opt.Name,
246-
Description: opt.Description,
247-
IssueLabels: opt.IssueLabels,
248-
Gitignores: opt.Gitignores,
249-
License: opt.License,
250-
Readme: opt.Readme,
251-
IsPrivate: opt.Private,
252-
AutoInit: opt.AutoInit,
253-
DefaultBranch: opt.DefaultBranch,
254-
TrustModel: models.ToTrustModel(opt.TrustModel),
255-
IsTemplate: opt.Template,
246+
Name: opt.Name,
247+
Description: opt.Description,
248+
IssueLabels: opt.IssueLabels,
249+
Gitignores: opt.Gitignores,
250+
License: opt.License,
251+
Readme: opt.Readme,
252+
IsPrivate: opt.Private,
253+
AutoInit: opt.AutoInit,
254+
DefaultBranch: opt.DefaultBranch,
255+
TrustModel: models.ToTrustModel(opt.TrustModel),
256+
IsTemplate: opt.Template,
257+
MirrorInterval: opt.MirrorInterval,
256258
})
257259
if err != nil {
258260
if models.IsErrRepoAlreadyExist(err) {
@@ -501,6 +503,12 @@ func Edit(ctx *context.APIContext, opts api.EditRepoOption) {
501503
}
502504
}
503505

506+
if opts.MirrorInterval != nil {
507+
if err := updateMirrorInterval(ctx, opts); err != nil {
508+
return
509+
}
510+
}
511+
504512
ctx.JSON(http.StatusOK, convert.ToRepo(ctx.Repo.Repository, ctx.Repo.AccessMode))
505513
}
506514

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

794+
// updateMirrorInterval updates the repo's mirror Interval
795+
func updateMirrorInterval(ctx *context.APIContext, opts api.EditRepoOption) error {
796+
repo := ctx.Repo.Repository
797+
798+
if opts.MirrorInterval != nil {
799+
if err := repo.GetMirror(); err != nil {
800+
return err
801+
}
802+
if interval, err := time.ParseDuration(*opts.MirrorInterval); err == nil {
803+
repo.Mirror.Interval = interval
804+
if err := repo.UpdateMirror(repo.Mirror); err != nil {
805+
log.Error("Failed to Set Mirror Interval: %s", err)
806+
ctx.Error(http.StatusInternalServerError, "MirrorInterval", err)
807+
return err
808+
}
809+
} else {
810+
log.Error("Wrong format for MirrorInternal Sent: %s", err)
811+
ctx.Error(http.StatusInternalServerError, "MirrorInterval", err)
812+
}
813+
}
814+
return nil
815+
}
816+
786817
// Delete one repository
787818
func Delete(ctx *context.APIContext) {
788819
// swagger:operation DELETE /repos/{owner}/{repo} repository repoDelete

templates/swagger/v1_json.tmpl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12491,6 +12491,11 @@
1249112491
"type": "string",
1249212492
"x-go-name": "License"
1249312493
},
12494+
"mirror_interval": {
12495+
"description": "MirrorInterval time when creating a mirror (used with mirrors)",
12496+
"type": "string",
12497+
"x-go-name": "MirrorInterval"
12498+
},
1249412499
"name": {
1249512500
"description": "Name of the repository to create",
1249612501
"type": "string",
@@ -13263,6 +13268,11 @@
1326313268
"internal_tracker": {
1326413269
"$ref": "#/definitions/InternalTracker"
1326513270
},
13271+
"mirror_interval": {
13272+
"description": "set to a string like `8h30m0s` to set the mirror interval time",
13273+
"type": "string",
13274+
"x-go-name": "MirrorInterval"
13275+
},
1326613276
"name": {
1326713277
"description": "name of the repository",
1326813278
"type": "string",
@@ -14248,6 +14258,10 @@
1424814258
"type": "boolean",
1424914259
"x-go-name": "Mirror"
1425014260
},
14261+
"mirror_interval": {
14262+
"type": "string",
14263+
"x-go-name": "MirrorInterval"
14264+
},
1425114265
"private": {
1425214266
"type": "boolean",
1425314267
"x-go-name": "Private"
@@ -14323,6 +14337,10 @@
1432314337
"type": "boolean",
1432414338
"x-go-name": "Mirror"
1432514339
},
14340+
"mirror_interval": {
14341+
"type": "string",
14342+
"x-go-name": "MirrorInterval"
14343+
},
1432614344
"private": {
1432714345
"type": "boolean",
1432814346
"x-go-name": "Private"
@@ -15307,6 +15325,10 @@
1530715325
"type": "boolean",
1530815326
"x-go-name": "Mirror"
1530915327
},
15328+
"mirror_interval": {
15329+
"type": "string",
15330+
"x-go-name": "MirrorInterval"
15331+
},
1531015332
"name": {
1531115333
"type": "string",
1531215334
"x-go-name": "Name"

0 commit comments

Comments
 (0)