Skip to content

Commit

Permalink
script: Add a script to generate version matrix (#1104)
Browse files Browse the repository at this point in the history
  • Loading branch information
breezewish authored Dec 21, 2021
1 parent 26502cc commit 1813ce9
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 1 deletion.
3 changes: 3 additions & 0 deletions scripts/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ module scripts
go 1.13

require (
github.com/codeskyblue/go-sh v0.0.0-20200712050446-30169cf553fe
github.com/olekukonko/tablewriter v0.0.5
github.com/pingcap/log v0.0.0-20210906054005-afc726e70354
github.com/pingcap/tidb-dashboard v0.0.0-00010101000000-000000000000
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 // indirect
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546
github.com/swaggo/swag v1.6.6-0.20200529100950-7c765ddd0476
github.com/vektra/mockery/v2 v2.9.4
go.uber.org/zap v1.19.0
golang.org/x/mod v0.5.1
)

replace github.com/pingcap/tidb-dashboard => ../
11 changes: 10 additions & 1 deletion scripts/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5P
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8=
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 h1:sDMmm+q/3+BukdIpxwO365v/Rbspp2Nt5XntgQRXq8Q=
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0/go.mod h1:4Zcjuz89kmFXt9morQgcfYZAYZ5n8WHjt81YYWIwtTM=
github.com/codeskyblue/go-sh v0.0.0-20200712050446-30169cf553fe h1:69JI97HlzP+PH5Mi1thcGlDoBr6PS2Oe+l3mNmAkbs4=
github.com/codeskyblue/go-sh v0.0.0-20200712050446-30169cf553fe/go.mod h1:VQx0hjo2oUeQkQUET7wRwradO6f+fN5jzXgB/zROxxE=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
Expand Down Expand Up @@ -251,6 +255,8 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.8/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
Expand All @@ -276,6 +282,8 @@ github.com/nfnt/resize v0.0.0-20160724205520-891127d8d1b5/go.mod h1:jpp1/29i3P1S
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/oleiade/reflections v1.0.1/go.mod h1:rdFxbxq4QXVZWj0F+e9jqjDkc7dbp97vkRixKo2JR60=
github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pascaldekloe/name v0.0.0-20180628100202-0fd16699aae1/go.mod h1:eD5JxqMiuNYyFNmyY9rkJ/slN8y59oEu4Ei7F8OoKWQ=
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
Expand Down Expand Up @@ -469,8 +477,9 @@ golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKG
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181005035420-146acd28ed58/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
141 changes: 141 additions & 0 deletions scripts/pd_version_matrix.go
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])
}
}
}

0 comments on commit 1813ce9

Please sign in to comment.