Skip to content

Commit 08eecba

Browse files
authored
When updating mirror repo intervals by API reschedule next update too (#19429) (#19433)
Backport #19429 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 9c2212d commit 08eecba

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
@@ -183,6 +183,8 @@ type EditRepoOption struct {
183183
Archived *bool `json:"archived,omitempty"`
184184
// set to a string like `8h30m0s` to set the mirror interval time
185185
MirrorInterval *string `json:"mirror_interval,omitempty"`
186+
// enable prune - remove obsolete remote-tracking references
187+
EnablePrune *bool `json:"enable_prune,omitempty"`
186188
}
187189

188190
// 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
@@ -624,7 +624,7 @@ func Edit(ctx *context.APIContext) {
624624
}
625625

626626
if opts.MirrorInterval != nil {
627-
if err := updateMirrorInterval(ctx, opts); err != nil {
627+
if err := updateMirror(ctx, opts); err != nil {
628628
return
629629
}
630630
}
@@ -949,37 +949,67 @@ func updateRepoArchivedState(ctx *context.APIContext, opts api.EditRepoOption) e
949949
return nil
950950
}
951951

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

956+
// only update mirror if interval or enable prune are provided
957+
if opts.MirrorInterval == nil && opts.EnablePrune == nil {
958+
return nil
959+
}
960+
961+
// these values only make sense if the repo is a mirror
962+
if !repo.IsMirror {
963+
err := fmt.Errorf("repo is not a mirror, can not change mirror interval")
964+
ctx.Error(http.StatusUnprocessableEntity, err.Error(), err)
965+
return err
966+
}
967+
968+
// get the mirror from the repo
969+
mirror, err := repo_model.GetMirrorByRepoID(repo.ID)
970+
if err != nil {
971+
log.Error("Failed to get mirror: %s", err)
972+
ctx.Error(http.StatusInternalServerError, "MirrorInterval", err)
973+
return err
974+
}
975+
976+
// update MirrorInterval
956977
if opts.MirrorInterval != nil {
957-
if !repo.IsMirror {
958-
err := fmt.Errorf("repo is not a mirror, can not change mirror interval")
959-
ctx.Error(http.StatusUnprocessableEntity, err.Error(), err)
960-
return err
961-
}
962-
mirror, err := repo_model.GetMirrorByRepoID(repo.ID)
978+
979+
// MirrorInterval should be a duration
980+
interval, err := time.ParseDuration(*opts.MirrorInterval)
963981
if err != nil {
964-
log.Error("Failed to get mirror: %s", err)
965-
ctx.Error(http.StatusInternalServerError, "MirrorInterval", err)
982+
log.Error("Wrong format for MirrorInternal Sent: %s", err)
983+
ctx.Error(http.StatusUnprocessableEntity, "MirrorInterval", err)
966984
return err
967985
}
968-
if interval, err := time.ParseDuration(*opts.MirrorInterval); err == nil {
969-
mirror.Interval = interval
970-
mirror.Repo = repo
971-
if err := repo_model.UpdateMirror(mirror); err != nil {
972-
log.Error("Failed to Set Mirror Interval: %s", err)
973-
ctx.Error(http.StatusUnprocessableEntity, "MirrorInterval", err)
974-
return err
975-
}
976-
log.Trace("Repository %s/%s Mirror Interval was Updated to %s", ctx.Repo.Owner.Name, repo.Name, interval)
977-
} else {
978-
log.Error("Wrong format for MirrorInternal Sent: %s", err)
986+
987+
// Ensure the provided duration is not too short
988+
if interval != 0 && interval < setting.Mirror.MinInterval {
989+
err := fmt.Errorf("invalid mirror interval: %s is below minimum interval: %s", interval, setting.Mirror.MinInterval)
979990
ctx.Error(http.StatusUnprocessableEntity, "MirrorInterval", err)
980991
return err
981992
}
993+
994+
mirror.Interval = interval
995+
mirror.Repo = repo
996+
mirror.ScheduleNextUpdate()
997+
log.Trace("Repository %s Mirror[%d] Set Interval: %s NextUpdateUnix: %s", repo.FullName(), mirror.ID, interval, mirror.NextUpdateUnix)
982998
}
999+
1000+
// update EnablePrune
1001+
if opts.EnablePrune != nil {
1002+
mirror.EnablePrune = *opts.EnablePrune
1003+
log.Trace("Repository %s Mirror[%d] Set EnablePrune: %t", repo.FullName(), mirror.ID, mirror.EnablePrune)
1004+
}
1005+
1006+
// finally update the mirror in the DB
1007+
if err := repo_model.UpdateMirror(mirror); err != nil {
1008+
log.Error("Failed to Set Mirror Interval: %s", err)
1009+
ctx.Error(http.StatusUnprocessableEntity, "MirrorInterval", err)
1010+
return err
1011+
}
1012+
9831013
return nil
9841014
}
9851015

routers/web/repo/setting.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"code.gitea.io/gitea/modules/repository"
3232
"code.gitea.io/gitea/modules/setting"
3333
"code.gitea.io/gitea/modules/structs"
34-
"code.gitea.io/gitea/modules/timeutil"
3534
"code.gitea.io/gitea/modules/typesniffer"
3635
"code.gitea.io/gitea/modules/util"
3736
"code.gitea.io/gitea/modules/validation"
@@ -194,11 +193,7 @@ func SettingsPost(ctx *context.Context) {
194193
} else {
195194
ctx.Repo.Mirror.EnablePrune = form.EnablePrune
196195
ctx.Repo.Mirror.Interval = interval
197-
if interval != 0 {
198-
ctx.Repo.Mirror.NextUpdateUnix = timeutil.TimeStampNow().AddDuration(interval)
199-
} else {
200-
ctx.Repo.Mirror.NextUpdateUnix = 0
201-
}
196+
ctx.Repo.Mirror.ScheduleNextUpdate()
202197
if err := repo_model.UpdateMirror(ctx.Repo.Mirror); err != nil {
203198
ctx.Data["Err_Interval"] = true
204199
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
@@ -14757,6 +14757,11 @@
1475714757
"type": "string",
1475814758
"x-go-name": "Description"
1475914759
},
14760+
"enable_prune": {
14761+
"description": "enable prune - remove obsolete remote-tracking references",
14762+
"type": "boolean",
14763+
"x-go-name": "EnablePrune"
14764+
},
1476014765
"external_tracker": {
1476114766
"$ref": "#/definitions/ExternalTracker"
1476214767
},

0 commit comments

Comments
 (0)