Skip to content

Commit 986e474

Browse files
committed
When updating mirror repo intervals by API reschedule next update too
When a mirror repo interval is updated by the UI it is rescheduled with that interval however the API does not do this. The API also lacks the enable_prune option. This PR adds this functionality in to the API Edit Repo endpoint. Signed-off-by: Andrew Thornton <[email protected]>
1 parent 18727df commit 986e474

File tree

4 files changed

+60
-28
lines changed

4 files changed

+60
-28
lines changed

modules/structs/repo.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ type EditRepoOption struct {
187187
Archived *bool `json:"archived,omitempty"`
188188
// set to a string like `8h30m0s` to set the mirror interval time
189189
MirrorInterval *string `json:"mirror_interval,omitempty"`
190+
// enable prune - remove obsolete remote-tracking references
191+
EnablePrune *bool `json:"enable_prune,omitempty"`
190192
}
191193

192194
// GenerateRepoOption options when creating repository using a template

routers/api/v1/repo/repo.go

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ func Edit(ctx *context.APIContext) {
614614
}
615615

616616
if opts.MirrorInterval != nil {
617-
if err := updateMirrorInterval(ctx, opts); err != nil {
617+
if err := updateMirror(ctx, opts); err != nil {
618618
return
619619
}
620620
}
@@ -943,37 +943,67 @@ func updateRepoArchivedState(ctx *context.APIContext, opts api.EditRepoOption) e
943943
return nil
944944
}
945945

946-
// updateMirrorInterval updates the repo's mirror Interval
947-
func updateMirrorInterval(ctx *context.APIContext, opts api.EditRepoOption) error {
946+
// updateMirror updates a repo's mirror Interval and EnablePrune
947+
func updateMirror(ctx *context.APIContext, opts api.EditRepoOption) error {
948948
repo := ctx.Repo.Repository
949949

950+
// only update mirror if interval or enable prune are provided
951+
if opts.MirrorInterval == nil && opts.EnablePrune == nil {
952+
return nil
953+
}
954+
955+
// these values only make sense if the repo is a mirror
956+
if !repo.IsMirror {
957+
err := fmt.Errorf("repo is not a mirror, can not change mirror interval")
958+
ctx.Error(http.StatusUnprocessableEntity, err.Error(), err)
959+
return err
960+
}
961+
962+
// get the mirror from the repo
963+
mirror, err := repo_model.GetMirrorByRepoID(repo.ID)
964+
if err != nil {
965+
log.Error("Failed to get mirror: %s", err)
966+
ctx.Error(http.StatusInternalServerError, "MirrorInterval", err)
967+
return err
968+
}
969+
970+
// update MirrorInterval
950971
if opts.MirrorInterval != nil {
951-
if !repo.IsMirror {
952-
err := fmt.Errorf("repo is not a mirror, can not change mirror interval")
953-
ctx.Error(http.StatusUnprocessableEntity, err.Error(), err)
954-
return err
955-
}
956-
mirror, err := repo_model.GetMirrorByRepoID(repo.ID)
972+
973+
// MirrorInterval should be a duration
974+
interval, err := time.ParseDuration(*opts.MirrorInterval)
957975
if err != nil {
958-
log.Error("Failed to get mirror: %s", err)
959-
ctx.Error(http.StatusInternalServerError, "MirrorInterval", err)
976+
log.Error("Wrong format for MirrorInternal Sent: %s", err)
977+
ctx.Error(http.StatusUnprocessableEntity, "MirrorInterval", err)
960978
return err
961979
}
962-
if interval, err := time.ParseDuration(*opts.MirrorInterval); err == nil {
963-
mirror.Interval = interval
964-
mirror.Repo = repo
965-
if err := repo_model.UpdateMirror(mirror); err != nil {
966-
log.Error("Failed to Set Mirror Interval: %s", err)
967-
ctx.Error(http.StatusUnprocessableEntity, "MirrorInterval", err)
968-
return err
969-
}
970-
log.Trace("Repository %s/%s Mirror Interval was Updated to %s", ctx.Repo.Owner.Name, repo.Name, interval)
971-
} else {
972-
log.Error("Wrong format for MirrorInternal Sent: %s", err)
980+
981+
// Ensure the provided duration is not too short
982+
if interval != 0 && interval < setting.Mirror.MinInterval {
983+
err := fmt.Errorf("invalid mirror interval: %s is below minimum interval: %s", interval, setting.Mirror.MinInterval)
973984
ctx.Error(http.StatusUnprocessableEntity, "MirrorInterval", err)
974985
return err
975986
}
987+
988+
mirror.Interval = interval
989+
mirror.Repo = repo
990+
mirror.ScheduleNextUpdate()
991+
log.Trace("Repository %s Mirror[%d] Set Interval: %s NextUpdateUnix: %s", repo.FullName(), mirror.ID, interval, mirror.NextUpdateUnix)
976992
}
993+
994+
// update EnablePrune
995+
if opts.EnablePrune != nil {
996+
mirror.EnablePrune = *opts.EnablePrune
997+
log.Trace("Repository %s Mirror[%d] Set EnablePrune: %t", repo.FullName(), mirror.ID, mirror.EnablePrune)
998+
}
999+
1000+
// finally update the mirror in the DB
1001+
if err := repo_model.UpdateMirror(mirror); err != nil {
1002+
log.Error("Failed to Set Mirror Interval: %s", err)
1003+
ctx.Error(http.StatusUnprocessableEntity, "MirrorInterval", err)
1004+
return err
1005+
}
1006+
9771007
return nil
9781008
}
9791009

routers/web/repo/setting.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"code.gitea.io/gitea/modules/repository"
3333
"code.gitea.io/gitea/modules/setting"
3434
"code.gitea.io/gitea/modules/structs"
35-
"code.gitea.io/gitea/modules/timeutil"
3635
"code.gitea.io/gitea/modules/typesniffer"
3736
"code.gitea.io/gitea/modules/util"
3837
"code.gitea.io/gitea/modules/validation"
@@ -195,11 +194,7 @@ func SettingsPost(ctx *context.Context) {
195194
} else {
196195
ctx.Repo.Mirror.EnablePrune = form.EnablePrune
197196
ctx.Repo.Mirror.Interval = interval
198-
if interval != 0 {
199-
ctx.Repo.Mirror.NextUpdateUnix = timeutil.TimeStampNow().AddDuration(interval)
200-
} else {
201-
ctx.Repo.Mirror.NextUpdateUnix = 0
202-
}
197+
ctx.Repo.Mirror.ScheduleNextUpdate()
203198
if err := repo_model.UpdateMirror(ctx.Repo.Mirror); err != nil {
204199
ctx.Data["Err_Interval"] = true
205200
ctx.RenderWithErr(ctx.Tr("repo.mirror_interval_invalid"), tplSettingsOptions, &form)

templates/swagger/v1_json.tmpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15084,6 +15084,11 @@
1508415084
"type": "string",
1508515085
"x-go-name": "Description"
1508615086
},
15087+
"enable_prune": {
15088+
"description": "enable prune - remove obsolete remote-tracking references",
15089+
"type": "boolean",
15090+
"x-go-name": "EnablePrune"
15091+
},
1508715092
"external_tracker": {
1508815093
"$ref": "#/definitions/ExternalTracker"
1508915094
},

0 commit comments

Comments
 (0)