-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
script: Add a script to generate version matrix (#1104)
- Loading branch information
1 parent
26502cc
commit 1813ce9
Showing
3 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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,141 @@ | ||
// Copyright 2021 PingCAP, Inc. Licensed under Apache-2.0. | ||
|
||
// This proram can be used to generate a version relationship matrix for TiDB Dashboard and PD. | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/codeskyblue/go-sh" | ||
"github.com/olekukonko/tablewriter" | ||
"github.com/pingcap/log" | ||
"go.uber.org/zap" | ||
"golang.org/x/mod/modfile" | ||
"golang.org/x/mod/semver" | ||
) | ||
|
||
var pdGitDir string | ||
var dashboardGitDir = flag.String("dashboard", "", "TiDB Dashboard git path") | ||
var outputFormat = flag.String("format", "mdtable", "Output format, accept values: text, mdtable, mdtable-link") | ||
|
||
func mustSuccess(err error) { | ||
if err != nil { | ||
log.Fatal("Execute failed", zap.Error(err)) | ||
} | ||
} | ||
|
||
func listPDTags() []string { | ||
output, err := sh.Command("git", "tag", sh.Dir(pdGitDir)).Output() | ||
mustSuccess(err) | ||
validTags := make([]string, 0) | ||
pdTags := strings.Split(string(output), "\n") | ||
for _, pdTag := range pdTags { | ||
if !semver.IsValid(pdTag) { | ||
continue | ||
} | ||
validTags = append(validTags, pdTag) | ||
} | ||
sort.SliceStable(validTags, func(i, j int) bool { | ||
return semver.Compare(validTags[i], validTags[j]) < 0 | ||
}) | ||
return validTags | ||
} | ||
|
||
func lookupDashboardCommit(pdTag string) string { | ||
output, err := sh.Command( | ||
"git", "show", fmt.Sprintf("%s:go.mod", pdTag), | ||
sh.Dir(pdGitDir)).Output() | ||
mustSuccess(err) | ||
|
||
f, err := modfile.Parse("go.mod", output, nil) | ||
mustSuccess(err) | ||
for _, r := range f.Require { | ||
if r.Mod.Path != "github.com/pingcap-incubator/tidb-dashboard" && | ||
r.Mod.Path != "github.com/pingcap/tidb-dashboard" { | ||
continue | ||
} | ||
if !semver.IsValid(r.Mod.Version) { | ||
continue | ||
} | ||
versionSegments := strings.Split(r.Mod.Version, "-") | ||
gitHash := versionSegments[2] | ||
return gitHash | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func lookupDashboardRelease(gitCommit string) string { | ||
sess := sh.NewSession() | ||
sess.Stderr = nil | ||
output, err := sess. | ||
Command("git", "show", fmt.Sprintf("%s:release-version", gitCommit), sh.Dir(*dashboardGitDir)). | ||
Output() | ||
if err != nil { | ||
// It might be possible that the TiDB Dashboard is not using a calver. | ||
return "" | ||
} | ||
|
||
lines := strings.Split(string(output), "\n") | ||
for _, line := range lines { | ||
if len(line) > 0 && !strings.HasPrefix(line, "#") { | ||
return line | ||
} | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
if flag.NArg() < 1 { | ||
fmt.Println("Usage: go run pd_version_matrix.go <pd_git_path>") | ||
os.Exit(1) | ||
return | ||
} | ||
|
||
pdGitDir = flag.Arg(0) | ||
output := make([][]string, 0) | ||
|
||
pdTags := listPDTags() | ||
for _, pdTag := range pdTags { | ||
if semver.Compare(pdTag, "v4.0.0") < 0 { | ||
continue | ||
} | ||
dashboardCommit := lookupDashboardCommit(pdTag) | ||
if dashboardCommit == "" { | ||
continue | ||
} | ||
dashboardRelease := lookupDashboardRelease(dashboardCommit) | ||
if dashboardRelease == "" { | ||
continue | ||
} | ||
|
||
output = append(output, []string{pdTag, dashboardRelease}) | ||
} | ||
|
||
switch *outputFormat { | ||
case "mdtable", "mdtable-link": | ||
if *outputFormat == "mdtable-link" { | ||
for _, row := range output { | ||
row[1] = fmt.Sprintf("[%s](https://github.com/pingcap/tidb-dashboard/releases/tag/v%s)", row[1], row[1]) | ||
} | ||
} | ||
table := tablewriter.NewWriter(os.Stdout) | ||
table.SetHeader([]string{"PD Version", "TiDB Dashboard Version"}) | ||
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false}) | ||
table.SetCenterSeparator("|") | ||
table.AppendBulk(output) | ||
table.Render() | ||
default: | ||
for _, row := range output { | ||
fmt.Printf("%s: %s\n", row[0], row[1]) | ||
} | ||
} | ||
} |