diff options
author | Aravinth Manivannan <realaravinth@batsense.net> | 2022-01-26 09:45:51 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-26 10:45:51 +0100 |
commit | 3bb028cc46401a8a54ecab7e7c035dbb24937b6c (patch) | |
tree | bff1aaaaa9d5eef6b9d1c520337e3cc51153b9e2 /services | |
parent | 49dd9067535538771ef13623ed1dd9698a4a2151 (diff) | |
download | gitea-3bb028cc46401a8a54ecab7e7c035dbb24937b6c.tar.gz gitea-3bb028cc46401a8a54ecab7e7c035dbb24937b6c.zip |
Validate migration files (#18203)
JSON Schema validation for data used by Gitea during migrations
Discussion at https://forum.forgefriends.org/t/common-json-schema-for-repository-information/563
Co-authored-by: Loïc Dachary <loic@dachary.org>
Diffstat (limited to 'services')
-rw-r--r-- | services/migrations/dump.go | 4 | ||||
-rw-r--r-- | services/migrations/restore.go | 42 |
2 files changed, 15 insertions, 31 deletions
diff --git a/services/migrations/dump.go b/services/migrations/dump.go index 01a946673e..1e4b3326b6 100644 --- a/services/migrations/dump.go +++ b/services/migrations/dump.go @@ -604,13 +604,13 @@ func updateOptionsUnits(opts *base.MigrateOptions, units []string) { } // RestoreRepository restore a repository from the disk directory -func RestoreRepository(ctx context.Context, baseDir, ownerName, repoName string, units []string) error { +func RestoreRepository(ctx context.Context, baseDir, ownerName, repoName string, units []string, validation bool) error { doer, err := user_model.GetAdminUser() if err != nil { return err } uploader := NewGiteaLocalUploader(ctx, doer, ownerName, repoName) - downloader, err := NewRepositoryRestorer(ctx, baseDir, ownerName, repoName) + downloader, err := NewRepositoryRestorer(ctx, baseDir, ownerName, repoName, validation) if err != nil { return err } diff --git a/services/migrations/restore.go b/services/migrations/restore.go index c927de0b2b..d30d90a6c4 100644 --- a/services/migrations/restore.go +++ b/services/migrations/restore.go @@ -19,23 +19,25 @@ import ( // RepositoryRestorer implements an Downloader from the local directory type RepositoryRestorer struct { base.NullDownloader - ctx context.Context - baseDir string - repoOwner string - repoName string + ctx context.Context + baseDir string + repoOwner string + repoName string + validation bool } // NewRepositoryRestorer creates a repository restorer which could restore repository from a dumped folder -func NewRepositoryRestorer(ctx context.Context, baseDir, owner, repoName string) (*RepositoryRestorer, error) { +func NewRepositoryRestorer(ctx context.Context, baseDir, owner, repoName string, validation bool) (*RepositoryRestorer, error) { baseDir, err := filepath.Abs(baseDir) if err != nil { return nil, err } return &RepositoryRestorer{ - ctx: ctx, - baseDir: baseDir, - repoOwner: owner, - repoName: repoName, + ctx: ctx, + baseDir: baseDir, + repoOwner: owner, + repoName: repoName, + validation: validation, }, nil } @@ -114,7 +116,7 @@ func (r *RepositoryRestorer) GetTopics() ([]string, error) { func (r *RepositoryRestorer) GetMilestones() ([]*base.Milestone, error) { milestones := make([]*base.Milestone, 0, 10) p := filepath.Join(r.baseDir, "milestone.yml") - _, err := os.Stat(p) + err := base.Load(p, &milestones, r.validation) if err != nil { if os.IsNotExist(err) { return nil, nil @@ -122,15 +124,6 @@ func (r *RepositoryRestorer) GetMilestones() ([]*base.Milestone, error) { return nil, err } - bs, err := os.ReadFile(p) - if err != nil { - return nil, err - } - - err = yaml.Unmarshal(bs, &milestones) - if err != nil { - return nil, err - } return milestones, nil } @@ -193,7 +186,7 @@ func (r *RepositoryRestorer) GetLabels() ([]*base.Label, error) { func (r *RepositoryRestorer) GetIssues(page, perPage int) ([]*base.Issue, bool, error) { issues := make([]*base.Issue, 0, 10) p := filepath.Join(r.baseDir, "issue.yml") - _, err := os.Stat(p) + err := base.Load(p, &issues, r.validation) if err != nil { if os.IsNotExist(err) { return nil, true, nil @@ -201,15 +194,6 @@ func (r *RepositoryRestorer) GetIssues(page, perPage int) ([]*base.Issue, bool, return nil, false, err } - bs, err := os.ReadFile(p) - if err != nil { - return nil, false, err - } - - err = yaml.Unmarshal(bs, &issues) - if err != nil { - return nil, false, err - } for _, issue := range issues { issue.Context = base.BasicIssueContext(issue.Number) } |