Skip to content

Commit e20325b

Browse files
committed
docs: generate attributes from source
1 parent e69ff16 commit e20325b

File tree

6 files changed

+118
-162
lines changed

6 files changed

+118
-162
lines changed

.github/workflows/docs.yml

+7
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,12 @@ jobs:
2121
version: 3.x
2222
repo-token: ${{ secrets.GITHUB_TOKEN }}
2323

24+
- uses: actions/setup-go@v5
25+
with:
26+
go-version-file: go.mod
27+
2428
- name: setup
29+
run: task setup
30+
31+
- name: deploy
2532
run: task docs:deploy

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616
/schema/ignore
1717
/scm-engine
1818
/scm-engine.exe
19+
/docs/gitlab/script-attributes.md

docs/gitlab/script-attributes.md

-149
This file was deleted.

schema/docs.tmpl

+53-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,55 @@
1-
{{- define "attributes" -}}
2-
{{- range .Attributes -}}
3-
{{ if .IsCustomType -}}{{- template "attributes" . }}{{- else }}
4-
- `{{ .BlockName }}` ({{ if .Optional }}optional {{ end }}{{ .Type }}) {{ .Description }}{{- end -}}
5-
{{- end }}
1+
{{- define "enum_attribute" -}}
2+
- `{{ .BlockName }}` ({{ if .Optional }}optional {{ end }}enum) {{ .Description }}
3+
{{- range .Enum.Values }}
4+
- `{{ .Name }}` - {{ .Description }}
5+
{{- end -}}
66
{{- end -}}
77

8-
### Attributes
9-
{{ template "attributes" . -}}
8+
{{- define "single_attribute" -}}- `{{ .BlockName }}` ({{ if .Optional }}optional {{ end }}{{ .Type }}){{ if .Description}} {{ .Description -}}{{ end }}{{- end -}}
9+
10+
{{- define "description" }}{{ if .Description }}
11+
{{ .Description }}
12+
{{ end -}}
13+
{{ end -}}
14+
15+
{{- define "headline" }}
16+
{{ .GetHeadline }}
17+
{{ end -}}
18+
19+
{{- define "custom_type" }}{{ template "headline" . }}{{ template "description" . }}
20+
{{ template "attributes" . }}{{ end -}}
21+
22+
{{- define "attributes" -}}{{- range .Attributes -}}
23+
{{- if .IsCustomType }}{{ template "custom_type" . }}
24+
{{- else if .IsEnum }}{{- template "enum_attribute" . }}
25+
{{- else }}{{- template "single_attribute" . }}
26+
{{ end }}
27+
{{- end -}}
28+
{{- end -}}
29+
30+
# Script Attributes
31+
32+
!!! tip "The [Expr Language Definition](https://expr-lang.org/docs/language-definition) is a great resource to learn more about the language"
33+
34+
!!! note
35+
36+
Missing an attribute? The `schema/gitlab.schema.graphqls` file are what is used to query GitLab, adding the missing `field` to the right `type` should make it accessible. Please open an issue or Pull Request if something is missing.
37+
38+
The following attributes are available in `script` fields.
39+
40+
They can be accessed exactly as shown in this list.
41+
42+
{{ template "attributes" . }}
43+
44+
## `webhook_event`
45+
46+
!!! tip "`webhook_event` attribute is only available in `server` mode"
47+
48+
You have access to the raw webhook event payload via `webhook_event.*` attributes (not listed below) in Expr script fields when using [`server`](#server) mode.
49+
50+
See the [GitLab Webhook Events documentation](https://docs.gitlab.com/ee/user/project/integrations/webhook_events.html) for available fields.
51+
52+
The attributes are named _exactly_ as documented in the GitLab documentation.
53+
54+
- [`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.
55+
- [`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.

schema/gitlab.go

+48-5
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import (
77
"cmp"
88
_ "embed"
99
"fmt"
10-
"html/template"
1110
"os"
1211
"os/exec"
1312
"path/filepath"
1413
"slices"
1514
"strings"
15+
"text/template"
1616

1717
"github.com/99designs/gqlgen/api"
1818
"github.com/99designs/gqlgen/codegen/config"
@@ -61,6 +61,10 @@ func main() {
6161
panic(err)
6262
}
6363

64+
if err := os.WriteFile(getRootPath()+"/docs/gitlab/script-attributes.md", []byte(index.String()), 0600); err != nil {
65+
panic(err)
66+
}
67+
6468
fmt.Println(index.String())
6569
}
6670

@@ -79,12 +83,16 @@ func nest(props []*Property) {
7983
Optional: nested.Optional,
8084
Type: nested.Type,
8185
IsSlice: nested.IsSlice,
86+
IsEnum: nested.IsEnum,
87+
Enum: nested.Enum,
8288
IsCustomType: nested.IsCustomType,
8389
})
8490
}
8591
}
8692

8793
nest(field.Attributes)
94+
95+
slices.SortFunc(field.Attributes, sortSlice)
8896
}
8997
}
9098

@@ -139,6 +147,12 @@ func fieldHook(td *ast.Definition, fd *ast.FieldDefinition, f *modelgen.Field) (
139147
}
140148

141149
func mutateHook(b *modelgen.ModelBuild) *modelgen.ModelBuild {
150+
enums := map[string]*modelgen.Enum{}
151+
152+
for _, enum := range b.Enums {
153+
enums[enum.Name] = enum
154+
}
155+
142156
for _, model := range b.Models {
143157
modelName := model.Name
144158

@@ -162,6 +176,11 @@ func mutateHook(b *modelgen.ModelBuild) *modelgen.ModelBuild {
162176
return b
163177
}
164178

179+
// We manually document this one in the template!
180+
if field.Name == "WebhookEvent" {
181+
continue
182+
}
183+
165184
if !strings.Contains(field.Tag, "expr:") {
166185
tags.Set(&structtag.Tag{Key: "expr", Name: strcase.ToSnake(field.Name)})
167186
}
@@ -189,9 +208,15 @@ func mutateHook(b *modelgen.ModelBuild) *modelgen.ModelBuild {
189208
fieldType = filepath.Base(fieldType)
190209
fieldType = strings.Split(fieldType, ".")[1]
191210
fieldType = strings.TrimPrefix(fieldType, "Context")
192-
fieldType = strcase.ToSnake(fieldType)
193211

194-
fieldProperty.IsCustomType = true
212+
// Check if our field is an enum
213+
if enum, ok := enums[fieldType]; ok {
214+
fieldProperty.IsEnum = true
215+
fieldProperty.Enum = enum
216+
}
217+
fieldProperty.IsCustomType = !fieldProperty.IsEnum
218+
219+
fieldType = strcase.ToSnake(fieldType)
195220
}
196221

197222
switch {
@@ -207,8 +232,6 @@ func mutateHook(b *modelgen.ModelBuild) *modelgen.ModelBuild {
207232
modelProperty.AddAttribute(fieldProperty)
208233
} // end expr tag is set
209234

210-
slices.SortFunc(modelProperty.Attributes, sortSlice)
211-
212235
field.Tag = tags.String()
213236
} // end fields loop
214237

@@ -240,6 +263,9 @@ type Property struct {
240263
// Used to show "String list" or "Block list" in the documentation output
241264
IsSlice bool
242265

266+
IsEnum bool
267+
Enum *modelgen.Enum
268+
243269
IsCustomType bool
244270

245271
// Contains any sub-attributes for this Property.
@@ -262,6 +288,10 @@ func (p *Property) AddAttribute(attrs ...*Property) {
262288
}
263289
}
264290

291+
func (p Property) GetHeadline() string {
292+
return fmt.Sprintf("%s `%s` {#%s data-toc-label=%q}", strings.Repeat("#", len(p.getHierarchy())+1), p.BlockName(), p.Name, p.Name)
293+
}
294+
265295
// getHierarchy returns a slice representing all ancestors of this Property
266296
// and its own Property name
267297
func (p Property) getHierarchy() []string {
@@ -293,5 +323,18 @@ func (p *Property) BlockName() string {
293323
}
294324

295325
func sortSlice(i, j *Property) int {
326+
// non-custom types first
327+
if i.IsCustomType && j.IsCustomType {
328+
return 0
329+
}
330+
331+
if i.IsCustomType && !j.IsCustomType {
332+
return 1
333+
}
334+
335+
if !i.IsCustomType && j.IsCustomType {
336+
return -1
337+
}
338+
296339
return cmp.Compare(i.Name, j.Name)
297340
}

0 commit comments

Comments
 (0)