Skip to content

Commit 68a76b5

Browse files
committed
feat: reorgnize commands into github/gitlab specific commands
1 parent 95c0df2 commit 68a76b5

14 files changed

+258
-110
lines changed

.github/workflows/docs.yml

+3
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,8 @@ jobs:
3131
- name: setup
3232
run: task setup
3333

34+
- name: generate
35+
run: task docs:generate
36+
3437
- name: deploy
3538
run: task docs:deploy

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@
1818
/scm-engine.exe
1919
/docs/gitlab/script-attributes.md
2020
/docs/github/script-attributes.md
21+
/docs/github/_partials/cmd-*
22+
/docs/gitlab/_partials/cmd-*

Taskfile.yml

+13
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ tasks:
3030
generates:
3131
- ./scm-engine
3232

33+
docs:generate:
34+
desc: "Generate docs"
35+
cmds:
36+
- mkdir -p docs/github/_partials
37+
- go run . -h > docs/github/_partials/cmd-root.md
38+
- go run . github -h > docs/github/_partials/cmd-github.md
39+
- go run . github evaluate -h > docs/github/_partials/cmd-github-evaluate.md
40+
41+
- mkdir -p docs/gitlab/_partials
42+
- go run . -h > docs/gitlab/_partials/cmd-root.md
43+
- go run . gitlab -h > docs/gitlab/_partials/cmd-gitlab.md
44+
- go run . gitlab evaluate -h > docs/gitlab/_partials/cmd-gitlab-evaluate.md
45+
- go run . gitlab server -h > docs/gitlab/_partials/cmd-gitlab-server.md
3346
docs:server:
3447
desc: Run Docs dev server with live preview
3548
cmds:

cmd/conventions.go

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const (
66
FlagConfigFile = "config"
77
FlagDryRun = "dry-run"
88
FlagMergeRequestID = "id"
9-
FlagProvider = "provider"
109
FlagSCMBaseURL = "base-url"
1110
FlagSCMProject = "project"
1211
FlagServerListen = "listen"

cmd/github.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package cmd
2+
3+
import (
4+
"github.com/jippi/scm-engine/pkg/state"
5+
"github.com/urfave/cli/v2"
6+
)
7+
8+
var GitHub = &cli.Command{
9+
Name: "github",
10+
Usage: "GitHub related commands",
11+
Before: func(ctx *cli.Context) error {
12+
ctx.Context = state.WithProvider(ctx.Context, "github")
13+
14+
return nil
15+
},
16+
Flags: []cli.Flag{
17+
&cli.StringFlag{
18+
Name: FlagAPIToken,
19+
Usage: "GitHub API token",
20+
EnvVars: []string{
21+
"SCM_ENGINE_TOKEN", // SCM Engine Native
22+
},
23+
},
24+
&cli.StringFlag{
25+
Name: FlagSCMBaseURL,
26+
Usage: "Base URL for the SCM instance",
27+
Value: "https://api.github.com/",
28+
EnvVars: []string{
29+
"SCM_ENGINE_BASE_URL", // SCM Engine Native
30+
},
31+
},
32+
},
33+
Subcommands: []*cli.Command{
34+
{
35+
Name: "evaluate",
36+
Usage: "Evaluate a Pull Request",
37+
Args: true,
38+
ArgsUsage: " [pr_id, pr_id, ...]",
39+
Action: Evaluate,
40+
Flags: []cli.Flag{
41+
&cli.StringFlag{
42+
Name: FlagSCMProject,
43+
Usage: "GitHub project (example: 'jippi/scm-engine')",
44+
Required: true,
45+
EnvVars: []string{
46+
"GITHUB_REPOSITORY", // GitHub Actions CI
47+
},
48+
},
49+
&cli.StringFlag{
50+
Name: FlagMergeRequestID,
51+
Usage: "The Pull Request ID to process, if not provided as a CLI flag",
52+
EnvVars: []string{
53+
"SCM_ENGINE_PULL_REQUEST_ID", // SCM Engine native
54+
},
55+
},
56+
&cli.StringFlag{
57+
Name: FlagCommitSHA,
58+
Usage: "The git commit sha",
59+
EnvVars: []string{
60+
"GITHUB_SHA", // GitHub Actions
61+
},
62+
},
63+
},
64+
},
65+
},
66+
}

cmd/gitlab.go

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package cmd
2+
3+
import (
4+
"github.com/jippi/scm-engine/pkg/state"
5+
"github.com/urfave/cli/v2"
6+
)
7+
8+
var GitLab = &cli.Command{
9+
Name: "gitlab",
10+
Usage: "GitLab related commands",
11+
Before: func(cCtx *cli.Context) error {
12+
cCtx.Context = state.WithBaseURL(cCtx.Context, cCtx.String(FlagSCMBaseURL))
13+
cCtx.Context = state.WithProvider(cCtx.Context, "gitlab")
14+
cCtx.Context = state.WithToken(cCtx.Context, cCtx.String(FlagAPIToken))
15+
16+
return nil
17+
},
18+
Flags: []cli.Flag{
19+
&cli.StringFlag{
20+
Name: FlagAPIToken,
21+
Usage: "GitLab API token",
22+
EnvVars: []string{
23+
"SCM_ENGINE_TOKEN", // SCM Engine Native
24+
},
25+
},
26+
&cli.StringFlag{
27+
Name: FlagSCMBaseURL,
28+
Usage: "Base URL for the SCM instance",
29+
Value: "https://gitlab.com/",
30+
EnvVars: []string{
31+
"SCM_ENGINE_BASE_URL", // SCM Engine Native
32+
"CI_SERVER_URL", // GitLab CI
33+
},
34+
},
35+
},
36+
Subcommands: []*cli.Command{
37+
{
38+
Name: "evaluate",
39+
Usage: "Evaluate a Merge Request",
40+
Args: true,
41+
ArgsUsage: " [mr_id, mr_id, ...]",
42+
Action: Evaluate,
43+
Flags: []cli.Flag{
44+
&cli.BoolFlag{
45+
Name: FlagUpdatePipeline,
46+
Usage: "Update the CI pipeline status with progress",
47+
Value: false,
48+
EnvVars: []string{
49+
"SCM_ENGINE_UPDATE_PIPELINE",
50+
},
51+
},
52+
&cli.StringFlag{
53+
Name: FlagSCMProject,
54+
Usage: "GitLab project (example: 'gitlab-org/gitlab')",
55+
EnvVars: []string{
56+
"GITLAB_PROJECT",
57+
"CI_PROJECT_PATH", // GitLab CI
58+
},
59+
},
60+
&cli.StringFlag{
61+
Name: FlagMergeRequestID,
62+
Usage: "The Merge Request ID to process, if not provided as a CLI flag",
63+
EnvVars: []string{
64+
"CI_MERGE_REQUEST_IID", // GitLab CI
65+
},
66+
},
67+
&cli.StringFlag{
68+
Name: FlagCommitSHA,
69+
Usage: "The git commit sha",
70+
EnvVars: []string{
71+
"CI_COMMIT_SHA", // GitLab CI
72+
},
73+
},
74+
},
75+
},
76+
{
77+
Name: "server",
78+
Usage: "Start HTTP server for webhook event driven usage",
79+
Hidden: true, // DEPRECATED
80+
Action: Server,
81+
Flags: []cli.Flag{
82+
&cli.StringFlag{
83+
Name: FlagWebhookSecret,
84+
Usage: "Used to validate received payloads. Sent with the request in the X-Gitlab-Token HTTP header",
85+
EnvVars: []string{
86+
"SCM_ENGINE_WEBHOOK_SECRET",
87+
},
88+
},
89+
&cli.StringFlag{
90+
Name: FlagServerListen,
91+
Usage: "IP + Port that the HTTP server should listen on",
92+
Value: "0.0.0.0:3000",
93+
EnvVars: []string{
94+
"SCM_ENGINE_LISTEN",
95+
},
96+
},
97+
&cli.BoolFlag{
98+
Name: FlagUpdatePipeline,
99+
Usage: "Update the CI pipeline status with progress",
100+
Value: true,
101+
EnvVars: []string{
102+
"SCM_ENGINE_UPDATE_PIPELINE",
103+
},
104+
},
105+
},
106+
},
107+
},
108+
}

cmd/cmd_evaluate.go cmd/gitlab_evaluate.go

-4
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ import (
1111

1212
func Evaluate(cCtx *cli.Context) error {
1313
ctx := state.WithProjectID(cCtx.Context, cCtx.String(FlagSCMProject))
14-
ctx = state.WithBaseURL(ctx, cCtx.String(FlagSCMBaseURL))
1514
ctx = state.WithCommitSHA(ctx, cCtx.String(FlagCommitSHA))
16-
ctx = state.WithDryRun(ctx, cCtx.Bool(FlagDryRun))
17-
ctx = state.WithProvider(ctx, cCtx.String(FlagProvider))
18-
ctx = state.WithToken(ctx, cCtx.String(FlagAPIToken))
1915
ctx = state.WithToken(ctx, cCtx.String(FlagAPIToken))
2016
ctx = state.WithUpdatePipeline(ctx, cCtx.Bool(FlagUpdatePipeline))
2117

cmd/cmd_server.go cmd/gitlab_server.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,7 @@ func errHandler(ctx context.Context, w http.ResponseWriter, code int, err error)
5656

5757
func Server(cCtx *cli.Context) error {
5858
// Initialize context
59-
ctx := state.WithDryRun(cCtx.Context, cCtx.Bool(FlagDryRun))
60-
ctx = state.WithBaseURL(ctx, cCtx.String(FlagSCMBaseURL))
61-
ctx = state.WithToken(ctx, cCtx.String(FlagAPIToken))
62-
ctx = state.WithProvider(ctx, cCtx.String(FlagProvider))
63-
ctx = state.WithUpdatePipeline(ctx, cCtx.Bool(FlagUpdatePipeline))
59+
ctx := state.WithUpdatePipeline(cCtx.Context, cCtx.Bool(FlagUpdatePipeline))
6460

6561
slogctx.Info(ctx, "Starting HTTP server", slog.String("listen", cCtx.String(FlagServerListen)))
6662

docs/commands/evaluate.md

-32
This file was deleted.

docs/commands/server.md

-40
This file was deleted.

docs/github/commands.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Commands
2+
3+
## `scm-engine`
4+
5+
```plain
6+
--8<-- "docs/github/_partials/cmd-root.md"
7+
```
8+
9+
## `scm-engine github`
10+
11+
```plain
12+
--8<-- "docs/github/_partials/cmd-github.md"
13+
```
14+
15+
## `scm-engine github evaluate`
16+
17+
```plain
18+
--8<-- "docs/github/_partials/cmd-github-evaluate.md"
19+
```

docs/gitlab/commands.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Commands
2+
3+
## `scm-engine`
4+
5+
```plain
6+
--8<-- "docs/gitlab/_partials/cmd-root.md"
7+
```
8+
9+
## `scm-engine gitlab`
10+
11+
```plain
12+
--8<-- "docs/gitlab/_partials/cmd-gitlab.md"
13+
```
14+
15+
## `scm-engine gitlab evaluate`
16+
17+
```plain
18+
--8<-- "docs/gitlab/_partials/cmd-gitlab-evaluate.md"
19+
```
20+
21+
## `scm-engine gitlab server`
22+
23+
Point your GitLab webhook at the `/gitlab` endpoint.
24+
25+
Support the following events, and they will both trigger an Merge Request `evaluation`
26+
27+
- [`Comments`](https://docs.gitlab.com/ee/user/project/integrations/webhook_events.html#comment-events) - A comment is made or edited on an issue or merge request.
28+
- [`Merge request events`](https://docs.gitlab.com/ee/user/project/integrations/webhook_events.html#merge-request-events) - A merge request is created, updated, or merged.
29+
30+
!!! tip
31+
32+
You have access to the raw webhook event payload via `webhook_event.*` fields in Expr script fields when using `server` mode. See the [GitLab Webhook Events documentation](https://docs.gitlab.com/ee/user/project/integrations/webhook_events.html) for available fields.
33+
34+
```plain
35+
--8<-- "docs/gitlab/_partials/cmd-gitlab-server.md"
36+
```

0 commit comments

Comments
 (0)