Skip to content

Commit bd6464a

Browse files
committed
feat: cache labels to speed up evaluating many MRs in one command
1 parent 787c5ac commit bd6464a

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

cmd/cmd_evaluate.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,37 @@ package cmd
33
import (
44
"fmt"
55

6+
"github.com/jippi/gitlab-labeller/pkg/config"
7+
"github.com/jippi/gitlab-labeller/pkg/scm/gitlab"
68
"github.com/jippi/gitlab-labeller/pkg/state"
79
"github.com/urfave/cli/v2"
810
)
911

1012
func Evaluate(cCtx *cli.Context) error {
1113
ctx := state.ContextWithProjectID(cCtx.Context, cCtx.String(FlagSCMProject))
1214

15+
cfg, err := config.LoadFile(cCtx.String(FlagConfigFile))
16+
if err != nil {
17+
return err
18+
}
19+
20+
client, err := gitlab.NewClient(cCtx.String(FlagAPIToken), cCtx.String(FlagSCMBaseURL))
21+
if err != nil {
22+
return err
23+
}
24+
1325
switch {
1426
// If the flag is set, use that for evaluation
1527
case cCtx.String(FlagMergeRequestID) != "":
16-
return ProcessMR(ctx, cCtx, cCtx.String(FlagMergeRequestID))
28+
return ProcessMR(ctx, client, cfg, cCtx.String(FlagMergeRequestID))
1729

1830
// If no flag is set, we require arguments
1931
case cCtx.Args().Len() == 0:
2032
return fmt.Errorf("Missing required argument: %s", FlagMergeRequestID)
2133

2234
default:
2335
for _, mr := range cCtx.Args().Slice() {
24-
if err := ProcessMR(ctx, cCtx, mr); err != nil {
36+
if err := ProcessMR(ctx, client, cfg, mr); err != nil {
2537
return err
2638
}
2739
}

cmd/shared.go

+1-13
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,15 @@ import (
77

88
"github.com/jippi/gitlab-labeller/pkg/config"
99
"github.com/jippi/gitlab-labeller/pkg/scm"
10-
"github.com/jippi/gitlab-labeller/pkg/scm/gitlab"
1110
"github.com/jippi/gitlab-labeller/pkg/state"
12-
"github.com/urfave/cli/v2"
1311
)
1412

15-
func ProcessMR(ctx context.Context, cCtx *cli.Context, mr string) error {
13+
func ProcessMR(ctx context.Context, client scm.Client, cfg *config.Config, mr string) error {
1614
ctx = state.ContextWithMergeRequestID(ctx, mr)
1715

1816
// for mr := 900; mr <= 1000; mr++ {
1917
fmt.Println("Processing MR", mr)
2018

21-
cfg, err := config.LoadFile(cCtx.String(FlagConfigFile))
22-
if err != nil {
23-
return err
24-
}
25-
26-
client, err := gitlab.NewClient(cCtx.String(FlagAPIToken), cCtx.String(FlagSCMBaseURL))
27-
if err != nil {
28-
return err
29-
}
30-
3119
remoteLabels, err := client.Labels().List(ctx)
3220
if err != nil {
3321
return err

pkg/scm/gitlab/client_label.go

+16
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,20 @@ var _ scm.LabelClient = (*LabelClient)(nil)
1414

1515
type LabelClient struct {
1616
client *Client
17+
18+
cache []*scm.Label
1719
}
1820

1921
func NewLabelClient(client *Client) *LabelClient {
2022
return &LabelClient{client: client}
2123
}
2224

2325
func (client *LabelClient) List(ctx context.Context) ([]*scm.Label, error) {
26+
// Check cache
27+
if len(client.cache) != 0 {
28+
return client.cache, nil
29+
}
30+
2431
var results []*scm.Label
2532

2633
// Load all existing labels
@@ -49,6 +56,9 @@ func (client *LabelClient) List(ctx context.Context) ([]*scm.Label, error) {
4956
opts.ListOptions.Page = resp.NextPage
5057
}
5158

59+
// Store cache
60+
client.cache = results
61+
5262
return results, nil
5363
}
5464

@@ -80,6 +90,9 @@ func (client *LabelClient) list(ctx context.Context, opt *scm.ListLabelsOptions)
8090
}
8191

8292
func (client *LabelClient) Create(ctx context.Context, opt *scm.CreateLabelOptions) (*scm.Label, *scm.Response, error) {
93+
// Invalidate cache
94+
client.cache = nil
95+
8396
project, err := ParseID(state.ProjectIDFromContext(ctx))
8497
if err != nil {
8598
return nil, nil, err
@@ -107,6 +120,9 @@ func (client *LabelClient) Create(ctx context.Context, opt *scm.CreateLabelOptio
107120
}
108121

109122
func (client *LabelClient) Update(ctx context.Context, opt *scm.UpdateLabelOptions) (*scm.Label, *scm.Response, error) {
123+
// Invalidate cache
124+
client.cache = nil
125+
110126
project, err := ParseID(state.ProjectIDFromContext(ctx))
111127
if err != nil {
112128
return nil, nil, err

0 commit comments

Comments
 (0)