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

WIP: add rules-keeper #78

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
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
172 changes: 172 additions & 0 deletions experimental/rules-keeper/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package main

import (
"context"
"io"
"net/http"
"time"

"github.com/bradleyfalzon/ghinstallation/v2"
"github.com/google/go-github/v50/github"
)

type fetchFunc[T any] func(*github.ListOptions) ([]T, *github.Response, error)

type iterator[T any] struct {
fetch fetchFunc[T]
nextPage int
buf []T
}

func newIterator[T any](f fetchFunc[T]) *iterator[T] {
return &iterator[T]{
fetch: f,
nextPage: 1,
}
}

func (it *iterator[T]) Next() (res T, _ error) {
if len(it.buf) == 0 && it.nextPage > 0 {
buf, resp, err := it.fetch(&github.ListOptions{
Page: it.nextPage,
})
if err != nil {
return res, err
}
it.nextPage = resp.NextPage
it.buf = buf
}
if len(it.buf) == 0 {
return res, io.EOF
}
res, it.buf = it.buf[0], it.buf[1:]
return res, nil
}

type app struct {
cli *github.Client
}

func newApp(appID int64, privateKey string) (*app, error) {
// TODO: find a better way for handling the token source.
itr, err := ghinstallation.NewAppsTransportKeyFromFile(http.DefaultTransport, appID, privateKey)
if err != nil {
return nil, err
}
return &app{github.NewClient(&http.Client{Transport: itr})}, nil
}

func (a *app) ListInstallations(ctx context.Context) *iterator[*installation] {
return newIterator(func(opts *github.ListOptions) ([]*installation, *github.Response, error) {
ins, resp, err := a.cli.Apps.ListInstallations(ctx, opts)
if err != nil {
return nil, nil, err
}
res := make([]*installation, 0, len(ins))
for _, i := range ins {
it, err := newInstallation(i.GetAppID(), i.GetID(), privateKey)
if err != nil {
return nil, nil, err
}
res = append(res, it)
}
return res, resp, nil
})
}

type installation struct {
cli *github.Client
}

func newInstallation(appID, installID int64, privateKey string) (*installation, error) {
itr, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, appID, installID, privateKey)
if err != nil {
return nil, err
}
return &installation{github.NewClient(&http.Client{Transport: itr})}, nil
}

func (n *installation) ListRepos(ctx context.Context) *iterator[*repo] {
return newIterator(func(opts *github.ListOptions) ([]*repo, *github.Response, error) {
r, resp, err := n.cli.Apps.ListRepos(ctx, opts)
if err != nil {
return nil, nil, err
}
res := make([]*repo, 0, len(r.Repositories))
for _, r := range r.Repositories {
res = append(res, &repo{
cli: n.cli,
owner: r.GetOwner().GetLogin(),
name: r.GetName(),
})
}
return res, resp, nil
})
}

type repo struct {
cli *github.Client
owner string
name string
}

func (r *repo) ListIssues(ctx context.Context, since time.Time) *iterator[*github.Issue] {
return newIterator(func(opts *github.ListOptions) ([]*github.Issue, *github.Response, error) {
return r.cli.Issues.ListByRepo(ctx, r.owner, r.name, &github.IssueListByRepoOptions{
State: "all",
Since: since,
ListOptions: *opts,
})
})
}

// ListIssueEvents returns all issue events for the repository. The iterator
// traverses events in reverse chronological order.
func (r *repo) ListIssueEvents(ctx context.Context) *iterator[*github.IssueEvent] {
return newIterator(func(opts *github.ListOptions) ([]*github.IssueEvent, *github.Response, error) {
return r.cli.Issues.ListRepositoryEvents(ctx, r.owner, r.name, opts)
})
}

func (r *repo) GetLicense(ctx context.Context) (string, error) {
l, _, err := r.cli.Repositories.License(ctx, r.owner, r.name)
if err != nil {
return "", err
}
return l.GetLicense().GetName(), nil
}

func (r *repo) GetReadme(ctx context.Context, ref string) (string, error) {
c, _, err := r.cli.Repositories.GetReadme(ctx, r.owner, r.name, &github.RepositoryContentGetOptions{
Ref: ref,
})
if err != nil {
return "", err
}
return c.GetContent()

}

func (r *repo) ListTags(ctx context.Context) *iterator[*github.RepositoryTag] {
return newIterator(func(opts *github.ListOptions) ([]*github.RepositoryTag, *github.Response, error) {
return r.cli.Repositories.ListTags(ctx, r.owner, r.name, opts)
})
}

func (r *repo) GetParticipations(ctx context.Context) (*github.RepositoryParticipation, error) {
pns, _, err := r.cli.Repositories.ListParticipation(ctx, r.owner, r.name)
return pns, err
}

// Note: it will return 404 for forked repos.
func (r *repo) GetCommunityHealthMetrics(ctx context.Context) (*github.CommunityHealthMetrics, error) {
chm, _, err := r.cli.Repositories.GetCommunityHealthMetrics(ctx, r.owner, r.name)
return chm, err
}

// ListReleases returns the releases for a repo.
func (r *repo) ListReleases(ctx context.Context) *iterator[*github.RepositoryRelease] {
return newIterator(func(opts *github.ListOptions) ([]*github.RepositoryRelease, *github.Response, error) {
return r.cli.Repositories.ListReleases(ctx, r.owner, r.name, opts)
})
}
37 changes: 37 additions & 0 deletions experimental/rules-keeper/github_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"fmt"
"net/http"
"time"

"github.com/ernesto-jimenez/httplogger"
"github.com/golang/glog"
)

func newLoggedHTTPClient() *http.Client {
return &http.Client{
Transport: httplogger.NewLoggedTransport(http.DefaultTransport, httpLogger{}),
}
}

type httpLogger struct{}

const logDepth = 2

func (httpLogger) LogRequest(req *http.Request) {
glog.InfoDepth(logDepth, fmt.Sprintf("Request %s %s", req.Method, req.URL))
}

func (httpLogger) LogResponse(req *http.Request, res *http.Response, err error, duration time.Duration) {
if err != nil {
glog.ErrorDepth(logDepth, err)
return
}
glog.InfoDepth(logDepth, fmt.Sprintf("Response method=%s status=%d durationMs=%d %s",
req.Method,
res.StatusCode,
duration.Milliseconds(),
req.URL,
))
}
23 changes: 23 additions & 0 deletions experimental/rules-keeper/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module github.com/bazel-contrib/SIG-rules-authors/experimental/rules-keeper

go 1.19

require (
github.com/bradleyfalzon/ghinstallation/v2 v2.1.0
github.com/ernesto-jimenez/httplogger v0.0.0-20220128121225-117514c3f345
github.com/gocarina/gocsv v0.0.0-20230226133904-70c27cb2918a
github.com/golang/glog v1.0.0
github.com/google/go-github/v50 v50.0.0
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be
google.golang.org/protobuf v1.28.1
)

require (
github.com/golang-jwt/jwt/v4 v4.4.1 // indirect
github.com/golang/protobuf v1.5.0 // indirect
github.com/google/go-github/v45 v45.2.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
google.golang.org/appengine v1.6.7 // indirect
)
46 changes: 46 additions & 0 deletions experimental/rules-keeper/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
github.com/bradleyfalzon/ghinstallation/v2 v2.1.0 h1:5+NghM1Zred9Z078QEZtm28G/kfDfZN/92gkDlLwGVA=
github.com/bradleyfalzon/ghinstallation/v2 v2.1.0/go.mod h1:Xg3xPRN5Mcq6GDqeUVhFbjEWMb4JHCyWEeeBGEYQoTU=
github.com/ernesto-jimenez/httplogger v0.0.0-20220128121225-117514c3f345 h1:AZLrCR38RDhsyCQakz1UxCx72As18Ai5mObrKvT8DK8=
github.com/ernesto-jimenez/httplogger v0.0.0-20220128121225-117514c3f345/go.mod h1:pw+gaKQ52Cl/SrERU62yQAiWauPpLgKpuR1hkxwL4tM=
github.com/gocarina/gocsv v0.0.0-20230226133904-70c27cb2918a h1:/5o1ejt5M0fNAN2lU1NBLtPzUSZru689EWJq01ptr+E=
github.com/gocarina/gocsv v0.0.0-20230226133904-70c27cb2918a/go.mod h1:5YoVOkjYAQumqlV356Hj3xeYh4BdZuLE0/nRkf2NKkI=
github.com/golang-jwt/jwt/v4 v4.4.1 h1:pC5DB52sCeK48Wlb9oPcdhnjkz1TKt1D/P7WKJ0kUcQ=
github.com/golang-jwt/jwt/v4 v4.4.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang/glog v1.0.0 h1:nfP3RFugxnNRyKgeWd4oI1nYvXpxrx8ck8ZrcizshdQ=
github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-github/v45 v45.2.0 h1:5oRLszbrkvxDDqBCNj2hjDZMKmvexaZ1xw/FCD+K3FI=
github.com/google/go-github/v45 v45.2.0/go.mod h1:FObaZJEDSTa/WGCzZ2Z3eoCDXWJKMenWWTrd8jrta28=
github.com/google/go-github/v50 v50.0.0 h1:gdO1AeuSZZK4iYWwVbjni7zg8PIQhp7QfmPunr016Jk=
github.com/google/go-github/v50 v50.0.0/go.mod h1:Ev4Tre8QoKiolvbpOSG3FIi4Mlon3S2Nt9W5JYqKiwA=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ=
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
Loading