diff --git a/examples/cmd/main.go b/examples/cmd/main.go index 6a06ea2..c2757cd 100644 --- a/examples/cmd/main.go +++ b/examples/cmd/main.go @@ -107,7 +107,9 @@ func (c *Ghsync) Execute(args []string) error { type bodyFunc = func(httpClient *http.Client, downloader *github.Downloader) error func (c *DownloaderCmd) ExecuteBody(logger log.Logger, fn bodyFunc) error { - client := oauth2.NewClient(context.TODO(), oauth2.StaticTokenSource( + ctx := context.Background() + + client := oauth2.NewClient(ctx, oauth2.StaticTokenSource( &oauth2.Token{AccessToken: c.Token}, )) @@ -147,7 +149,7 @@ func (c *DownloaderCmd) ExecuteBody(logger log.Logger, fn bodyFunc) error { downloader, err = github.NewDownloader(client, db) } - rate0, err := downloader.RateRemaining(context.TODO()) + rate0, err := downloader.RateRemaining(ctx) if err != nil { return err } @@ -158,18 +160,18 @@ func (c *DownloaderCmd) ExecuteBody(logger log.Logger, fn bodyFunc) error { return err } - err = downloader.SetCurrent(c.Version) + err = downloader.SetCurrent(ctx, c.Version) if err != nil { return err } if c.Cleanup { - return downloader.Cleanup(c.Version) + return downloader.Cleanup(ctx, c.Version) } elapsed := time.Since(t0) - rate1, err := downloader.RateRemaining(context.TODO()) + rate1, err := downloader.RateRemaining(ctx) if err != nil { return err } diff --git a/github/downloader.go b/github/downloader.go index b5a42ef..464a727 100644 --- a/github/downloader.go +++ b/github/downloader.go @@ -25,22 +25,22 @@ const ( ) type storer interface { - SaveOrganization(organization *graphql.Organization) error - SaveUser(user *graphql.UserExtended) error - SaveRepository(repository *graphql.RepositoryFields, topics []string) error - SaveIssue(repositoryOwner, repositoryName string, issue *graphql.Issue, assignees []string, labels []string) error - SaveIssueComment(repositoryOwner, repositoryName string, issueNumber int, comment *graphql.IssueComment) error - SavePullRequest(repositoryOwner, repositoryName string, pr *graphql.PullRequest, assignees []string, labels []string) error - SavePullRequestComment(repositoryOwner, repositoryName string, pullRequestNumber int, comment *graphql.IssueComment) error - SavePullRequestReview(repositoryOwner, repositoryName string, pullRequestNumber int, review *graphql.PullRequestReview) error - SavePullRequestReviewComment(repositoryOwner, repositoryName string, pullRequestNumber int, pullRequestReviewId int, comment *graphql.PullRequestReviewComment) error + SaveOrganization(ctx context.Context, organization *graphql.Organization) error + SaveUser(ctx context.Context, user *graphql.UserExtended) error + SaveRepository(ctx context.Context, repository *graphql.RepositoryFields, topics []string) error + SaveIssue(ctx context.Context, repositoryOwner, repositoryName string, issue *graphql.Issue, assignees []string, labels []string) error + SaveIssueComment(ctx context.Context, repositoryOwner, repositoryName string, issueNumber int, comment *graphql.IssueComment) error + SavePullRequest(ctx context.Context, repositoryOwner, repositoryName string, pr *graphql.PullRequest, assignees []string, labels []string) error + SavePullRequestComment(ctx context.Context, repositoryOwner, repositoryName string, pullRequestNumber int, comment *graphql.IssueComment) error + SavePullRequestReview(ctx context.Context, repositoryOwner, repositoryName string, pullRequestNumber int, review *graphql.PullRequestReview) error + SavePullRequestReviewComment(ctx context.Context, repositoryOwner, repositoryName string, pullRequestNumber int, pullRequestReviewId int, comment *graphql.PullRequestReviewComment) error Begin() error Commit() error Rollback() error Version(v int) - SetActiveVersion(v int) error - Cleanup(currentVersion int) error + SetActiveVersion(ctx context.Context, v int) error + Cleanup(ctx context.Context, currentVersion int) error } // Downloader fetches GitHub data using the v4 API @@ -141,7 +141,7 @@ func (d Downloader) DownloadRepository(ctx context.Context, owner string, name s return err } - err = d.storer.SaveRepository(&q.Repository.RepositoryFields, topics) + err = d.storer.SaveRepository(ctx, &q.Repository.RepositoryFields, topics) if err != nil { return fmt.Errorf("failed to save repository %v: %v", q.Repository.NameWithOwner, err) } @@ -236,7 +236,7 @@ func (d Downloader) downloadIssues(ctx context.Context, owner string, name strin return err } - err = d.storer.SaveIssue(owner, name, issue, assignees, labels) + err = d.storer.SaveIssue(ctx, owner, name, issue, assignees, labels) if err != nil { return err } @@ -397,7 +397,7 @@ func (d Downloader) downloadIssueLabels(ctx context.Context, issue *graphql.Issu func (d Downloader) downloadIssueComments(ctx context.Context, owner string, name string, issue *graphql.Issue) error { // save first page of comments for _, comment := range issue.Comments.Nodes { - err := d.storer.SaveIssueComment(owner, name, issue.Number, &comment) + err := d.storer.SaveIssueComment(ctx, owner, name, issue.Number, &comment) if err != nil { return err } @@ -432,7 +432,7 @@ func (d Downloader) downloadIssueComments(ctx context.Context, owner string, nam } for _, comment := range q.Node.Issue.Comments.Nodes { - err := d.storer.SaveIssueComment(owner, name, issue.Number, &comment) + err := d.storer.SaveIssueComment(ctx, owner, name, issue.Number, &comment) if err != nil { return fmt.Errorf("failed to save issue comments for issue #%v: %v", issue.Number, err) } @@ -457,7 +457,7 @@ func (d Downloader) downloadPullRequests(ctx context.Context, owner string, name return err } - err = d.storer.SavePullRequest(owner, name, pr, assignees, labels) + err = d.storer.SavePullRequest(ctx, owner, name, pr, assignees, labels) if err != nil { return err } @@ -631,7 +631,7 @@ func (d Downloader) downloadPullRequestLabels(ctx context.Context, pr *graphql.P func (d Downloader) downloadPullRequestComments(ctx context.Context, owner string, name string, pr *graphql.PullRequest) error { // save first page of comments for _, comment := range pr.Comments.Nodes { - err := d.storer.SavePullRequestComment(owner, name, pr.Number, &comment) + err := d.storer.SavePullRequestComment(ctx, owner, name, pr.Number, &comment) if err != nil { return fmt.Errorf("failed to save PR comments for PR #%v: %v", pr.Number, err) } @@ -666,7 +666,7 @@ func (d Downloader) downloadPullRequestComments(ctx context.Context, owner strin } for _, comment := range q.Node.PullRequest.Comments.Nodes { - err := d.storer.SavePullRequestComment(owner, name, pr.Number, &comment) + err := d.storer.SavePullRequestComment(ctx, owner, name, pr.Number, &comment) if err != nil { return fmt.Errorf("failed to save PR comments for PR #%v: %v", pr.Number, err) } @@ -681,7 +681,7 @@ func (d Downloader) downloadPullRequestComments(ctx context.Context, owner strin func (d Downloader) downloadPullRequestReviews(ctx context.Context, owner string, name string, pr *graphql.PullRequest) error { process := func(review *graphql.PullRequestReview) error { - err := d.storer.SavePullRequestReview(owner, name, pr.Number, review) + err := d.storer.SavePullRequestReview(ctx, owner, name, pr.Number, review) if err != nil { return fmt.Errorf("failed to save PR review for PR #%v: %v", pr.Number, err) } @@ -743,7 +743,7 @@ func (d Downloader) downloadPullRequestReviews(ctx context.Context, owner string func (d Downloader) downloadReviewComments(ctx context.Context, repositoryOwner, repositoryName string, pullRequestNumber int, review *graphql.PullRequestReview) error { process := func(comment *graphql.PullRequestReviewComment) error { - err := d.storer.SavePullRequestReviewComment(repositoryOwner, repositoryName, pullRequestNumber, review.DatabaseId, comment) + err := d.storer.SavePullRequestReviewComment(ctx, repositoryOwner, repositoryName, pullRequestNumber, review.DatabaseId, comment) if err != nil { return fmt.Errorf( "failed to save PullRequestReviewComment for PR #%v, review ID %v: %v", @@ -844,7 +844,7 @@ func (d Downloader) DownloadOrganization(ctx context.Context, name string, versi return fmt.Errorf("organization query failed: %v", err) } - err = d.storer.SaveOrganization(&q.Organization) + err = d.storer.SaveOrganization(ctx, &q.Organization) if err != nil { return fmt.Errorf("failed to save organization %v: %v", name, err) } @@ -860,7 +860,7 @@ func (d Downloader) DownloadOrganization(ctx context.Context, name string, versi func (d Downloader) downloadUsers(ctx context.Context, name string, organization *graphql.Organization) error { process := func(user *graphql.UserExtended) error { - err := d.storer.SaveUser(user) + err := d.storer.SaveUser(ctx, user) if err != nil { return fmt.Errorf("failed to save UserExtended: %v", err) } @@ -917,8 +917,8 @@ func (d Downloader) downloadUsers(ctx context.Context, name string, organization } // SetCurrent enables the given version as the current one accessible in the DB -func (d Downloader) SetCurrent(version int) error { - err := d.storer.SetActiveVersion(version) +func (d Downloader) SetCurrent(ctx context.Context, version int) error { + err := d.storer.SetActiveVersion(ctx, version) if err != nil { return fmt.Errorf("failed to set current DB version to %v: %v", version, err) } @@ -926,8 +926,8 @@ func (d Downloader) SetCurrent(version int) error { } // Cleanup deletes from the DB all records that do not belong to the currentVersion -func (d Downloader) Cleanup(currentVersion int) error { - err := d.storer.Cleanup(currentVersion) +func (d Downloader) Cleanup(ctx context.Context, currentVersion int) error { + err := d.storer.Cleanup(ctx, currentVersion) if err != nil { return fmt.Errorf("failed to do cleanup for DB version %v: %v", currentVersion, err) } diff --git a/github/store/db.go b/github/store/db.go index 23161f7..e0fb5b0 100644 --- a/github/store/db.go +++ b/github/store/db.go @@ -1,6 +1,7 @@ package store import ( + "context" "crypto/sha256" "database/sql" "fmt" @@ -56,60 +57,60 @@ var tables = []string{ "pull_request_comments_versioned", } -func (s *DB) SetActiveVersion(v int) error { +func (s *DB) SetActiveVersion(ctx context.Context, v int) error { // TODO: for some reason the normal parameter interpolation $1 fails with // pq: got 1 parameters but the statement requires 0 - _, err := s.DB.Exec(fmt.Sprintf(`CREATE OR REPLACE VIEW organizations AS + _, err := s.DB.ExecContext(ctx, fmt.Sprintf(`CREATE OR REPLACE VIEW organizations AS SELECT %s FROM organizations_versioned WHERE %v = ANY(versions)`, organizationsCols, v)) if err != nil { return fmt.Errorf("failed to create VIEW organizations: %v", err) } - _, err = s.DB.Exec(fmt.Sprintf(`CREATE OR REPLACE VIEW users AS + _, err = s.DB.ExecContext(ctx, fmt.Sprintf(`CREATE OR REPLACE VIEW users AS SELECT %s FROM users_versioned WHERE %v = ANY(versions)`, usersCols, v)) if err != nil { return fmt.Errorf("failed to create VIEW users: %v", err) } - _, err = s.DB.Exec(fmt.Sprintf(`CREATE OR REPLACE VIEW repositories AS + _, err = s.DB.ExecContext(ctx, fmt.Sprintf(`CREATE OR REPLACE VIEW repositories AS SELECT %s FROM repositories_versioned WHERE %v = ANY(versions)`, repositoriesCols, v)) if err != nil { return fmt.Errorf("failed to create VIEW repositories: %v", err) } - _, err = s.DB.Exec(fmt.Sprintf(`CREATE OR REPLACE VIEW issues AS + _, err = s.DB.ExecContext(ctx, fmt.Sprintf(`CREATE OR REPLACE VIEW issues AS SELECT %s FROM issues_versioned WHERE %v = ANY(versions)`, issuesCols, v)) if err != nil { return fmt.Errorf("failed to create VIEW issues: %v", err) } - _, err = s.DB.Exec(fmt.Sprintf(`CREATE OR REPLACE VIEW issue_comments AS + _, err = s.DB.ExecContext(ctx, fmt.Sprintf(`CREATE OR REPLACE VIEW issue_comments AS SELECT %s FROM issue_comments_versioned WHERE %v = ANY(versions)`, issueCommentsCols, v)) if err != nil { return fmt.Errorf("failed to create VIEW issue_comments: %v", err) } - _, err = s.DB.Exec(fmt.Sprintf(`CREATE OR REPLACE VIEW pull_requests AS + _, err = s.DB.ExecContext(ctx, fmt.Sprintf(`CREATE OR REPLACE VIEW pull_requests AS SELECT %s FROM pull_requests_versioned WHERE %v = ANY(versions)`, pullRequestsCol, v)) if err != nil { return fmt.Errorf("failed to create VIEW pull_requests: %v", err) } - _, err = s.DB.Exec(fmt.Sprintf(`CREATE OR REPLACE VIEW pull_request_reviews AS + _, err = s.DB.ExecContext(ctx, fmt.Sprintf(`CREATE OR REPLACE VIEW pull_request_reviews AS SELECT %s FROM pull_request_reviews_versioned WHERE %v = ANY(versions)`, pullRequestReviewsCols, v)) if err != nil { return fmt.Errorf("failed to create VIEW pull_request_reviews: %v", err) } - _, err = s.DB.Exec(fmt.Sprintf(`CREATE OR REPLACE VIEW pull_request_comments AS + _, err = s.DB.ExecContext(ctx, fmt.Sprintf(`CREATE OR REPLACE VIEW pull_request_comments AS SELECT %s FROM pull_request_comments_versioned WHERE %v = ANY(versions)`, pullRequestReviewCommentsCols, v)) if err != nil { @@ -119,17 +120,17 @@ func (s *DB) SetActiveVersion(v int) error { return nil } -func (s *DB) Cleanup(currentVersion int) error { +func (s *DB) Cleanup(ctx context.Context, currentVersion int) error { for _, table := range tables { // Delete all entries that do not belong to currentVersion - _, err := s.DB.Exec(fmt.Sprintf(`DELETE FROM %s WHERE %v <> ALL(versions)`, table, currentVersion)) + _, err := s.DB.ExecContext(ctx, fmt.Sprintf(`DELETE FROM %s WHERE %v <> ALL(versions)`, table, currentVersion)) if err != nil { return fmt.Errorf("failed in cleanup method, delete: %v", err) } // All remaining entries belong to currentVersion, replace the list of versions // with an array of 1 entry - _, err = s.DB.Exec(fmt.Sprintf(`UPDATE %s SET versions = array[%v]`, table, currentVersion)) + _, err = s.DB.ExecContext(ctx, fmt.Sprintf(`UPDATE %s SET versions = array[%v]`, table, currentVersion)) if err != nil { return fmt.Errorf("failed in cleanup method, update: %v", err) } @@ -138,7 +139,7 @@ func (s *DB) Cleanup(currentVersion int) error { return nil } -func (s *DB) SaveOrganization(organization *graphql.Organization) error { +func (s *DB) SaveOrganization(ctx context.Context, organization *graphql.Organization) error { statement := fmt.Sprintf( `INSERT INTO organizations_versioned (sum256, versions, %s) @@ -153,7 +154,7 @@ func (s *DB) SaveOrganization(organization *graphql.Organization) error { hash := sha256.Sum256([]byte(st)) hashString := fmt.Sprintf("%x", hash) - _, err := s.tx.Exec(statement, + _, err := s.tx.ExecContext(ctx, statement, hashString, pq.Array([]int{s.v}), @@ -187,7 +188,7 @@ func (s *DB) SaveOrganization(organization *graphql.Organization) error { return nil } -func (s *DB) SaveUser(user *graphql.UserExtended) error { +func (s *DB) SaveUser(ctx context.Context, user *graphql.UserExtended) error { statement := fmt.Sprintf( `INSERT INTO users_versioned (sum256, versions, %s) @@ -202,7 +203,7 @@ func (s *DB) SaveUser(user *graphql.UserExtended) error { hash := sha256.Sum256([]byte(st)) hashString := fmt.Sprintf("%x", hash) - _, err := s.tx.Exec(statement, + _, err := s.tx.ExecContext(ctx, statement, hashString, pq.Array([]int{s.v}), @@ -239,7 +240,7 @@ func (s *DB) SaveUser(user *graphql.UserExtended) error { return nil } -func (s *DB) SaveRepository(repository *graphql.RepositoryFields, topics []string) error { +func (s *DB) SaveRepository(ctx context.Context, repository *graphql.RepositoryFields, topics []string) error { statement := fmt.Sprintf( `INSERT INTO repositories_versioned (sum256, versions, %s) @@ -255,7 +256,7 @@ func (s *DB) SaveRepository(repository *graphql.RepositoryFields, topics []strin hash := sha256.Sum256([]byte(st)) hashString := fmt.Sprintf("%x", hash) - _, err := s.tx.Exec(statement, + _, err := s.tx.ExecContext(ctx, statement, hashString, pq.Array([]int{s.v}), @@ -312,7 +313,7 @@ func repoOwnerID(repository *graphql.RepositoryFields) int { } } -func (s *DB) SaveIssue(repositoryOwner, repositoryName string, issue *graphql.Issue, assignees []string, labels []string) error { +func (s *DB) SaveIssue(ctx context.Context, repositoryOwner, repositoryName string, issue *graphql.Issue, assignees []string, labels []string) error { statement := fmt.Sprintf( `INSERT INTO issues_versioned (sum256, versions, %s) @@ -335,7 +336,7 @@ func (s *DB) SaveIssue(repositoryOwner, repositoryName string, issue *graphql.Is closedByLogin = issue.ClosedBy.Nodes[0].ClosedEvent.Actor.Login } - _, err := s.tx.Exec(statement, + _, err := s.tx.ExecContext(ctx, statement, hashString, pq.Array([]int{s.v}), @@ -371,7 +372,7 @@ func (s *DB) SaveIssue(repositoryOwner, repositoryName string, issue *graphql.Is return nil } -func (s *DB) SaveIssueComment(repositoryOwner, repositoryName string, issueNumber int, comment *graphql.IssueComment) error { +func (s *DB) SaveIssueComment(ctx context.Context, repositoryOwner, repositoryName string, issueNumber int, comment *graphql.IssueComment) error { statement := fmt.Sprintf(`INSERT INTO issue_comments_versioned (sum256, versions, %s) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) @@ -384,7 +385,7 @@ func (s *DB) SaveIssueComment(repositoryOwner, repositoryName string, issueNumbe hash := sha256.Sum256([]byte(st)) hashString := fmt.Sprintf("%x", hash) - _, err := s.tx.Exec(statement, + _, err := s.tx.ExecContext(ctx, statement, hashString, pq.Array([]int{s.v}), @@ -410,7 +411,7 @@ func (s *DB) SaveIssueComment(repositoryOwner, repositoryName string, issueNumbe return nil } -func (s *DB) SavePullRequest(repositoryOwner, repositoryName string, pr *graphql.PullRequest, assignees []string, labels []string) error { +func (s *DB) SavePullRequest(ctx context.Context, repositoryOwner, repositoryName string, pr *graphql.PullRequest, assignees []string, labels []string) error { statement := fmt.Sprintf( `INSERT INTO pull_requests_versioned (sum256, versions, %s) @@ -426,7 +427,7 @@ func (s *DB) SavePullRequest(repositoryOwner, repositoryName string, pr *graphql hash := sha256.Sum256([]byte(st)) hashString := fmt.Sprintf("%x", hash) - _, err := s.tx.Exec(statement, + _, err := s.tx.ExecContext(ctx, statement, hashString, pq.Array([]int{s.v}), @@ -482,12 +483,12 @@ func (s *DB) SavePullRequest(repositoryOwner, repositoryName string, pr *graphql return nil } -func (s *DB) SavePullRequestComment(repositoryOwner, repositoryName string, pullRequestNumber int, comment *graphql.IssueComment) error { +func (s *DB) SavePullRequestComment(ctx context.Context, repositoryOwner, repositoryName string, pullRequestNumber int, comment *graphql.IssueComment) error { // ghsync saves both Issue and PRs comments in the same table, issue_comments - return s.SaveIssueComment(repositoryOwner, repositoryName, pullRequestNumber, comment) + return s.SaveIssueComment(ctx, repositoryOwner, repositoryName, pullRequestNumber, comment) } -func (s *DB) SavePullRequestReview(repositoryOwner, repositoryName string, pullRequestNumber int, review *graphql.PullRequestReview) error { +func (s *DB) SavePullRequestReview(ctx context.Context, repositoryOwner, repositoryName string, pullRequestNumber int, review *graphql.PullRequestReview) error { statement := fmt.Sprintf(`INSERT INTO pull_request_reviews_versioned (sum256, versions, %s) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) @@ -500,7 +501,7 @@ func (s *DB) SavePullRequestReview(repositoryOwner, repositoryName string, pullR hash := sha256.Sum256([]byte(st)) hashString := fmt.Sprintf("%x", hash) - _, err := s.tx.Exec(statement, + _, err := s.tx.ExecContext(ctx, statement, hashString, pq.Array([]int{s.v}), @@ -526,7 +527,7 @@ func (s *DB) SavePullRequestReview(repositoryOwner, repositoryName string, pullR return nil } -func (s *DB) SavePullRequestReviewComment(repositoryOwner, repositoryName string, pullRequestNumber int, pullRequestReviewId int, comment *graphql.PullRequestReviewComment) error { +func (s *DB) SavePullRequestReviewComment(ctx context.Context, repositoryOwner, repositoryName string, pullRequestNumber int, pullRequestReviewId int, comment *graphql.PullRequestReviewComment) error { statement := fmt.Sprintf(`INSERT INTO pull_request_comments_versioned (sum256, versions, %s) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, @@ -540,7 +541,7 @@ func (s *DB) SavePullRequestReviewComment(repositoryOwner, repositoryName string hash := sha256.Sum256([]byte(st)) hashString := fmt.Sprintf("%x", hash) - _, err := s.tx.Exec(statement, + _, err := s.tx.ExecContext(ctx, statement, hashString, pq.Array([]int{s.v}), diff --git a/github/store/stdout.go b/github/store/stdout.go index 7660499..ae42f8d 100644 --- a/github/store/stdout.go +++ b/github/store/stdout.go @@ -1,6 +1,7 @@ package store import ( + "context" "fmt" "github.com/src-d/metadata-retrieval/github/graphql" @@ -8,47 +9,47 @@ import ( type Stdout struct{} -func (s *Stdout) SaveOrganization(organization *graphql.Organization) error { +func (s *Stdout) SaveOrganization(ctx context.Context, organization *graphql.Organization) error { fmt.Printf("organization data fetched for %s\n", organization.Login) return nil } -func (s *Stdout) SaveUser(user *graphql.UserExtended) error { +func (s *Stdout) SaveUser(ctx context.Context, user *graphql.UserExtended) error { fmt.Printf("user data fetched for %s\n", user.Login) return nil } -func (s *Stdout) SaveRepository(repository *graphql.RepositoryFields, topics []string) error { +func (s *Stdout) SaveRepository(ctx context.Context, repository *graphql.RepositoryFields, topics []string) error { fmt.Printf("repository data fetched for %s/%s\n", repository.Owner.Login, repository.Name) return nil } -func (s *Stdout) SaveIssue(repositoryOwner, repositoryName string, issue *graphql.Issue, assignees []string, labels []string) error { +func (s *Stdout) SaveIssue(ctx context.Context, repositoryOwner, repositoryName string, issue *graphql.Issue, assignees []string, labels []string) error { fmt.Printf("issue data fetched for #%v %s\n", issue.Number, issue.Title) return nil } -func (s *Stdout) SaveIssueComment(repositoryOwner, repositoryName string, issueNumber int, comment *graphql.IssueComment) error { +func (s *Stdout) SaveIssueComment(ctx context.Context, repositoryOwner, repositoryName string, issueNumber int, comment *graphql.IssueComment) error { fmt.Printf(" issue comment data fetched by %s at %v: %q\n", comment.Author.Login, comment.CreatedAt, trim(comment.Body)) return nil } -func (s *Stdout) SavePullRequest(repositoryOwner, repositoryName string, pr *graphql.PullRequest, assignees []string, labels []string) error { +func (s *Stdout) SavePullRequest(ctx context.Context, repositoryOwner, repositoryName string, pr *graphql.PullRequest, assignees []string, labels []string) error { fmt.Printf("PR data fetched for #%v %s\n", pr.Number, pr.Title) return nil } -func (s *Stdout) SavePullRequestComment(repositoryOwner, repositoryName string, pullRequestNumber int, comment *graphql.IssueComment) error { +func (s *Stdout) SavePullRequestComment(ctx context.Context, repositoryOwner, repositoryName string, pullRequestNumber int, comment *graphql.IssueComment) error { fmt.Printf(" pr comment data fetched by %s at %v: %q\n", comment.Author.Login, comment.CreatedAt, trim(comment.Body)) return nil } -func (s *Stdout) SavePullRequestReview(repositoryOwner, repositoryName string, pullRequestNumber int, review *graphql.PullRequestReview) error { +func (s *Stdout) SavePullRequestReview(ctx context.Context, repositoryOwner, repositoryName string, pullRequestNumber int, review *graphql.PullRequestReview) error { fmt.Printf(" PR Review data fetched by %s at %v: %q\n", review.Author.Login, review.SubmittedAt, trim(review.Body)) return nil } -func (s *Stdout) SavePullRequestReviewComment(repositoryOwner, repositoryName string, pullRequestNumber int, pullRequestReviewId int, comment *graphql.PullRequestReviewComment) error { +func (s *Stdout) SavePullRequestReviewComment(ctx context.Context, repositoryOwner, repositoryName string, pullRequestNumber int, pullRequestReviewId int, comment *graphql.PullRequestReviewComment) error { fmt.Printf(" PR review comment data fetched by %s at %v: %q\n", comment.Author.Login, comment.CreatedAt, trim(comment.Body)) return nil } @@ -68,11 +69,11 @@ func (s *Stdout) Rollback() error { func (s *Stdout) Version(v int) { } -func (s *Stdout) SetActiveVersion(v int) error { +func (s *Stdout) SetActiveVersion(ctx context.Context, v int) error { return nil } -func (s *Stdout) Cleanup(currentVersion int) error { +func (s *Stdout) Cleanup(ctx context.Context, currentVersion int) error { return nil } diff --git a/testutils/memory.go b/testutils/memory.go index c73cd79..e90d735 100644 --- a/testutils/memory.go +++ b/testutils/memory.go @@ -1,6 +1,8 @@ package testutils import ( + "context" + "github.com/src-d/metadata-retrieval/github/graphql" "gopkg.in/src-d/go-log.v1" @@ -18,7 +20,7 @@ type Memory struct { // SaveOrganization stores an organization in memory, // it also initializes the list of users -func (s *Memory) SaveOrganization(organization *graphql.Organization) error { +func (s *Memory) SaveOrganization(ctx context.Context, organization *graphql.Organization) error { log.Infof("organization data fetched for %s\n", organization.Login) s.Organization = organization // Initialize users to 0 for each repo @@ -27,7 +29,7 @@ func (s *Memory) SaveOrganization(organization *graphql.Organization) error { } // SaveUser appends a user to the user list in memory -func (s *Memory) SaveUser(user *graphql.UserExtended) error { +func (s *Memory) SaveUser(ctx context.Context, user *graphql.UserExtended) error { log.Infof("user data fetched for %s\n", user.Login) s.Users = append(s.Users, user) return nil @@ -35,7 +37,7 @@ func (s *Memory) SaveUser(user *graphql.UserExtended) error { // SaveRepository stores a repository and its topics in memory and // initializes PRs and PR comments -func (s *Memory) SaveRepository(repository *graphql.RepositoryFields, topics []string) error { +func (s *Memory) SaveRepository(ctx context.Context, repository *graphql.RepositoryFields, topics []string) error { log.Infof("repository data fetched for %s/%s\n", repository.Owner.Login, repository.Name) s.Repository = repository s.Topics = topics @@ -48,39 +50,39 @@ func (s *Memory) SaveRepository(repository *graphql.RepositoryFields, topics []s // TODO(kyrcha): add memory in noop methods as the tests expand // SaveIssue noop -func (s *Memory) SaveIssue(repositoryOwner, repositoryName string, issue *graphql.Issue, assignees []string, labels []string) error { +func (s *Memory) SaveIssue(ctx context.Context, repositoryOwner, repositoryName string, issue *graphql.Issue, assignees []string, labels []string) error { log.Infof("issue data fetched for #%v %s\n", issue.Number, issue.Title) return nil } // SaveIssueComment noop -func (s *Memory) SaveIssueComment(repositoryOwner, repositoryName string, issueNumber int, comment *graphql.IssueComment) error { +func (s *Memory) SaveIssueComment(ctx context.Context, repositoryOwner, repositoryName string, issueNumber int, comment *graphql.IssueComment) error { log.Infof(" \tissue comment data fetched by %s at %v: %q\n", comment.Author.Login, comment.CreatedAt, trim(comment.Body)) return nil } // SavePullRequest appends an PR to the PR list in memory -func (s *Memory) SavePullRequest(repositoryOwner, repositoryName string, pr *graphql.PullRequest, assignees []string, labels []string) error { +func (s *Memory) SavePullRequest(ctx context.Context, repositoryOwner, repositoryName string, pr *graphql.PullRequest, assignees []string, labels []string) error { log.Infof("PR data fetched for #%v %s\n", pr.Number, pr.Title) s.PRs = append(s.PRs, pr) return nil } // SavePullRequestComment appends an PR comment to the PR comment list in memory -func (s *Memory) SavePullRequestComment(repositoryOwner, repositoryName string, pullRequestNumber int, comment *graphql.IssueComment) error { +func (s *Memory) SavePullRequestComment(ctx context.Context, repositoryOwner, repositoryName string, pullRequestNumber int, comment *graphql.IssueComment) error { log.Infof("\tpr comment data fetched by %s at %v: %q\n", comment.Author.Login, comment.CreatedAt, trim(comment.Body)) s.PRComments = append(s.PRComments, comment) return nil } // SavePullRequestReview noop -func (s *Memory) SavePullRequestReview(repositoryOwner, repositoryName string, pullRequestNumber int, review *graphql.PullRequestReview) error { +func (s *Memory) SavePullRequestReview(ctx context.Context, repositoryOwner, repositoryName string, pullRequestNumber int, review *graphql.PullRequestReview) error { log.Infof(" \tPR Review data fetched by %s at %v: %q\n", review.Author.Login, review.SubmittedAt, trim(review.Body)) return nil } // SavePullRequestReviewComment noop -func (s *Memory) SavePullRequestReviewComment(repositoryOwner, repositoryName string, pullRequestNumber int, pullRequestReviewID int, comment *graphql.PullRequestReviewComment) error { +func (s *Memory) SavePullRequestReviewComment(ctx context.Context, repositoryOwner, repositoryName string, pullRequestNumber int, pullRequestReviewID int, comment *graphql.PullRequestReviewComment) error { log.Infof("\t\tPR review comment data fetched by %s at %v: %q\n", comment.Author.Login, comment.CreatedAt, trim(comment.Body)) return nil } @@ -105,12 +107,12 @@ func (s *Memory) Version(v int) { } // SetActiveVersion is a noop method at the moment -func (s *Memory) SetActiveVersion(v int) error { +func (s *Memory) SetActiveVersion(ctx context.Context, v int) error { return nil } // Cleanup is a noop method at the moment -func (s *Memory) Cleanup(currentVersion int) error { +func (s *Memory) Cleanup(ctx context.Context, currentVersion int) error { return nil }