From b707352bf9f694dc8d64b178377497148f4470f1 Mon Sep 17 00:00:00 2001 From: John Olheiser Date: Sun, 26 Jan 2020 03:43:05 +0000 Subject: [PATCH] CI and Linter (#27) Add S3 for master/releases Signed-off-by: jolheiser Move to arm64 for more RAM Signed-off-by: jolheiser Add lint to CI Signed-off-by: jolheiser Rename default to compliance and un-test releases Signed-off-by: jolheiser Test Signed-off-by: jolheiser Initial work Signed-off-by: jolheiser Co-authored-by: jolheiser Reviewed-on: https://gitea.com/gitea/changelog/pulls/27 Reviewed-by: Lunny Xiao Reviewed-by: 6543 <6543@noreply.gitea.io> --- .drone.yml | 102 +++++++++++++++++++++++++++++++++++++++++++--- .golangci.yml | 24 +++++++++++ Makefile | 68 +++++++++++++++++++++++++++++++ cmd/generate.go | 99 ++++++++++++++++++++++++-------------------- main.go | 4 +- service/gitea.go | 2 +- service/github.go | 2 +- 7 files changed, 248 insertions(+), 53 deletions(-) create mode 100644 .golangci.yml create mode 100644 Makefile diff --git a/.drone.yml b/.drone.yml index 48cbfc5..c8499fa 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,20 +1,32 @@ --- kind: pipeline -name: default +name: compliance platform: os: linux - arch: amd64 + arch: arm64 + +trigger: + event: + - pull_request steps: - - name: build pull: always image: golang:1.13 environment: GOPROXY: https://goproxy.cn,direct commands: - - go test -race + - go test -race ./... + - go build + +- name: check + pull: always + image: golang:1.13 + environment: + GOPROXY: https://goproxy.cn,direct + commands: + - make lint - name: discord pull: always @@ -32,6 +44,86 @@ steps: - changed - failure +--- +kind: pipeline +name: release + +platform: + os: linux + arch: amd64 + trigger: branch: - - master + - master + event: + - push + - tag + +steps: + - name: fetch-tags + pull: always + image: docker:git + commands: + - git fetch --tags --force + + - name: release + pull: always + image: techknowlogick/xgo:latest + environment: + GOPROXY: https://goproxy.cn,direct + commands: + - export PATH=$PATH:$GOPATH/bin + - make release + + - name: bucket-master + pull: always + image: plugins/s3:1 + settings: + acl: public-read + bucket: releases + endpoint: https://storage.gitea.io + path_style: true + source: "dist/*" + strip_prefix: dist/ + target: /changelog-tool/master + environment: + AWS_ACCESS_KEY_ID: + from_secret: aws_access_key_id + AWS_SECRET_ACCESS_KEY: + from_secret: aws_secret_access_key + when: + event: + - push + + - name: bucket-tag + pull: always + image: plugins/s3:1 + settings: + acl: public-read + bucket: releases + endpoint: https://storage.gitea.io + path_style: true + source: "dist/*" + strip_prefix: dist/ + target: "/changelog-tool/${DRONE_TAG##v}" + environment: + AWS_ACCESS_KEY_ID: + from_secret: aws_access_key_id + AWS_SECRET_ACCESS_KEY: + from_secret: aws_secret_access_key + when: + event: + - tag + + - name: gitea + pull: always + image: plugins/gitea-release:1 + settings: + api_key: + from_secret: gitea_token + base_url: https://gitea.com + files: + - "dist/*" + when: + event: + - tag \ No newline at end of file diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..6561c3f --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,24 @@ +linters: + enable: + - deadcode + - dogsled + - dupl + - errcheck + - funlen + - gocognit + - goconst + - gocritic + - gocyclo + - gofmt + - golint + - gosimple + - govet + - maligned + - misspell + - prealloc + - staticcheck + - structcheck + - typecheck + - unparam + - unused + - varcheck \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..16e96a8 --- /dev/null +++ b/Makefile @@ -0,0 +1,68 @@ +DIST := dist +GO ?= go + +ifneq ($(DRONE_TAG),) + SHORT_VERSION ?= $(subst v,,$(DRONE_TAG)) + LONG_VERSION ?= $(SHORT_VERSION) +else + SHORT_VERSION ?= $(shell git describe --tags --always --abbrev=0 | sed 's/-/+/' | sed 's/^v//') + LONG_VERSION ?= $(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//') +endif + +LDFLAGS := $(LDFLAGS) -X "main.Version=$(LONG_VERSION)" + +.PHONY: build +build: generate + $(GO) build -ldflags '-s -w $(LDFLAGS)' + +.PHONY: generate +generate: + $(GO) generate ./... + +.PHONY: lint +lint: + @hash golangci-lint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ + export BINARY="golangci-lint"; \ + curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.23.1; \ + fi + golangci-lint run --timeout 5m + +.PHONY: fmt +fmt: + go fmt ./... + +.PHONY: release +release: release-dirs check-xgo release-windows release-linux release-darwin release-compress release-check + +.PHONY: release-dirs +release-dirs: + mkdir -p $(DIST)/ + +.PHONY: check-xgo +check-xgo: + @hash xgo > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ + $(GO) get -u src.techknowlogick.com/xgo; \ + fi + +.PHONY: release-linux +release-linux: + xgo -dest $(DIST)/ -targets 'linux/amd64,linux/386,linux/arm-5,linux/arm-6,linux/arm64,linux/mips64le,linux/mips,linux/mipsle' -out changelog-$(SHORT_VERSION) . + +.PHONY: release-windows +release-windows: + xgo -dest $(DIST)/ -targets 'windows/*' -out changelog-$(SHORT_VERSION) . + +.PHONY: release-darwin +release-darwin: + xgo -dest $(DIST)/ -targets 'darwin/*' -out changelog-$(SHORT_VERSION) . + +.PHONY: release-check +release-check: + cd $(DIST)/; for file in `find . -type f -name "*"`; do echo "checksumming $${file}" && $(SHASUM) `echo $${file} | sed 's/^..//'` > $${file}.sha256; done; + +.PHONY: release-compress +release-compress: + @hash gxz > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ + go get -u github.com/ulikunitz/xz/cmd/gxz; \ + fi + cd $(DIST)/; for file in `find . -type f -name "*"`; do echo "compressing $${file}" && gxz -k -9 $${file}; done; \ No newline at end of file diff --git a/cmd/generate.go b/cmd/generate.go index bd89e42..da28cb2 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -6,6 +6,7 @@ package cmd import ( "fmt" + "regexp" "code.gitea.io/changelog/config" "code.gitea.io/changelog/service" @@ -13,11 +14,16 @@ import ( "github.com/urfave/cli/v2" ) -var Generate = &cli.Command{ - Name: "generate", - Usage: "Generates a changelog", - Action: runGenerate, -} +var ( + Generate = &cli.Command{ + Name: "generate", + Usage: "Generates a changelog", + Action: runGenerate, + } + labels = make(map[string]string) + entries = make(map[string][]service.PullRequest) + defaultGroup string +) func runGenerate(cmd *cli.Context) error { cfg, err := config.New(ConfigPathFlag) @@ -25,22 +31,7 @@ func runGenerate(cmd *cli.Context) error { return err } - labels := make(map[string]string) - entries := make(map[string][]service.PullRequest) - var defaultGroup string - for _, g := range cfg.Groups { - entries[g.Name] = []service.PullRequest{} - for _, l := range g.Labels { - labels[l] = g.Name - } - if g.Default { - defaultGroup = g.Name - } - } - - if defaultGroup == "" { - defaultGroup = cfg.Groups[len(cfg.Groups)-1].Name - } + processGroups(cfg.Groups) s, err := service.New(cfg.Service, cfg.Repo, cfg.BaseURL, MilestoneFlag, TokenFlag) if err != nil { @@ -52,29 +43,7 @@ func runGenerate(cmd *cli.Context) error { return err } -PRLoop: // labels in Go, let's get old school - for _, pr := range prs { - if pr.Index < AfterFlag { - continue - } - - var label string - for _, lb := range pr.Labels { - if cfg.SkipRegex != nil && cfg.SkipRegex.MatchString(lb.Name) { - continue PRLoop - } - - if g, ok := labels[lb.Name]; ok && len(label) == 0 { - label = g - } - } - - if len(label) > 0 { - entries[label] = append(entries[label], pr) - } else { - entries[defaultGroup] = append(entries[defaultGroup], pr) - } - } + processPRs(prs, cfg.SkipRegex) fmt.Println(title) for _, g := range cfg.Groups { @@ -99,3 +68,45 @@ PRLoop: // labels in Go, let's get old school return nil } + +func processGroups(groups []config.Group) { + for _, g := range groups { + entries[g.Name] = []service.PullRequest{} + for _, l := range g.Labels { + labels[l] = g.Name + } + if g.Default { + defaultGroup = g.Name + } + } + + if defaultGroup == "" { + defaultGroup = groups[len(groups)-1].Name + } +} + +func processPRs(prs []service.PullRequest, skip *regexp.Regexp) { +PRLoop: // labels in Go, let's get old school + for _, pr := range prs { + if pr.Index < AfterFlag { + continue + } + + var label string + for _, lb := range pr.Labels { + if skip != nil && skip.MatchString(lb.Name) { + continue PRLoop + } + + if g, ok := labels[lb.Name]; ok && len(label) == 0 { + label = g + } + } + + if len(label) > 0 { + entries[label] = append(entries[label], pr) + } else { + entries[defaultGroup] = append(entries[defaultGroup], pr) + } + } +} diff --git a/main.go b/main.go index f23b659..e828239 100644 --- a/main.go +++ b/main.go @@ -15,9 +15,9 @@ import ( "github.com/urfave/cli/v2" ) -const ( +var ( // Version of changelog - Version = "0.2" + Version = "development" ) func main() { diff --git a/service/gitea.go b/service/gitea.go index cfcb642..6783a79 100644 --- a/service/gitea.go +++ b/service/gitea.go @@ -112,7 +112,7 @@ func (ge *Gitea) Contributors() (ContributorList, error) { } contributors := make(ContributorList, 0, len(contributorsMap)) - for contributor, _ := range contributorsMap { + for contributor := range contributorsMap { contributors = append(contributors, Contributor{ Name: contributor, Profile: fmt.Sprintf("%s/%s", ge.BaseURL, contributor), diff --git a/service/github.go b/service/github.go index bea8f09..34cf8c6 100644 --- a/service/github.go +++ b/service/github.go @@ -101,7 +101,7 @@ func (gh *GitHub) Contributors() (ContributorList, error) { } contributors := make(ContributorList, 0, len(contributorsMap)) - for contributor, _ := range contributorsMap { + for contributor := range contributorsMap { contributors = append(contributors, Contributor{ Name: contributor, Profile: fmt.Sprintf("https://github.com/%s", contributor),