Skip to content

Commit

Permalink
Fix tagging
Browse files Browse the repository at this point in the history
  • Loading branch information
LandonTClipp committed Jan 2, 2025
1 parent bb6b25f commit cd34e8b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/tag-and-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ jobs:
needs: test
outputs:
tag_result: ${{ steps.tag.outputs.tag_result }}
requested_version: ${{ steps.tag.outputs.requested_version }}
previous_version: ${{ steps.tag.outputs.previous_version }}
steps:
- run: sudo apt update && sudo apt install -y git && git --version
- uses: actions/checkout@v2
Expand All @@ -37,7 +39,12 @@ jobs:
id: tag
run: |
set +e
task -x tag
task -x tag 1>/tmp/versions.txt
printf "versions: $(cat /tmp/versions.txt)\n"
echo "requested_version=$(cut -d',' -f 1 /tmp/versions.txt)" >> $GITHUB_OUTPUT
echo "previous_version=$(cut -d',' -f 2 /tmp/versions.txt)" >> $GITHUB_OUTPUT
tag_result="$?"
echo "tag_result=$tag_result" >> $GITHUB_OUTPUT
# The range between 8 and 63 inclusive is reserved for custom
Expand Down Expand Up @@ -86,4 +93,6 @@ jobs:
version: "<2"
env:
GITHUB_TOKEN: ${{ secrets.GORELEASER_GITHUB_TOKEN }}
HOMEBREW_TAP_TOKEN: ${{ secrets.GORELEASER_HOMEBREW_TAP_TOKEN }}
HOMEBREW_TAP_TOKEN: ${{ secrets.GORELEASER_HOMEBREW_TAP_TOKEN }}
GORELEASER_CURRENT_TAG: ${{ needs.tag.outputs.requested_version }}
GORELEASER_PREVIOUS_TAG: ${{ needs.tag.outputs.previous_version }}
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,4 @@ tasks:
tag.push:
desc: Push tags to origin
cmds:
- git push origin --tags
- git push origin --tags --force
6 changes: 3 additions & 3 deletions tools/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewRootCmd() *cobra.Command {
}

logger = zerolog.New(zerolog.ConsoleWriter{
Out: os.Stdout,
Out: os.Stderr,
}).Hook(timestampHook{})

subCommands := []func(v *viper.Viper) (*cobra.Command, error){
Expand All @@ -64,8 +64,8 @@ func printStack(err error) {
}
newErr, ok := err.(*errors.Error)
if ok {
fmt.Printf("%v\n", newErr.ErrorStack())
fmt.Fprintf(os.Stderr, "%v\n", newErr.ErrorStack())
} else {
fmt.Printf("%v\n", err)
fmt.Fprintf(os.Stderr, "%v\n", err)
}
}
46 changes: 27 additions & 19 deletions tools/cmd/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ func NewTagCmd(v *viper.Viper) (*cobra.Command, error) {
printStack(err)
os.Exit(1)
}
if err := tagger.Tag(); err != nil {
requestedVersion, previousVersion, err := tagger.Tag()
if requestedVersion != nil && previousVersion != nil {
fmt.Fprintf(os.Stdout, "v%s,v%s", requestedVersion.String(), previousVersion.String())
}
if err != nil {
if errors.Is(ErrNoNewVersion, err) {
os.Exit(EXIT_CODE_NO_NEW_VERSION)
}
Expand Down Expand Up @@ -61,6 +65,9 @@ func (t *Tagger) createTag(repo *git.Repository, version string) error {
}
majorVersion := strings.Split(version, ".")[0]
for _, v := range []string{version, majorVersion} {
if err := repo.DeleteTag(v); err != nil {
logger.Info().Err(err).Str("tag", v).Msg("failed to delete tag, but probably not an issue.")
}
_, err = repo.CreateTag(v, hash.Hash(), &git.CreateTagOptions{
Tagger: &object.Signature{
Name: "Landon Clipp",
Expand Down Expand Up @@ -113,6 +120,11 @@ func (t *Tagger) largestTagSemver(repo *git.Repository, major uint64) (*semver.V
} else {
versionString = tag.Name
}
versionParts := strings.Split(versionString, ".")
if len(versionParts) < 3 {
// This is not a full version tag, so ignore it
return nil
}

version, err := semver.NewVersion(versionString)
if err != nil {
Expand Down Expand Up @@ -147,59 +159,55 @@ type Tagger struct {
Version string `mapstructure:"version" validate:"required"`
}

func (t *Tagger) Tag() error {
func (t *Tagger) Tag() (requestedVersion *semver.Version, previousVersion *semver.Version, err error) {
repo, err := git.PlainOpen(".")
if err != nil {
return errors.New(err)
return nil, nil, errors.New(err)
}

requestedVersion, err := semver.NewVersion(t.Version)
requestedVersion, err = semver.NewVersion(t.Version)
if err != nil {
logger.Err(err).Str("requested-version", string(t.Version)).Msg("error when constructing semver from version config")
return errors.New(err)
return requestedVersion, nil, errors.New(err)
}

largestTag, err := t.largestTagSemver(repo, requestedVersion.Major())
previousVersion, err = t.largestTagSemver(repo, requestedVersion.Major())
if err != nil {
return err
}
taggedVersion, err := semver.NewVersion(largestTag.String())
if err != nil {
return errors.New(err)
return requestedVersion, previousVersion, err
}
logger := logger.With().
Stringer("tagged-version", taggedVersion).Logger()
Stringer("previous-version", previousVersion).Logger()

logger.Info().Msg("found largest semver tag")

logger = logger.With().
Stringer("requested-version", requestedVersion).
Logger()
if !requestedVersion.GreaterThan(taggedVersion) {
if !requestedVersion.GreaterThan(previousVersion) {
logger.Info().
Msg("VERSION is not greater than latest git tag, nothing to do.")
return ErrNoNewVersion
return requestedVersion, previousVersion, ErrNoNewVersion
}

worktree, err := repo.Worktree()
if err != nil {
return errors.New(err)
return requestedVersion, previousVersion, errors.New(err)
}

status, err := worktree.Status()
if err != nil {
return errors.New(err)
return requestedVersion, previousVersion, errors.New(err)
}
if !status.IsClean() {
logger.Error().Msg("git is in a dirty state, can't tag.")
fmt.Println(status.String())
return errors.New("dirty git state")
return requestedVersion, previousVersion, errors.New("dirty git state")
}

if err := t.createTag(repo, fmt.Sprintf("v%s", requestedVersion.String())); err != nil {
return err
return requestedVersion, previousVersion, err
}
logger.Info().Msg("created new tag. Push to origin still required.")

return nil
return requestedVersion, previousVersion, nil
}

0 comments on commit cd34e8b

Please sign in to comment.