[bridge/github] use context.WithTimeout on all graphql queries

Amine Hilaly created

Change summary

bridge/github/export.go   | 62 ++++++++++++++++++++++++++++++++++------
bridge/github/import.go   |  6 +++
bridge/github/iterator.go | 25 +++++++++++++--
3 files changed, 78 insertions(+), 15 deletions(-)

Detailed changes

bridge/github/export.go 🔗

@@ -429,7 +429,12 @@ func markOperationAsExported(b *cache.BugCache, target git.Hash, githubID, githu
 func (ge *githubExporter) getGithubLabelID(gc *githubv4.Client, label string) (string, error) {
 	q := labelQuery{}
 	variables := map[string]interface{}{"name": label}
-	if err := gc.Query(context.TODO(), &q, variables); err != nil {
+
+	parentCtx := context.Background()
+	ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
+	defer cancel()
+
+	if err := gc.Query(ctx, &q, variables); err != nil {
 		return "", err
 	}
 
@@ -486,7 +491,11 @@ func (ge *githubExporter) createGithubLabelV4(gc *githubv4.Client, label, labelC
 		Color:        githubv4.String(labelColor),
 	}
 
-	if err := gc.Mutate(context.TODO(), m, input, nil); err != nil {
+	parentCtx := context.Background()
+	ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
+	defer cancel()
+
+	if err := gc.Mutate(ctx, m, input, nil); err != nil {
 		return "", err
 	}
 
@@ -558,7 +567,11 @@ func createGithubIssue(gc *githubv4.Client, repositoryID, title, body string) (s
 		Body:         (*githubv4.String)(&body),
 	}
 
-	if err := gc.Mutate(context.TODO(), m, input, nil); err != nil {
+	parentCtx := context.Background()
+	ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
+	defer cancel()
+
+	if err := gc.Mutate(ctx, m, input, nil); err != nil {
 		return "", "", err
 	}
 
@@ -574,7 +587,11 @@ func addCommentGithubIssue(gc *githubv4.Client, subjectID string, body string) (
 		Body:      githubv4.String(body),
 	}
 
-	if err := gc.Mutate(context.TODO(), m, input, nil); err != nil {
+	parentCtx := context.Background()
+	ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
+	defer cancel()
+
+	if err := gc.Mutate(ctx, m, input, nil); err != nil {
 		return "", "", err
 	}
 
@@ -589,7 +606,11 @@ func editCommentGithubIssue(gc *githubv4.Client, commentID, body string) (string
 		Body: githubv4.String(body),
 	}
 
-	if err := gc.Mutate(context.TODO(), m, input, nil); err != nil {
+	parentCtx := context.Background()
+	ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
+	defer cancel()
+
+	if err := gc.Mutate(ctx, m, input, nil); err != nil {
 		return "", "", err
 	}
 
@@ -611,7 +632,11 @@ func updateGithubIssueStatus(gc *githubv4.Client, id string, status bug.Status)
 		State: &state,
 	}
 
-	if err := gc.Mutate(context.TODO(), m, input, nil); err != nil {
+	parentCtx := context.Background()
+	ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
+	defer cancel()
+
+	if err := gc.Mutate(ctx, m, input, nil); err != nil {
 		return err
 	}
 
@@ -625,7 +650,11 @@ func updateGithubIssueBody(gc *githubv4.Client, id string, body string) error {
 		Body: (*githubv4.String)(&body),
 	}
 
-	if err := gc.Mutate(context.TODO(), m, input, nil); err != nil {
+	parentCtx := context.Background()
+	ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
+	defer cancel()
+
+	if err := gc.Mutate(ctx, m, input, nil); err != nil {
 		return err
 	}
 
@@ -639,7 +668,11 @@ func updateGithubIssueTitle(gc *githubv4.Client, id, title string) error {
 		Title: (*githubv4.String)(&title),
 	}
 
-	if err := gc.Mutate(context.TODO(), m, input, nil); err != nil {
+	parentCtx := context.Background()
+	ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
+	defer cancel()
+
+	if err := gc.Mutate(ctx, m, input, nil); err != nil {
 		return err
 	}
 
@@ -648,6 +681,7 @@ func updateGithubIssueTitle(gc *githubv4.Client, id, title string) error {
 
 // update github issue labels
 func (ge *githubExporter) updateGithubIssueLabels(gc *githubv4.Client, labelableID string, added, removed []bug.Label) error {
+
 	addedIDs, err := ge.getLabelsIDs(gc, labelableID, added)
 	if err != nil {
 		return errors.Wrap(err, "getting added labels ids")
@@ -659,11 +693,16 @@ func (ge *githubExporter) updateGithubIssueLabels(gc *githubv4.Client, labelable
 		LabelIDs:    addedIDs,
 	}
 
+	parentCtx := context.Background()
+	ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
+
 	// add labels
-	if err := gc.Mutate(context.TODO(), m, inputAdd, nil); err != nil {
+	if err := gc.Mutate(ctx, m, inputAdd, nil); err != nil {
+		cancel()
 		return err
 	}
 
+	cancel()
 	removedIDs, err := ge.getLabelsIDs(gc, labelableID, added)
 	if err != nil {
 		return errors.Wrap(err, "getting added labels ids")
@@ -675,8 +714,11 @@ func (ge *githubExporter) updateGithubIssueLabels(gc *githubv4.Client, labelable
 		LabelIDs:    removedIDs,
 	}
 
+	ctx2, cancel2 := context.WithTimeout(parentCtx, defaultTimeout)
+	defer cancel2()
+
 	// remove label labels
-	if err := gc.Mutate(context.TODO(), m2, inputRemove, nil); err != nil {
+	if err := gc.Mutate(ctx2, m2, inputRemove, nil); err != nil {
 		return err
 	}
 

bridge/github/import.go 🔗

@@ -513,7 +513,11 @@ func (gi *githubImporter) getGhost(repo *cache.RepoCache) (*cache.IdentityCache,
 
 	gc := buildClient(gi.conf[keyToken])
 
-	err = gc.Query(context.TODO(), &q, variables)
+	parentCtx := context.Background()
+	ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
+	defer cancel()
+
+	err = gc.Query(ctx, &q, variables)
 	if err != nil {
 		return nil, err
 	}

bridge/github/iterator.go 🔗

@@ -147,7 +147,11 @@ func (i *iterator) Error() error {
 }
 
 func (i *iterator) queryIssue() bool {
-	if err := i.gc.Query(context.TODO(), &i.timeline.query, i.timeline.variables); err != nil {
+	parentCtx := context.Background()
+	ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
+	defer cancel()
+
+	if err := i.gc.Query(ctx, &i.timeline.query, i.timeline.variables); err != nil {
 		i.err = err
 		return false
 	}
@@ -220,7 +224,12 @@ func (i *iterator) NextTimelineItem() bool {
 
 	// more timelines, query them
 	i.timeline.variables["timelineAfter"] = i.timeline.query.Repository.Issues.Nodes[0].Timeline.PageInfo.EndCursor
-	if err := i.gc.Query(context.TODO(), &i.timeline.query, i.timeline.variables); err != nil {
+
+	parentCtx := context.Background()
+	ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
+	defer cancel()
+
+	if err := i.gc.Query(ctx, &i.timeline.query, i.timeline.variables); err != nil {
 		i.err = err
 		return false
 	}
@@ -236,7 +245,11 @@ func (i *iterator) TimelineItemValue() timelineItem {
 }
 
 func (i *iterator) queryIssueEdit() bool {
-	if err := i.gc.Query(context.TODO(), &i.issueEdit.query, i.issueEdit.variables); err != nil {
+	parentCtx := context.Background()
+	ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
+	defer cancel()
+
+	if err := i.gc.Query(ctx, &i.issueEdit.query, i.issueEdit.variables); err != nil {
 		i.err = err
 		//i.timeline.issueEdit.index = -1
 		return false
@@ -334,7 +347,11 @@ func (i *iterator) IssueEditValue() userContentEdit {
 }
 
 func (i *iterator) queryCommentEdit() bool {
-	if err := i.gc.Query(context.TODO(), &i.commentEdit.query, i.commentEdit.variables); err != nil {
+	parentCtx := context.Background()
+	ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
+	defer cancel()
+
+	if err := i.gc.Query(ctx, &i.commentEdit.query, i.commentEdit.variables); err != nil {
 		i.err = err
 		return false
 	}