Commit c9e9bafc authored by Jaime Martinez's avatar Jaime Martinez 🔴
Browse files

Merge branch '191-fix-create-from-file-example' into 'master'

Fix test release file and update `create-from-file` test to check values

Closes #189 and #191

See merge request gitlab-org/release-cli!180



Merged-by: default avatarJaime Martinez <jmartinez@gitlab.com>
Approved-by: default avatarHalil Coban <hcoban@gitlab.com>
Approved-by: default avatarJaime Martinez <jmartinez@gitlab.com>
Co-authored-by: default avatarAaron Goldenthal <aaron.goldenthal@gmail.com>
parents 164b5e27 81fdd3d6
Loading
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -115,6 +115,8 @@ OPTIONS:
   --help, -h                 Show help (default: false)
```

An example file showing all valid properties can be found [here](internal/testdata/release.yml).

### Get an existing release by tag name

The `get` command uses the [Get a Release by tag name](https://docs.gitlab.com/ee/api/releases/#get-a-release-by-a-tag-name) endpoint
+64 −1
Original line number Diff line number Diff line
package commands

import (
	"encoding/json"
	"flag"
	"io"
	http "net/http"
	"os"
	"testing"
	"time"

	"github.com/sirupsen/logrus"
	testlog "github.com/sirupsen/logrus/hooks/test"
@@ -39,6 +42,29 @@ func TestCreate(t *testing.T) {
}

func TestCreateFromFile(t *testing.T) {
	releasedAt, _ := time.Parse(time.RFC3339, "2019-01-03T01:55:18.203Z")
	expectedRelease := gitlab.CreateReleaseRequest{
		Name:        "Release for test",
		Description: "This is a release for testing only.",
		TagName:     getEnvOrDefault("CI_COMMIT_TAG", "v1.2.3"),
		ReleasedAt:  &releasedAt,
		Ref:         getEnvOrDefault("CI_COMMIT_SHA", "abcdefgh12345678"),
		Assets: &gitlab.Assets{
			Links: []*gitlab.Link{
				{
					Name:            "asset1",
					URL:             "https://gitlab.com/gitlab-org/release-cli",
					DirectAssetPath: "/bin/binary_file",
				},
				{
					Name: "asset2",
					URL:  "https://gitlab.com/",
				},
			},
		},
		Milestones: []string{"v2.0", "v3.0"},
	}

	logger, hook := testlog.NewNullLogger()

	mhc := &gitlab.MockHTTPClient{}
@@ -53,8 +79,12 @@ func TestCreateFromFile(t *testing.T) {
	err := createFromFile.Run(ctx)
	require.NoError(t, err)

	// test the release passed to the HTTPClient since the response is mocked
	release := getReleaseFromRequest(t, mhc)
	require.Equal(t, expectedRelease, release)

	for _, entry := range hook.AllEntries() {
		require.Contains(t, []string{"Creating Release...", "file does not exist, using string value for --description", "release created successfully!"}, entry.Message)
		require.Contains(t, []string{"Creating Release...", "release created successfully!"}, entry.Message)
	}
}

@@ -425,3 +455,36 @@ func newTestCreateFromFile(t *testing.T, logger logrus.FieldLogger, mhc gitlab.H

	return create, ctx
}

// getReleaseFromRequest reads the request body from the mock server call and
// unmarshals it into a CreateReleaseRequest
func getReleaseFromRequest(t *testing.T, mhc *gitlab.MockHTTPClient) gitlab.CreateReleaseRequest {
	// check that mock was called once with an http request
	require.Len(t, mhc.Mock.Calls, 1)
	require.Len(t, mhc.Mock.Calls[0].Arguments, 1)
	request := mhc.Mock.Calls[0].Arguments[0].(*http.Request)
	require.NotNil(t, request)

	body, err := request.GetBody()
	require.NoError(t, err)

	data, err := io.ReadAll(body)
	require.NoError(t, err)

	release := gitlab.CreateReleaseRequest{}
	err = json.Unmarshal(data, &release)
	require.NoError(t, err)

	return release
}

// getEnvOrDefault returns the value of an environment variable if it exists,
// otherwise the provided default value
func getEnvOrDefault(env string, val string) string {
	result := os.Getenv(env)
	if result == "" {
		result = val
	}

	return result
}
+4 −2
Original line number Diff line number Diff line
---
name: Release for test
description: This is a release for testing only.
# The tag-name given here is used unless CI_COMMIT_TAG is specified
tag-name: v1.2.3
released_at: 2019-01-03T01:55:18.203Z
released-at: 2019-01-03T01:55:18.203Z
# If tag_name doesn’t exist, the ref given here is used unless CI_COMMIT_SHA is specified
ref: abcdefgh12345678
milestones:
milestone:
  - 'v2.0'
  - 'v3.0'
assets-link: