Skip to content

Commit dbb5388

Browse files
committed
tweak
1 parent 132446d commit dbb5388

10 files changed

+125
-124
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,6 @@ They can be accessed exactly as shown in this list.
287287
- `project.name` (string)
288288
- `project.path` (string)
289289
- `project.topics[]` (array of string)
290-
- `project.updated_at` (time)
291290
- `project.visibility` (string)
292291

293292
#### project.labels

cmd/scm-engine/cmd_evaluate.go cmd/cmd_evaluate.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package cmd
22

33
import (
44
"fmt"
@@ -7,7 +7,7 @@ import (
77
"github.com/urfave/cli/v2"
88
)
99

10-
func evaluateCmd(cCtx *cli.Context) error {
10+
func Evaluate(cCtx *cli.Context) error {
1111
ctx := state.ContextWithProjectID(cCtx.Context, cCtx.String(FlagSCMProject))
1212

1313
switch {

cmd/scm-engine/cmd_server.go cmd/cmd_server.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package cmd
22

33
import (
44
"encoding/json"
@@ -10,7 +10,7 @@ import (
1010
"github.com/urfave/cli/v2"
1111
)
1212

13-
func serverCmd(cCtx *cli.Context) error {
13+
func Server(_ *cli.Context) error { //nolint:unparam
1414
mux := http.NewServeMux()
1515

1616
mux.HandleFunc("POST /mr", func(writer http.ResponseWriter, reader *http.Request) {

cmd/conventions.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package cmd
2+
3+
const (
4+
FlagConfigFile = "config"
5+
FlagAPIToken = "api-token"
6+
FlagSCMProject = "project"
7+
FlagSCMBaseURL = "base-url"
8+
FlagMergeRequestID = "id"
9+
)

cmd/scm-engine/help.go cmd/help.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package main
1+
package cmd
22

3-
var globalOptionsTemplate = `{{if .VisibleFlags}}
3+
var GlobalOptionsTemplate = `{{if .VisibleFlags}}
44
GLOBAL OPTIONS:
55
{{range $index, $option := .VisibleFlags}}{{if $index}}
66
{{end}}{{$option}}{{end}}

cmd/scm-engine/main.go cmd/shared.go

+2-111
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,17 @@
1-
package main
1+
package cmd
22

33
import (
44
"context"
55
"fmt"
6-
"io"
7-
"log"
86
"net/http"
9-
"os"
107

11-
"github.com/davecgh/go-spew/spew"
128
"github.com/jippi/gitlab-labeller/pkg/config"
139
"github.com/jippi/gitlab-labeller/pkg/scm"
1410
"github.com/jippi/gitlab-labeller/pkg/scm/gitlab"
1511
"github.com/jippi/gitlab-labeller/pkg/state"
1612
"github.com/urfave/cli/v2"
1713
)
1814

19-
const (
20-
FlagConfigFile = "config"
21-
FlagAPIToken = "api-token"
22-
FlagSCMProject = "project"
23-
FlagSCMBaseURL = "base-url"
24-
FlagMergeRequestID = "id"
25-
)
26-
27-
func main() {
28-
spew.Config.DisableMethods = true
29-
30-
app := &cli.App{
31-
Name: "scm-engine",
32-
Usage: "GitHub/GitLab automation",
33-
Copyright: "Christian Winther",
34-
EnableBashCompletion: true,
35-
Suggest: true,
36-
Authors: []*cli.Author{
37-
{
38-
Name: "Christian Winther",
39-
Email: "gitlab-engine@jippi.dev",
40-
},
41-
},
42-
Flags: []cli.Flag{
43-
&cli.StringFlag{
44-
Name: FlagConfigFile,
45-
Usage: "Path to the scm-engine config file",
46-
Value: ".scm-engine.yml",
47-
TakesFile: true,
48-
EnvVars: []string{
49-
"SCM_ENGINE_CONFIG_FILE",
50-
},
51-
},
52-
&cli.StringFlag{
53-
Name: FlagAPIToken,
54-
Usage: "GitHub/GitLab API token",
55-
Required: true,
56-
EnvVars: []string{
57-
"GITLAB_TOKEN",
58-
"SCM_ENGINE_TOKEN",
59-
},
60-
},
61-
&cli.StringFlag{
62-
Name: FlagSCMProject,
63-
Usage: "GitLab project (example: 'gitlab-org/gitlab')",
64-
Required: true,
65-
EnvVars: []string{
66-
"GITLAB_PROJECT",
67-
"CI_PROJECT_PATH",
68-
},
69-
},
70-
&cli.StringFlag{
71-
Name: FlagSCMBaseURL,
72-
Usage: "Base URL for the SCM instance",
73-
Value: "https://gitlab.com/",
74-
EnvVars: []string{
75-
"GITLAB_BASEURL",
76-
"CI_SERVER_URL",
77-
},
78-
},
79-
},
80-
Commands: []*cli.Command{
81-
{
82-
Name: "evaluate",
83-
Usage: "Evaluate a Merge Request",
84-
Action: evaluateCmd,
85-
Flags: []cli.Flag{
86-
&cli.StringFlag{
87-
Name: FlagMergeRequestID,
88-
Usage: "The pull/merge to process, if not provided as a CLI flag",
89-
Aliases: []string{
90-
"merge-request-id", // GitLab naming
91-
"pull-request-id", // GitHub naming
92-
},
93-
EnvVars: []string{
94-
"CI_MERGE_REQUEST_IID", // GitLab CI
95-
},
96-
},
97-
},
98-
},
99-
{
100-
Name: "server",
101-
Usage: "Start HTTP server for webhook event driven usage",
102-
Action: serverCmd,
103-
},
104-
},
105-
}
106-
107-
origHelpPrinterCustom := cli.HelpPrinterCustom
108-
cli.HelpPrinterCustom = func(out io.Writer, templ string, data interface{}, customFuncs map[string]interface{}) {
109-
origHelpPrinterCustom(out, templ, data, customFuncs)
110-
111-
if data != app {
112-
origHelpPrinterCustom(app.Writer, globalOptionsTemplate, app, nil)
113-
}
114-
}
115-
116-
if err := app.Run(os.Args); err != nil {
117-
log.Fatal(err)
118-
}
119-
}
120-
12115
func ProcessMR(ctx context.Context, cCtx *cli.Context, mr string) error {
12216
ctx = state.ContextWithMergeRequestID(ctx, mr)
12317

@@ -200,11 +94,8 @@ func apply(ctx context.Context, client scm.Client, remoteLabels []scm.Evaluation
20094
AddLabels: &add,
20195
RemoveLabels: &remove,
20296
})
203-
if err != nil {
204-
return err
205-
}
20697

207-
return nil
98+
return err
20899
}
209100

210101
func sync(ctx context.Context, client scm.Client, remote []*scm.Label, required []scm.EvaluationResult) error {

main.go

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package main
2+
3+
import (
4+
"io"
5+
"log"
6+
"os"
7+
8+
"github.com/davecgh/go-spew/spew"
9+
"github.com/jippi/gitlab-labeller/cmd"
10+
"github.com/urfave/cli/v2"
11+
)
12+
13+
func main() {
14+
spew.Config.DisableMethods = true
15+
16+
app := &cli.App{
17+
Name: "scm-engine",
18+
Usage: "GitHub/GitLab automation",
19+
Copyright: "Christian Winther",
20+
EnableBashCompletion: true,
21+
Suggest: true,
22+
Authors: []*cli.Author{
23+
{
24+
Name: "Christian Winther",
25+
Email: "gitlab-engine@jippi.dev",
26+
},
27+
},
28+
Flags: []cli.Flag{
29+
&cli.StringFlag{
30+
Name: cmd.FlagConfigFile,
31+
Usage: "Path to the scm-engine config file",
32+
Value: ".scm-engine.yml",
33+
TakesFile: true,
34+
EnvVars: []string{
35+
"SCM_ENGINE_CONFIG_FILE",
36+
},
37+
},
38+
&cli.StringFlag{
39+
Name: cmd.FlagAPIToken,
40+
Usage: "GitHub/GitLab API token",
41+
Required: true,
42+
EnvVars: []string{
43+
"GITLAB_TOKEN",
44+
"SCM_ENGINE_TOKEN",
45+
},
46+
},
47+
&cli.StringFlag{
48+
Name: cmd.FlagSCMProject,
49+
Usage: "GitLab project (example: 'gitlab-org/gitlab')",
50+
Required: true,
51+
EnvVars: []string{
52+
"GITLAB_PROJECT",
53+
"CI_PROJECT_PATH",
54+
},
55+
},
56+
&cli.StringFlag{
57+
Name: cmd.FlagSCMBaseURL,
58+
Usage: "Base URL for the SCM instance",
59+
Value: "https://gitlab.com/",
60+
EnvVars: []string{
61+
"GITLAB_BASEURL",
62+
"CI_SERVER_URL",
63+
},
64+
},
65+
},
66+
Commands: []*cli.Command{
67+
{
68+
Name: "evaluate",
69+
Usage: "Evaluate a Merge Request",
70+
Action: cmd.Evaluate,
71+
Flags: []cli.Flag{
72+
&cli.StringFlag{
73+
Name: cmd.FlagMergeRequestID,
74+
Usage: "The pull/merge to process, if not provided as a CLI flag",
75+
Aliases: []string{
76+
"merge-request-id", // GitLab naming
77+
"pull-request-id", // GitHub naming
78+
},
79+
EnvVars: []string{
80+
"CI_MERGE_REQUEST_IID", // GitLab CI
81+
},
82+
},
83+
},
84+
},
85+
{
86+
Name: "server",
87+
Usage: "Start HTTP server for webhook event driven usage",
88+
Action: cmd.Server,
89+
},
90+
},
91+
}
92+
93+
origHelpPrinterCustom := cli.HelpPrinterCustom
94+
cli.HelpPrinterCustom = func(out io.Writer, templ string, data interface{}, customFuncs map[string]interface{}) {
95+
origHelpPrinterCustom(out, templ, data, customFuncs)
96+
97+
if data != app {
98+
origHelpPrinterCustom(app.Writer, cmd.GlobalOptionsTemplate, app, nil)
99+
}
100+
}
101+
102+
if err := app.Run(os.Args); err != nil {
103+
log.Fatal(err)
104+
}
105+
}

pkg/config/loader.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"errors"
66
"io"
7+
"io/fs"
78
"os"
89
"os/exec"
910
"path/filepath"
@@ -56,7 +57,7 @@ func findFileAtStandardLocation() string {
5657
// fileExist checks if a normal file exists at the path specified.
5758
func fileExists(path string) bool {
5859
info, err := os.Stat(path)
59-
if os.IsNotExist(err) {
60+
if errors.Is(err, fs.ErrNotExist) {
6061
return false
6162
}
6263

pkg/scm/gitlab/client_merge_request.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ func (client *MergeRequestClient) Update(ctx context.Context, opt *scm.UpdateMer
4040
m := new(go_gitlab.MergeRequest)
4141

4242
resp, err := client.client.wrapped.Do(req, m)
43-
if err != nil {
44-
return convertResponse(resp), err
45-
}
4643

47-
return convertResponse(resp), nil
44+
return convertResponse(resp), err
4845
}

pkg/scm/gitlab/context_project.go

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ type ContextProject struct {
1515
Labels []ContextLabel `expr:"labels" graphql:"-"`
1616
LastActivityAt time.Time `expr:"last_activity_at" graphql:"lastActivityAt"`
1717
CreatedAt time.Time `expr:"created_at" graphql:"createdAt"`
18-
UpdatedAt time.Time `expr:"updated_at" graphql:"updatedAt"`
1918

2019
// Internal state
2120
MergeRequest *ContextMergeRequest `expr:"-" graphql:"mergeRequest(iid: $mr_id)"`

0 commit comments

Comments
 (0)