Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance upgrade #102

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/slsa-goreleaser/darwin-amd64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ main: ./main.go
binary: go-semver-release-{{ .Os }}-{{ .Arch }}

ldflags:
- "-X github.com/s0ders/go-semver-release/v5/cmd.cmdVersion={{ .Env.VERSION }}"
- "-X github.com/s0ders/go-semver-release/v5/cmd.buildNumber={{ .Env.BUILD_NUMBER }}"
- "-X github.com/s0ders/go-semver-release/v5/cmd.buildCommitHash={{ .Env.COMMIT_HASH }}"
- "-X github.com/s0ders/go-semver-release/v6/cmd.cmdVersion={{ .Env.VERSION }}"
- "-X github.com/s0ders/go-semver-release/v6/cmd.buildNumber={{ .Env.BUILD_NUMBER }}"
- "-X github.com/s0ders/go-semver-release/v6/cmd.buildCommitHash={{ .Env.COMMIT_HASH }}"
- "-w"
- "-s"
6 changes: 3 additions & 3 deletions .github/slsa-goreleaser/darwin-arm64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ main: ./main.go
binary: go-semver-release-{{ .Os }}-{{ .Arch }}

ldflags:
- "-X github.com/s0ders/go-semver-release/v5/cmd.cmdVersion={{ .Env.VERSION }}"
- "-X github.com/s0ders/go-semver-release/v5/cmd.buildNumber={{ .Env.BUILD_NUMBER }}"
- "-X github.com/s0ders/go-semver-release/v5/cmd.buildCommitHash={{ .Env.COMMIT_HASH }}"
- "-X github.com/s0ders/go-semver-release/v6/cmd.cmdVersion={{ .Env.VERSION }}"
- "-X github.com/s0ders/go-semver-release/v6/cmd.buildNumber={{ .Env.BUILD_NUMBER }}"
- "-X github.com/s0ders/go-semver-release/v6/cmd.buildCommitHash={{ .Env.COMMIT_HASH }}"
- "-w"
- "-s"
6 changes: 3 additions & 3 deletions .github/slsa-goreleaser/linux-amd64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ main: ./main.go
binary: go-semver-release-{{ .Os }}-{{ .Arch }}

ldflags:
- "-X github.com/s0ders/go-semver-release/v5/cmd.cmdVersion={{ .Env.VERSION }}"
- "-X github.com/s0ders/go-semver-release/v5/cmd.buildNumber={{ .Env.BUILD_NUMBER }}"
- "-X github.com/s0ders/go-semver-release/v5/cmd.buildCommitHash={{ .Env.COMMIT_HASH }}"
- "-X github.com/s0ders/go-semver-release/v6/cmd.cmdVersion={{ .Env.VERSION }}"
- "-X github.com/s0ders/go-semver-release/v6/cmd.buildNumber={{ .Env.BUILD_NUMBER }}"
- "-X github.com/s0ders/go-semver-release/v6/cmd.buildCommitHash={{ .Env.COMMIT_HASH }}"
- "-w"
- "-s"
6 changes: 3 additions & 3 deletions .github/slsa-goreleaser/linux-arm64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ main: ./main.go
binary: go-semver-release-{{ .Os }}-{{ .Arch }}

ldflags:
- "-X github.com/s0ders/go-semver-release/v5/cmd.cmdVersion={{ .Env.VERSION }}"
- "-X github.com/s0ders/go-semver-release/v5/cmd.buildNumber={{ .Env.BUILD_NUMBER }}"
- "-X github.com/s0ders/go-semver-release/v5/cmd.buildCommitHash={{ .Env.COMMIT_HASH }}"
- "-X github.com/s0ders/go-semver-release/v6/cmd.cmdVersion={{ .Env.VERSION }}"
- "-X github.com/s0ders/go-semver-release/v6/cmd.buildNumber={{ .Env.BUILD_NUMBER }}"
- "-X github.com/s0ders/go-semver-release/v6/cmd.buildCommitHash={{ .Env.COMMIT_HASH }}"
- "-w"
- "-s"
6 changes: 3 additions & 3 deletions .github/slsa-goreleaser/windows-amd64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ main: ./main.go
binary: go-semver-release-{{ .Os }}-{{ .Arch }}.exe

ldflags:
- "-X github.com/s0ders/go-semver-release/v5/cmd.cmdVersion={{ .Env.VERSION }}"
- "-X github.com/s0ders/go-semver-release/v5/cmd.buildNumber={{ .Env.BUILD_NUMBER }}"
- "-X github.com/s0ders/go-semver-release/v5/cmd.buildCommitHash={{ .Env.COMMIT_HASH }}"
- "-X github.com/s0ders/go-semver-release/v6/cmd.cmdVersion={{ .Env.VERSION }}"
- "-X github.com/s0ders/go-semver-release/v6/cmd.buildNumber={{ .Env.BUILD_NUMBER }}"
- "-X github.com/s0ders/go-semver-release/v6/cmd.buildCommitHash={{ .Env.COMMIT_HASH }}"
- "-w"
- "-s"
6 changes: 3 additions & 3 deletions .github/slsa-goreleaser/windows-arm64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ main: ./main.go
binary: go-semver-release-{{ .Os }}-{{ .Arch }}.exe

ldflags:
- "-X github.com/s0ders/go-semver-release/v5/cmd.cmdVersion={{ .Env.VERSION }}"
- "-X github.com/s0ders/go-semver-release/v5/cmd.buildNumber={{ .Env.BUILD_NUMBER }}"
- "-X github.com/s0ders/go-semver-release/v5/cmd.buildCommitHash={{ .Env.COMMIT_HASH }}"
- "-X github.com/s0ders/go-semver-release/v6/cmd.cmdVersion={{ .Env.VERSION }}"
- "-X github.com/s0ders/go-semver-release/v6/cmd.buildNumber={{ .Env.BUILD_NUMBER }}"
- "-X github.com/s0ders/go-semver-release/v6/cmd.buildCommitHash={{ .Env.COMMIT_HASH }}"
- "-w"
- "-s"
4 changes: 0 additions & 4 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ on:
branches: [ "main" ]

env:
DOCKER_REPO: docker.io/s0ders
DOCKER_IMAGE: docker.io/s0ders/go-semver-release
GIT_CI_USERNAME: go-semver-ci
GIT_CI_EMAIL: [email protected]
GO_VERSION: 1.23.1

permissions: read-all
Expand Down
4 changes: 2 additions & 2 deletions .justfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ appVersion := "v0.0.0+local"
buildNumber := "local"
commitHash := "local"

importPath := "github.com/s0ders/go-semver-release/v5/"
importPath := "github.com/s0ders/go-semver-release/v6/"
ldFlags := "-X " + importPath + "cmd.cmdVersion=" + appVersion + " -X " + importPath + "cmd.buildNumber=" + buildNumber + " -X " + importPath + "cmd.buildCommitHash=" + commitHash + " -w -s"

tests:
Expand Down Expand Up @@ -38,7 +38,7 @@ clean:
rm -rf ./bin/*

lint:
golangci-lint run
golangci-lint run ./...
gocyclo -over 15 .

vuln:
Expand Down
2 changes: 1 addition & 1 deletion .semver.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
remote: true
remote-name: origin
git-name: Go Semver Release
git-email: [email protected]
git-email: [email protected]
tag-prefix: v
branches:
- name: main
Expand Down
2 changes: 1 addition & 1 deletion build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ WORKDIR /app
COPY .. /app

RUN go mod download
RUN CGO_ENABLED=0 go build -ldflags="-X github.com/s0ders/go-semver-release/v5/cmd.cmdVersion=$APP_VERSION -X github.com/s0ders/go-semver-release/v5/cmd.buildNumber=$APP_BUILD_NUMBER -X github.com/s0ders/go-semver-release/v5/cmd.buildCommitHash=$APP_COMMIT_HASH -w -s" -v -o app .
RUN CGO_ENABLED=0 go build -ldflags="-X github.com/s0ders/go-semver-release/v6/cmd.cmdVersion=$APP_VERSION -X github.com/s0ders/go-semver-release/v6/cmd.buildNumber=$APP_BUILD_NUMBER -X github.com/s0ders/go-semver-release/v6/cmd.buildCommitHash=$APP_COMMIT_HASH -w -s" -v -o app .

# alpine:3.20.3
FROM alpine@sha256:beefdbd8a1da6d2915566fde36db9db0b524eb737fc57cd1367effd16dc0d06d AS vulnscan
Expand Down
138 changes: 62 additions & 76 deletions cmd/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ import (
"github.com/go-git/go-git/v5"
"github.com/spf13/cobra"

"github.com/s0ders/go-semver-release/v5/internal/branch"
"github.com/s0ders/go-semver-release/v5/internal/ci"
"github.com/s0ders/go-semver-release/v5/internal/gpg"
"github.com/s0ders/go-semver-release/v5/internal/monorepo"
"github.com/s0ders/go-semver-release/v5/internal/parser"
"github.com/s0ders/go-semver-release/v5/internal/remote"
"github.com/s0ders/go-semver-release/v5/internal/rule"
"github.com/s0ders/go-semver-release/v5/internal/tag"
"github.com/s0ders/go-semver-release/v6/internal/appcontext"
"github.com/s0ders/go-semver-release/v6/internal/branch"
"github.com/s0ders/go-semver-release/v6/internal/ci"
"github.com/s0ders/go-semver-release/v6/internal/gpg"
"github.com/s0ders/go-semver-release/v6/internal/monorepo"
"github.com/s0ders/go-semver-release/v6/internal/parser"
"github.com/s0ders/go-semver-release/v6/internal/remote"
"github.com/s0ders/go-semver-release/v6/internal/rule"
"github.com/s0ders/go-semver-release/v6/internal/tag"
)

func NewReleaseCmd(ctx *AppContext) *cobra.Command {
func NewReleaseCmd(ctx *appcontext.AppContext) *cobra.Command {
releaseCmd := &cobra.Command{
Use: "release <REPOSITORY_PATH_OR_URL>",
Short: "Version a Git repository according the the given configuration",
Expand All @@ -32,97 +33,82 @@ func NewReleaseCmd(ctx *AppContext) *cobra.Command {
origin *remote.Remote
)

if ctx.RemoteModeFlag {
origin = remote.New(ctx.RemoteNameFlag, ctx.AccessTokenFlag)
repository, err = origin.Clone(args[0])
if err != nil {
return fmt.Errorf("cloning Git repository: %w", err)
}
} else {
repository, err = git.PlainOpen(args[0])
if err != nil {
return fmt.Errorf("opening local Git repository: %w", err)
}
}

entity, err := configureGPGKey(ctx)
if err != nil {
return fmt.Errorf("configuring GPG key: %w", err)
}

rules, err := configureRules(ctx)
ctx.Rules, err = configureRules(ctx)
if err != nil {
return fmt.Errorf("loading rules configuration: %w", err)
}

branches, err := configureBranches(ctx)
ctx.Branches, err = configureBranches(ctx)
if err != nil {
return fmt.Errorf("loading branches configuration: %w", err)
}

projects, err := configureProjects(ctx)
ctx.Projects, err = configureProjects(ctx)
if err != nil {
return fmt.Errorf("loading projects configuration: %w", err)
}

origin = remote.New(ctx.RemoteNameFlag, ctx.AccessTokenFlag)

repository, err = origin.Clone(args[0])
if err != nil {
return fmt.Errorf("cloning Git repository: %w", err)
}

outputs, err := parser.New(ctx).Run(context.Background(), repository)
if err != nil {
return fmt.Errorf("computing new semver: %w", err)
}

tagger := tag.NewTagger(ctx.GitNameFlag, ctx.GitEmailFlag, tag.WithTagPrefix(ctx.TagPrefixFlag), tag.WithSignKey(entity))
semverParser := parser.New(ctx.Logger, tagger, rules, parser.WithBuildMetadata(ctx.BuildMetadataFlag), parser.WithProjects(projects))

for _, branch := range branches {
semverParser.SetBranch(branch.Name)
semverParser.SetPrerelease(branch.Prerelease)
semverParser.SetPrereleaseIdentifier(branch.Name)
for _, output := range outputs {
semver := output.Semver
release := output.NewRelease
commitHash := output.CommitHash
project := output.Project.Name

outputs, err := semverParser.Run(context.Background(), repository)
err = ci.GenerateGitHubOutput(semver, output.Branch, ci.WithNewRelease(release), ci.WithTagPrefix(ctx.TagPrefixFlag), ci.WithProject(project))
if err != nil {
return fmt.Errorf("computing new semver: %w", err)
return fmt.Errorf("generating github output: %w", err)
}

for _, output := range outputs {
semver := output.Semver
release := output.NewRelease
commitHash := output.CommitHash
project := output.Project.Name

err = ci.GenerateGitHubOutput(semver, branch.Name, ci.WithNewRelease(release), ci.WithTagPrefix(ctx.TagPrefixFlag), ci.WithProject(project))
if err != nil {
return fmt.Errorf("generating github output: %w", err)
}
logEvent := ctx.Logger.Info()
logEvent.Bool("new-release", release)
logEvent.Str("version", semver.String())
logEvent.Str("branch", output.Branch)

logEvent := ctx.Logger.Info()
logEvent.Bool("new-release", release)
logEvent.Str("version", semver.String())
logEvent.Str("branch", branch.Name)
if project != "" {
logEvent.Str("project", project)

if project != "" {
logEvent.Str("project", project)
tagger.SetProjectName(project)
}

tagger.SetProjectName(project)
switch {
case !release:
logEvent.Msg("no new release")
return nil
case release && ctx.DryRunFlag:
logEvent.Msg("dry-run enabled, next release found")
return nil
default:
logEvent.Msg("new release found")

err = tagger.TagRepository(repository, semver, commitHash)
if err != nil {
return fmt.Errorf("tagging repository: %w", err)
}

switch {
case !release:
logEvent.Msg("no new release")
return nil
case release && ctx.DryRunFlag:
logEvent.Msg("dry-run enabled, next release found")
return nil
default:
logEvent.Msg("new release found")

err = tagger.TagRepository(repository, semver, commitHash)
if err != nil {
return fmt.Errorf("tagging repository: %w", err)
}

ctx.Logger.Debug().Str("tag", tagger.Format(semver)).Msg("new tag added to repository")

if ctx.RemoteModeFlag {
err = origin.PushTag(tagger.Format(semver))
if err != nil {
return fmt.Errorf("pushing tag to remote: %w", err)
}
}
ctx.Logger.Debug().Str("tag", tagger.Format(semver)).Msg("new tag added to repository")

err = origin.PushTag(tagger.Format(semver))
if err != nil {
return fmt.Errorf("pushing tag to remote: %w", err)
}
}
}
Expand All @@ -134,7 +120,7 @@ func NewReleaseCmd(ctx *AppContext) *cobra.Command {
return releaseCmd
}

func configureRules(ctx *AppContext) (rule.Rules, error) {
func configureRules(ctx *appcontext.AppContext) (rule.Rules, error) {
flag := ctx.RulesFlag

if flag.String() == "{}" {
Expand All @@ -151,7 +137,7 @@ func configureRules(ctx *AppContext) (rule.Rules, error) {
return unmarshalledRules, nil
}

func configureBranches(ctx *AppContext) ([]branch.Branch, error) {
func configureBranches(ctx *appcontext.AppContext) ([]branch.Branch, error) {
branchesJSON := []map[string]any(ctx.BranchesFlag)

unmarshalledBranches, err := branch.Unmarshall(branchesJSON)
Expand All @@ -162,7 +148,7 @@ func configureBranches(ctx *AppContext) ([]branch.Branch, error) {
return unmarshalledBranches, nil
}

func configureProjects(ctx *AppContext) ([]monorepo.Project, error) {
func configureProjects(ctx *appcontext.AppContext) ([]monorepo.Project, error) {
flag := ctx.MonorepositoryFlag

if flag.String() == "[]" {
Expand All @@ -179,7 +165,7 @@ func configureProjects(ctx *AppContext) ([]monorepo.Project, error) {
return projects, nil
}

func configureGPGKey(ctx *AppContext) (*openpgp.Entity, error) {
func configureGPGKey(ctx *appcontext.AppContext) (*openpgp.Entity, error) {
flag := ctx.GPGKeyPathFlag

if flag == "" {
Expand Down
Loading