Skip to content

Commit

Permalink
perf!: various refactor to maximize performances
Browse files Browse the repository at this point in the history
This commits adds the following:
- repository, local and remote, are now cloned (faster for local
  repository instead of using `PlainOpen`)
- commits are now parsed one by one and not in bulk (around 1.5% faster)
- branch, monorepo and rules flags types are updated to "JSON string"
- AppContext has been moved to its own internal package
- parser now only uses AppContext for configuration

BREAKING CHANGE
  • Loading branch information
s0ders committed Oct 27, 2024
1 parent d3cab1c commit 911417c
Show file tree
Hide file tree
Showing 39 changed files with 551 additions and 500 deletions.
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
2 changes: 1 addition & 1 deletion .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
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
142 changes: 63 additions & 79 deletions cmd/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@ import (
"bytes"
"context"
"fmt"
"os"

"github.com/ProtonMail/go-crypto/openpgp"
"github.com/go-git/go-git/v5"
"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"
"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"
"os"
)

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 +31,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 +118,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 +135,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 +146,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 +163,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

0 comments on commit 911417c

Please sign in to comment.