|
| 1 | +// Copyright 2020 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package task |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "slices" |
| 10 | + "strings" |
| 11 | + |
| 12 | + wf "golang.org/x/build/internal/workflow" |
| 13 | + "golang.org/x/mod/semver" |
| 14 | +) |
| 15 | + |
| 16 | +// ReleaseGoplsTasks implements a new workflow definition include all the tasks |
| 17 | +// to release a gopls. |
| 18 | +type ReleaseGoplsTasks struct { |
| 19 | + Gerrit GerritClient |
| 20 | +} |
| 21 | + |
| 22 | +// NewDefinition create a new workflow definition for releasing gopls. |
| 23 | +func (r *ReleaseGoplsTasks) NewDefinition() *wf.Definition { |
| 24 | + wd := wf.New() |
| 25 | + |
| 26 | + // TODO(hxjiang): provide potential release versions in the relui where the |
| 27 | + // coordinator can choose which version to release instead of manual input. |
| 28 | + version := wf.Param(wd, wf.ParamDef[string]{Name: "version"}) |
| 29 | + isValid := wf.Task1(wd, "validating input version", r.isValidVersion, version) |
| 30 | + wf.Output(wd, "valid", isValid) |
| 31 | + return wd |
| 32 | +} |
| 33 | + |
| 34 | +func (r *ReleaseGoplsTasks) isValidVersion(ctx *wf.TaskContext, ver string) (bool, error) { |
| 35 | + if !semver.IsValid(ver) { |
| 36 | + return false, nil |
| 37 | + } |
| 38 | + |
| 39 | + versions, err := r.possibleGoplsVersions(ctx) |
| 40 | + if err != nil { |
| 41 | + return false, fmt.Errorf("failed to get latest Gopls version tags from x/tool: %w", err) |
| 42 | + } |
| 43 | + |
| 44 | + return slices.Contains(versions, ver), nil |
| 45 | +} |
| 46 | + |
| 47 | +// semversion is a parsed semantic version. |
| 48 | +type semversion struct { |
| 49 | + major, minor, patch int |
| 50 | + pre string |
| 51 | +} |
| 52 | + |
| 53 | +// parseSemver attempts to parse semver components out of the provided semver |
| 54 | +// v. If v is not valid semver in canonical form, parseSemver returns false. |
| 55 | +func parseSemver(v string) (_ semversion, ok bool) { |
| 56 | + var parsed semversion |
| 57 | + v, parsed.pre, _ = strings.Cut(v, "-") |
| 58 | + if _, err := fmt.Sscanf(v, "v%d.%d.%d", &parsed.major, &parsed.minor, &parsed.patch); err == nil { |
| 59 | + ok = true |
| 60 | + } |
| 61 | + return parsed, ok |
| 62 | +} |
| 63 | + |
| 64 | +// possibleGoplsVersions identifies suitable versions for the upcoming release |
| 65 | +// based on the current tags in the repo. |
| 66 | +func (r *ReleaseGoplsTasks) possibleGoplsVersions(ctx *wf.TaskContext) ([]string, error) { |
| 67 | + tags, err := r.Gerrit.ListTags(ctx, "tools") |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + |
| 72 | + var semVersions []semversion |
| 73 | + majorMinorPatch := map[int]map[int]map[int]bool{} |
| 74 | + for _, tag := range tags { |
| 75 | + v, ok := strings.CutPrefix(tag, "gopls/") |
| 76 | + if !ok { |
| 77 | + continue |
| 78 | + } |
| 79 | + |
| 80 | + if !semver.IsValid(v) { |
| 81 | + continue |
| 82 | + } |
| 83 | + |
| 84 | + // Skip for pre-release versions. |
| 85 | + if semver.Prerelease(v) != "" { |
| 86 | + continue |
| 87 | + } |
| 88 | + |
| 89 | + semv, ok := parseSemver(v) |
| 90 | + semVersions = append(semVersions, semv) |
| 91 | + |
| 92 | + if majorMinorPatch[semv.major] == nil { |
| 93 | + majorMinorPatch[semv.major] = map[int]map[int]bool{} |
| 94 | + } |
| 95 | + if majorMinorPatch[semv.major][semv.minor] == nil { |
| 96 | + majorMinorPatch[semv.major][semv.minor] = map[int]bool{} |
| 97 | + } |
| 98 | + majorMinorPatch[semv.major][semv.minor][semv.patch] = true |
| 99 | + } |
| 100 | + |
| 101 | + var possible []string |
| 102 | + seen := map[string]bool{} |
| 103 | + for _, v := range semVersions { |
| 104 | + nextMajor := fmt.Sprintf("v%d.%d.%d", v.major+1, 0, 0) |
| 105 | + if _, ok := majorMinorPatch[v.major+1]; !ok && !seen[nextMajor] { |
| 106 | + seen[nextMajor] = true |
| 107 | + possible = append(possible, nextMajor) |
| 108 | + } |
| 109 | + |
| 110 | + nextMinor := fmt.Sprintf("v%d.%d.%d", v.major, v.minor+1, 0) |
| 111 | + if _, ok := majorMinorPatch[v.major][v.minor+1]; !ok && !seen[nextMinor] { |
| 112 | + seen[nextMinor] = true |
| 113 | + possible = append(possible, nextMinor) |
| 114 | + } |
| 115 | + |
| 116 | + nextPatch := fmt.Sprintf("v%d.%d.%d", v.major, v.minor, v.patch+1) |
| 117 | + if _, ok := majorMinorPatch[v.major][v.minor][v.patch+1]; !ok && !seen[nextPatch] { |
| 118 | + seen[nextPatch] = true |
| 119 | + possible = append(possible, nextPatch) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + semver.Sort(possible) |
| 124 | + return possible, nil |
| 125 | +} |
0 commit comments