Skip to content

Commit

Permalink
internal/task: consider tagged commits as green
Browse files Browse the repository at this point in the history
When a root repository being tagged has no activity since being tagged
the previous time, its latest commit will still be the highest release
tag. The MaybeTag task will see that and skip tagging. There's no need
for AwaitGreen to take its time, so add the same check to it too.

Change-Id: I3cd6b131084beaff592af86ca7de53e4e5ecff25
Reviewed-on: https://go-review.googlesource.com/c/build/+/524765
Reviewed-by: Heschi Kreinick <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Auto-Submit: Dmitri Shuralyov <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
  • Loading branch information
dmitshur authored and gopherbot committed Sep 1, 2023
1 parent a66d631 commit 066ec55
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions internal/task/tagx.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (x *TagXReposTasks) readRepo(ctx *wf.TaskContext, project string) (*TagRepo
return nil, nil
}

currentTag, err := x.latestReleaseTag(ctx, project)
currentTag, _, err := x.latestReleaseTag(ctx, project, "")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -558,6 +558,15 @@ func (x *TagXReposTasks) AwaitGoMod(ctx *wf.TaskContext, changeID, repo, branch
}

func (x *TagXReposTasks) AwaitGreen(ctx *wf.TaskContext, repo TagRepo, commit string) (string, error) {
// Check if commit is already the latest tagged version.
// If so, it's deemed green and there's no need to wait.
if _, highestReleaseTagIsCommit, err := x.latestReleaseTag(ctx, repo.Name, commit); err != nil {
return "", err
} else if highestReleaseTagIsCommit {
return commit, nil
}

// Wait for a green commit.
return AwaitCondition(ctx, time.Minute, func() (string, bool, error) {
return x.findGreen(ctx, repo, commit, false)
})
Expand Down Expand Up @@ -710,46 +719,49 @@ func (x *TagXReposTasks) getBuildStatus(modPath string) (*types.BuildStatus, err
// MaybeTag tags repo at commit with the next version, unless commit is already
// the latest tagged version. repo is returned with NewerVersion populated.
func (x *TagXReposTasks) MaybeTag(ctx *wf.TaskContext, repo TagRepo, commit string) (TagRepo, error) {
highestRelease, err := x.latestReleaseTag(ctx, repo.Name)
// Check if commit is already the latest tagged version.
highestRelease, highestReleaseTagIsCommit, err := x.latestReleaseTag(ctx, repo.Name, commit)
if err != nil {
return TagRepo{}, err
}

if highestRelease == "" {
} else if highestRelease == "" {
return TagRepo{}, fmt.Errorf("no semver tags found in %v", repo.Name)
}
tagInfo, err := x.Gerrit.GetTag(ctx, repo.Name, highestRelease)
if err != nil {
return TagRepo{}, fmt.Errorf("reading project %v tag %v: %v", repo.Name, highestRelease, err)
}
if tagInfo.Revision == commit {
if highestReleaseTagIsCommit {
repo.NewerVersion = highestRelease
return repo, nil
}

// Tag commit.
repo.NewerVersion, err = nextMinor(highestRelease)
if err != nil {
return TagRepo{}, fmt.Errorf("couldn't pick next version for %v: %v", repo.Name, err)
}

ctx.Printf("Tagging %v at %v as %v", repo.Name, commit, repo.NewerVersion)
return repo, x.Gerrit.Tag(ctx, repo.Name, repo.NewerVersion, commit)
}

// latestReleaseTag fetches tags for repo and returns the latest release tag,
// or the empty string if there are no release tags.
func (x *TagXReposTasks) latestReleaseTag(ctx context.Context, repo string) (string, error) {
// or the empty string if there are no release tags. It also reports whether
// commit, if provided, matches the latest release tag's revision.
func (x *TagXReposTasks) latestReleaseTag(ctx context.Context, repo, commit string) (highestRelease string, isCommit bool, _ error) {
tags, err := x.Gerrit.ListTags(ctx, repo)
if err != nil {
return "", err
return "", false, fmt.Errorf("listing project %q tags: %v", repo, err)
}
highestRelease := ""
for _, tag := range tags {
if semver.IsValid(tag) && semver.Prerelease(tag) == "" &&
(highestRelease == "" || semver.Compare(highestRelease, tag) < 0) {
highestRelease = tag
}
}
return highestRelease, nil
if commit != "" && highestRelease != "" {
tagInfo, err := x.Gerrit.GetTag(ctx, repo, highestRelease)
if err != nil {
return "", false, fmt.Errorf("reading project %q tag %q: %v", repo, highestRelease, err)
}
isCommit = tagInfo.Revision == commit
}
return highestRelease, isCommit, nil
}

var majorMinorRestRe = regexp.MustCompile(`^v(\d+)\.(\d+)\..*$`)
Expand Down

0 comments on commit 066ec55

Please sign in to comment.