@@ -624,7 +624,7 @@ func Edit(ctx *context.APIContext) {
624
624
}
625
625
626
626
if opts .MirrorInterval != nil {
627
- if err := updateMirrorInterval (ctx , opts ); err != nil {
627
+ if err := updateMirror (ctx , opts ); err != nil {
628
628
return
629
629
}
630
630
}
@@ -949,37 +949,67 @@ func updateRepoArchivedState(ctx *context.APIContext, opts api.EditRepoOption) e
949
949
return nil
950
950
}
951
951
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 {
954
954
repo := ctx .Repo .Repository
955
955
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
956
977
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 )
963
981
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 )
966
984
return err
967
985
}
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 )
979
990
ctx .Error (http .StatusUnprocessableEntity , "MirrorInterval" , err )
980
991
return err
981
992
}
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 )
982
998
}
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
+
983
1013
return nil
984
1014
}
985
1015
0 commit comments