]> source.dussan.org Git - gitea.git/commitdiff
Add MirrorInterval to the API (#14163)
authorPaul Barton <28630076+paulbarton90@users.noreply.github.com>
Sat, 2 Jan 2021 23:47:47 +0000 (23:47 +0000)
committerGitHub <noreply@github.com>
Sat, 2 Jan 2021 23:47:47 +0000 (18:47 -0500)
* Added MirrorInterval to the API

* Remove MirrorInterval from CreateRepository

* Removed Duplicate UpdateMirror Function

* Updated Error Logging

* Update Log Message for is not Mirror

Co-authored-by: 6543 <6543@obermui.de>
* Delete Debug Statement that snuck in

Co-authored-by: zeripath <art27@cantab.net>
* Add Check for If Interval is too small

* Output to API Call

* Add Error Object when time is Less than Min Interval

* Frequency Error Message

Co-authored-by: zeripath <art27@cantab.net>
* Allow Zero Mirror Interval

Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: zeripath <art27@cantab.net>
models/repo.go
modules/auth/repo_form.go
modules/convert/repository.go
modules/migrations/base/options.go
modules/migrations/gitea_uploader.go
modules/repository/repo.go
modules/structs/repo.go
routers/api/v1/repo/migrate.go
routers/api/v1/repo/repo.go
templates/swagger/v1_json.tmpl

index 7dfe14c85b81aacbe126d8cfc8570f2a47db149c..f2453cc5c73a951a8b59dc8164e61c55e4a4dfdf 100644 (file)
@@ -979,6 +979,7 @@ type CreateRepoOptions struct {
        AutoInit       bool
        Status         RepositoryStatus
        TrustModel     TrustModelType
+       MirrorInterval string
 }
 
 // GetRepoInitFile returns repository init files
index 24c2478fa4ef3ac5e4c9a8fb7290f926a5213041..87f2a535106793226bf7f4c14fb719ce1a74d73e 100644 (file)
@@ -68,16 +68,17 @@ type MigrateRepoForm struct {
        // required: true
        UID int64 `json:"uid" binding:"Required"`
        // required: true
-       RepoName     string `json:"repo_name" binding:"Required;AlphaDashDot;MaxSize(100)"`
-       Mirror       bool   `json:"mirror"`
-       Private      bool   `json:"private"`
-       Description  string `json:"description" binding:"MaxSize(255)"`
-       Wiki         bool   `json:"wiki"`
-       Milestones   bool   `json:"milestones"`
-       Labels       bool   `json:"labels"`
-       Issues       bool   `json:"issues"`
-       PullRequests bool   `json:"pull_requests"`
-       Releases     bool   `json:"releases"`
+       RepoName       string `json:"repo_name" binding:"Required;AlphaDashDot;MaxSize(100)"`
+       Mirror         bool   `json:"mirror"`
+       Private        bool   `json:"private"`
+       Description    string `json:"description" binding:"MaxSize(255)"`
+       Wiki           bool   `json:"wiki"`
+       Milestones     bool   `json:"milestones"`
+       Labels         bool   `json:"labels"`
+       Issues         bool   `json:"issues"`
+       PullRequests   bool   `json:"pull_requests"`
+       Releases       bool   `json:"releases"`
+       MirrorInterval string `json:"mirror_interval"`
 }
 
 // Validate validates the fields
index 0f470144b4222adba98b250f9ae1da75cab9c383..813201ca6844cf6a73be836cc73025199f136c7e 100644 (file)
@@ -91,6 +91,13 @@ func innerToRepo(repo *models.Repository, mode models.AccessMode, isParent bool)
 
        numReleases, _ := models.GetReleaseCountByRepoID(repo.ID, models.FindReleasesOptions{IncludeDrafts: false, IncludeTags: true})
 
+       mirrorInterval := ""
+       if repo.IsMirror {
+               if err := repo.GetMirror(); err == nil {
+                       mirrorInterval = repo.Mirror.Interval.String()
+               }
+       }
+
        return &api.Repository{
                ID:                        repo.ID,
                Owner:                     ToUser(repo.Owner, mode != models.AccessModeNone, mode >= models.AccessModeAdmin),
@@ -134,5 +141,6 @@ func innerToRepo(repo *models.Repository, mode models.AccessMode, isParent bool)
                AllowSquash:               allowSquash,
                AvatarURL:                 repo.AvatarLink(),
                Internal:                  !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate,
+               MirrorInterval:            mirrorInterval,
        }
 }
index 3c9b2c22fcab9d870c4141025039ecdc555e4f19..168f9848c813d42a914dd7cf52eead6635060757 100644 (file)
@@ -33,4 +33,5 @@ type MigrateOptions struct {
        PullRequests    bool
        ReleaseAssets   bool
        MigrateToRepoID int64
+       MirrorInterval  string `json:"mirror_interval"`
 }
index 6118b3b5c1fb46fa2217bd9050f8b0602c1b588d..2c79bd4b0f660a9284dacb5770e1ccdf2114909f 100644 (file)
@@ -142,6 +142,7 @@ func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.Migrate
                Private:        repo.IsPrivate,
                Wiki:           opts.Wiki,
                Releases:       opts.Releases, // if didn't get releases, then sync them from tags
+               MirrorInterval: opts.MirrorInterval,
        })
 
        g.repo = r
index 8ecb43ede6e11d90b3d1301b6c500e7cace2438d..ede714673ab16e91459e147efc2faa10a4588dba 100644 (file)
@@ -127,12 +127,33 @@ func MigrateRepositoryGitData(ctx context.Context, u *models.User, repo *models.
        }
 
        if opts.Mirror {
-               if err = models.InsertMirror(&models.Mirror{
+               mirrorModel := models.Mirror{
                        RepoID:         repo.ID,
                        Interval:       setting.Mirror.DefaultInterval,
                        EnablePrune:    true,
                        NextUpdateUnix: timeutil.TimeStampNow().AddDuration(setting.Mirror.DefaultInterval),
-               }); err != nil {
+               }
+
+               if opts.MirrorInterval != "" {
+                       parsedInterval, err := time.ParseDuration(opts.MirrorInterval)
+                       if err != nil {
+                               log.Error("Failed to set Interval: %v", err)
+                               return repo, err
+                       }
+                       if parsedInterval == 0 {
+                               mirrorModel.Interval = 0
+                               mirrorModel.NextUpdateUnix = 0
+                       } else if parsedInterval < setting.Mirror.MinInterval {
+                               err := fmt.Errorf("Interval %s is set below Minimum Interval of %s", parsedInterval, setting.Mirror.MinInterval)
+                               log.Error("Interval: %s is too frequent", opts.MirrorInterval)
+                               return repo, err
+                       } else {
+                               mirrorModel.Interval = parsedInterval
+                               mirrorModel.NextUpdateUnix = timeutil.TimeStampNow().AddDuration(parsedInterval)
+                       }
+               }
+
+               if err = models.InsertMirror(&mirrorModel); err != nil {
                        return repo, fmt.Errorf("InsertOne: %v", err)
                }
 
index c12f8e1c18e113ab3d9e51acd1006cbcf672541e..309273d2fa3373fcf867d524764221ae4534780b 100644 (file)
@@ -91,6 +91,7 @@ type Repository struct {
        AllowSquash               bool             `json:"allow_squash_merge"`
        AvatarURL                 string           `json:"avatar_url"`
        Internal                  bool             `json:"internal"`
+       MirrorInterval            string           `json:"mirror_interval"`
 }
 
 // CreateRepoOption options when creating repository
@@ -168,6 +169,8 @@ type EditRepoOption struct {
        AllowSquash *bool `json:"allow_squash_merge,omitempty"`
        // set to `true` to archive this repository.
        Archived *bool `json:"archived,omitempty"`
+       // set to a string like `8h30m0s` to set the mirror interval time
+       MirrorInterval *string `json:"mirror_interval,omitempty"`
 }
 
 // CreateBranchRepoOption options when creating a branch in a repository
@@ -249,15 +252,16 @@ type MigrateRepoOptions struct {
        AuthPassword string `json:"auth_password"`
        AuthToken    string `json:"auth_token"`
 
-       Mirror       bool   `json:"mirror"`
-       Private      bool   `json:"private"`
-       Description  string `json:"description" binding:"MaxSize(255)"`
-       Wiki         bool   `json:"wiki"`
-       Milestones   bool   `json:"milestones"`
-       Labels       bool   `json:"labels"`
-       Issues       bool   `json:"issues"`
-       PullRequests bool   `json:"pull_requests"`
-       Releases     bool   `json:"releases"`
+       Mirror         bool   `json:"mirror"`
+       Private        bool   `json:"private"`
+       Description    string `json:"description" binding:"MaxSize(255)"`
+       Wiki           bool   `json:"wiki"`
+       Milestones     bool   `json:"milestones"`
+       Labels         bool   `json:"labels"`
+       Issues         bool   `json:"issues"`
+       PullRequests   bool   `json:"pull_requests"`
+       Releases       bool   `json:"releases"`
+       MirrorInterval string `json:"mirror_interval"`
 }
 
 // TokenAuth represents whether a service type supports token-based auth
index 3fd93009042a9fbb6347026c90f82f7f214eb0a8..f07599399c7b0963a2155e0076805a4134c86dbb 100644 (file)
@@ -141,6 +141,7 @@ func Migrate(ctx *context.APIContext, form api.MigrateRepoOptions) {
                PullRequests:   form.PullRequests,
                Releases:       form.Releases,
                GitServiceType: gitServiceType,
+               MirrorInterval: form.MirrorInterval,
        }
        if opts.Mirror {
                opts.Issues = false
index f1df31ccac4b89aae531373d2ed5fb2bdc953372..82d380a814715c1ede0a352246d4dfb34e7cafe3 100644 (file)
@@ -9,6 +9,7 @@ import (
        "fmt"
        "net/http"
        "strings"
+       "time"
 
        "code.gitea.io/gitea/models"
        "code.gitea.io/gitea/modules/context"
@@ -501,6 +502,12 @@ func Edit(ctx *context.APIContext, opts api.EditRepoOption) {
                }
        }
 
+       if opts.MirrorInterval != nil {
+               if err := updateMirrorInterval(ctx, opts); err != nil {
+                       return
+               }
+       }
+
        ctx.JSON(http.StatusOK, convert.ToRepo(ctx.Repo.Repository, ctx.Repo.AccessMode))
 }
 
@@ -783,6 +790,38 @@ func updateRepoArchivedState(ctx *context.APIContext, opts api.EditRepoOption) e
        return nil
 }
 
+// updateMirrorInterval updates the repo's mirror Interval
+func updateMirrorInterval(ctx *context.APIContext, opts api.EditRepoOption) error {
+       repo := ctx.Repo.Repository
+
+       if opts.MirrorInterval != nil {
+               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
+               }
+               if err := repo.GetMirror(); err != nil {
+                       log.Error("Failed to get mirror: %s", err)
+                       ctx.Error(http.StatusInternalServerError, "MirrorInterval", err)
+                       return err
+               }
+               if interval, err := time.ParseDuration(*opts.MirrorInterval); err == nil {
+                       repo.Mirror.Interval = interval
+                       if err := models.UpdateMirror(repo.Mirror); err != nil {
+                               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)
+                       return err
+               }
+       }
+       return nil
+}
+
 // Delete one repository
 func Delete(ctx *context.APIContext) {
        // swagger:operation DELETE /repos/{owner}/{repo} repository repoDelete
index 5de056f3c74902e0365ed854f9b06eb2bb70312a..81ccf4f7253462beb612400ad68d52bd8eb94470 100644 (file)
         "internal_tracker": {
           "$ref": "#/definitions/InternalTracker"
         },
+        "mirror_interval": {
+          "description": "set to a string like `8h30m0s` to set the mirror interval time",
+          "type": "string",
+          "x-go-name": "MirrorInterval"
+        },
         "name": {
           "description": "name of the repository",
           "type": "string",
           "type": "boolean",
           "x-go-name": "Mirror"
         },
+        "mirror_interval": {
+          "type": "string",
+          "x-go-name": "MirrorInterval"
+        },
         "private": {
           "type": "boolean",
           "x-go-name": "Private"
           "type": "boolean",
           "x-go-name": "Mirror"
         },
+        "mirror_interval": {
+          "type": "string",
+          "x-go-name": "MirrorInterval"
+        },
         "private": {
           "type": "boolean",
           "x-go-name": "Private"
           "type": "boolean",
           "x-go-name": "Mirror"
         },
+        "mirror_interval": {
+          "type": "string",
+          "x-go-name": "MirrorInterval"
+        },
         "name": {
           "type": "string",
           "x-go-name": "Name"