Release attachments must belong to the intended repo (#36347)
This commit is contained in:
@@ -221,28 +221,25 @@ func MakeRepoPrivate(ctx context.Context, repo *repo_model.Repository) (err erro
|
||||
})
|
||||
}
|
||||
|
||||
// LinkedRepository returns the linked repo if any
|
||||
func LinkedRepository(ctx context.Context, a *repo_model.Attachment) (*repo_model.Repository, unit.Type, error) {
|
||||
// GetAttachmentLinkedType returns the linked type of attachment if any
|
||||
func GetAttachmentLinkedType(ctx context.Context, a *repo_model.Attachment) (unit.Type, error) {
|
||||
if a.IssueID != 0 {
|
||||
iss, err := issues_model.GetIssueByID(ctx, a.IssueID)
|
||||
if err != nil {
|
||||
return nil, unit.TypeIssues, err
|
||||
return unit.TypeIssues, err
|
||||
}
|
||||
repo, err := repo_model.GetRepositoryByID(ctx, iss.RepoID)
|
||||
unitType := unit.TypeIssues
|
||||
if iss.IsPull {
|
||||
unitType = unit.TypePullRequests
|
||||
}
|
||||
return repo, unitType, err
|
||||
} else if a.ReleaseID != 0 {
|
||||
rel, err := repo_model.GetReleaseByID(ctx, a.ReleaseID)
|
||||
if err != nil {
|
||||
return nil, unit.TypeReleases, err
|
||||
}
|
||||
repo, err := repo_model.GetRepositoryByID(ctx, rel.RepoID)
|
||||
return repo, unit.TypeReleases, err
|
||||
return unitType, nil
|
||||
}
|
||||
return nil, -1, nil
|
||||
|
||||
if a.ReleaseID != 0 {
|
||||
_, err := repo_model.GetReleaseByID(ctx, a.ReleaseID)
|
||||
return unit.TypeReleases, err
|
||||
}
|
||||
return unit.TypeInvalid, nil
|
||||
}
|
||||
|
||||
// CheckDaemonExportOK creates/removes git-daemon-export-ok for git-daemon...
|
||||
|
||||
@@ -16,28 +16,24 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestLinkedRepository(t *testing.T) {
|
||||
func TestAttachLinkedType(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
testCases := []struct {
|
||||
name string
|
||||
attachID int64
|
||||
expectedRepo *repo_model.Repository
|
||||
expectedUnitType unit.Type
|
||||
}{
|
||||
{"LinkedIssue", 1, &repo_model.Repository{ID: 1}, unit.TypeIssues},
|
||||
{"LinkedComment", 3, &repo_model.Repository{ID: 1}, unit.TypePullRequests},
|
||||
{"LinkedRelease", 9, &repo_model.Repository{ID: 1}, unit.TypeReleases},
|
||||
{"Notlinked", 10, nil, -1},
|
||||
{"LinkedIssue", 1, unit.TypeIssues},
|
||||
{"LinkedComment", 3, unit.TypePullRequests},
|
||||
{"LinkedRelease", 9, unit.TypeReleases},
|
||||
{"Notlinked", 10, unit.TypeInvalid},
|
||||
}
|
||||
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)
|
||||
repo, unitType, err := LinkedRepository(t.Context(), attach)
|
||||
unitType, err := GetAttachmentLinkedType(t.Context(), attach)
|
||||
assert.NoError(t, err)
|
||||
if tc.expectedRepo != nil {
|
||||
assert.Equal(t, tc.expectedRepo.ID, repo.ID)
|
||||
}
|
||||
assert.Equal(t, tc.expectedUnitType, unitType)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user