Skip to content

Commit ae396ac

Browse files
authored
Fix DownloadFunc when migrating releases (#27887)
We should not use `asset.ID` in DownloadFunc because DownloadFunc is a closure. https://github.com/go-gitea/gitea/blob/1bf5527eac6b947010c8faf408f6747de2a2384f/services/migrations/gitea_downloader.go#L284-L295 A similar bug when migrating from GitHub has been fixed in #14703. This PR fixes the bug when migrating from Gitea and GitLab.
1 parent 1bf5527 commit ae396ac

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

services/migrations/gitea_downloader.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ func (g *GiteaDownloader) convertGiteaRelease(rel *gitea_sdk.Release) *base.Rele
282282
httpClient := NewMigrationHTTPClient()
283283

284284
for _, asset := range rel.Attachments {
285+
assetID := asset.ID // Don't optimize this, for closure we need a local variable
286+
assetDownloadURL := asset.DownloadURL
285287
size := int(asset.Size)
286288
dlCount := int(asset.DownloadCount)
287289
r.Assets = append(r.Assets, &base.ReleaseAsset{
@@ -292,18 +294,18 @@ func (g *GiteaDownloader) convertGiteaRelease(rel *gitea_sdk.Release) *base.Rele
292294
Created: asset.Created,
293295
DownloadURL: &asset.DownloadURL,
294296
DownloadFunc: func() (io.ReadCloser, error) {
295-
asset, _, err := g.client.GetReleaseAttachment(g.repoOwner, g.repoName, rel.ID, asset.ID)
297+
asset, _, err := g.client.GetReleaseAttachment(g.repoOwner, g.repoName, rel.ID, assetID)
296298
if err != nil {
297299
return nil, err
298300
}
299301

300-
if !hasBaseURL(asset.DownloadURL, g.baseURL) {
301-
WarnAndNotice("Unexpected AssetURL for assetID[%d] in %s: %s", asset.ID, g, asset.DownloadURL)
302+
if !hasBaseURL(assetDownloadURL, g.baseURL) {
303+
WarnAndNotice("Unexpected AssetURL for assetID[%d] in %s: %s", assetID, g, assetDownloadURL)
302304
return io.NopCloser(strings.NewReader(asset.DownloadURL)), nil
303305
}
304306

305307
// FIXME: for a private download?
306-
req, err := http.NewRequest("GET", asset.DownloadURL, nil)
308+
req, err := http.NewRequest("GET", assetDownloadURL, nil)
307309
if err != nil {
308310
return nil, err
309311
}

services/migrations/gitlab.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,20 +310,21 @@ func (g *GitlabDownloader) convertGitlabRelease(rel *gitlab.Release) *base.Relea
310310
httpClient := NewMigrationHTTPClient()
311311

312312
for k, asset := range rel.Assets.Links {
313+
assetID := asset.ID // Don't optimize this, for closure we need a local variable
313314
r.Assets = append(r.Assets, &base.ReleaseAsset{
314315
ID: int64(asset.ID),
315316
Name: asset.Name,
316317
ContentType: &rel.Assets.Sources[k].Format,
317318
Size: &zero,
318319
DownloadCount: &zero,
319320
DownloadFunc: func() (io.ReadCloser, error) {
320-
link, _, err := g.client.ReleaseLinks.GetReleaseLink(g.repoID, rel.TagName, asset.ID, gitlab.WithContext(g.ctx))
321+
link, _, err := g.client.ReleaseLinks.GetReleaseLink(g.repoID, rel.TagName, assetID, gitlab.WithContext(g.ctx))
321322
if err != nil {
322323
return nil, err
323324
}
324325

325326
if !hasBaseURL(link.URL, g.baseURL) {
326-
WarnAndNotice("Unexpected AssetURL for assetID[%d] in %s: %s", asset.ID, g, link.URL)
327+
WarnAndNotice("Unexpected AssetURL for assetID[%d] in %s: %s", assetID, g, link.URL)
327328
return io.NopCloser(strings.NewReader(link.URL)), nil
328329
}
329330

0 commit comments

Comments
 (0)