@ -614,7 +614,7 @@ func Edit(ctx *context.APIContext) {
}
}
if opts . MirrorInterval != nil {
if opts . MirrorInterval != nil {
if err := updateMirrorInterval ( ctx , opts ) ; err != nil {
if err := updateMirror ( ctx , opts ) ; err != nil {
return
return
}
}
}
}
@ -943,37 +943,67 @@ func updateRepoArchivedState(ctx *context.APIContext, opts api.EditRepoOption) e
return nil
return nil
}
}
// updateMirrorInterval updates the repo's mirror Interval
// updateMirror updates a repo's mirror Interval and EnablePrune
func updateMirrorInterval ( ctx * context . APIContext , opts api . EditRepoOption ) error {
func updateMirror ( ctx * context . APIContext , opts api . EditRepoOption ) error {
repo := ctx . Repo . Repository
repo := ctx . Repo . Repository
// only update mirror if interval or enable prune are provided
if opts . MirrorInterval == nil && opts . EnablePrune == nil {
return nil
}
// these values only make sense if the repo is a mirror
if ! repo . IsMirror {
err := fmt . Errorf ( "repo is not a mirror, can not change mirror interval" )
ctx . Error ( http . StatusUnprocessableEntity , err . Error ( ) , err )
return err
}
// get the mirror from the repo
mirror , err := repo_model . GetMirrorByRepoID ( repo . ID )
if err != nil {
log . Error ( "Failed to get mirror: %s" , err )
ctx . Error ( http . StatusInternalServerError , "MirrorInterval" , err )
return err
}
// update MirrorInterval
if opts . MirrorInterval != nil {
if opts . MirrorInterval != nil {
if ! repo . IsMirror {
err := fmt . Errorf ( "repo is not a mirror, can not change mirror interval" )
// MirrorInterval should be a duration
ctx . Error ( http . StatusUnprocessableEntity , err . Error ( ) , err )
interval , err := time . ParseDuration ( * opts . MirrorInterval )
return err
}
mirror , err := repo_model . GetMirrorByRepoID ( repo . ID )
if err != nil {
if err != nil {
log . Error ( "Failed to get mirror: %s" , err )
log . Error ( "Wrong format for MirrorInternal Sent: %s" , err )
ctx . Error ( http . StatusInternalServerError , "MirrorInterval" , err )
ctx . Error ( http . StatusUnprocessableEntity , "MirrorInterval" , err )
return err
return err
}
}
if interval , err := time . ParseDuration ( * opts . MirrorInterval ) ; err == nil {
mirror . Interval = interval
// Ensure the provided duration is not too short
mirror . Repo = repo
if interval != 0 && interval < setting . Mirror . MinInterval {
if err := repo_model . UpdateMirror ( mirror ) ; err != nil {
err := fmt . Errorf ( "invalid mirror interval: %s is below minimum interval: %s" , interval , setting . Mirror . MinInterval )
log . Error ( "Failed to Set Mirror Interval: %s" , err )
ctx . Error ( http . StatusUnprocessableEntity , "MirrorInterval" , err )
return err
}
log . Trace ( "Repository %s/%s Mirror Interval was Updated to %s" , ctx . Repo . Owner . Name , repo . Name , interval )
} else {
log . Error ( "Wrong format for MirrorInternal Sent: %s" , err )
ctx . Error ( http . StatusUnprocessableEntity , "MirrorInterval" , err )
ctx . Error ( http . StatusUnprocessableEntity , "MirrorInterval" , err )
return err
return err
}
}
mirror . Interval = interval
mirror . Repo = repo
mirror . ScheduleNextUpdate ( )
log . Trace ( "Repository %s Mirror[%d] Set Interval: %s NextUpdateUnix: %s" , repo . FullName ( ) , mirror . ID , interval , mirror . NextUpdateUnix )
}
}
// update EnablePrune
if opts . EnablePrune != nil {
mirror . EnablePrune = * opts . EnablePrune
log . Trace ( "Repository %s Mirror[%d] Set EnablePrune: %t" , repo . FullName ( ) , mirror . ID , mirror . EnablePrune )
}
// finally update the mirror in the DB
if err := repo_model . UpdateMirror ( mirror ) ; err != nil {
log . Error ( "Failed to Set Mirror Interval: %s" , err )
ctx . Error ( http . StatusUnprocessableEntity , "MirrorInterval" , err )
return err
}
return nil
return nil
}
}