11package util
22
33import (
4+ "context"
45 "encoding/hex"
5- "encoding/json"
66 "errors"
77 "fmt"
88 "io/ioutil"
99 "net/http"
1010 "path/filepath"
11+ "regexp"
1112 "strings"
1213
14+ "github.com/google/go-github/v31/github"
15+ "github.com/hashicorp/go-version"
1316 "github.com/renproject/darknode-cli/darknode"
1417 "github.com/renproject/darknode-cli/darknode/addr"
1518 "golang.org/x/crypto/ssh"
@@ -160,27 +163,48 @@ func ValidateTags(have, required string) bool {
160163 return true
161164}
162165
163- // LatestReleaseVersion checks the darknode release repo and return the version
166+ // LatestStableRelease checks the darknode release repo and return the version
164167// of the latest release.
165- func LatestReleaseVersion () (string , error ) {
166- url := "https://api. github.com/repos/renproject/darknode-release/releases/latest"
167- response , err := http . Get ( url )
168+ func LatestStableRelease () (string , error ) {
169+ client := github .NewClient ( nil )
170+ releases , response , err := client . Repositories . ListReleases ( context . Background (), "renproject" , "darknode-release" , nil )
168171 if err != nil {
169172 return "" , err
170173 }
171174 if response .StatusCode != http .StatusOK {
172175 return "" , fmt .Errorf ("cannot get latest darknode release from github, error code = %v" , response .StatusCode )
173176 }
174177
175- resp := struct {
176- TagName string `json:"tag_name"`
177- }{}
178- err = json .NewDecoder (response .Body ).Decode (& resp )
179- return resp .TagName , err
178+ latest , err := version .NewVersion ("0.0.0" )
179+ if err != nil {
180+ return "" , err
181+ }
182+ verReg := "^v?[0-9]+\\ .[0-9]+\\ .[0-9]+$"
183+ for _ , release := range releases {
184+ match , err := regexp .MatchString (verReg , * release .TagName )
185+ if err != nil {
186+ return "" , err
187+ }
188+ if match {
189+ ver , err := version .NewVersion (* release .TagName )
190+ if err != nil {
191+ return "" , err
192+ }
193+ if ver .GreaterThan (latest ){
194+ latest = ver
195+ }
196+ }
197+ }
198+ if latest .String () == "0.0.0" {
199+ return "" , errors .New ("cannot find any stable release" )
200+ }
201+
202+ return latest .String (), nil
180203}
181204
205+
182206func isDeployed (name string ) bool {
183207 path := NodePath (name )
184208 script := fmt .Sprintf ("cd %v && terraform output ip" , path )
185209 return SilentRun ("bash" , "-c" , script ) == nil
186- }
210+ }
0 commit comments