From 8c4bf4c3b401f31d9c2ef800413f2f18bc0acf50 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 16 May 2021 09:24:28 +0200 Subject: [PATCH] GitHub: migrate draft releases too (#15884) (#15888) * GitHub: migrate draft releases too * refactor --- modules/migrations/gitea_uploader.go | 21 +++++++------ modules/migrations/github.go | 44 ++++++++++++---------------- 2 files changed, 31 insertions(+), 34 deletions(-) diff --git a/modules/migrations/gitea_uploader.go b/modules/migrations/gitea_uploader.go index 868a0ea5e7..435b4244a1 100644 --- a/modules/migrations/gitea_uploader.go +++ b/modules/migrations/gitea_uploader.go @@ -248,14 +248,16 @@ func (g *GiteaLocalUploader) CreateReleases(releases ...*base.Release) error { rel.OriginalAuthorID = release.PublisherID } - // calc NumCommits - commit, err := g.gitRepo.GetCommit(rel.TagName) - if err != nil { - return fmt.Errorf("GetCommit: %v", err) - } - rel.NumCommits, err = commit.CommitsCount() - if err != nil { - return fmt.Errorf("CommitsCount: %v", err) + // calc NumCommits if no draft + if !release.Draft { + commit, err := g.gitRepo.GetCommit(rel.TagName) + if err != nil { + return fmt.Errorf("GetCommit: %v", err) + } + rel.NumCommits, err = commit.CommitsCount() + if err != nil { + return fmt.Errorf("CommitsCount: %v", err) + } } for _, asset := range release.Assets { @@ -268,9 +270,10 @@ func (g *GiteaLocalUploader) CreateReleases(releases ...*base.Release) error { } // download attachment - err = func() error { + err := func() error { // asset.DownloadURL maybe a local file var rc io.ReadCloser + var err error if asset.DownloadURL == nil { rc, err = asset.DownloadFunc() if err != nil { diff --git a/modules/migrations/github.go b/modules/migrations/github.go index 282e3b4786..8a3f5d34c7 100644 --- a/modules/migrations/github.go +++ b/modules/migrations/github.go @@ -264,34 +264,29 @@ func (g *GithubDownloaderV3) GetLabels() ([]*base.Label, error) { } func (g *GithubDownloaderV3) convertGithubRelease(rel *github.RepositoryRelease) *base.Release { - var ( - name string - desc string - ) - if rel.Body != nil { - desc = *rel.Body - } - if rel.Name != nil { - name = *rel.Name - } - - var email string - if rel.Author.Email != nil { - email = *rel.Author.Email - } - r := &base.Release{ TagName: *rel.TagName, TargetCommitish: *rel.TargetCommitish, - Name: name, - Body: desc, Draft: *rel.Draft, Prerelease: *rel.Prerelease, Created: rel.CreatedAt.Time, PublisherID: *rel.Author.ID, PublisherName: *rel.Author.Login, - PublisherEmail: email, - Published: rel.PublishedAt.Time, + } + + if rel.Body != nil { + r.Body = *rel.Body + } + if rel.Name != nil { + r.Name = *rel.Name + } + + if rel.Author.Email != nil { + r.PublisherEmail = *rel.Author.Email + } + + if rel.PublishedAt != nil { + r.Published = rel.PublishedAt.Time } for _, asset := range rel.Assets { @@ -306,18 +301,17 @@ func (g *GithubDownloaderV3) convertGithubRelease(rel *github.RepositoryRelease) Updated: asset.UpdatedAt.Time, DownloadFunc: func() (io.ReadCloser, error) { g.sleep() - asset, redir, err := g.client.Repositories.DownloadReleaseAsset(g.ctx, g.repoOwner, g.repoName, assetID, nil) + asset, redirectURL, err := g.client.Repositories.DownloadReleaseAsset(g.ctx, g.repoOwner, g.repoName, assetID, nil) if err != nil { return nil, err } - err = g.RefreshRate() - if err != nil { + if err := g.RefreshRate(); err != nil { log.Error("g.client.RateLimits: %s", err) } if asset == nil { - if redir != "" { + if redirectURL != "" { g.sleep() - req, err := http.NewRequestWithContext(g.ctx, "GET", redir, nil) + req, err := http.NewRequestWithContext(g.ctx, "GET", redirectURL, nil) if err != nil { return nil, err } -- 2.39.5