Skip to content

Commit

Permalink
feat(yolo): use cache + refactor table
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Nov 7, 2018
1 parent 967ccd2 commit fb29e15
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 24 deletions.
18 changes: 8 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
module github.com/berty/staff/tools/release

require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/inconshreveable/mousetrap v1.0.0
github.com/hako/durafmt v0.0.0-20180520121703-7b7ae1e72ead
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jszwedko/go-circleci v0.2.0
github.com/labstack/echo v0.0.0-20180911044237-1abaa3049251
github.com/labstack/gommon v0.2.7
github.com/mattn/go-colorable v0.0.9
github.com/mattn/go-isatty v0.0.4
github.com/labstack/gommon v0.2.7 // indirect
github.com/mattn/go-isatty v0.0.4 // indirect
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.2
github.com/valyala/bytebufferpool v1.0.0
github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4
golang.org/x/crypto v0.0.0-20181001203147-e3636079e1a4
golang.org/x/sys v0.0.0-20180928133829-e4b3c5e90611
github.com/spf13/pflag v1.0.2 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
golang.org/x/crypto v0.0.0-20181001203147-e3636079e1a4 // indirect
golang.org/x/sys v0.0.0-20180928133829-e4b3c5e90611 // indirect
howett.net/plist v0.0.0-20180609054337-500bd5b9081b
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/hako/durafmt v0.0.0-20180520121703-7b7ae1e72ead h1:Y9WOGZY2nw5ksbEf5AIpk+vK52Tdg/VN/rHFRfEeeGQ=
github.com/hako/durafmt v0.0.0-20180520121703-7b7ae1e72ead/go.mod h1:5Scbynm8dF1XAPwIwkGPqzkM/shndPm79Jd1003hTjE=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jszwedko/go-circleci v0.2.0 h1:ytpLKtpjQn2wq+/IArlBvqfb1CgbwA2UiBGXCwWqNw4=
github.com/jszwedko/go-circleci v0.2.0/go.mod h1:z1630OiB7oGxZwE90het04Ld7rIu0AKvY9JCRnaBdoE=
Expand All @@ -14,11 +18,13 @@ github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaO
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/pflag v1.0.2 h1:Fy0orTDgHdbnzHcsOgfCN4LtHf0ec3wwtiwJqwvf3Gc=
github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/testify v1.2.1 h1:52QO5WkIUcHGIR7EnGagH88x1bUzqGXTC5/1bDTUQ7U=
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/valyala/bytebufferpool v0.0.0-20160817181652-e746df99fe4a/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
Expand All @@ -29,6 +35,7 @@ golang.org/x/crypto v0.0.0-20180312195533-182114d58262/go.mod h1:6SG95UA2DQfeDnf
golang.org/x/crypto v0.0.0-20181001203147-e3636079e1a4 h1:Vk3wNqEZwyGyei9yq5ekj7frek2u7HUfffJ1/opblzc=
golang.org/x/crypto v0.0.0-20181001203147-e3636079e1a4/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/sys v0.0.0-20180312081825-c28acc882ebc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180928133829-e4b3c5e90611 h1:O33LKL7WyJgjN9CvxfTIomjIClbd/Kq86/iipowHQU0=
golang.org/x/sys v0.0.0-20180928133829-e4b3c5e90611/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
howett.net/plist v0.0.0-20180609054337-500bd5b9081b h1:r4LwkXZhdblHVSgAvfRjsFpQBorl6S9pAH+AOHVs+28=
howett.net/plist v0.0.0-20180609054337-500bd5b9081b/go.mod h1:jInWmjR7JRkkon4jlLXDZGVEeY/wo3kOOJEWYhNE+9Y=
44 changes: 30 additions & 14 deletions server/handler.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package server

import (
"encoding/json"
"fmt"
"net/http"
"regexp"
"strconv"
"strings"
"time"

"github.com/hako/durafmt"
circleci "github.com/jszwedko/go-circleci"
"github.com/labstack/echo"
)
Expand Down Expand Up @@ -104,43 +104,59 @@ func (s *Server) GetIPA(c echo.Context) error {
}

func (s *Server) ListReleaseIOS(c echo.Context) error {
builds, err := s.client.Builds("", JOB_IOS, 100, 0)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
html := `<table style="width:100%;font-size:150%;text-align:left;"><thead>`
html += `<th>branch</th>`
html += `<th>build</th>`
html += `<th>user</th>`
html += `<th>status</th>`
//html += `<th>status</th>`
html += `<th>download</th>`
html += `<th>date</th>`
html += `<th>duration</th>`
html += `<th>diff</th>`
html += `</thead><tbody>`
for _, build := range builds {
out, _ := json.Marshal(build)
fmt.Println(string(out))
oncePerBranch := map[string]bool{}
for _, build := range s.cache.builds {
if build.BuildParameters["CIRCLE_JOB"] != "client.rn.ios" {
continue
}

token := s.getHash(build.Branch)
if _, found := oncePerBranch[build.Branch]; found && build.Branch != "master" {
continue
}

//out, _ := json.Marshal(build)
//fmt.Println(string(out))

oncePerBranch[build.Branch] = true

status := `<span style="color:green">success</span>`
/*status := `<span style="color:green">success</span>`
if build.Status != "success" {
status = fmt.Sprintf(`<span color="red">%s</span>`, build.Status)
}
}*/

branchLink := fmt.Sprintf("https://github.com/berty/berty/tree/%s", build.Branch)
if strings.HasPrefix(build.Branch, "pull/") {
branchLink = fmt.Sprintf("https://github.com/berty/berty/%s", build.Branch)
}

branchColor := "#880088"
if build.Branch == "master" {
branchColor = "#008888"
}

elems := []string{
fmt.Sprintf(`<a href="%s">%s</a>`, branchLink, build.Branch),
fmt.Sprintf(`<a href="%s" style="color:%s">%s</a>`, branchLink, branchColor, build.Branch),
fmt.Sprintf(`<a href="%s">%d</a>`, build.BuildURL, build.BuildNum),
build.User.Login,
status,
//status,

// FIXME: create a link /itms/release/TOKEN/ID instead of /itms/release/TOKEN/BRANCH (this way we can handle multiple artifacts per branch)
fmt.Sprintf(`<a href="itms-services://?action=download-manifest&url=https://%s/itms/release/%s/%[3]s">download</a> `, s.hostname, token, build.Branch),

fmt.Sprintf("%s ago", time.Since(*build.StopTime)),
fmt.Sprintf("%s ago", durafmt.ParseShort(time.Since(*build.StopTime))),
fmt.Sprintf("%s", durafmt.ParseShort(time.Duration(*build.BuildTimeMillis)*time.Millisecond)),
fmt.Sprintf(`<a href="%s">diff</a>`, build.Compare),
}
html += fmt.Sprintf(`<tr><td>%s</td></tr>`, strings.Join(elems, "</td><td>"))
}
Expand Down
76 changes: 76 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package server

import (
"crypto/md5"
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"regexp"
"time"

"github.com/berty/staff/tools/release/pkg/circle"
circleci "github.com/jszwedko/go-circleci"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
Expand All @@ -26,12 +30,23 @@ type ServerConfig struct {
Password string
}

type Cache struct {
builds []*circleci.Build
mostRecentBuild time.Time
}

func (c Cache) String() string {
out, _ := json.Marshal(c)
return string(out)
}

type Server struct {
client *circle.Client
addr string
hostname string
salt string
e *echo.Echo
cache Cache
}

var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
Expand Down Expand Up @@ -105,5 +120,66 @@ func (s *Server) getHash(id string) string {
}

func (s *Server) Start() error {
go func() {
for {
if err := s.refreshCache(); err != nil {
log.Printf("refresh failed: %+v", err)
}
time.Sleep(5 * time.Second)
}
}()
return s.e.Start(s.addr)
}

func (s *Server) refreshCache() error {
var (
allBuilds []*circleci.Build
mostRecentBuild = s.cache.mostRecentBuild
)

if s.cache.mostRecentBuild.IsZero() { // first fill
log.Print("fetch all builds")
for page := 0; page < 10; page++ {
builds, err := s.client.Builds("", "", 100, page*100)
if err != nil {
return err
}
for _, build := range builds {
if build.StopTime.After(mostRecentBuild) {
mostRecentBuild = *build.StopTime
}
}
allBuilds = append(allBuilds, builds...)
if len(builds) < 100 {
break
}
s.cache.builds = allBuilds
}
} else { // just the difference
allBuilds = s.cache.builds
previousMostRecentBuild := mostRecentBuild
builds, err := s.client.Builds("", "", 100, 0)
if err != nil {
return err
}
hasChanged := false
for i := len(builds) - 1; i >= 0; i-- {
build := builds[i]
if build.StopTime.After(mostRecentBuild) {
mostRecentBuild = *build.StopTime
}
if build.StopTime.After(previousMostRecentBuild) {
allBuilds = append([]*circleci.Build{build}, allBuilds...)
hasChanged = true
}
}
if !hasChanged {
return nil
}
}

// FIXME: lock
s.cache.builds = allBuilds
s.cache.mostRecentBuild = mostRecentBuild
return nil
}

0 comments on commit fb29e15

Please sign in to comment.