Skip to content

Commit

Permalink
Open PRs on known SDKs when the vectors change (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
finn-block authored Dec 21, 2023
1 parent 1d7e55a commit a875625
Show file tree
Hide file tree
Showing 9 changed files with 408 additions and 23 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build-report.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ jobs:
mv _site ../
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CICD_ROBOT_GITHUB_APP_NAME: ${{ secrets.CICD_ROBOT_GITHUB_APP_NAME }}
CICD_ROBOT_GITHUB_APP_PRIVATE_KEY: ${{ secrets.CICD_ROBOT_GITHUB_APP_PRIVATE_KEY }}
CICD_ROBOT_GITHUB_APP_ID: ${{ secrets.CICD_ROBOT_GITHUB_APP_ID }}
CICD_ROBOT_GITHUB_APP_INSTALLATION_ID: ${{ secrets.CICD_ROBOT_GITHUB_APP_INSTALLATION_ID }}
- uses: actions/upload-pages-artifact@v2
- name: deploy GitHub Pages
uses: actions/deploy-pages@v3
Expand Down
23 changes: 23 additions & 0 deletions .github/workflows/sync-vectors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: sync vectors

on:
push:
branches: [main]
paths:
- 'web5-test-vectors/**/*.json'
workflow_dispatch:

jobs:
build-report:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
- name: sync vectors
run: cd reports && go run ./cmd/sync-vectors
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CICD_ROBOT_GITHUB_APP_NAME: ${{ secrets.CICD_ROBOT_GITHUB_APP_NAME }}
CICD_ROBOT_GITHUB_APP_PRIVATE_KEY: ${{ secrets.CICD_ROBOT_GITHUB_APP_PRIVATE_KEY }}
CICD_ROBOT_GITHUB_APP_ID: ${{ secrets.CICD_ROBOT_GITHUB_APP_ID }}
CICD_ROBOT_GITHUB_APP_INSTALLATION_ID: ${{ secrets.CICD_ROBOT_GITHUB_APP_INSTALLATION_ID }}
33 changes: 33 additions & 0 deletions reports/cmd/sync-vectors/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"os"

"github.com/TBD54566975/sdk-development/reports"
"golang.org/x/exp/slog"
)

func main() {
defer reports.CleanupGitAuth()
if err := reports.ConfigureGitAuth(); err != nil {
panic(err)
}

errs := make(map[string]error)
for _, sdk := range reports.SDKs {
if err := reports.SyncSDK(sdk); err != nil {
errs[sdk.Name] = err
}
}

if err := reports.CleanupGitAuth(); err != nil {
panic(err)
}

if len(errs) > 0 {
for sdk, err := range errs {
slog.Error("error", "sdk", sdk, "error", err)
}
os.Exit(1)
}
}
56 changes: 56 additions & 0 deletions reports/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package reports

import (
"context"
"fmt"
"net/http"
"os"
"strconv"

ghinstallation "github.com/bradleyfalzon/ghinstallation/v2"
"github.com/google/go-github/v57/github"
"golang.org/x/exp/slog"
)

var (
ghAppName = os.Getenv("CICD_ROBOT_GITHUB_APP_NAME")
ghAppPrivateKey = os.Getenv("CICD_ROBOT_GITHUB_APP_PRIVATE_KEY")
ghAppIDString = os.Getenv("CICD_ROBOT_GITHUB_APP_ID")
ghAppInstallationIDString = os.Getenv("CICD_ROBOT_GITHUB_APP_INSTALLATION_ID")

gh *github.Client
ghTransport *ghinstallation.Transport

ghUserName = fmt.Sprintf("%s[bot]", ghAppName)
)

func init() {
ghAppID, err := strconv.ParseInt(ghAppIDString, 10, 32)
if err != nil {
slog.Error("invalid or unset app ID. Please set environment variable CICD_ROBOT_GITHUB_APP_ID to a valid integer")
panic(err)
}

ghInstallationID, err := strconv.ParseInt(ghAppInstallationIDString, 10, 32)
if err != nil {
slog.Error("invalid or unset installation ID. Please set environment variable CICD_ROBOT_GITHUB_APP_INSTALLATION_ID to a valid integer")
panic(err)
}

ghTransport, err = ghinstallation.New(http.DefaultTransport, ghAppID, ghInstallationID, []byte(ghAppPrivateKey))
if err != nil {
slog.Error("error initializing github auth transport.")
panic(err)
}

gh = github.NewClient(&http.Client{Transport: ghTransport})

user, _, err := gh.Users.Get(context.Background(), ghUserName)
if err != nil {
slog.Error("error getting own (app) user info")
panic(err)
}

gitConfig["user.email"] = fmt.Sprintf("%d+%s@users.noreply.github.com", user.GetID(), ghUserName)
gitConfig["user.name"] = ghUserName
}
8 changes: 7 additions & 1 deletion reports/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ module github.com/TBD54566975/sdk-development/reports
go 1.20

require (
github.com/bradleyfalzon/ghinstallation/v2 v2.8.0
github.com/google/go-github/v57 v57.0.0
github.com/joshdk/go-junit v1.0.0
golang.org/x/exp v0.0.0-20231127185646-65229373498e
)

require github.com/google/go-querystring v1.1.0 // indirect
require (
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/google/go-github/v56 v56.0.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
)
14 changes: 11 additions & 3 deletions reports/go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/bradleyfalzon/ghinstallation/v2 v2.8.0 h1:yUmoVv70H3J4UOqxqsee39+KlXxNEDfTbAp8c/qULKk=
github.com/bradleyfalzon/ghinstallation/v2 v2.8.0/go.mod h1:fmPmvCiBWhJla3zDv9ZTQSZc8AbwyRnGW1yg5ep1Pcs=
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/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-github/v56 v56.0.0 h1:TysL7dMa/r7wsQi44BjqlwaHvwlFlqkK8CtBWCX3gb4=
github.com/google/go-github/v56 v56.0.0/go.mod h1:D8cdcX98YWJvi7TLo7zM4/h8ZTx6u6fwGEkCdisopo0=
github.com/google/go-github/v57 v57.0.0 h1:L+Y3UPTY8ALM8x+TV0lg+IEBI+upibemtBD8Q9u7zHs=
github.com/google/go-github/v57 v57.0.0/go.mod h1:s0omdnye0hvK/ecLvpsGfJMiRt85PimQh4oygmLIxHw=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
Expand All @@ -11,11 +18,12 @@ github.com/joshdk/go-junit v1.0.0/go.mod h1:TiiV0PqkaNfFXjEiyjWM3XXrhVyCa1K4Zfga
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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/exp v0.0.0-20231127185646-65229373498e h1:Gvh4YaCaXNs6dKTlfgismwWZKyjVZXwOPfIyUaqU3No=
golang.org/x/exp v0.0.0-20231127185646-65229373498e/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
1 change: 1 addition & 0 deletions reports/reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type SDKMeta struct {
ArtifactName string
FeatureRegex *regexp.Regexp
VectorRegex *regexp.Regexp
VectorPath string
}

type Report struct {
Expand Down
34 changes: 15 additions & 19 deletions reports/sdks.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,51 @@ import (
"fmt"
"io"
"net/http"
"os"
"regexp"
"strings"

"github.com/google/go-github/v57/github"
"golang.org/x/exp/slog"
)

var (
ghToken = os.Getenv("GITHUB_TOKEN")
gh = github.NewClient(nil).WithAuthToken(ghToken)
SDKs = []SDKMeta{
SDKs = []SDKMeta{
{
Name: "web5-js",
Repo: "TBD54566975/web5-js",
ArtifactName: "junit-results",
FeatureRegex: regexp.MustCompile(`Web5TestVectors(\w+)`),
VectorRegex: regexp.MustCompile(`\w+ \w+ (\w+)`),
VectorPath: "test-vectors",
},
{
Name: "web5-kt",
Repo: "TBD54566975/web5-kt",
ArtifactName: "test-results",
FeatureRegex: regexp.MustCompile(`web5\.sdk\.\w+.Web5TestVectors(\w+)`),
VectorRegex: regexp.MustCompile(`(\w+)\(\)`),
VectorPath: "test-vectors",
},
}
)

func init() {
if ghToken == "" {
panic("please set environment variable GITHUB_TOKEN to a valid github token (generate one at https://github.com/settings/tokens?type=beta)")
}
}

func GetAllReports() ([]Report, error) {
ctx := context.TODO()
ctx := context.Background()

reports := []Report{}
var reports []Report
for _, sdk := range SDKs {
artifact, err := downloadArtifact(ctx, sdk)
if err != nil {
return nil, err
return nil, fmt.Errorf("error downloading artifact from %s: %v", sdk.Repo, err)
}

suites, err := readArtifactZip(artifact)
if err != nil {
return nil, err
return nil, fmt.Errorf("error parsing artifact from %s: %v", sdk.Repo, err)
}

report, err := sdk.buildReport(suites)
if err != nil {
return nil, err
return nil, fmt.Errorf("error processing data from %s: %v", sdk.Repo, err)
}

reports = append(reports, report)
Expand Down Expand Up @@ -90,16 +82,20 @@ func downloadArtifact(ctx context.Context, sdk SDKMeta) ([]byte, error) {
if err != nil {
return nil, err
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", ghToken))
bearer, err := ghTransport.Token(ctx)
if err != nil {
return nil, fmt.Errorf("error getting github token: %v", err)
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", bearer))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
return nil, fmt.Errorf("error making http request to %s: %v", artifactURL, err)
}
defer resp.Body.Close()

artifact, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
return nil, fmt.Errorf("error reading response body: %v", err)
}

slog.Info("downloaded artifact", "sdk", sdk.Repo, "size", len(artifact))
Expand Down
Loading

0 comments on commit a875625

Please sign in to comment.