-
Notifications
You must be signed in to change notification settings - Fork 7
List endpoints #32
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
List endpoints #32
Changes from all commits
51ab089
a722c3e
190f44a
dbc9caf
1cb262b
46f4e26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,24 @@ | ||
| package subcmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "fmt" | ||
| "net/http" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/src-d/ghsync/models/migrations" | ||
| "github.com/src-d/ghsync/utils" | ||
| "gopkg.in/src-d/go-log.v1" | ||
|
|
||
| "github.com/golang-migrate/migrate/v4" | ||
| _ "github.com/golang-migrate/migrate/v4/database/postgres" | ||
| bindata "github.com/golang-migrate/migrate/v4/source/go_bindata" | ||
| "github.com/google/go-github/github" | ||
| "github.com/gregjones/httpcache" | ||
| "github.com/gregjones/httpcache/diskcache" | ||
| "golang.org/x/oauth2" | ||
| ) | ||
|
|
||
| const maxVersion uint = 1560510971 | ||
|
|
@@ -25,6 +36,45 @@ func (o PostgresOpt) URL() string { | |
| o.User, o.Password, o.Host, o.Port, o.DB) | ||
| } | ||
|
|
||
| func (o PostgresOpt) initDB() (db *sql.DB, err error) { | ||
| db, err = sql.Open("postgres", o.URL()) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| defer func() { | ||
| if err != nil { | ||
| db.Close() | ||
| db = nil | ||
| } | ||
| }() | ||
|
|
||
| if err = db.Ping(); err != nil { | ||
| return db, err | ||
| } | ||
|
|
||
| m, err := newMigrate(o.URL()) | ||
| if err != nil { | ||
| return db, err | ||
| } | ||
|
|
||
| dbVersion, _, err := m.Version() | ||
|
|
||
| if err != nil && err != migrate.ErrNilVersion { | ||
| return db, err | ||
| } | ||
|
|
||
| if dbVersion != maxVersion { | ||
| return db, fmt.Errorf( | ||
| "database version mismatch. Current version is %v, but this binary needs version %v. "+ | ||
| "Use the 'migrate' subcommand to upgrade your database", dbVersion, maxVersion) | ||
| } | ||
|
|
||
| log.With(log.Fields{"db-version": dbVersion}).Debugf("the DB version is up to date") | ||
| log.Infof("connection with the DB established") | ||
| return db, nil | ||
| } | ||
|
|
||
| func newMigrate(url string) (*migrate.Migrate, error) { | ||
| // wrap assets into Resource | ||
| s := bindata.Resource(migrations.AssetNames(), | ||
|
|
@@ -38,3 +88,32 @@ func newMigrate(url string) (*migrate.Migrate, error) { | |
| } | ||
| return migrate.NewWithSourceInstance("go-bindata", d, url) | ||
| } | ||
|
|
||
| func newClient(token string) (*github.Client, error) { | ||
| http := oauth2.NewClient(context.TODO(), oauth2.StaticTokenSource( | ||
| &oauth2.Token{AccessToken: token}, | ||
| )) | ||
|
|
||
| dirPath := filepath.Join(os.TempDir(), "ghsync") | ||
| err := os.MkdirAll(dirPath, os.ModePerm) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error while creating directory %s: %v", dirPath, err) | ||
| } | ||
|
|
||
| t := httpcache.NewTransport(diskcache.New(dirPath)) | ||
| t.Transport = &RemoveHeaderTransport{utils.NewRateLimitTransport(http.Transport)} | ||
| http.Transport = t | ||
|
|
||
| return github.NewClient(http), nil | ||
| } | ||
|
|
||
| type RemoveHeaderTransport struct { | ||
| T http.RoundTripper | ||
| } | ||
|
|
||
| func (t *RemoveHeaderTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please explain the reason for removing these headers? I was also reading the doc, and it says:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know the reason. I didn't pay any attention to this, I just blindly copy-pasted from
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. most probably if we don't remove these headers requests won't hit the cache. Our cache lib checks for headers as well. (though I didn't really check in details just saying according to my prior knowledge) |
||
| req.Header.Del("X-Ratelimit-Limit") | ||
| req.Header.Del("X-Ratelimit-Remaining") | ||
| req.Header.Del("X-Ratelimit-Reset") | ||
| return t.T.RoundTrip(req) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package subcmd | ||
|
|
||
| import ( | ||
| "github.com/src-d/ghsync/deep" | ||
|
|
||
| "gopkg.in/src-d/go-cli.v0" | ||
| "gopkg.in/src-d/go-log.v1" | ||
| "gopkg.in/src-d/go-queue.v1" | ||
| _ "gopkg.in/src-d/go-queue.v1/amqp" | ||
| _ "gopkg.in/src-d/go-queue.v1/memory" | ||
| ) | ||
|
|
||
| type DeepCommand struct { | ||
| cli.Command `name:"deep" short-description:"Deep sync of GitHub data" long-description:"Deep sync of GitHub data"` | ||
|
|
||
| Token string `long:"token" env:"GHSYNC_TOKEN" description:"GitHub personal access token" required:"true"` | ||
| Org string `long:"org" env:"GHSYNC_ORG" description:"Name of the GitHub organization" required:"true"` | ||
|
|
||
| QueueOpt struct { | ||
| Queue string `long:"queue" env:"GHSYNC_QUEUE" description:"queue name. If it's not set the organization name will be used"` | ||
| Broker string `long:"broker" env:"GHSYNC_BROKER" default:"amqp://localhost:5672" description:"broker service URI"` | ||
| } `group:"go-queue connection options"` | ||
|
|
||
| Postgres PostgresOpt `group:"PostgreSQL connection options"` | ||
| } | ||
|
|
||
| func (c *DeepCommand) Execute(args []string) error { | ||
| db, err := c.Postgres.initDB() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer db.Close() | ||
|
|
||
| client, err := newClient(c.Token) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| broker, err := queue.NewBroker(c.QueueOpt.Broker) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| qName := c.QueueOpt.Queue | ||
| if qName == "" { | ||
| qName = c.Org | ||
| } | ||
| queue, err := broker.Queue(qName) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| syncer := deep.NewSyncer(db, client, queue) | ||
|
|
||
| go func() { | ||
| err := syncer.DoOrganization(c.Org) | ||
| if err != nil { | ||
| log.Errorf(err, "syncer.DoOrganization finished with error") | ||
| } | ||
| }() | ||
|
|
||
| return syncer.Wait() | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package subcmd | ||
|
|
||
| import ( | ||
| "github.com/src-d/ghsync/shallow" | ||
|
|
||
| "gopkg.in/src-d/go-cli.v0" | ||
| ) | ||
|
|
||
| type ShallowCommand struct { | ||
| cli.Command `name:"shallow" short-description:"Shallow sync of GitHub data" long-description:"Shallow sync of GitHub data"` | ||
|
|
||
| Token string `long:"token" env:"GHSYNC_TOKEN" description:"GitHub personal access token" required:"true"` | ||
| Org string `long:"org" env:"GHSYNC_ORG" description:"Name of the GitHub organization" required:"true"` | ||
|
|
||
| Postgres PostgresOpt `group:"PostgreSQL connection options"` | ||
| } | ||
|
|
||
| func (c *ShallowCommand) Execute(args []string) error { | ||
| db, err := c.Postgres.initDB() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer db.Close() | ||
|
|
||
| client, err := newClient(c.Token) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| orgSyncer := shallow.NewOrganizationSyncer(db, client) | ||
| return orgSyncer.Sync(c.Org) | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package ghsync | ||
| package deep | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package ghsync | ||
| package deep | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package ghsync | ||
| package deep | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package ghsync | ||
| package deep | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package ghsync | ||
| package deep | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package ghsync | ||
| package deep | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package ghsync | ||
| package deep | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package ghsync | ||
| package deep | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package ghsync | ||
| package deep | ||
|
|
||
| import ( | ||
| "database/sql" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe clearer if renamed to
ShallowSyncCommand? I'd have:ShallowSyncCommandandSyncCommand, orShallowSyncCommandandDeepSyncCommand, orShallowCommandandDeepCommandas maybe theSyncis implicit in the project purpose itself.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, done in 46f4e26