From 8643656339aee0b93390f93f92febe423100e6b4 Mon Sep 17 00:00:00 2001 From: Nightapes Date: Sat, 31 Aug 2019 17:05:10 +0200 Subject: [PATCH] feat(changelog): add docker usage to changelog --- .gitignore | 1 + .release.yml | 23 +-- README.md | 180 ++++++++++++++++++++++-- README2.md | 2 - cmd/go-semantic-release/commands/zip.go | 2 +- .travis.yml => examples/.travis.yml | 0 internal/changelog/changelog.go | 52 ++++--- internal/releaser/gitlab/gitlab.go | 11 +- internal/releaser/gitlab/gitlab_test.go | 32 +++-- internal/releaser/releaser.go | 8 +- pkg/config/config.go | 20 ++- 11 files changed, 257 insertions(+), 74 deletions(-) delete mode 100644 README2.md rename .travis.yml => examples/.travis.yml (100%) diff --git a/.gitignore b/.gitignore index e623bb9..45eee87 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ go-semantic-release .vscode/settings.json CHANGELOG.md cover.html +build/ \ No newline at end of file diff --git a/.release.yml b/.release.yml index 4f1a577..77a2e85 100644 --- a/.release.yml +++ b/.release.yml @@ -1,21 +1,12 @@ -commitFormat: angular -title: "go-semantic-release release" -branch: - master: beta - rc: rc - beta: beta - alpha: alpha -changelog: - printAll: false - template: '' - templatePath: '' release: 'github' +github: + repo: "go-semantic-release" + user: "nightapes" +commitFormat: angular +branch: + master: release assets: - name: ./build/go-semantic-release compress: false - name: ./build/go-semantic-release.exe - compress: false -github: - repo: "go-semantic-release" - user: "nightapes" - customUrl: "" + compress: false \ No newline at end of file diff --git a/README.md b/README.md index 85aa0c4..44d80f8 100644 --- a/README.md +++ b/README.md @@ -2,24 +2,178 @@ ## Release Types -| Type | Git tag | Changelog | Release | Write access git | Api token | -|--- |:---: |:---: |:---: |:---: |:---: | -| `git` | :white_check_mark: | | | :white_check_mark:| | -| `github` | :white_check_mark: | :white_check_mark: | :white_check_mark:| | :white_check_mark: | -| `gitlab` | :white_check_mark: | :white_check_mark: | :white_check_mark:| | :white_check_mark: | +| Type | Implemendet | Git tag | Changelog | Release | Write access git | Api token | +| ---------- | :----------------: | :----------------: | :----------------: | :----------------: | :----------------: | :----------------: | +| `github` | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | | :white_check_mark: | +| `gitlab` | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | | :white_check_mark: | +| `git` | Comming soon | :white_check_mark: | | | :white_check_mark: | | +| `bitbuckt` | Comming soon | :white_check_mark: | | | :white_check_mark: | | + + +## Supported CI Pipelines + +* Github Actions +* Gitlab CI +* Travis CI +* Custom CI, set enviroment `CI=true` + +## How to use + +`go-semantic-release` config file +Create a file with the name `.release.yml` or anything else, but you need to set to every command `-c ` + +### Example config + +```yml +commitFormat: angular +branch: + master: release +release: 'github' +github: + repo: "go-semantic-release" + user: "nightapes" +assets: + - name: ./build/go-semantic-release + compress: false + - name: ./build/go-semantic-release.exe + compress: false +``` + +#### CommitFormat + +Set the commit format, at the moment we support ony [angular](https://github.com/angular/angular/blob/master/CONTRIBUTING.md#commit-message-format), more coming soon. + +```yml +commitFormat: angular +``` + +#### Branch + +You can define which kind of release should be created for different branches. + +Supported release kinds: + +* `release` -> `v1.0.0` +* `rc` -> `v1.0.0-rc.0` +* `beta` -> `v1.0.0-beta.0` +* `alpha` -> `v1.0.0-alpha.0` + +Add a branch config to your config + +```yml +branch: + : +``` + +#### Relase + +At the moment we support releases to gitlab and github. + +##### Github + +```yml +release: 'github' +github: + user: "" + ## Optional, if your not using github.com + customURL: +``` + +##### Gitlab + +```yml +release: 'gitlab' +gitlab: + repo: "" ## Example group/project + ## Optional, if your not using gitlab.com + customURL: +``` + +#### Assets + +You can upload assets to a release + +Support for gitlab and github. +If you want, you can let the file be compressed before uploading + +```yml +assets: + - name: ./build/go-semantic-release + compress: false +``` + +#### Changelog + +```yml +changelog: + printAll: false ## Print all valid commits to changelog +``` + +##### Docker + +You can print a help text for a docker image + +```yml +changelog: + docker: + latest: false ## If you uploaded a latest image + repository: ## Your docker repository, which is used for docker run +``` + +### Version + +`go-semantic-release` has two modes for calcualting the version: automatic or manual. + +#### Automatic + +Version will be calculated on the `next` or `release` command + +#### Manual + +If you don't want that `go-semantic-release` is calculating the version from the commits, you can set the version by hand with +following command: + +```bash +./go-semantic-release set 1.1.1 +``` + +### Print version + +Print the next version, can be used to add version to your program + +```bash +./go-semantic-release next +``` +Example with go-lang + +```bash +go build -ldflags "--X main.version=`./go-semantic-release next`" +``` + +### Create release + +```bash +./go-semantic-release release +``` -## Build +## Build from source -`go build ./cmd/go-semantic-release/` +```bash +go build ./cmd/go-semantic-release/ +``` -## Run +### Testing -Print the next version +```bash +go test ./... +``` -`./go-semantic-release version next` +### Linting -Set a version - -`./go-semantic-release version set v1.1.1` +``` +curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.16.0 +golangci-lint run ./... +``` \ No newline at end of file diff --git a/README2.md b/README2.md deleted file mode 100644 index e68c535..0000000 --- a/README2.md +++ /dev/null @@ -1,2 +0,0 @@ -go build ./cmd/main.go && ./main.exe version next --path /f/Repro/ambassador/ -go build ./cmd/main.go && ./main.exe --loglevel debug version set v1.1.1 --path /f/Repro/ambassador/ \ No newline at end of file diff --git a/cmd/go-semantic-release/commands/zip.go b/cmd/go-semantic-release/commands/zip.go index a459c32..7cda34d 100644 --- a/cmd/go-semantic-release/commands/zip.go +++ b/cmd/go-semantic-release/commands/zip.go @@ -11,7 +11,7 @@ func init() { var zipCmd = &cobra.Command{ Use: "zip", - Short: "Zip configured artifact from release config", + Short: "Zip configured artifact from release config (internal)", RunE: func(cmd *cobra.Command, args []string) error { config, err := cmd.Flags().GetString("config") if err != nil { diff --git a/.travis.yml b/examples/.travis.yml similarity index 100% rename from .travis.yml rename to examples/.travis.yml diff --git a/internal/changelog/changelog.go b/internal/changelog/changelog.go index cdbe754..ba1d87c 100644 --- a/internal/changelog/changelog.go +++ b/internal/changelog/changelog.go @@ -31,17 +31,34 @@ introduced by commit: {{ end -}} {{ end -}} {{ end -}} +{{ if .HasDocker}} +## Docker image + +New docker image is released under {{$.Backtick}}{{.DockerRepository}}:{{.Version}}{{$.Backtick}} + +### Usage + +{{$.Backtick}}docker run {{.DockerRepository}}:{{.Version}}{{$.Backtick}} +{{ if .HasDockerLatest}} +or + +{{$.Backtick}}docker run {{.DockerRepository}}:latest{{$.Backtick}} +{{ end -}} +{{ end -}} ` type changelogContent struct { - Commits map[string][]shared.AnalyzedCommit - BreakingChanges []shared.AnalyzedCommit - Order []string - Version string - Now time.Time - Backtick string - HasURL bool - URL string + Commits map[string][]shared.AnalyzedCommit + BreakingChanges []shared.AnalyzedCommit + Order []string + Version string + Now time.Time + Backtick string + HasURL bool + URL string + HasDocker bool + HasDockerLatest bool + DockerRepository string } //Changelog struct @@ -92,14 +109,17 @@ func (c *Changelog) GenerateChanglog(templateConfig shared.ChangelogTemplateConf } changelogContent := changelogContent{ - Version: templateConfig.Version, - Commits: commitsPerScope, - Now: c.releaseTime, - BreakingChanges: commitsBreakingChange, - Backtick: "`", - Order: order, - HasURL: templateConfig.CommitURL != "", - URL: templateConfig.CommitURL, + Version: templateConfig.Version, + Commits: commitsPerScope, + Now: c.releaseTime, + BreakingChanges: commitsBreakingChange, + Backtick: "`", + Order: order, + HasURL: templateConfig.CommitURL != "", + URL: templateConfig.CommitURL, + HasDocker: c.config.Changelog.Docker.Repository != "", + HasDockerLatest: c.config.Changelog.Docker.Latest, + DockerRepository: c.config.Changelog.Docker.Repository, } title, err := generateTemplate(defaultChangelogTitle, changelogContent) diff --git a/internal/releaser/gitlab/gitlab.go b/internal/releaser/gitlab/gitlab.go index 4e08f9f..1451ac1 100644 --- a/internal/releaser/gitlab/gitlab.go +++ b/internal/releaser/gitlab/gitlab.go @@ -2,7 +2,6 @@ package gitlab import ( "bytes" - "context" "encoding/json" "fmt" "io" @@ -26,7 +25,6 @@ const GITLAB = "gitlab" // Client type struct type Client struct { config *config.GitLabProvider - context context.Context client *http.Client baseURL string apiURL string @@ -36,8 +34,12 @@ type Client struct { } // New initialize a new gitlabRelease -func New(config *config.GitLabProvider, accessToken string) (*Client, error) { - ctx := context.Background() +func New(config *config.GitLabProvider) (*Client, error) { + accessToken, err := util.GetAccessToken(fmt.Sprintf("%s_ACCESS_TOKEN", strings.ToUpper(GITLAB))) + if err != nil { + return nil, err + } + tokenHeader := util.NewAddHeaderTransport(nil, "PRIVATE-TOKEN", accessToken) acceptHeader := util.NewAddHeaderTransport(tokenHeader, "Accept", "application/json") httpClient := &http.Client{ @@ -65,7 +67,6 @@ func New(config *config.GitLabProvider, accessToken string) (*Client, error) { return &Client{ token: accessToken, config: config, - context: ctx, baseURL: config.CustomURL, apiURL: config.CustomURL + "/api/v4", client: httpClient, diff --git a/internal/releaser/gitlab/gitlab_test.go b/internal/releaser/gitlab/gitlab_test.go index da51df4..134b05f 100644 --- a/internal/releaser/gitlab/gitlab_test.go +++ b/internal/releaser/gitlab/gitlab_test.go @@ -20,47 +20,55 @@ import ( ) func TestGetCommitURL(t *testing.T) { - + os.Setenv("GITLAB_ACCESS_TOKEN", "XXX") + defer os.Unsetenv("GITLAB_ACCESS_TOKEN") client, err := gitlab.New(&config.GitLabProvider{ CustomURL: "https://localhost/", Repo: "test/test", - }, "aToken") + }) assert.NoError(t, err) assert.Equal(t, "https://localhost/test/test/commit/{{hash}}", client.GetCommitURL()) } func TestGetCompareURL(t *testing.T) { - + os.Setenv("GITLAB_ACCESS_TOKEN", "XXX") + defer os.Unsetenv("GITLAB_ACCESS_TOKEN") client, err := gitlab.New(&config.GitLabProvider{ CustomURL: "https://localhost/", Repo: "test/test", - }, "aToken") + }) assert.NoError(t, err) assert.Equal(t, "https://localhost/test/test/compare/1.0.0...1.0.1", client.GetCompareURL("1.0.0", "1.0.1")) } func TestValidateConfig_EmptyRepro(t *testing.T) { + os.Setenv("GITLAB_ACCESS_TOKEN", "XXX") + defer os.Unsetenv("GITLAB_ACCESS_TOKEN") _, err := gitlab.New(&config.GitLabProvider{ CustomURL: "https://localhost/", - }, "aToken") + }) assert.Error(t, err) } func TestValidateConfig_DefaultURL(t *testing.T) { + os.Setenv("GITLAB_ACCESS_TOKEN", "XXX") + defer os.Unsetenv("GITLAB_ACCESS_TOKEN") config := &config.GitLabProvider{ Repo: "localhost/test", } - _, err := gitlab.New(config, "aToken") + _, err := gitlab.New(config) assert.NoError(t, err) assert.Equal(t, "https://gitlab.com", config.CustomURL) } func TestValidateConfig_CustomURL(t *testing.T) { + os.Setenv("GITLAB_ACCESS_TOKEN", "XXX") + defer os.Unsetenv("GITLAB_ACCESS_TOKEN") config := &config.GitLabProvider{ Repo: "/localhost/test/", CustomURL: "https://localhost/", } - _, err := gitlab.New(config, "aToken") + _, err := gitlab.New(config) assert.NoError(t, err) assert.Equal(t, "https://localhost", config.CustomURL) assert.Equal(t, "localhost/test", config.Repo) @@ -181,8 +189,9 @@ func TestCreateRelease(t *testing.T) { if testObject.config.CustomURL == "" { testObject.config.CustomURL = testServer.URL } - - client, err := gitlab.New(&testObject.config, "aToken") + os.Setenv("GITLAB_ACCESS_TOKEN", "aToken") + defer os.Unsetenv("GITLAB_ACCESS_TOKEN") + client, err := gitlab.New(&testObject.config) assert.NoError(t, err) err = client.CreateRelease(testObject.releaseVersion, testObject.generatedChangelog) @@ -307,8 +316,9 @@ func TestUploadAssets(t *testing.T) { if testObject.config.CustomURL == "" { testObject.config.CustomURL = testServer.URL } - - client, err := gitlab.New(&testObject.config, "aToken") + os.Setenv("GITLAB_ACCESS_TOKEN", "aToken") + defer os.Unsetenv("GITLAB_ACCESS_TOKEN") + client, err := gitlab.New(&testObject.config) assert.NoError(t, err) client.Release = "1.0.0" diff --git a/internal/releaser/releaser.go b/internal/releaser/releaser.go index 0aa21f3..b2a9863 100644 --- a/internal/releaser/releaser.go +++ b/internal/releaser/releaser.go @@ -2,11 +2,9 @@ package releaser import ( "fmt" - "strings" "github.com/Nightapes/go-semantic-release/internal/releaser/github" "github.com/Nightapes/go-semantic-release/internal/releaser/gitlab" - "github.com/Nightapes/go-semantic-release/internal/releaser/util" "github.com/Nightapes/go-semantic-release/internal/shared" "github.com/Nightapes/go-semantic-release/pkg/config" @@ -42,11 +40,7 @@ func (r *Releasers) GetReleaser() (Releaser, error) { return github.New(&r.config.GitHubProvider) case gitlab.GITLAB: log.Debugf("initialize new %s-provider", gitlab.GITLAB) - accessToken, err := util.GetAccessToken(fmt.Sprintf("%s_ACCESS_TOKEN", strings.ToUpper(gitlab.GITLAB))) - if err != nil { - return nil, err - } - return gitlab.New(&r.config.GitLabProvider, accessToken) + return gitlab.New(&r.config.GitLabProvider) } return nil, fmt.Errorf("could not initialize a releaser from this type: %s", r.config.Release) } diff --git a/pkg/config/config.go b/pkg/config/config.go index a2767ba..1a036b5 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -10,9 +10,23 @@ import ( // ChangelogConfig struct type ChangelogConfig struct { - PrintAll bool `yaml:"printAll,omitempty"` - Template string `yaml:"template,omitempty"` - TemplatePath string `yaml:"templatePath,omitempty"` + PrintAll bool `yaml:"printAll,omitempty"` + Template string `yaml:"template,omitempty"` + TemplatePath string `yaml:"templatePath,omitempty"` + Docker ChangelogDocker `yaml:"docker,omitempty"` + NPM ChangelogNPM `yaml:"npm,omitempty"` +} + +//ChangelogDocker type struct +type ChangelogDocker struct { + Latest bool `yaml:"latest"` + Repository string `yaml:"repository"` +} + +//ChangelogNPM type struct +type ChangelogNPM struct { + YARN bool `yaml:"latest"` + Repository string `yaml:"repository"` } //Asset type struct