Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion cmd/ghsync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ var (
var app = cli.New("ghsync", version, build, "GitHub metadata sync")

func main() {
app.AddCommand(&subcmd.SyncCommand{})
app.AddCommand(&subcmd.ShallowCommand{})

Copy link
Copy Markdown
Contributor

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:

  • ShallowSyncCommand and SyncCommand, or
  • ShallowSyncCommand and DeepSyncCommand, or
  • ShallowCommand and DeepCommand as maybe the Sync is implicit in the project purpose itself.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, done in 46f4e26

app.AddCommand(&subcmd.DeepCommand{})
app.AddCommand(&subcmd.MigrateCommand{})

app.RunMain()
Expand Down
79 changes: 79 additions & 0 deletions cmd/ghsync/subcmd/common.go
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
Expand All @@ -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(),
Expand All @@ -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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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:

   // RoundTrip should not modify the request, except for
   // consuming and closing the Request's Body.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 sync.go.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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)
}
63 changes: 63 additions & 0 deletions cmd/ghsync/subcmd/deep.go
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()
}
32 changes: 32 additions & 0 deletions cmd/ghsync/subcmd/shallow.go
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)
}
129 changes: 0 additions & 129 deletions cmd/ghsync/subcmd/sync.go

This file was deleted.

2 changes: 1 addition & 1 deletion common.go → deep/common.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ghsync
package deep

import (
"strings"
Expand Down
2 changes: 1 addition & 1 deletion issue.go → deep/issue.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ghsync
package deep

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion issue_comment.go → deep/issue_comment.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ghsync
package deep

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion organization.go → deep/organization.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ghsync
package deep

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion pull_request.go → deep/pull_request.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ghsync
package deep

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion pull_request_comment.go → deep/pull_request_comment.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ghsync
package deep

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion pull_request_review.go → deep/pull_request_review.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ghsync
package deep

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion repository.go → deep/repository.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ghsync
package deep

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion syncer.go → deep/syncer.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ghsync
package deep

import (
"database/sql"
Expand Down
Loading