|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +// under the Apache License Version 2.0. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +// Copyright 2024 Datadog, Inc. |
| 5 | + |
| 6 | +package main |
| 7 | + |
| 8 | +import ( |
| 9 | + "encoding/json" |
| 10 | + "fmt" |
| 11 | + "sort" |
| 12 | + "github.com/Masterminds/semver/v3" |
| 13 | + "golang.org/x/mod/modfile" |
| 14 | + "net/http" |
| 15 | + "os" |
| 16 | + "regexp" |
| 17 | + "strings" |
| 18 | +) |
| 19 | + |
| 20 | +type Tag struct { |
| 21 | + Name string |
| 22 | +} |
| 23 | + |
| 24 | +func getLatestMajorVersion(repo string) (string, error) { |
| 25 | + // Get latest major version available for repo from github. |
| 26 | + const apiURL = "https://api.github.com/repos/%s/tags" |
| 27 | + url := fmt.Sprintf(apiURL, repo) |
| 28 | + |
| 29 | + resp, err := http.Get(url) |
| 30 | + if err != nil { |
| 31 | + return "", err |
| 32 | + } |
| 33 | + defer resp.Body.Close() |
| 34 | + |
| 35 | + if resp.StatusCode != http.StatusOK { |
| 36 | + return "", fmt.Errorf("failed to fetch tags: %s", resp.Status) |
| 37 | + } |
| 38 | + |
| 39 | + var tags []struct { |
| 40 | + Name string `json:"name"` |
| 41 | + } |
| 42 | + |
| 43 | + if err := json.NewDecoder(resp.Body).Decode(&tags); err != nil { |
| 44 | + return "", err |
| 45 | + } |
| 46 | + latestByMajor := make(map[int]*semver.Version) |
| 47 | + |
| 48 | + for _, tag := range tags { |
| 49 | + v, err := semver.NewVersion(tag.Name) |
| 50 | + if err != nil { |
| 51 | + continue // Skip invalid versions |
| 52 | + } |
| 53 | + |
| 54 | + if v.Prerelease() != "" { |
| 55 | + continue // Ignore pre-release versions |
| 56 | + } |
| 57 | + |
| 58 | + major := int(v.Major()) |
| 59 | + if current, exists := latestByMajor[major]; !exists || v.GreaterThan(current) { |
| 60 | + latestByMajor[major] = v |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + var latestMajor *semver.Version |
| 65 | + for _, v := range latestByMajor { |
| 66 | + if latestMajor == nil || v.Major() > latestMajor.Major() { |
| 67 | + latestMajor = v |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if latestMajor != nil { |
| 72 | + return fmt.Sprintf("v%d", latestMajor.Major()), nil |
| 73 | + } |
| 74 | + |
| 75 | + return "", fmt.Errorf("no valid versions found") |
| 76 | + |
| 77 | +} |
| 78 | + |
| 79 | +func main() { |
| 80 | + |
| 81 | + data, err := os.ReadFile("integration_go.mod") |
| 82 | + if err != nil { |
| 83 | + fmt.Println("Error reading integration_go.mod:", err) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + modFile, err := modfile.Parse("integration_go.mod", data, nil) |
| 88 | + if err != nil { |
| 89 | + fmt.Println("Error parsing integration_go.mod:", err) |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + latestMajor := make(map[string]*semver.Version) |
| 94 | + |
| 95 | + // Match on versions with /v{major} |
| 96 | + versionRegex := regexp.MustCompile(`^(?P<module>.+?)/v(\d+)$`) |
| 97 | + |
| 98 | + // Iterate over the required modules and update latest major version if necessary |
| 99 | + for _, req := range modFile.Require { |
| 100 | + module := req.Mod.Path |
| 101 | + |
| 102 | + if match := versionRegex.FindStringSubmatch(module); match != nil { |
| 103 | + url := match[1] // base module URL (e.g., github.com/foo) |
| 104 | + majorVersionStr := "v" + match[2] // Create semantic version string (e.g., "v2") |
| 105 | + |
| 106 | + moduleName := strings.TrimPrefix(strings.TrimSpace(url), "github.com/") |
| 107 | + |
| 108 | + // Parse the semantic version |
| 109 | + majorVersion, err := semver.NewVersion(majorVersionStr) |
| 110 | + if err != nil { |
| 111 | + fmt.Printf("Skip invalid version for module %s: %v\n", module, err) |
| 112 | + continue |
| 113 | + } |
| 114 | + |
| 115 | + if existing, ok := latestMajor[moduleName]; !ok || majorVersion.GreaterThan(existing) { |
| 116 | + latestMajor[moduleName] = majorVersion |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Output latest major version that we support. |
| 122 | + // Check if a new major version in Github is available that we don't support. |
| 123 | + // If so, output that a new latest is available. |
| 124 | + |
| 125 | + // Sort the output |
| 126 | + modules := make([]string, 0, len(latestMajor)) |
| 127 | + for module := range latestMajor { |
| 128 | + modules = append(modules, module) |
| 129 | + } |
| 130 | + sort.Strings(modules) |
| 131 | + |
| 132 | + for _, module := range modules { |
| 133 | + major := latestMajor[module] |
| 134 | + |
| 135 | + latestVersion, err := getLatestMajorVersion(module) // latest version available |
| 136 | + if err != nil { |
| 137 | + fmt.Printf("Error fetching latest version for module '%s': %v\n", module, err) |
| 138 | + continue |
| 139 | + } |
| 140 | + |
| 141 | + latestVersionParsed, err := semver.NewVersion(latestVersion) |
| 142 | + if err != nil { |
| 143 | + fmt.Printf("Error parsing latest version '%s' for module '%s': %v\n", latestVersion, module, err) |
| 144 | + continue |
| 145 | + } |
| 146 | + |
| 147 | + fmt.Printf("Latest DD major version of %s: %d\n", module, major.Major()) |
| 148 | + if major.LessThan(latestVersionParsed) { |
| 149 | + fmt.Printf("New latest major version of %s available: %d\n", module, latestVersionParsed.Major()) |
| 150 | + } |
| 151 | + |
| 152 | + } |
| 153 | +} |
0 commit comments