Skip to content

Commit 4accf29

Browse files
committed
Implement support for job inherits keyword
Adds the `inherits` keyword to job definitions, as described on https://docs.gitlab.com/ee/ci/yaml/index.html#inherit. Also examples/inherit-examples.dhall as a test & example.
1 parent 428aec4 commit 4accf29

File tree

10 files changed

+168
-1
lines changed

10 files changed

+168
-1
lines changed

GitLab/Inherit/Type.dhall

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- Several fields either allow you to set a global default (true/false)
2+
-- or a list of keywords (such as variable names). So we define `Choice`
3+
-- to represent those fields.
4+
let Choice = <Defaults : Bool | Keywords : List Text>
5+
6+
-- Implements the `inherit` keyword provided for jobs (https://docs.gitlab.com/ee/ci/yaml/index.html#inherit)
7+
8+
let Inherit = {
9+
`default`: Optional Choice,
10+
variables: Optional Choice,
11+
interruptible: Optional Bool
12+
}
13+
14+
in { Inherit, Choice }

GitLab/Inherit/package.dhall

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
let Types = ./Type.dhall
2+
3+
in { Type = Types.Inherit
4+
, Choice = Types.Choice
5+
, default = None Types.Inherit
6+
, toJSON = ./toJSON.dhall
7+
}

GitLab/Inherit/toJSON.dhall

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
let Prelude = ../Prelude.dhall
2+
3+
let Optional/map = Prelude.Optional.map
4+
5+
let Map = Prelude.Map
6+
7+
let JSON = Prelude.JSON
8+
9+
10+
let dropNones = ../utils/dropNones.dhall
11+
12+
let Types = ./Type.dhall
13+
14+
let choiceToJSON = λ(choice : Types.Choice) merge {
15+
Defaults = λ(b : Bool) JSON.bool b,
16+
Keywords = λ(ks : List Text) -> JSON.array
17+
( Prelude.List.map
18+
Text
19+
JSON.Type
20+
JSON.string
21+
ks
22+
)
23+
} choice
24+
25+
let Inherit/toJSON : Types.Inherit JSON.Type = λ(inherit : Types.Inherit)
26+
let everything = toMap {
27+
`default` = Optional/map Types.Choice JSON.Type choiceToJSON inherit.`default`,
28+
variables = Optional/map Types.Choice JSON.Type choiceToJSON inherit.variables,
29+
interruptible = Optional/map Bool JSON.Type JSON.bool inherit.interruptible
30+
}
31+
32+
in JSON.object (dropNones Text JSON.Type everything)
33+
34+
in Inherit/toJSON

GitLab/Job/Type.dhall

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ let Rule = ../Rule/Type.dhall
1616

1717
let Trigger = ../Trigger/Type.dhall
1818

19+
let Inherit = ../Inherit/package.dhall
20+
1921
in { stage : Optional Text
2022
, image : Optional Image
2123
, variables : Prelude.Map.Type Text Text
@@ -35,4 +37,5 @@ in { stage : Optional Text
3537
, trigger : Optional Trigger
3638
, timeout : Optional Text
3739
, extends : List Text
40+
, inherit : Optional Inherit.Type
3841
}

GitLab/Job/append.dhall

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ let Rule = ../Rule/package.dhall
1919

2020
let Trigger = ../Trigger/package.dhall
2121

22+
let Inherit = ../Inherit/package.dhall
23+
2224
let mergeOptional = ../utils/mergeOptional.dhall
2325

2426
let mergeOptionalRight = ../utils/mergeOptionalRight.dhall
@@ -60,6 +62,7 @@ let append
6062
mergeOptional Trigger.Type Trigger.append a.trigger b.trigger
6163
, timeout = mergeOptionalRight Text a.timeout b.timeout
6264
, extends = a.extends # b.extends
65+
, inherit = mergeOptionalRight Inherit.Type a.inherit b.inherit
6366
}
6467

6568
in append

GitLab/Job/default.dhall

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ let Environment = ../Environment/Type.dhall
1616

1717
let Trigger = ../Trigger/Type.dhall
1818

19+
let Inherit = ../Inherit/package.dhall
20+
1921
in { stage = None Text
2022
, image = None Image
2123
, variables = Prelude.Map.empty Text Text
@@ -35,5 +37,6 @@ in { stage = None Text
3537
, trigger = None Trigger
3638
, timeout = None Text
3739
, extends = [] : List Text
40+
, inherit = None Inherit.Type
3841
}
3942
: ./Type.dhall

GitLab/Job/toJSON.dhall

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ let Optional/map = Prelude.Optional.map
3232

3333
let Optional/toList = Prelude.Optional.toList
3434

35+
let Inherit = ../Inherit/package.dhall
36+
37+
let Inherit/toJSON = Inherit.toJSON
38+
3539
in let Job/toJSON
3640
: Job JSON.Type
3741
= λ(job : Job)
@@ -155,6 +159,8 @@ in let Job/toJSON
155159
job.extends
156160
)
157161
)
162+
, inherit =
163+
Optional/map Inherit.Type JSON.Type Inherit/toJSON job.inherit
158164
}
159165

160166
in JSON.object (dropNones Text JSON.Type everything)

GitLab/package.dhall

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@
1717
, Defaults = ./Defaults/package.dhall
1818
, Service = ./Service/package.dhall
1919
, CachePolicy = ./CachePolicy/package.dhall
20+
, Inherit = ./Inherit/package.dhall
2021
}

Prelude.dhall

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@
2121
-}
2222

2323
env:DHALL_PRELUDE
24-
? https://raw.githubusercontent.com/dhall-lang/dhall-lang/v17.0.0/Prelude/package.dhall sha256:0c04cbe34f1f2d408e8c8b8cb0aa3ff4d5656336910f7e86190a6d14326f966d
24+
? https://raw.githubusercontent.com/dhall-lang/dhall-lang/v17.0.0/Prelude/package.dhall sha256:10db3c919c25e9046833df897a8ffe2701dc390fa0893d958c3430524be5a43e
2525
? https://raw.githubusercontent.com/dhall-lang/dhall-lang/v17.0.0/Prelude/package.dhall

examples/inherit-examples.dhall

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
let GitLab = ../package.dhall
2+
3+
let Prelude = GitLab.Prelude
4+
5+
let renderTop = GitLab.Top.toJSON
6+
7+
-- No inheritance setting
8+
let inherit_absent =
9+
GitLab.Job::{
10+
, stage = Some "build"
11+
, image = Some { name = "alpine:latest", entrypoint = Some [ " " ] }
12+
, script = [ "echo 'Hello World'" ]
13+
, inherit = None GitLab.Inherit.Type
14+
}
15+
16+
-- Use default inheritance setting
17+
let inherit_inherit_default =
18+
GitLab.Job::{
19+
, stage = Some "build"
20+
, image = Some { name = "alpine:latest", entrypoint = Some [ " " ] }
21+
, script = [ "echo 'Hello World'" ]
22+
, inherit = Some {
23+
`default` = Some (GitLab.Inherit.Choice.Defaults True),
24+
variables = None GitLab.Inherit.Choice,
25+
interruptible = None Bool
26+
}
27+
}
28+
29+
-- Use variable default setting
30+
let inherit_variables_default =
31+
GitLab.Job::{
32+
, stage = Some "build"
33+
, image = Some { name = "alpine:latest", entrypoint = Some [ " " ] }
34+
, script = [ "echo 'Hello World'" ]
35+
, inherit = Some {
36+
`default` = None GitLab.Inherit.Choice,
37+
variables = Some (GitLab.Inherit.Choice.Defaults True),
38+
interruptible = None Bool
39+
}
40+
}
41+
42+
-- Use variable list
43+
let inherit_variables_list =
44+
GitLab.Job::{
45+
, stage = Some "build"
46+
, image = Some { name = "alpine:latest", entrypoint = Some [ " " ] }
47+
, script = [ "echo 'Hello World'" ]
48+
, inherit = Some {
49+
`default` = None GitLab.Inherit.Choice,
50+
variables = Some (GitLab.Inherit.Choice.Keywords ["a", "b"]),
51+
interruptible = None Bool
52+
}
53+
}
54+
55+
-- Use variable list
56+
let inherit_interruptible =
57+
GitLab.Job::{
58+
, stage = Some "build"
59+
, image = Some { name = "alpine:latest", entrypoint = Some [ " " ] }
60+
, script = [ "echo 'Hello World'" ]
61+
, inherit = Some {
62+
`default` = None GitLab.Inherit.Choice,
63+
variables = None GitLab.Inherit.Choice,
64+
interruptible = Some True
65+
}
66+
}
67+
68+
let inherit_all_fields =
69+
GitLab.Job::{
70+
, stage = Some "build"
71+
, image = Some { name = "alpine:latest", entrypoint = Some [ " " ] }
72+
, script = [ "echo 'Hello World'" ]
73+
, inherit = Some {
74+
`default` = Some (GitLab.Inherit.Choice.Defaults True),
75+
variables = Some (GitLab.Inherit.Choice.Keywords ["a", "b"]),
76+
interruptible = Some True
77+
}
78+
}
79+
80+
let top = GitLab.Top::{ jobs = toMap {
81+
-- `inherits` field absent
82+
inherit_absent,
83+
-- `inherits` sets `default` to true.
84+
inherit_inherit_default,
85+
-- `inherits` sets `variables` to `true`
86+
inherit_variables_default,
87+
-- `inherits` sets `variables` to a list
88+
inherit_variables_list,
89+
-- `inherits` sets `interruptible`.
90+
inherit_interruptible,
91+
-- `inherits` sets all fields to a value (not necessarily useful but shows coverage).
92+
inherit_all_fields
93+
}
94+
}
95+
96+
in Prelude.JSON.renderYAML (renderTop top)

0 commit comments

Comments
 (0)