Skip to content
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

R4R: added slack notification to runsim #4547

Merged
merged 21 commits into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Start working on gh integration
  • Loading branch information
alessio committed Jun 14, 2019
commit cdf06c6dc13bb7936f181df31bc31c439ae18ab9
27 changes: 21 additions & 6 deletions contrib/runsim/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import (
)

const (
SlackConfigSep = ","
GithubConfigSep = ","
SlackConfigSep = ","
)

var (
Expand Down Expand Up @@ -49,13 +50,18 @@ var (
period string
testname string
genesis string
slackToken string
slackChannel string
slackThread string
seedsStrRepr string
mircea-c marked this conversation as resolved.
Show resolved Hide resolved
exitOnFail bool
githubConfig string
slackConfig string

// integration with Slack and Github
githubToken string
githubPr string
slackToken string
slackChannel string
slackThread string

// logs temporary directory
tempdir string
)
Expand All @@ -71,11 +77,12 @@ func init() {
flag.StringVar(&genesis, "g", "", "Genesis file")
flag.StringVar(&seedOverrideList, "seeds", "", "run the supplied comma-separated list of seeds instead of defaults")
flag.BoolVar(&exitOnFail, "e", false, "Exit on fail during multi-sim, print error")
flag.StringVar(&githubConfig, "github", "", "Report results to Github's PR")
flag.StringVar(&slackConfig, "slack", "", "Report results to slack channel")

flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(),
`Usage: %s [-j maxprocs] [-seeds comma-separated-seed-list] [-g genesis.json] [-e] [-slack token,channel,thread] [package] [blocks] [period] [testname]
`Usage: %s [-j maxprocs] [-seeds comma-separated-seed-list] [-g genesis.json] [-e] [-github token,pr-url] [-slack token,channel,thread] [package] [blocks] [period] [testname]
Run simulations in parallel

`, filepath.Base(os.Args[0]))
Expand All @@ -91,10 +98,18 @@ func main() {
log.Fatal("wrong number of arguments")
}

if githubConfig != "" {
opts := strings.Split(githubConfig, GithubConfigSep)
if len(opts) != 2 {
log.Fatal("incorrect github config string format")
}
githubToken, githubPr = opts[0], opts[1]
}

if slackConfig != "" {
opts := strings.Split(slackConfig, SlackConfigSep)
if len(opts) != 3 {
log.Fatal("please don't fuck with slack config format")
log.Fatal("incorrect slack config string format")
}
slackToken, slackChannel, slackThread = opts[0], opts[1], opts[2]
}
Expand Down
159 changes: 158 additions & 1 deletion contrib/runsim/notification.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,46 @@
package main

import (
"github.com/nlopes/slack"
"bytes"
"context"
"encoding/json"
"log"
"net/http"
"time"

"github.com/aws/aws-lambda-go/events"
"github.com/bradleyfalzon/ghinstallation"
"github.com/google/go-github/v26/github"
"github.com/nlopes/slack"
)

type GithubPayload struct {
mircea-c marked this conversation as resolved.
Show resolved Hide resolved
Issue struct {
Number int `json:"number"`
Pull struct {
Url string `json:"url,omitempty"`
} `json:"pull_request,omitempty"`
} `json:"issue"`

Comment struct {
Body string `json:"body"`
} `json:"comment"`

Repository struct {
Name string `json:"name"`
Owner struct {
Login string `json:"login"`
} `json:"owner"`
} `json:"repository"`
}

type PullRequestDetails struct {
mircea-c marked this conversation as resolved.
Show resolved Hide resolved
Head struct {
Ref string `json:"ref"`
Sha string `json:"sha"`
} `json:"head"`
}

func slackMessage(token string, channel string, threadTS *string, message string) {
client := slack.New(token)
if threadTS != nil {
Expand All @@ -20,3 +56,124 @@ func slackMessage(token string, channel string, threadTS *string, message string
}

}

func createCheckRun(client *github.Client, payload GithubPayload, pr PullRequestDetails) error {
mircea-c marked this conversation as resolved.
Show resolved Hide resolved
var opt github.CreateCheckRunOptions
opt.Name = "Test Check"
opt.HeadBranch = pr.Head.Ref
opt.HeadSHA = pr.Head.Sha

checkRUn, resp, err := client.Checks.CreateCheckRun(context.Background(), payload.Repository.Owner.Login, payload.Repository.Name, opt)
log.Printf("%v", resp)
log.Printf("%v", checkRUn)
if err != nil {
log.Printf("ERROR: CreateCheckRun: %v", err.Error())
return err
}
return err
}

func getPrDetails(pr_url string) (*PullRequestDetails, error) {
mircea-c marked this conversation as resolved.
Show resolved Hide resolved
request, err := http.Get(pr_url)
if err != nil {
return nil, err
}

var details PullRequestDetails
if err := json.NewDecoder(request.Body).Decode(&details); err != nil {
return nil, err
}

return &details, nil
}

func updateCheckRun(client *github.Client, payload GithubPayload, pr PullRequestDetails) error {
mircea-c marked this conversation as resolved.
Show resolved Hide resolved
status := "completed"
conclusion := "success"
var opt github.UpdateCheckRunOptions
opt.Name = "Test Check"
opt.Status = &status
opt.Conclusion = &conclusion
ts := github.Timestamp{Time: time.Now()}
opt.CompletedAt = &ts

updatedCheck, resp, err := client.Checks.UpdateCheckRun(context.Background(), payload.Repository.Owner.Login, payload.Repository.Name, 136693316, opt)
log.Printf("%v", updatedCheck)
log.Printf("%v", resp)
if err != nil {
log.Printf("ERROR: UpdateCheckRun: %v", err.Error())
return err
}
return nil
}

func githubCheckHandler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
mircea-c marked this conversation as resolved.
Show resolved Hide resolved
response := events.APIGatewayProxyResponse{StatusCode: 200}
var comment GithubPayload
if err := json.NewDecoder(bytes.NewBufferString(request.Body)).Decode(&comment); err != nil {
response.StatusCode = 500
response.Body = err.Error()
return response, err
}

itr, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, 30867, 997580, "github-integration/gaia-sim.2019-05-16.private-key.pem")
if err != nil {
response.StatusCode = 500
response.Body = err.Error()
log.Printf("AuthError: %v", err)
return response, err
}
client := github.NewClient(&http.Client{Transport: itr})
message := "App comment"
issue := new(github.IssueComment)
issue.Body = &message

if comment.Comment.Body == "Start sim" && comment.Issue.Pull.Url != "" {
prDetails, err := getPrDetails(comment.Issue.Pull.Url)
if err != nil {
response.StatusCode = 500
response.Body = err.Error()
log.Printf("ERROR: getPrDetails: %v", err.Error())
return response, err
}
log.Printf("%v", prDetails)

if err := createCheckRun(client, comment, *prDetails); err != nil {
response.StatusCode = 500
response.Body = err.Error()
return response, err
}

comments, resp, err := client.Issues.CreateComment(context.Background(),
comment.Repository.Owner.Login, comment.Repository.Name, comment.Issue.Number, issue)

log.Printf("%v", resp)
log.Printf("%v", comments)
if err != nil {
log.Printf("ERROR: CreateComment: %v", err.Error())
response.StatusCode = 500
response.Body = err.Error()
return response, err
}
}

if comment.Comment.Body == "Update check" && comment.Issue.Pull.Url != "" {
prDetails, err := getPrDetails(comment.Issue.Pull.Url)
if err != nil {
response.StatusCode = 500
response.Body = err.Error()
log.Printf("ERROR: getPrDetails: %v", err.Error())
return response, err
}
log.Printf("%v", prDetails)

if err := updateCheckRun(client, comment, *prDetails); err != nil {
response.StatusCode = 500
response.Body = err.Error()
log.Printf("ERROR: getPrDetails: %v", err.Error())
return response, err
}
}

return response, nil
}
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ module github.com/cosmos/cosmos-sdk

require (
github.com/VividCortex/gohistogram v1.0.0 // indirect
github.com/aws/aws-lambda-go v1.11.1
github.com/bartekn/go-bip39 v0.0.0-20171116152956-a05967ea095d
github.com/bgentry/speakeasy v0.1.0
github.com/bradleyfalzon/ghinstallation v0.1.2
github.com/btcsuite/btcd v0.0.0-20190115013929-ed77733ec07d
github.com/cosmos/go-bip39 v0.0.0-20180618194314-52158e4697b8
github.com/cosmos/ledger-cosmos-go v0.10.3
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/fortytw2/leaktest v1.3.0 // indirect
github.com/go-logfmt/logfmt v0.4.0 // indirect
github.com/gogo/protobuf v1.1.1
github.com/golang/protobuf v1.3.0
github.com/golang/snappy v0.0.1 // indirect
github.com/google/go-github/v26 v26.0.2
github.com/gorilla/mux v1.7.0
github.com/gorilla/websocket v1.4.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
Expand Down Expand Up @@ -42,7 +46,7 @@ require (
github.com/tendermint/go-amino v0.15.0
github.com/tendermint/iavl v0.12.2
github.com/tendermint/tendermint v0.31.5
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
google.golang.org/grpc v1.19.0 // indirect
gopkg.in/yaml.v2 v2.2.2
)
Expand Down
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/aws/aws-lambda-go v1.11.1 h1:wuOnhS5aqzPOWns71FO35PtbtBKHr4MYsPVt5qXLSfI=
github.com/aws/aws-lambda-go v1.11.1/go.mod h1:Rr2SMTLeSMKgD45uep9V/NP8tnbCcySgu04cx0k/6cw=
github.com/bartekn/go-bip39 v0.0.0-20171116152956-a05967ea095d h1:1aAija9gr0Hyv4KfQcRcwlmFIrhkDmIj2dz5bkg/s/8=
github.com/bartekn/go-bip39 v0.0.0-20171116152956-a05967ea095d/go.mod h1:icNx/6QdFblhsEjZehARqbNumymUT/ydwlLojFdv7Sk=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/bgentry/speakeasy v0.1.0 h1:ByYyxL9InA1OWqxJqqp2A5pYHUrCiAL6K3J+LKSsQkY=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bradleyfalzon/ghinstallation v0.1.2 h1:9fdqVadlvEX/EUts5/aIGvx2ujKnGNIMcuCuUrM6s6Q=
github.com/bradleyfalzon/ghinstallation v0.1.2/go.mod h1:VQsLlCoNa54/CNXcc2DuCfNZrZxqQcyPeqKUugF/2h8=
github.com/btcsuite/btcd v0.0.0-20190115013929-ed77733ec07d h1:xG8Pj6Y6J760xwETNmMzmlt38QSwz0BLp1cZ09g27uw=
github.com/btcsuite/btcd v0.0.0-20190115013929-ed77733ec07d/go.mod h1:d3C0AkH6BRcvO8T0UEPu53cnw4IbV63x1bEjildYhO0=
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
Expand All @@ -34,6 +38,8 @@ github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/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/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
Expand All @@ -56,6 +62,10 @@ github.com/golang/protobuf v1.3.0 h1:kbxbvI4Un1LUWKxufD+BiE6AEExYYgkQLQmLFqA1LFk
github.com/golang/protobuf v1.3.0/go.mod h1:Qd/q+1AKNOZr9uGQzbzCmRO6sUih6GTPZv6a1/R87v0=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-github/v26 v26.0.2 h1:tiPWJNapF2n6Mxbmue/g0uDkSuWf34nzlsNp4Ym7qb8=
github.com/google/go-github/v26 v26.0.2/go.mod h1:v6/FmX9au22j4CtYxnMhJJkP+JfOQDXALk7hI+MPDNM=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf h1:+RRA9JqSOZFfKrOeqr2z77+8R2RKyh8PG66dcu1V0ck=
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI=
github.com/gorilla/mux v1.7.0 h1:tOSd0UKHQd6urX6ApfOn4XdBMY6Sh1MfxV3kmaazO+U=
Expand Down Expand Up @@ -136,6 +146,7 @@ github.com/spf13/viper v1.0.3 h1:z5LPUc2iz8VLT5Cw1UyrESG6FUUnOGecYGY08BLKSuc=
github.com/spf13/viper v1.0.3/go.mod h1:A8kyI5cUJhb8N+3pkfONlcEcZbueH6nhAm0Fq7SrnBM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
Expand All @@ -160,6 +171,8 @@ golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc h1:a3CU5tJYVj92DY2LaA1kUkrsqD5/3mLDhx2NcNqyW+0=
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -191,6 +204,7 @@ gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/urfave/cli.v1 v1.20.0/go.mod h1:vuBzUtMdQeixQj8LVd+/98pzhxNGQoyuPBlsXHOQNO0=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down