-
Notifications
You must be signed in to change notification settings - Fork 76
add CI validation for server dependency version #933
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chaptersix
wants to merge
6
commits into
temporalio:main
Choose a base branch
from
chaptersix:alex/validate-server-version
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+213
−1
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f409d93
feat: add CI validation for server dependency version
chaptersix 89bff51
fix: remove semver dependency, use simple regex parsing
chaptersix 96ad084
Merge branch 'main' into alex/validate-server-version
chaptersix 224cee1
feat: add CI validation for server dependency version
chaptersix 425644a
Merge branch 'main' into alex/validate-server-version
chaptersix 805bd1a
Merge branch 'main' into alex/validate-server-version
chaptersix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "os" | ||
|
|
||
| "golang.org/x/mod/modfile" | ||
| "golang.org/x/mod/module" | ||
| "golang.org/x/mod/semver" | ||
| ) | ||
|
|
||
| const ( | ||
| serverModule = "go.temporal.io/server" | ||
| releaseURL = "https://api.github.com/repos/temporalio/temporal/releases/latest" | ||
| ) | ||
|
|
||
| func main() { | ||
| if err := run(); err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| } | ||
|
|
||
| func run() error { | ||
| serverVersion, err := getServerVersion() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| fmt.Printf("Found server dependency: %s@%s\n", serverModule, serverVersion) | ||
|
|
||
| if module.IsPseudoVersion(serverVersion) { | ||
| return fmt.Errorf("server dependency must be a tagged version, not a pseudo-version: %s", serverVersion) | ||
| } | ||
| fmt.Println("✓ Version is a valid tagged version") | ||
|
|
||
| latestRelease, err := fetchLatestRelease() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| fmt.Printf("Latest GitHub release: %s\n", latestRelease) | ||
|
|
||
| if err := validateVersionConstraint(serverVersion, latestRelease); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| fmt.Println("✓ Server dependency version validation passed!") | ||
| return nil | ||
| } | ||
|
|
||
| func getServerVersion() (string, error) { | ||
| data, err := os.ReadFile("go.mod") | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to read go.mod: %w", err) | ||
| } | ||
|
|
||
| f, err := modfile.Parse("go.mod", data, nil) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to parse go.mod: %w", err) | ||
| } | ||
|
|
||
| for _, req := range f.Require { | ||
| if req.Mod.Path == serverModule { | ||
| return req.Mod.Version, nil | ||
| } | ||
| } | ||
|
|
||
| return "", fmt.Errorf("server dependency %s not found in go.mod", serverModule) | ||
| } | ||
|
|
||
| func fetchLatestRelease() (string, error) { | ||
| req, err := http.NewRequest("GET", releaseURL, nil) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| req.Header.Set("Accept", "application/vnd.github.v3+json") | ||
| if token := os.Getenv("GITHUB_TOKEN"); token != "" { | ||
| req.Header.Set("Authorization", "token "+token) | ||
| } | ||
|
|
||
| resp, err := http.DefaultClient.Do(req) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return "", fmt.Errorf("GitHub API returned %s", resp.Status) | ||
| } | ||
|
|
||
| var release struct { | ||
| TagName string `json:"tag_name"` | ||
| } | ||
| if err := json.NewDecoder(resp.Body).Decode(&release); err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| return release.TagName, nil | ||
| } | ||
|
|
||
| // validateVersionConstraint ensures serverVersion is at most one minor version ahead of latestRelease. | ||
| func validateVersionConstraint(serverVersion, latestRelease string) error { | ||
| if !semver.IsValid(serverVersion) { | ||
| return fmt.Errorf("invalid server version: %s", serverVersion) | ||
| } | ||
| if !semver.IsValid(latestRelease) { | ||
| return fmt.Errorf("invalid latest release version: %s", latestRelease) | ||
| } | ||
|
|
||
| serverMM := semver.MajorMinor(serverVersion) | ||
| latestMM := semver.MajorMinor(latestRelease) | ||
|
|
||
| var latestMajor, latestMinor int | ||
| fmt.Sscanf(latestMM, "v%d.%d", &latestMajor, &latestMinor) | ||
| maxAllowedMM := fmt.Sprintf("v%d.%d", latestMajor, latestMinor+1) | ||
|
|
||
| fmt.Printf(" Server version: %s.x\n", serverMM) | ||
| fmt.Printf(" Latest release: %s.x\n", latestMM) | ||
| fmt.Printf(" Max allowed: %s.x\n", maxAllowedMM) | ||
|
|
||
| if semver.Compare(serverMM, maxAllowedMM) > 0 { | ||
| return fmt.Errorf( | ||
| "server dependency version %s exceeds allowed range\n"+ | ||
| " Max allowed: %s.x (latest release + 1 minor)\n"+ | ||
| " Latest release: %s", | ||
| serverVersion, maxAllowedMM, latestRelease, | ||
| ) | ||
| } | ||
|
|
||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "golang.org/x/mod/module" | ||
| ) | ||
|
|
||
| func TestIsPseudoVersion(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| version string | ||
| want bool | ||
| }{ | ||
| {"v1.30.0-148.4", false}, | ||
| {"v1.29.2", false}, | ||
| {"v1.30.0-rc.1", false}, | ||
| {"v0.0.0-20240101120000-abcdef123456", true}, | ||
| {"v1.29.1-0.20240101120000-abcdef123456", true}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.version, func(t *testing.T) { | ||
| t.Parallel() | ||
| if got := module.IsPseudoVersion(tt.version); got != tt.want { | ||
| t.Errorf("IsPseudoVersion(%q) = %v, want %v", tt.version, got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestValidateVersionConstraint(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| serverVersion string | ||
| latestRelease string | ||
| wantErr bool | ||
| }{ | ||
| {"same minor", "v1.29.0-142.0", "v1.29.2", false}, | ||
| {"one minor ahead", "v1.30.0-148.4", "v1.29.2", false}, | ||
| {"two minors ahead", "v1.31.0-150.0", "v1.29.2", true}, | ||
| {"major ahead", "v2.0.0-1.0", "v1.29.2", true}, | ||
| {"exact match", "v1.29.2", "v1.29.2", false}, | ||
| {"invalid server", "invalid", "v1.29.2", true}, | ||
| {"invalid release", "v1.30.0", "invalid", true}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| err := validateVersionConstraint(tt.serverVersion, tt.latestRelease) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Errorf("validateVersionConstraint(%q, %q) error = %v, wantErr %v", | ||
| tt.serverVersion, tt.latestRelease, err, tt.wantErr) | ||
| } | ||
| }) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.