@@ -614,7 +614,7 @@ func Edit(ctx *context.APIContext) {
614
614
}
615
615
616
616
if opts .MirrorInterval != nil {
617
- if err := updateMirrorInterval (ctx , opts ); err != nil {
617
+ if err := updateMirror (ctx , opts ); err != nil {
618
618
return
619
619
}
620
620
}
@@ -943,37 +943,67 @@ func updateRepoArchivedState(ctx *context.APIContext, opts api.EditRepoOption) e
943
943
return nil
944
944
}
945
945
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 {
948
948
repo := ctx .Repo .Repository
949
949
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
950
971
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 )
957
975
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 )
960
978
return err
961
979
}
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 )
973
984
ctx .Error (http .StatusUnprocessableEntity , "MirrorInterval" , err )
974
985
return err
975
986
}
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 )
976
992
}
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
+
977
1007
return nil
978
1008
}
979
1009
0 commit comments