mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-01-18 05:18:24 +02:00
feat(githubRelease): publish multiple assets (#3702)
* add parameter for uploading multiple assets * use assetPathList parameter * add test case * fix typo * fix test case * generage ghClient mock * add test files * make function testable * add test case * regenerate mock * regenerate mocks
This commit is contained in:
parent
6676da1f9d
commit
465bab8ddf
@ -16,7 +16,8 @@ import (
|
||||
piperGithub "github.com/SAP/jenkins-library/pkg/github"
|
||||
)
|
||||
|
||||
type githubRepoClient interface {
|
||||
// mock generated with: mockery --name GithubRepoClient --dir cmd --output cmd/mocks
|
||||
type GithubRepoClient interface {
|
||||
CreateRelease(ctx context.Context, owner string, repo string, release *github.RepositoryRelease) (*github.RepositoryRelease, *github.Response, error)
|
||||
DeleteReleaseAsset(ctx context.Context, owner string, repo string, id int64) (*github.Response, error)
|
||||
GetLatestRelease(ctx context.Context, owner string, repo string) (*github.RepositoryRelease, *github.Response, error)
|
||||
@ -41,7 +42,7 @@ func githubPublishRelease(config githubPublishReleaseOptions, telemetryData *tel
|
||||
}
|
||||
}
|
||||
|
||||
func runGithubPublishRelease(ctx context.Context, config *githubPublishReleaseOptions, ghRepoClient githubRepoClient, ghIssueClient githubIssueClient) error {
|
||||
func runGithubPublishRelease(ctx context.Context, config *githubPublishReleaseOptions, ghRepoClient GithubRepoClient, ghIssueClient githubIssueClient) error {
|
||||
|
||||
var publishedAt github.Timestamp
|
||||
|
||||
@ -95,6 +96,8 @@ func runGithubPublishRelease(ctx context.Context, config *githubPublishReleaseOp
|
||||
|
||||
if len(config.AssetPath) > 0 {
|
||||
return uploadReleaseAsset(ctx, createdRelease.GetID(), config, ghRepoClient)
|
||||
} else if len(config.AssetPathList) > 0 {
|
||||
return uploadReleaseAssetList(ctx, createdRelease.GetID(), config, ghRepoClient)
|
||||
}
|
||||
|
||||
return nil
|
||||
@ -157,7 +160,18 @@ func getReleaseDeltaText(config *githubPublishReleaseOptions, lastRelease *githu
|
||||
return releaseDeltaText
|
||||
}
|
||||
|
||||
func uploadReleaseAsset(ctx context.Context, releaseID int64, config *githubPublishReleaseOptions, ghRepoClient githubRepoClient) error {
|
||||
func uploadReleaseAssetList(ctx context.Context, releaseID int64, config *githubPublishReleaseOptions, ghRepoClient GithubRepoClient) error {
|
||||
for _, asset := range config.AssetPathList {
|
||||
config.AssetPath = asset
|
||||
err := uploadReleaseAsset(ctx, releaseID, config, ghRepoClient)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to upload release asset: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func uploadReleaseAsset(ctx context.Context, releaseID int64, config *githubPublishReleaseOptions, ghRepoClient GithubRepoClient) error {
|
||||
|
||||
assets, _, err := ghRepoClient.ListReleaseAssets(ctx, config.Owner, config.Repository, releaseID, &github.ListOptions{})
|
||||
if err != nil {
|
||||
|
@ -20,6 +20,7 @@ type githubPublishReleaseOptions struct {
|
||||
AddDeltaToLastRelease bool `json:"addDeltaToLastRelease,omitempty"`
|
||||
APIURL string `json:"apiUrl,omitempty"`
|
||||
AssetPath string `json:"assetPath,omitempty"`
|
||||
AssetPathList []string `json:"assetPathList,omitempty"`
|
||||
Commitish string `json:"commitish,omitempty"`
|
||||
ExcludeLabels []string `json:"excludeLabels,omitempty"`
|
||||
Labels []string `json:"labels,omitempty"`
|
||||
@ -137,6 +138,7 @@ func addGithubPublishReleaseFlags(cmd *cobra.Command, stepConfig *githubPublishR
|
||||
cmd.Flags().BoolVar(&stepConfig.AddDeltaToLastRelease, "addDeltaToLastRelease", false, "If set to `true`, a link will be added to the release information that brings up all commits since the last release.")
|
||||
cmd.Flags().StringVar(&stepConfig.APIURL, "apiUrl", `https://api.github.com`, "Set the GitHub API url.")
|
||||
cmd.Flags().StringVar(&stepConfig.AssetPath, "assetPath", os.Getenv("PIPER_assetPath"), "Path to a release asset which should be uploaded to the list of release assets.")
|
||||
cmd.Flags().StringSliceVar(&stepConfig.AssetPathList, "assetPathList", []string{}, "List of paths to a release asset which should be uploaded to the list of release assets.")
|
||||
cmd.Flags().StringVar(&stepConfig.Commitish, "commitish", `master`, "Target git commitish for the release")
|
||||
cmd.Flags().StringSliceVar(&stepConfig.ExcludeLabels, "excludeLabels", []string{}, "Allows to exclude issues with dedicated list of labels.")
|
||||
cmd.Flags().StringSliceVar(&stepConfig.Labels, "labels", []string{}, "Labels to include in issue search.")
|
||||
@ -209,6 +211,15 @@ func githubPublishReleaseMetadata() config.StepData {
|
||||
Aliases: []config.Alias{},
|
||||
Default: os.Getenv("PIPER_assetPath"),
|
||||
},
|
||||
{
|
||||
Name: "assetPathList",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
|
||||
Type: "[]string",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
Default: []string{},
|
||||
},
|
||||
{
|
||||
Name: "commitish",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
|
@ -9,8 +9,10 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/SAP/jenkins-library/cmd/mocks"
|
||||
"github.com/google/go-github/v32/github"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type ghRCMock struct {
|
||||
@ -408,6 +410,55 @@ func TestUploadReleaseAsset(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestUploadReleaseAssetList(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
owner := "OWNER"
|
||||
repository := "REPOSITORY"
|
||||
var releaseID int64 = 1
|
||||
|
||||
t.Run("Success - multiple asset", func(t *testing.T) {
|
||||
// init
|
||||
assetURL := mock.Anything
|
||||
asset1 := filepath.Join("testdata", t.Name()+"_1_test.txt")
|
||||
asset2 := filepath.Join("testdata", t.Name()+"_2_test.txt")
|
||||
assetName1 := filepath.Base(asset1)
|
||||
assetName2 := filepath.Base(asset2)
|
||||
var assetID1 int64 = 11
|
||||
var assetID2 int64 = 12
|
||||
stepConfig := githubPublishReleaseOptions{
|
||||
Owner: owner,
|
||||
Repository: repository,
|
||||
AssetPathList: []string{asset1, asset2},
|
||||
}
|
||||
// mocking
|
||||
ghClient := &mocks.GithubRepoClient{}
|
||||
ghClient.Test(t)
|
||||
ghClient.
|
||||
On("ListReleaseAssets", ctx, owner, repository, releaseID, mock.AnythingOfType("*github.ListOptions")).Return(
|
||||
[]*github.ReleaseAsset{
|
||||
{Name: &assetName1, ID: &assetID1, URL: &assetURL},
|
||||
{Name: &assetName2, ID: &assetID2, URL: &assetURL},
|
||||
},
|
||||
nil,
|
||||
nil,
|
||||
).
|
||||
On("DeleteReleaseAsset", ctx, owner, repository, mock.AnythingOfType("int64")).Return(
|
||||
&github.Response{Response: &http.Response{StatusCode: 200}},
|
||||
nil,
|
||||
).
|
||||
On("UploadReleaseAsset", ctx, owner, repository, releaseID, mock.AnythingOfType("*github.UploadOptions"), mock.AnythingOfType("*os.File")).Return(
|
||||
&github.ReleaseAsset{URL: &assetURL},
|
||||
&github.Response{Response: &http.Response{StatusCode: 200}},
|
||||
nil,
|
||||
)
|
||||
// test
|
||||
err := uploadReleaseAssetList(ctx, releaseID, &stepConfig, ghClient)
|
||||
// asserts
|
||||
assert.NoError(t, err)
|
||||
ghClient.AssertExpectations(t)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIsExcluded(t *testing.T) {
|
||||
|
||||
l1 := "label1"
|
||||
|
168
cmd/mocks/GithubRepoClient.go
Normal file
168
cmd/mocks/GithubRepoClient.go
Normal file
@ -0,0 +1,168 @@
|
||||
// Code generated by mockery v2.10.4. DO NOT EDIT.
|
||||
|
||||
package mocks
|
||||
|
||||
import (
|
||||
context "context"
|
||||
|
||||
github "github.com/google/go-github/v32/github"
|
||||
mock "github.com/stretchr/testify/mock"
|
||||
|
||||
os "os"
|
||||
)
|
||||
|
||||
// GithubRepoClient is an autogenerated mock type for the GithubRepoClient type
|
||||
type GithubRepoClient struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// CreateRelease provides a mock function with given fields: ctx, owner, repo, release
|
||||
func (_m *GithubRepoClient) CreateRelease(ctx context.Context, owner string, repo string, release *github.RepositoryRelease) (*github.RepositoryRelease, *github.Response, error) {
|
||||
ret := _m.Called(ctx, owner, repo, release)
|
||||
|
||||
var r0 *github.RepositoryRelease
|
||||
if rf, ok := ret.Get(0).(func(context.Context, string, string, *github.RepositoryRelease) *github.RepositoryRelease); ok {
|
||||
r0 = rf(ctx, owner, repo, release)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(*github.RepositoryRelease)
|
||||
}
|
||||
}
|
||||
|
||||
var r1 *github.Response
|
||||
if rf, ok := ret.Get(1).(func(context.Context, string, string, *github.RepositoryRelease) *github.Response); ok {
|
||||
r1 = rf(ctx, owner, repo, release)
|
||||
} else {
|
||||
if ret.Get(1) != nil {
|
||||
r1 = ret.Get(1).(*github.Response)
|
||||
}
|
||||
}
|
||||
|
||||
var r2 error
|
||||
if rf, ok := ret.Get(2).(func(context.Context, string, string, *github.RepositoryRelease) error); ok {
|
||||
r2 = rf(ctx, owner, repo, release)
|
||||
} else {
|
||||
r2 = ret.Error(2)
|
||||
}
|
||||
|
||||
return r0, r1, r2
|
||||
}
|
||||
|
||||
// DeleteReleaseAsset provides a mock function with given fields: ctx, owner, repo, id
|
||||
func (_m *GithubRepoClient) DeleteReleaseAsset(ctx context.Context, owner string, repo string, id int64) (*github.Response, error) {
|
||||
ret := _m.Called(ctx, owner, repo, id)
|
||||
|
||||
var r0 *github.Response
|
||||
if rf, ok := ret.Get(0).(func(context.Context, string, string, int64) *github.Response); ok {
|
||||
r0 = rf(ctx, owner, repo, id)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(*github.Response)
|
||||
}
|
||||
}
|
||||
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(1).(func(context.Context, string, string, int64) error); ok {
|
||||
r1 = rf(ctx, owner, repo, id)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// GetLatestRelease provides a mock function with given fields: ctx, owner, repo
|
||||
func (_m *GithubRepoClient) GetLatestRelease(ctx context.Context, owner string, repo string) (*github.RepositoryRelease, *github.Response, error) {
|
||||
ret := _m.Called(ctx, owner, repo)
|
||||
|
||||
var r0 *github.RepositoryRelease
|
||||
if rf, ok := ret.Get(0).(func(context.Context, string, string) *github.RepositoryRelease); ok {
|
||||
r0 = rf(ctx, owner, repo)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(*github.RepositoryRelease)
|
||||
}
|
||||
}
|
||||
|
||||
var r1 *github.Response
|
||||
if rf, ok := ret.Get(1).(func(context.Context, string, string) *github.Response); ok {
|
||||
r1 = rf(ctx, owner, repo)
|
||||
} else {
|
||||
if ret.Get(1) != nil {
|
||||
r1 = ret.Get(1).(*github.Response)
|
||||
}
|
||||
}
|
||||
|
||||
var r2 error
|
||||
if rf, ok := ret.Get(2).(func(context.Context, string, string) error); ok {
|
||||
r2 = rf(ctx, owner, repo)
|
||||
} else {
|
||||
r2 = ret.Error(2)
|
||||
}
|
||||
|
||||
return r0, r1, r2
|
||||
}
|
||||
|
||||
// ListReleaseAssets provides a mock function with given fields: ctx, owner, repo, id, opt
|
||||
func (_m *GithubRepoClient) ListReleaseAssets(ctx context.Context, owner string, repo string, id int64, opt *github.ListOptions) ([]*github.ReleaseAsset, *github.Response, error) {
|
||||
ret := _m.Called(ctx, owner, repo, id, opt)
|
||||
|
||||
var r0 []*github.ReleaseAsset
|
||||
if rf, ok := ret.Get(0).(func(context.Context, string, string, int64, *github.ListOptions) []*github.ReleaseAsset); ok {
|
||||
r0 = rf(ctx, owner, repo, id, opt)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).([]*github.ReleaseAsset)
|
||||
}
|
||||
}
|
||||
|
||||
var r1 *github.Response
|
||||
if rf, ok := ret.Get(1).(func(context.Context, string, string, int64, *github.ListOptions) *github.Response); ok {
|
||||
r1 = rf(ctx, owner, repo, id, opt)
|
||||
} else {
|
||||
if ret.Get(1) != nil {
|
||||
r1 = ret.Get(1).(*github.Response)
|
||||
}
|
||||
}
|
||||
|
||||
var r2 error
|
||||
if rf, ok := ret.Get(2).(func(context.Context, string, string, int64, *github.ListOptions) error); ok {
|
||||
r2 = rf(ctx, owner, repo, id, opt)
|
||||
} else {
|
||||
r2 = ret.Error(2)
|
||||
}
|
||||
|
||||
return r0, r1, r2
|
||||
}
|
||||
|
||||
// UploadReleaseAsset provides a mock function with given fields: ctx, owner, repo, id, opt, file
|
||||
func (_m *GithubRepoClient) UploadReleaseAsset(ctx context.Context, owner string, repo string, id int64, opt *github.UploadOptions, file *os.File) (*github.ReleaseAsset, *github.Response, error) {
|
||||
ret := _m.Called(ctx, owner, repo, id, opt, file)
|
||||
|
||||
var r0 *github.ReleaseAsset
|
||||
if rf, ok := ret.Get(0).(func(context.Context, string, string, int64, *github.UploadOptions, *os.File) *github.ReleaseAsset); ok {
|
||||
r0 = rf(ctx, owner, repo, id, opt, file)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(*github.ReleaseAsset)
|
||||
}
|
||||
}
|
||||
|
||||
var r1 *github.Response
|
||||
if rf, ok := ret.Get(1).(func(context.Context, string, string, int64, *github.UploadOptions, *os.File) *github.Response); ok {
|
||||
r1 = rf(ctx, owner, repo, id, opt, file)
|
||||
} else {
|
||||
if ret.Get(1) != nil {
|
||||
r1 = ret.Get(1).(*github.Response)
|
||||
}
|
||||
}
|
||||
|
||||
var r2 error
|
||||
if rf, ok := ret.Get(2).(func(context.Context, string, string, int64, *github.UploadOptions, *os.File) error); ok {
|
||||
r2 = rf(ctx, owner, repo, id, opt, file)
|
||||
} else {
|
||||
r2 = ret.Error(2)
|
||||
}
|
||||
|
||||
return r0, r1, r2
|
||||
}
|
1
cmd/testdata/TestUploadReleaseAssetList/Success_-_multiple_asset_1_test.txt
vendored
Normal file
1
cmd/testdata/TestUploadReleaseAssetList/Success_-_multiple_asset_1_test.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
TEST
|
1
cmd/testdata/TestUploadReleaseAssetList/Success_-_multiple_asset_2_test.txt
vendored
Normal file
1
cmd/testdata/TestUploadReleaseAssetList/Success_-_multiple_asset_2_test.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
TEST
|
@ -25,7 +25,6 @@ func TestPiperGithubPublishRelease(t *testing.T) {
|
||||
if len(token) == 0 {
|
||||
t.Fatal("No GitHub token maintained")
|
||||
}
|
||||
|
||||
owner := os.Getenv("PIPER_INTEGRATION_GITHUB_OWNER")
|
||||
if len(owner) == 0 {
|
||||
owner = "OliverNocon"
|
||||
@ -34,7 +33,6 @@ func TestPiperGithubPublishRelease(t *testing.T) {
|
||||
if len(repository) == 0 {
|
||||
repository = "piper-integration"
|
||||
}
|
||||
|
||||
dir, err := ioutil.TempDir("", "")
|
||||
defer os.RemoveAll(dir) // clean up
|
||||
assert.NoError(t, err, "Error when creating temp dir")
|
||||
@ -42,7 +40,11 @@ func TestPiperGithubPublishRelease(t *testing.T) {
|
||||
testAsset := filepath.Join(dir, "test.txt")
|
||||
err = ioutil.WriteFile(testAsset, []byte("Test"), 0644)
|
||||
assert.NoError(t, err, "Error when writing temporary file")
|
||||
test2Asset := filepath.Join(dir, "test2.txt")
|
||||
err = ioutil.WriteFile(test2Asset, []byte("Test"), 0644)
|
||||
assert.NoError(t, err, "Error when writing temporary file")
|
||||
|
||||
t.Run("test single asset - success", func(t *testing.T) {
|
||||
//prepare pipeline environment
|
||||
now := time.Now()
|
||||
piperenv.SetResourceParameter(filepath.Join(dir, ".pipeline"), "commonPipelineEnvironment", "artifactVersion", now.Format("20060102150405"))
|
||||
@ -65,6 +67,33 @@ func TestPiperGithubPublishRelease(t *testing.T) {
|
||||
|
||||
err = cmd.RunExecutable(getPiperExecutable(), piperOptions...)
|
||||
assert.NoError(t, err, "Calling piper with arguments %v failed.", piperOptions)
|
||||
})
|
||||
t.Run("test multiple assets - success", func(t *testing.T) {
|
||||
//prepare pipeline environment
|
||||
now := time.Now()
|
||||
piperenv.SetResourceParameter(filepath.Join(dir, ".pipeline"), "commonPipelineEnvironment", "artifactVersion", now.Format("20060102150405"))
|
||||
|
||||
cmd := command.Command{}
|
||||
cmd.SetDir(dir)
|
||||
|
||||
piperOptions := []string{
|
||||
"githubPublishRelease",
|
||||
"--owner",
|
||||
owner,
|
||||
"--repository",
|
||||
repository,
|
||||
"--token",
|
||||
token,
|
||||
"--assetPathList",
|
||||
testAsset,
|
||||
"--assetPathList",
|
||||
test2Asset,
|
||||
"--noTelemetry",
|
||||
}
|
||||
|
||||
err = cmd.RunExecutable(getPiperExecutable(), piperOptions...)
|
||||
assert.NoError(t, err, "Calling piper with arguments %v failed.", piperOptions)
|
||||
})
|
||||
}
|
||||
|
||||
func TestGithubFetchCommitStatistics(t *testing.T) {
|
||||
|
@ -54,6 +54,13 @@ spec:
|
||||
- STAGES
|
||||
- STEPS
|
||||
type: string
|
||||
- name: assetPathList
|
||||
description: List of paths to a release asset which should be uploaded to the list of release assets.
|
||||
scope:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
- STEPS
|
||||
type: "[]string"
|
||||
- name: commitish
|
||||
description: "Target git commitish for the release"
|
||||
scope:
|
||||
|
Loading…
x
Reference in New Issue
Block a user