Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Nov 26, 2018
1 parent d5a78bd commit adbb5ac
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 264 deletions.
220 changes: 8 additions & 212 deletions cmd_airtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type airtableOptions struct {
Targets []Target `mapstructure:"targets"`
}

var globalAirtableOptions airtableOptions

func (opts airtableOptions) String() string {
out, _ := json.Marshal(opts)
return string(out)
Expand Down Expand Up @@ -53,21 +55,18 @@ func newAirtableCommand() *cobra.Command {
}

func newAirtableSyncCommand() *cobra.Command {
opts := &airtableOptions{}
cmd := &cobra.Command{
Use: "sync",
RunE: func(cmd *cobra.Command, args []string) error {
opts := globalAirtableOptions
var err error
if err = viper.Unmarshal(opts); err != nil {
return err
}
if opts.Targets, err = ParseTargets(args); err != nil {
return errors.Wrap(err, "invalid targets")
}
return airtableSync(opts)
return airtableSync(&opts)
},
}
airtableSetupFlags(cmd.Flags(), opts)
airtableSetupFlags(cmd.Flags(), &globalAirtableOptions)
return cmd
}

Expand All @@ -94,8 +93,8 @@ func airtableSync(opts *airtableOptions) error {
if err != nil {
return errors.Wrap(err, "failed to load issues")
}
issues.FilterByTargets(opts.Targets)
logger().Debug("fetch db entries", zap.Int("count", len(issues)))
filtered := issues.FilterByTargets(opts.Targets)
logger().Debug("fetch db entries", zap.Int("count", len(filtered)))

// unique entries
var (
Expand All @@ -106,7 +105,7 @@ func airtableSync(opts *airtableOptions) error {
milestoneMap = make(map[string]*Milestone)
issueMap = make(map[string]*Issue)
)
for _, issue := range issues {
for _, issue := range filtered {
// providers
providerMap[issue.Repository.Provider.ID] = issue.Repository.Provider

Expand Down Expand Up @@ -517,206 +516,3 @@ func airtableSync(opts *airtableOptions) error {

return nil
}

func LEGACY(opts *airtableOptions) error {
issues := []*Issue{} // tmp for compilation

at := airtable.Client{
APIKey: opts.Token,
BaseID: opts.BaseID,
Limiter: airtable.RateLimiter(5),
}
table := at.Table(opts.IssuesTableName)

alreadyInAirtable := map[string]bool{}

records := []airtableIssueRecord{}
if err := table.List(&records, &airtable.Options{}); err != nil {
return err
}
logger().Debug("fetched airtable records", zap.Int("count", len(records)))

// create new records
for _, record := range records {
alreadyInAirtable[record.Fields.URL] = true
}
for _, issue := range issues {
if issue.IsHidden {
continue
}
if _, found := alreadyInAirtable[issue.URL]; found {
continue
}
logger().Debug("creating airtable record without slices", zap.String("URL", issue.URL))
r := minimalAirtableIssueRecord{
Fields: minimalAirtableIssue{
URL: issue.URL,
Errors: "initialization",
},
}
if err := table.Create(&r); err != nil {
return err
}
records = append(records, airtableIssueRecord{
ID: r.ID,
Fields: airtableIssue{
URL: issue.URL,
},
})
}

/*
// update/destroy existing ones
for _, record := range records {
if issue := issues.Get(record.Fields.URL); issue != nil {
if opts.DestroyInvalidRecords {
logger().Debug("destroying airtable record", zap.String("URL", record.Fields.URL))
if err := table.Delete(&record); err != nil {
return errors.Wrap(err, "failed to destroy record")
}
}
} else {
if issue.Hidden {
continue
}
if issue.ToAirtableRecord().Fields.Equals(record.Fields) {
continue
}
logger().Debug("updating airtable record", zap.String("URL", issue.URL))
record.Fields = issue.ToAirtableRecord().Fields
if err := table.Update(&record); err != nil {
logger().Warn("failed to update record, retrying without slices", zap.String("URL", issue.URL), zap.Error(err))
record := minimalAirtableIssueRecord{
ID: record.ID,
Fields: minimalAirtableIssue{
URL: issue.URL,
},
}
if typedErr, ok := err.(airtable.ErrClientRequest); ok {
record.Fields.Errors = typedErr.Err.Error()
} else {
record.Fields.Errors = err.Error()
}
if err := table.Update(&record); err != nil {
logger().Error("failed to update record without slices", zap.String("URL", issue.URL), zap.Error(err))
}
}
}
}
*/

return nil
}

type airtableIssueRecord struct {
ID string `json:"id,omitempty"`
Fields airtableIssue `json:"fields,omitempty"`
}

// fixme: remove the "minimal" hack, thanks to multi table

type minimalAirtableIssueRecord struct {
ID string `json:"id,omitempty"`
Fields minimalAirtableIssue `json:"fields,omitempty"`
}

func (i Issue) ToAirtableRecord() airtableIssueRecord {
typ := "issue"
if i.IsPR {
typ = "pull-request"
}
labels := []string{}
for _, label := range i.Labels {
labels = append(labels, label.ID)
}
assignees := []string{}
for _, assignee := range i.Assignees {
assignees = append(assignees, assignee.ID)
}

return airtableIssueRecord{
ID: "",
Fields: airtableIssue{
URL: i.URL,
Type: typ,
/*
Created: i.CreatedAt,
Updated: i.UpdatedAt,
Completed: i.CompletedAt,
Title: i.Title,
Labels: labels,
Assignees: assignees,
Provider: i.Repository.Provider.URL,
RepoURL: i.Repository.URL,
Body: i.Body,
State: i.State,
IsLocked: i.IsLocked,
IsOrphan: i.IsOrphan,
Author: i.AuthorID,
Comments: i.Comments,
Milestone: i.Milestone,
Upvotes: i.Upvotes,
Downvotes: i.Downvotes,
Weight: i.Weight(),
Errors: "",
*/
},
}
}

type airtableIssue struct {
URL string
Title string
Type string
/*
Created time.Time
Updated time.Time
Completed time.Time
Provider string
State string
Body string
RepoURL string
IsLocked bool
Author string
Comments int
Milestone string
Upvotes int
Downvotes int
IsOrphan bool
Labels []string
Assignees []string
Weight int
Errors string
*/
}

type minimalAirtableIssue struct {
URL string
Errors string
}

func (ai airtableIssue) Equals(other airtableIssue) bool {
return ai.URL == other.URL /* &&
ai.Created.Truncate(time.Millisecond).UTC() == other.Created.Truncate(time.Millisecond).UTC() &&
ai.Updated.Truncate(time.Millisecond).UTC() == other.Updated.Truncate(time.Millisecond).UTC() &&
ai.Completed.Truncate(time.Millisecond).UTC() == other.Completed.Truncate(time.Millisecond).UTC() &&
ai.Title == other.Title &&
ai.Provider == other.Provider &&
ai.State == other.State &&
ai.Body == other.Body &&
ai.RepoURL == other.RepoURL &&
ai.Type == other.Type &&
ai.Locked == other.Locked &&
ai.Author == other.Author &&
ai.Comments == other.Comments &&
ai.Milestone == other.Milestone &&
ai.Weight == other.Weight &&
ai.IsOrphan == other.IsOrphan &&
ai.Upvotes == other.Upvotes &&
ai.Downvotes == other.Downvotes &&
isSameStringSlice(ai.Labels, other.Labels) &&
isSameStringSlice(ai.Assignees, other.Assignees) &&
ai.Errors == other.Errors*/
}
15 changes: 8 additions & 7 deletions cmd_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

type dbOptions struct{}

var globalDBOptions dbOptions

func (opts dbOptions) String() string {
out, _ := json.Marshal(opts)
return string(out)
Expand All @@ -29,17 +31,14 @@ func newDBCommand() *cobra.Command {
}

func newDBDumpCommand() *cobra.Command {
opts := &dbOptions{}
cmd := &cobra.Command{
Use: "dump",
RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.Unmarshal(opts); err != nil {
return err
}
return dbDump(opts)
opts := globalDBOptions
return dbDump(&opts)
},
}
dbSetupFlags(cmd.Flags(), opts)
dbSetupFlags(cmd.Flags(), &globalDBOptions)
return cmd
}

Expand All @@ -65,7 +64,9 @@ func loadIssues(targets []string) (Issues, error) {
query := db.Model(Issue{}).Order("created_at")
if len(targets) > 0 {
return nil, fmt.Errorf("not implemented")
// query = query.Where("repo_url IN (?)", canonicalTargets(targets))
// query = query.Where("repo_url IN (?)", canonicalTargets(targets))
// OR WHERE parents IN ....
// etc
}

perPage := 100
Expand Down
19 changes: 9 additions & 10 deletions cmd_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type graphOptions struct {
// NoExternal
}

var globalGraphOptions graphOptions

func (opts graphOptions) String() string {
out, _ := json.Marshal(opts)
return string(out)
Expand All @@ -53,21 +55,18 @@ func graphSetupFlags(flags *pflag.FlagSet, opts *graphOptions) {
}

func newGraphCommand() *cobra.Command {
opts := &graphOptions{}
cmd := &cobra.Command{
Use: "graph",
RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.Unmarshal(opts); err != nil {
return err
}
opts := globalGraphOptions
var err error
if opts.Targets, err = ParseTargets(args); err != nil {
return errors.Wrap(err, "invalid targets")
}
return graph(opts)
return graph(&opts)
},
}
graphSetupFlags(cmd.Flags(), opts)
graphSetupFlags(cmd.Flags(), &globalGraphOptions)
return cmd
}

Expand All @@ -77,9 +76,9 @@ func graph(opts *graphOptions) error {
if err != nil {
return errors.Wrap(err, "failed to load issues")
}
issues.FilterByTargets(opts.Targets)
filtered := issues.FilterByTargets(opts.Targets)

out, err := graphviz(issues, opts)
out, err := graphviz(filtered, opts)
if err != nil {
return errors.Wrap(err, "failed to render graph")
}
Expand Down Expand Up @@ -358,7 +357,7 @@ func (i Issue) AddNodeToGraph(g *gographviz.Graph, parent string) error {
attrs["color"] = "gray"
}

logger().Debug("add node to graph", zap.String("url", i.URL))
//logger().Debug("add node to graph", zap.String("url", i.URL))
return g.AddNode(
parent,
escape(i.URL),
Expand Down Expand Up @@ -391,7 +390,7 @@ func (i Issue) AddEdgesToGraph(g *gographviz.Graph, opts *graphOptions) error {
attrs["style"] = "dashed"
}
//log.Print("edge", escape(i.URL), "->", escape(dependency.URL))
logger().Debug("add edge to graph", zap.String("url", i.URL), zap.String("dep", dependency.URL))
//logger().Debug("add edge to graph", zap.String("url", i.URL), zap.String("dep", dependency.URL))
if err := g.AddEdge(
escape(i.URL),
escape(dependency.URL),
Expand Down
11 changes: 5 additions & 6 deletions cmd_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type pullOptions struct {
Targets Targets `mapstructure:"targets"`
}

var globalPullOptions pullOptions

func (opts pullOptions) String() string {
out, _ := json.Marshal(opts)
return string(out)
Expand All @@ -33,21 +35,18 @@ func pullSetupFlags(flags *pflag.FlagSet, opts *pullOptions) {
}

func newPullCommand() *cobra.Command {
opts := &pullOptions{}
cmd := &cobra.Command{
Use: "pull",
RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.Unmarshal(opts); err != nil {
return err
}
opts := globalPullOptions
var err error
if opts.Targets, err = ParseTargets(args); err != nil {
return errors.Wrap(err, "invalid targets")
}
return pullAndCompute(opts)
return pullAndCompute(&opts)
},
}
pullSetupFlags(cmd.Flags(), opts)
pullSetupFlags(cmd.Flags(), &globalPullOptions)
return cmd
}

Expand Down
Loading

0 comments on commit adbb5ac

Please sign in to comment.