mirror of
https://github.com/go-gitea/gitea
synced 2026-02-03 08:50:36 +00:00
Fix missing repository id when migrating release attachments (#36389)
This PR fixes missed repo_id on the migration of attachments to Gitea. It also provides a doctor check to fix the dirty data on the database.
This commit is contained in:
@@ -318,6 +318,7 @@ func (g *GiteaLocalUploader) CreateReleases(ctx context.Context, releases ...*ba
|
||||
}
|
||||
attach := repo_model.Attachment{
|
||||
UUID: uuid.New().String(),
|
||||
RepoID: g.repo.ID,
|
||||
Name: asset.Name,
|
||||
DownloadCount: int64(*asset.DownloadCount),
|
||||
Size: int64(*asset.Size),
|
||||
|
||||
@@ -221,25 +221,28 @@ func MakeRepoPrivate(ctx context.Context, repo *repo_model.Repository) (err erro
|
||||
})
|
||||
}
|
||||
|
||||
// GetAttachmentLinkedType returns the linked type of attachment if any
|
||||
func GetAttachmentLinkedType(ctx context.Context, a *repo_model.Attachment) (unit.Type, error) {
|
||||
// GetAttachmentLinkedTypeAndRepoID returns the linked type and repository id of attachment if any
|
||||
func GetAttachmentLinkedTypeAndRepoID(ctx context.Context, a *repo_model.Attachment) (unit.Type, int64, error) {
|
||||
if a.IssueID != 0 {
|
||||
iss, err := issues_model.GetIssueByID(ctx, a.IssueID)
|
||||
if err != nil {
|
||||
return unit.TypeIssues, err
|
||||
return unit.TypeIssues, 0, err
|
||||
}
|
||||
unitType := unit.TypeIssues
|
||||
if iss.IsPull {
|
||||
unitType = unit.TypePullRequests
|
||||
}
|
||||
return unitType, nil
|
||||
return unitType, iss.RepoID, nil
|
||||
}
|
||||
|
||||
if a.ReleaseID != 0 {
|
||||
_, err := repo_model.GetReleaseByID(ctx, a.ReleaseID)
|
||||
return unit.TypeReleases, err
|
||||
rel, err := repo_model.GetReleaseByID(ctx, a.ReleaseID)
|
||||
if err != nil {
|
||||
return unit.TypeReleases, 0, err
|
||||
}
|
||||
return unit.TypeReleases, rel.RepoID, nil
|
||||
}
|
||||
return unit.TypeInvalid, nil
|
||||
return unit.TypeInvalid, 0, nil
|
||||
}
|
||||
|
||||
// CheckDaemonExportOK creates/removes git-daemon-export-ok for git-daemon...
|
||||
|
||||
@@ -16,25 +16,27 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestAttachLinkedType(t *testing.T) {
|
||||
func TestAttachLinkedTypeAndRepoID(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
testCases := []struct {
|
||||
name string
|
||||
attachID int64
|
||||
expectedUnitType unit.Type
|
||||
expectedRepoID int64
|
||||
}{
|
||||
{"LinkedIssue", 1, unit.TypeIssues},
|
||||
{"LinkedComment", 3, unit.TypePullRequests},
|
||||
{"LinkedRelease", 9, unit.TypeReleases},
|
||||
{"Notlinked", 10, unit.TypeInvalid},
|
||||
{"LinkedIssue", 1, unit.TypeIssues, 1},
|
||||
{"LinkedComment", 3, unit.TypePullRequests, 1},
|
||||
{"LinkedRelease", 9, unit.TypeReleases, 1},
|
||||
{"Notlinked", 10, unit.TypeInvalid, 0},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
attach, err := repo_model.GetAttachmentByID(t.Context(), tc.attachID)
|
||||
assert.NoError(t, err)
|
||||
unitType, err := GetAttachmentLinkedType(t.Context(), attach)
|
||||
unitType, repoID, err := GetAttachmentLinkedTypeAndRepoID(t.Context(), attach)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tc.expectedUnitType, unitType)
|
||||
assert.Equal(t, tc.expectedRepoID, repoID)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user