@@ -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
0 commit comments