Skip to content

Commit 97da624

Browse files
author
anton.voskresensky
committed
add context timeout
1 parent 1395206 commit 97da624

File tree

5 files changed

+21
-9
lines changed

5 files changed

+21
-9
lines changed

cmd/projects/files.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"regexp"
1010
"strings"
1111
"text/tabwriter"
12+
"time"
1213

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

238239
defer wg.Done()
239240

240-
ctx := context.TODO()
241+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
242+
defer cancel()
241243
wg.Lock()
242244
list, resp, err := h.GithubClient.Repositories.ListByOrg(context.WithValue(ctx, github.SleepUntilPrimaryRateLimitResetWhenRateLimited, true), h.Org, &opt)
243245
if err != nil {
@@ -302,7 +304,8 @@ func getRawFileFromGithub(h *client.Host, repository *github.Repository, filepat
302304
targetRef = repository.GetDefaultBranch()
303305
}
304306
// TODO:
305-
ctx := context.TODO()
307+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
308+
defer cancel()
306309
wg.Lock()
307310
fileContent, _, resp, err := h.GithubClient.Repositories.GetContents(context.WithValue(ctx, github.SleepUntilPrimaryRateLimitResetWhenRateLimited, true),
308311
repository.Owner.GetLogin(),

cmd/projects/list.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
go_sort "sort"
99
"strings"
1010
"text/tabwriter"
11+
"time"
1112

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

350351
// TODO:
351352
if h.GithubClient != nil {
352-
ctx := context.TODO()
353+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
354+
defer cancel()
353355
list, resp, err := h.GithubClient.Repositories.ListByOrg(context.WithValue(ctx, github.SleepUntilPrimaryRateLimitResetWhenRateLimited, true), h.Org,
354356
&github.RepositoryListByOrgOptions{ListOptions: github.ListOptions{PerPage: 100}},
355357
)

cmd/projects/mr.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"regexp"
99
"text/tabwriter"
10+
"time"
1011

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

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

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

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

492-
ctx := context.TODO()
496+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
497+
defer cancel()
493498
wg.Lock()
494499
list, resp, err := h.GithubClient.PullRequests.List(context.WithValue(ctx, github.SleepUntilPrimaryRateLimitResetWhenRateLimited, true), h.Org, repository.GetName(), &opt)
495500
if err != nil {

cmd/projects/schedules.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"regexp"
99
"strings"
1010
"text/tabwriter"
11+
"time"
1112

1213
go_sort "sort"
1314

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

692693
defer wg.Done()
693694

694-
ctx := context.TODO()
695+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
696+
defer cancel()
695697
wg.Lock()
696698
list, resp, err := h.GithubClient.Actions.ListWorkflows(context.WithValue(ctx, github.SleepUntilPrimaryRateLimitResetWhenRateLimited, true), h.Org, repository.GetName(), &opt)
697699
if err != nil {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/flant/glaball
22

3-
go 1.23.3
3+
go 1.23
44

55
require (
66
dario.cat/mergo v1.0.1

0 commit comments

Comments
 (0)