From 1ae68c6ac06a825bb267577443ed8b76913cc52e Mon Sep 17 00:00:00 2001 From: dark0dave Date: Thu, 14 Dec 2023 11:00:00 +0000 Subject: [PATCH] feat(commitizen): Use commitizen instead of custom script and add git cliff Signed-off-by: dark0dave --- .github/workflows/main.yaml | 4 +- CHANGELOG.md | 46 --------------------- Dockerfile | 2 +- Makefile | 17 ++++---- cliff.toml | 77 ++++++++++++++++++++++++++++++++++++ scripts/alpine-setup.sh | 11 +----- scripts/changelog.tmpl | 72 --------------------------------- scripts/git-check-commits.sh | 21 ---------- scripts/github-changelog.sh | 21 ---------- 9 files changed, 89 insertions(+), 182 deletions(-) delete mode 100644 CHANGELOG.md create mode 100644 cliff.toml delete mode 100644 scripts/changelog.tmpl delete mode 100755 scripts/git-check-commits.sh delete mode 100755 scripts/github-changelog.sh diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index a96e2dd5..db04474e 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -21,7 +21,7 @@ jobs: test: name: test runs-on: ubuntu-latest - container: golang:1.21-alpine3.17 + container: golang:1.21-alpine3.19 steps: - name: Install git run: apk add --update --no-cache git @@ -37,7 +37,7 @@ jobs: build: name: build runs-on: ubuntu-latest - container: golang:1.21-alpine3.17 + container: golang:1.21-alpine3.19 strategy: matrix: os: [linux, darwin, windows] diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index bc0524a6..00000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,46 +0,0 @@ -## kubent Release Notes - -### 0.3.2 (2020-09-22) - -**Fixes**: -- Fixes missing resources with File Collector (#40) - -**Internal/Misc**: -- Fixes git sha in binary (#41) -- Increased test coverage (storeCollector) and introduced K8s fake client tests (ClusterCollector) (#37) - -### 0.3.1 (2020-09-04) - -**Fixes**: -- Fix missing resources (#34) -- Fix panic when collector fails to initialize (#32) - -### 0.3.0 (2020-08-11) - -**Features**: -- Added stdin support (#19) -- Added support for reading manifests from files (#15) -- Improved Error reporting (#20) - -**Fixes**: -- Support resources without namespace (#14) - -**Internal/misc**: -- Added first and many other tests (#9, #19), thanks @david-doit-intl 🚀 -- Cleaned up logic in main (#16) -- Added release notes and minor deprecated message improvement (#22) - -### 0.2.1 (2020-05-24) - -**Features**: -- Added install script (#3, #4) - -**Internal/misc**: -- Moved logic to unpack last-applied config to collector (7) - -### 0.2.0 (2020-04-15) - -**Fixes**: -- Produce static binaries (#2) - -### 0.1.0 - Initial Release (2020-04-09) diff --git a/Dockerfile b/Dockerfile index 05e6f568..3ee18e7f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM --platform=$BUILDPLATFORM golang:1.21-alpine3.17 AS builder +FROM --platform=$BUILDPLATFORM golang:1.21-alpine3.19 AS builder ARG GITHUB_REF GITHUB_SHA WORKDIR /src COPY go.mod go.sum ./ diff --git a/Makefile b/Makefile index c79ebf9a..7442ad50 100644 --- a/Makefile +++ b/Makefile @@ -34,6 +34,9 @@ GOARCH ?= $(shell go env GOARCH) BIN_ARCH ?= $(GOOS)-$(GOARCH) RELEASE_SUFFIX ?= $(GIT_REF)-$(BIN_ARCH).tar.gz +OPA_REPO ?= github.com/open-policy-agent/opa +OPA_VERSION ?= v0.22.0 + BIN_DIR ?= bin CMD_DIR ?= cmd RELEASE_DIR ?= release-artifacts @@ -47,7 +50,7 @@ SRC ?= $(shell find . -iname '*.go') GOCMD ?= go GOBUILD ?= $(GOCMD) build -REQ_BINS = go opa +REQ_BINS = go _ := $(foreach exec,$(REQ_BINS), \ $(if $(shell which $(exec)),some string,$(error "No $(exec) binary in $$PATH"))) @@ -81,21 +84,16 @@ $(RELEASE_DIR)/%-$(RELEASE_SUFFIX): $(BIN_DIR)/%-$(BIN_ARCH)$(BIN_SUFFIX) $(TAR) -cvz --transform 's,$(BIN_DIR)/$(*)-$(BIN_ARCH)$(BIN_SUFFIX),$(*)$(BIN_SUFFIX),gi' -f "$@" "$<" ## Run Go tests -test: test-fmt test-git +test: test-fmt go test -v -coverprofile fmtcoverage.html ./... .PHONY: test ## Run go and opt fmt checks test-fmt: - test -z "$$(opa fmt -l pkg/rules/rego/*)" + test -z "$$(go run $(OPA_REPO)@$(OPA_VERSION) fmt -l pkg/rules/rego/*)" test -z "$$(go fmt ./...)" .PHONY: test-fmt -## Check git commits formatting -test-git: - ./scripts/git-check-commits.sh -.PHONY: test-git - ## Clean build artifacts clean: rm -rf $(BIN_DIR) $(RELEASE_DIR) @@ -103,5 +101,6 @@ clean: ## Generate Changelog based on PRs changelog: - OUTPUT_FILE=$(RELEASE_DIR)/$(CHANGELOG) ./scripts/github-changelog.sh + mkdir -p $(RELEASE_DIR) + git cliff -u --latest -o $(RELEASE_DIR)/$(CHANGELOG) .PHONY: changelog diff --git a/cliff.toml b/cliff.toml new file mode 100644 index 00000000..5cd3b7c7 --- /dev/null +++ b/cliff.toml @@ -0,0 +1,77 @@ +# git-cliff ~ default configuration file +# https://git-cliff.org/docs/configuration +# +# Lines starting with "#" are comments. +# Configuration options are organized into tables and keys. +# See documentation for more information on available options. + +[changelog] +# changelog header +header = "" +# template for the changelog body +# https://tera.netlify.app/docs +body = """ +**Docker Image**: {{ get_env(name='REGISTRY', default='') }}/{{ get_env(name='IMAG_NAME', default='') }}:{{ get_env(name='GITHUB_REF_NAME', default='') }} + +### Changelog +{% for group, commits in commits | group_by(attribute="group") %} + \n#### {{ group | striptags | trim | upper_first }}: + {% for commit in commits + | filter(attribute="group") + | sort(attribute="scope") %} + - {{commit.scope}}: \ + {{ commit.message }} ([{{ commit.id | truncate(length=7, end="") }}](https://github.com/doitintl/kube-no-trouble/commit/{{ commit.id }}) by [{{ commit.author.name }}](https://github.com/{{ commit.author.name }})) + {%- endfor -%} +{% endfor %} +""" +# remove the leading and trailing whitespace from the template +trim = true +# changelog footer +footer = "" +# postprocessors +postprocessors = [ + # { pattern = '', replace = "https://github.com/orhun/git-cliff" }, # replace repository URL +] +[git] +# parse the commits based on https://www.conventionalcommits.org +conventional_commits = true +# filter out the commits that are not conventional +filter_unconventional = false +# process each line of a commit as an individual commit +split_commits = false +# regex for preprocessing the commit messages +commit_preprocessors = [ + # { pattern = '\((\w+\s)?#([0-9]+)\)', replace = "([#${2}](/issues/${2}))"}, # replace issue numbers +] +# regex for parsing and grouping commits +commit_parsers = [ + { message = "^(refactor|ref)", group = "Internal/Other", scope="ref" }, + { message = "^build", group = "Internal/Other", scope="build" }, + { message = "^chore", group = "Internal/Other", scope="chore" }, + { message = "^ci", group = "Internal/Other", scope="ci" }, + { message = "^dep", group = "Internal/Other", scope="dep" }, + { message = "^doc", group = "Internal/Other", scope="doc" }, + { message = "^feat", group = "Features", scope="feat" }, + { message = "^fix", group = "Fixes", scope="fix" }, + { message = "^perf", group = "Internal/Other", scope="perf" }, + { message = "^style", group = "Internal/Other", scope="style" }, + { message = "^test", group = "Internal/Other", scope="test" }, + { body = ".*security", group = "Internal/Other", scope="sec" }, + { message = "^revert", group = "Internal/Other", scope="revert" }, +] +# protect breaking changes from being skipped due to matching a skipping commit_parser +protect_breaking_commits = false +# filter out the commits that are not matched by commit parsers +filter_commits = false +# glob pattern for matching git tags +tag_pattern = "^[0-9]+.[0-9]+.[0-9]+$" +# regex for skipping tags +skip_tags = "^nightly-.*" +# regex for ignoring tags +ignore_tags = "^nightly-.*" +# sort the tags topologically +topo_order = false +# sort the commits inside sections by oldest/newest order +sort_commits = "oldest" +# limit the number of commits included in the changelog. +# limit_commits = 42 diff --git a/scripts/alpine-setup.sh b/scripts/alpine-setup.sh index a9aa42dc..994a4e3f 100755 --- a/scripts/alpine-setup.sh +++ b/scripts/alpine-setup.sh @@ -4,21 +4,12 @@ set -eou pipefail LC_CTYPE=C -OPA_VERSION="0.22.0" -CHANGELOG_VERSION="1.1.0" - apk add --update --no-cache \ bash \ curl \ git \ + git-cliff \ jq \ make \ tar \ xz - -wget -q -O "/usr/local/bin/opa" "https://github.com/open-policy-agent/opa/releases/download/v${OPA_VERSION}/opa_linux_amd64" -chmod +x "/usr/local/bin/opa" - -wget -q "https://github.com/paultyng/changelog-gen/releases/download/v${CHANGELOG_VERSION}/changelog-gen_Linux_x86_64.tar.gz" -O - | tar -xz -chmod +x changelog-gen -mv changelog-gen /usr/local/bin/changelog-gen diff --git a/scripts/changelog.tmpl b/scripts/changelog.tmpl deleted file mode 100644 index 9c1cd4d0..00000000 --- a/scripts/changelog.tmpl +++ /dev/null @@ -1,72 +0,0 @@ -{{- $announcements := newStringList -}} -{{- $breaking := newStringList -}} -{{- $features := newStringList -}} -{{- $changes := newStringList -}} -{{- $fixes := newStringList -}} -{{- $other := newStringList -}} - -{{- range . -}} - {{if has "announcement" .Labels -}} - {{$features = append $announcements (renderReleaseNote .) -}} - {{else if .BreakingChange -}} - {{$breaking = append $breaking (renderReleaseNote .) -}} - {{else if has "feature" .Labels -}} - {{$features = append $features (renderReleaseNote .) -}} - {{else if has "change" .Labels -}} - {{$fixes = append $changes (renderReleaseNote .) -}} - {{else if has "fix" .Labels -}} - {{$fixes = append $fixes (renderReleaseNote .) -}} - {{else -}} - {{$other = append $other (renderReleaseNote .) -}} - {{end -}} -{{- end -}} - -{{- if gt (len $announcements) 0 -}} -{{range $announcements | sortAlpha -}} -{{. }} -{{end -}} -{{- end -}} - -**Docker Image**: `{{ expandenv "$REGISTRY/$IMAGE_NAME:$GITHUB_REF_NAME" }}` - -### Changelog - -{{- if gt (len $breaking) 0}} -**Breaking Changes**: - -{{range $breaking | sortAlpha -}} -* {{. }} -{{end -}} -{{- end -}} - -{{- if gt (len $features) 0}} -**Features**: - -{{range $features | sortAlpha -}} -* {{. }} -{{end -}} -{{- end -}} - -{{- if gt (len $changes) 0}} -**Changes**: - -{{range $changes | sortAlpha -}} -* {{. }} -{{end -}} -{{- end -}} - -{{- if gt (len $fixes) 0}} -**Fixes**: - -{{range $fixes | sortAlpha -}} -* {{. }} -{{end -}} -{{- end -}} - -{{- if gt (len $other) 0}} -**Internal/Other**: - -{{range $other | sortAlpha -}} -* {{. }} -{{end -}} -{{- end -}} diff --git a/scripts/git-check-commits.sh b/scripts/git-check-commits.sh deleted file mode 100755 index 13fc7c74..00000000 --- a/scripts/git-check-commits.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env sh -# Set strict error checking -set -eou pipefail - -BASE_REF="origin/master" -COMMIT_TYPES="build|chore|ci|dep|docs|feat|fix|ref|style|test" -COMMIT_REGEXP="^(${COMMIT_TYPES}): [A-Z]+.{5,}[^.]$" - -# Needed for ci -git config --global --add safe.directory "${GITHUB_WORKSPACE:-$PWD}" || true - -COMMITS_COUNT="$(git log --oneline --no-merges "${BASE_REF}..HEAD" | wc -l)" - -echo "- Checking commit messages: ${COMMITS_COUNT} commits" - -BAD_COMMITS="$(git log --oneline --no-merges -E --invert-grep --grep="${COMMIT_REGEXP}" "${BASE_REF}..HEAD")" -if [ -n "${BAD_COMMITS}" ]; then - echo "${BAD_COMMITS}" - echo "Error: commit messages do no confirm to required format: ${COMMIT_REGEXP}" - exit 1 -fi diff --git a/scripts/github-changelog.sh b/scripts/github-changelog.sh deleted file mode 100755 index 2ae44a4d..00000000 --- a/scripts/github-changelog.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env sh -# Set strict error checking -set -eou pipefail - -GH_ORG="doitintl" -GH_REPO="kube-no-trouble" -CHANGELOG_TEMPLATE="./scripts/changelog.tmpl" -OUTPUT_FILE="${OUTPUT_FILE:="./changelog.md"}" - -# Needed for ci -git config --global --add safe.directory "${GITHUB_WORKSPACE:-$PWD}" || true - -RELEASE_SHA="$(curl --silent "https://api.github.com/repos/${GH_ORG}/${GH_REPO}/releases/latest" \ - | jq -r '.target_commitish')" -MASTER_SHA="$(git rev-parse origin/master)" - -echo "- Generating changelog (${OUTPUT_FILE})" - -changelog-gen -changelog "${CHANGELOG_TEMPLATE}" \ - -owner "${GH_ORG}" -repo "${GH_REPO}" \ - "${RELEASE_SHA}" "${MASTER_SHA}" | tee "${OUTPUT_FILE}"