Skip to content

Commit 7576e37

Browse files
paulbarton906543zeripath
authored
Add MirrorInterval to the API (#14163)
* Added MirrorInterval to the API * Remove MirrorInterval from CreateRepository * Removed Duplicate UpdateMirror Function * Updated Error Logging * Update Log Message for is not Mirror Co-authored-by: 6543 <[email protected]> * Delete Debug Statement that snuck in Co-authored-by: zeripath <[email protected]> * Add Check for If Interval is too small * Output to API Call * Add Error Object when time is Less than Min Interval * Frequency Error Message Co-authored-by: zeripath <[email protected]> * Allow Zero Mirror Interval Co-authored-by: 6543 <[email protected]> Co-authored-by: zeripath <[email protected]>
1 parent 3abea9e commit 7576e37

File tree

10 files changed

+115
-21
lines changed

10 files changed

+115
-21
lines changed

models/repo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,7 @@ type CreateRepoOptions struct {
979979
AutoInit bool
980980
Status RepositoryStatus
981981
TrustModel TrustModelType
982+
MirrorInterval string
982983
}
983984

984985
// 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-
Wiki 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+
Wiki 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/convert/repository.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ func innerToRepo(repo *models.Repository, mode models.AccessMode, isParent bool)
9191

9292
numReleases, _ := models.GetReleaseCountByRepoID(repo.ID, models.FindReleasesOptions{IncludeDrafts: false, IncludeTags: true})
9393

94+
mirrorInterval := ""
95+
if repo.IsMirror {
96+
if err := repo.GetMirror(); err == nil {
97+
mirrorInterval = repo.Mirror.Interval.String()
98+
}
99+
}
100+
94101
return &api.Repository{
95102
ID: repo.ID,
96103
Owner: ToUser(repo.Owner, mode != models.AccessModeNone, mode >= models.AccessModeAdmin),
@@ -134,5 +141,6 @@ func innerToRepo(repo *models.Repository, mode models.AccessMode, isParent bool)
134141
AllowSquash: allowSquash,
135142
AvatarURL: repo.AvatarLink(),
136143
Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate,
144+
MirrorInterval: mirrorInterval,
137145
}
138146
}

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
Wiki: opts.Wiki,
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: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,33 @@ func MigrateRepositoryGitData(ctx context.Context, u *models.User, repo *models.
127127
}
128128

129129
if opts.Mirror {
130-
if err = models.InsertMirror(&models.Mirror{
130+
mirrorModel := models.Mirror{
131131
RepoID: repo.ID,
132132
Interval: setting.Mirror.DefaultInterval,
133133
EnablePrune: true,
134134
NextUpdateUnix: timeutil.TimeStampNow().AddDuration(setting.Mirror.DefaultInterval),
135-
}); err != nil {
135+
}
136+
137+
if opts.MirrorInterval != "" {
138+
parsedInterval, err := time.ParseDuration(opts.MirrorInterval)
139+
if err != nil {
140+
log.Error("Failed to set Interval: %v", err)
141+
return repo, err
142+
}
143+
if parsedInterval == 0 {
144+
mirrorModel.Interval = 0
145+
mirrorModel.NextUpdateUnix = 0
146+
} else if parsedInterval < setting.Mirror.MinInterval {
147+
err := fmt.Errorf("Interval %s is set below Minimum Interval of %s", parsedInterval, setting.Mirror.MinInterval)
148+
log.Error("Interval: %s is too frequent", opts.MirrorInterval)
149+
return repo, err
150+
} else {
151+
mirrorModel.Interval = parsedInterval
152+
mirrorModel.NextUpdateUnix = timeutil.TimeStampNow().AddDuration(parsedInterval)
153+
}
154+
}
155+
156+
if err = models.InsertMirror(&mirrorModel); err != nil {
136157
return repo, fmt.Errorf("InsertOne: %v", err)
137158
}
138159

modules/structs/repo.go

Lines changed: 13 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
@@ -168,6 +169,8 @@ type EditRepoOption struct {
168169
AllowSquash *bool `json:"allow_squash_merge,omitempty"`
169170
// set to `true` to archive this repository.
170171
Archived *bool `json:"archived,omitempty"`
172+
// set to a string like `8h30m0s` to set the mirror interval time
173+
MirrorInterval *string `json:"mirror_interval,omitempty"`
171174
}
172175

173176
// CreateBranchRepoOption options when creating a branch in a repository
@@ -249,15 +252,16 @@ type MigrateRepoOptions struct {
249252
AuthPassword string `json:"auth_password"`
250253
AuthToken string `json:"auth_token"`
251254

252-
Mirror bool `json:"mirror"`
253-
Private bool `json:"private"`
254-
Description string `json:"description" binding:"MaxSize(255)"`
255-
Wiki 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"`
255+
Mirror bool `json:"mirror"`
256+
Private bool `json:"private"`
257+
Description string `json:"description" binding:"MaxSize(255)"`
258+
Wiki bool `json:"wiki"`
259+
Milestones bool `json:"milestones"`
260+
Labels bool `json:"labels"`
261+
Issues bool `json:"issues"`
262+
PullRequests bool `json:"pull_requests"`
263+
Releases bool `json:"releases"`
264+
MirrorInterval string `json:"mirror_interval"`
261265
}
262266

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

routers/api/v1/repo/migrate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ func Migrate(ctx *context.APIContext, form api.MigrateRepoOptions) {
141141
PullRequests: form.PullRequests,
142142
Releases: form.Releases,
143143
GitServiceType: gitServiceType,
144+
MirrorInterval: form.MirrorInterval,
144145
}
145146
if opts.Mirror {
146147
opts.Issues = false

routers/api/v1/repo/repo.go

Lines changed: 39 additions & 0 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"
@@ -501,6 +502,12 @@ func Edit(ctx *context.APIContext, opts api.EditRepoOption) {
501502
}
502503
}
503504

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

@@ -783,6 +790,38 @@ func updateRepoArchivedState(ctx *context.APIContext, opts api.EditRepoOption) e
783790
return nil
784791
}
785792

793+
// updateMirrorInterval updates the repo's mirror Interval
794+
func updateMirrorInterval(ctx *context.APIContext, opts api.EditRepoOption) error {
795+
repo := ctx.Repo.Repository
796+
797+
if opts.MirrorInterval != nil {
798+
if !repo.IsMirror {
799+
err := fmt.Errorf("repo is not a mirror, can not change mirror interval")
800+
ctx.Error(http.StatusUnprocessableEntity, err.Error(), err)
801+
return err
802+
}
803+
if err := repo.GetMirror(); err != nil {
804+
log.Error("Failed to get mirror: %s", err)
805+
ctx.Error(http.StatusInternalServerError, "MirrorInterval", err)
806+
return err
807+
}
808+
if interval, err := time.ParseDuration(*opts.MirrorInterval); err == nil {
809+
repo.Mirror.Interval = interval
810+
if err := models.UpdateMirror(repo.Mirror); err != nil {
811+
log.Error("Failed to Set Mirror Interval: %s", err)
812+
ctx.Error(http.StatusUnprocessableEntity, "MirrorInterval", err)
813+
return err
814+
}
815+
log.Trace("Repository %s/%s Mirror Interval was Updated to %s", ctx.Repo.Owner.Name, repo.Name, interval)
816+
} else {
817+
log.Error("Wrong format for MirrorInternal Sent: %s", err)
818+
ctx.Error(http.StatusUnprocessableEntity, "MirrorInterval", err)
819+
return err
820+
}
821+
}
822+
return nil
823+
}
824+
786825
// Delete one repository
787826
func Delete(ctx *context.APIContext) {
788827
// swagger:operation DELETE /repos/{owner}/{repo} repository repoDelete

templates/swagger/v1_json.tmpl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13263,6 +13263,11 @@
1326313263
"internal_tracker": {
1326413264
"$ref": "#/definitions/InternalTracker"
1326513265
},
13266+
"mirror_interval": {
13267+
"description": "set to a string like `8h30m0s` to set the mirror interval time",
13268+
"type": "string",
13269+
"x-go-name": "MirrorInterval"
13270+
},
1326613271
"name": {
1326713272
"description": "name of the repository",
1326813273
"type": "string",
@@ -14248,6 +14253,10 @@
1424814253
"type": "boolean",
1424914254
"x-go-name": "Mirror"
1425014255
},
14256+
"mirror_interval": {
14257+
"type": "string",
14258+
"x-go-name": "MirrorInterval"
14259+
},
1425114260
"private": {
1425214261
"type": "boolean",
1425314262
"x-go-name": "Private"
@@ -14323,6 +14332,10 @@
1432314332
"type": "boolean",
1432414333
"x-go-name": "Mirror"
1432514334
},
14335+
"mirror_interval": {
14336+
"type": "string",
14337+
"x-go-name": "MirrorInterval"
14338+
},
1432614339
"private": {
1432714340
"type": "boolean",
1432814341
"x-go-name": "Private"
@@ -15307,6 +15320,10 @@
1530715320
"type": "boolean",
1530815321
"x-go-name": "Mirror"
1530915322
},
15323+
"mirror_interval": {
15324+
"type": "string",
15325+
"x-go-name": "MirrorInterval"
15326+
},
1531015327
"name": {
1531115328
"type": "string",
1531215329
"x-go-name": "Name"

0 commit comments

Comments
 (0)