Skip to content

Commit

Permalink
add context timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
anton.voskresensky committed Nov 25, 2024
1 parent 1395206 commit 97da624
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
7 changes: 5 additions & 2 deletions cmd/projects/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"regexp"
"strings"
"text/tabwriter"
"time"

"github.com/flant/glaball/pkg/client"
"github.com/flant/glaball/pkg/limiter"
Expand Down Expand Up @@ -237,7 +238,8 @@ func listProjectsFilesFromGithub(h *client.Host, filepath, ref string, re []*reg

defer wg.Done()

ctx := context.TODO()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
defer cancel()
wg.Lock()
list, resp, err := h.GithubClient.Repositories.ListByOrg(context.WithValue(ctx, github.SleepUntilPrimaryRateLimitResetWhenRateLimited, true), h.Org, &opt)
if err != nil {
Expand Down Expand Up @@ -302,7 +304,8 @@ func getRawFileFromGithub(h *client.Host, repository *github.Repository, filepat
targetRef = repository.GetDefaultBranch()
}
// TODO:
ctx := context.TODO()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
defer cancel()
wg.Lock()
fileContent, _, resp, err := h.GithubClient.Repositories.GetContents(context.WithValue(ctx, github.SleepUntilPrimaryRateLimitResetWhenRateLimited, true),
repository.Owner.GetLogin(),
Expand Down
4 changes: 3 additions & 1 deletion cmd/projects/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
go_sort "sort"
"strings"
"text/tabwriter"
"time"

"github.com/flant/glaball/pkg/client"
"github.com/flant/glaball/pkg/limiter"
Expand Down Expand Up @@ -349,7 +350,8 @@ func listProjects(h *client.Host, opt gitlab.ListProjectsOptions, wg *limiter.Li

// TODO:
if h.GithubClient != nil {
ctx := context.TODO()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
defer cancel()
list, resp, err := h.GithubClient.Repositories.ListByOrg(context.WithValue(ctx, github.SleepUntilPrimaryRateLimitResetWhenRateLimited, true), h.Org,
&github.RepositoryListByOrgOptions{ListOptions: github.ListOptions{PerPage: 100}},
)
Expand Down
13 changes: 9 additions & 4 deletions cmd/projects/mr.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"regexp"
"text/tabwriter"
"time"

"github.com/flant/glaball/pkg/limiter"
"github.com/flant/glaball/pkg/sort/v2"
Expand Down Expand Up @@ -225,7 +226,8 @@ func listRepositories(h *client.Host, archived bool, opt github.RepositoryListBy
wg *limiter.Limiter, data chan<- interface{}) error {
defer wg.Done()

ctx := context.TODO()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
defer cancel()
wg.Lock()
list, resp, err := h.GithubClient.Repositories.ListByOrg(context.WithValue(ctx, github.SleepUntilPrimaryRateLimitResetWhenRateLimited, true), h.Org, &opt)
if err != nil {
Expand Down Expand Up @@ -259,7 +261,8 @@ func listRepositoriesByNamespace(h *client.Host, namespaces []string, archived b
wg *limiter.Limiter, data chan<- interface{}) error {
defer wg.Done()

ctx := context.TODO()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
defer cancel()
wg.Lock()
list, resp, err := h.GithubClient.Repositories.ListByOrg(context.WithValue(ctx, github.SleepUntilPrimaryRateLimitResetWhenRateLimited, true), h.Org, &opt)
if err != nil {
Expand Down Expand Up @@ -434,7 +437,8 @@ func listPullRequestsByAssigneeOrAuthorID(h *client.Host, repository *github.Rep
opt github.PullRequestListOptions, wg *limiter.Limiter, data chan<- interface{}) error {
defer wg.Done()

ctx := context.TODO()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
defer cancel()
wg.Lock()
list, resp, err := h.GithubClient.PullRequests.List(context.WithValue(ctx, github.SleepUntilPrimaryRateLimitResetWhenRateLimited, true), h.Org, repository.GetName(), &opt)
if err != nil {
Expand Down Expand Up @@ -489,7 +493,8 @@ func listPullRequests(h *client.Host, repository *github.Repository, opt github.
wg *limiter.Limiter, data chan<- interface{}) error {
defer wg.Done()

ctx := context.TODO()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
defer cancel()
wg.Lock()
list, resp, err := h.GithubClient.PullRequests.List(context.WithValue(ctx, github.SleepUntilPrimaryRateLimitResetWhenRateLimited, true), h.Org, repository.GetName(), &opt)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion cmd/projects/schedules.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"regexp"
"strings"
"text/tabwriter"
"time"

go_sort "sort"

Expand Down Expand Up @@ -691,7 +692,8 @@ func listWorkflowRuns(h *client.Host, repository *github.Repository, opt github.

defer wg.Done()

ctx := context.TODO()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
defer cancel()
wg.Lock()
list, resp, err := h.GithubClient.Actions.ListWorkflows(context.WithValue(ctx, github.SleepUntilPrimaryRateLimitResetWhenRateLimited, true), h.Org, repository.GetName(), &opt)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/flant/glaball

go 1.23.3
go 1.23

require (
dario.cat/mergo v1.0.1
Expand Down

0 comments on commit 97da624

Please sign in to comment.