Skip to content

Commit

Permalink
Various improvements (#15)
Browse files Browse the repository at this point in the history
* Add Dynamic to matrix build
* Support Listing for Job.runs-on
* Update env. to support Boolean and Int
* Add Job.needs
* Add Step.shell
* Add branches(-ignore) and path(-ignore) to triggers
* Add timeout-minutes
* Use Number for env variable
* Add Job.environment
* Change default values
  • Loading branch information
StefMa authored Sep 19, 2024
1 parent 33e334e commit dd9a971
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 13 deletions.
50 changes: 41 additions & 9 deletions GitHubAction.pkl
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ class Public extends Trigger
/// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
class PullRequest extends Trigger {
types: Listing<PullRequestType>?
branches: Listing<String>?
`branches-ignore`: Listing<String>?
paths: Listing<String>?
`paths-ignore`: Listing<String>?
}
typealias PullRequestType =
"assigned"
Expand Down Expand Up @@ -332,6 +336,10 @@ typealias PullRequestReviewCommentType =
/// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target
class PullRequestTarget extends Trigger {
types: Listing<PullRequestTargetType>?
branches: Listing<String>?
`branches-ignore`: Listing<String>?
paths: Listing<String>?
`paths-ignore`: Listing<String>?
}
typealias PullRequestTargetType =
"assigned"
Expand Down Expand Up @@ -475,6 +483,8 @@ typealias WorkflowInputType = "boolean"|"string"|"choice"|"environment"|"number"
class WorkflowRun extends Trigger {
types: Listing<WorkflowRunType>?
workflows: Listing<String>(length > 0)
branches: Listing<String>?
`branches-ignore`: Listing<String>?
}
typealias WorkflowRunType =
"requested"
Expand Down Expand Up @@ -521,7 +531,7 @@ class On {
}

// Environment Variables
typealias EnvironmentVariables = Mapping<String, String>
typealias EnvironmentVariables = Mapping<String, String|Boolean|Number>

// Permissions
class Permissions {
Expand Down Expand Up @@ -550,11 +560,14 @@ class Concurrency {
// Jobs
class Job {
name: String?
`runs-on`: Machine|String
`runs-on`: *String|Machine|Listing<*String|Machine>
`if`: String?
needs: (String|*Listing<String>)?
`timeout-minutes`: Number?
env: EnvironmentVariables?
concurrency: Concurrency?
strategy: Strategy?
environment: Environment?
steps: Listing<Step>(stepsHasOnlyRunOrUses(this))
}

Expand All @@ -572,24 +585,35 @@ class WindowsLatest extends Machine {
name = "windows-latest"
}

// Strategy, part of Jobs
class Strategy {
matrix: Mapping<String, Listing<String|*Dynamic>>
`fail-fast`: Boolean?
`max-parallel`: (Number|String)?
}

// Environment, part of Jobs
class Environment {
name: String
url: String?
}

// Step, part of Jobs
class Step {
name: String?
id: String?
`if`: String?
`timeout-minutes`: Number?
env: EnvironmentVariables?
`working-directory`: String?
shell: Shell?
run: String?
uses: String?
with: Mapping<String, String|Number|Boolean>?
}

// Strategy, part of Jobs
class Strategy {
matrix: Mapping<String, Listing<String>>
`fail-fast`: Boolean?
`max-parallel`: (Number|String)?
}
// Shell, part of Steps
typealias Shell = "pwsh" | "bash" | "sh" | "cmd" | "powershell" | "python"

// Templating

Expand Down Expand Up @@ -648,7 +672,8 @@ output {
text = "# Do not modify!\n# This file was generated from a template using https://github.com/StefMa/pkl-gha\n\n\(super.text)"
renderer = new YamlRenderer {
converters {
["runs-on"] = (runsOn: String|Machine) -> if (runsOn is Machine) runsOn.name else runsOn
["runs-on"] = (runsOn: String|Machine|Listing<String|Machine>) ->
if (runsOn is Listing<String|Machine>) convertMachinesToString(runsOn) else convertMachineToString(runsOn)
["schedule"] = (schedule: Schedule?) -> schedule.ifNonNull((_) ->
schedule.cron.toList().map((cr) -> Map("cron", new RenderDirective { text = " " + jsonRenderer.renderValue(cr) }))
)
Expand All @@ -661,3 +686,10 @@ const local function stepsHasOnlyRunOrUses(steps: Listing<Step>): Boolean = step
.every((step) -> !(containsRunAndUses(step) || containsNeitherRunNorUses(step)))
const local function containsRunAndUses(step: Step): Boolean = step.run != null && step.uses != null
const local function containsNeitherRunNorUses(step: Step): Boolean = step.run == null && step.uses == null

const local function convertMachinesToString(runsOn: Listing<String|Machine>): Listing<String> = runsOn
.toList()
.map((type) -> convertMachineToString(type))
.toListing()
const local function convertMachineToString(machine: String|Machine): String =
if (machine is Machine) machine.name else machine
16 changes: 16 additions & 0 deletions examples/MatrixTest.pkl
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,35 @@ on {
jobs {
["matrixTest"] {
`runs-on` = "ubuntu-latest"
`timeout-minutes` = 5.5
strategy {
matrix {
["os"] = new {
"ubuntu-latest"
"macos-latest"
"windows-latest"
}
["soemthingElse"] = new {
new {
os = "ubuntu-latest"
runtime = "linux-x64"
}
new {
os = "windows-latest"
runtime = "windows-x64"
}
new {
os = "macos-latest"
runtime = "macos-x64"
}
}
}
}
steps {
new {
name = "Checkout"
uses = "actions/checkout@v4"
`timeout-minutes` = 2.3
}
new {
name = "Run on ${{ matrix.os }}"
Expand Down
16 changes: 15 additions & 1 deletion examples/TestWorkflow.pkl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ permissions {

env {
["PR_NUMBER"] = "${{github.event.number}}"
["SOME_INT"] = 3
["SOME_FLOAT"] = 3.2
["SOME_BOOL"] = true
}

concurrency {
Expand All @@ -52,6 +55,13 @@ jobs {
["hello_dependabot"] {
`runs-on` = new MacOsLatest {}
`if` = "${{ github.actor == 'dependabot[bot]' }}"
needs {
"first_job"
"second_job"
}
environment {
name = "production"
}
steps {
new {
name = "Checkout"
Expand All @@ -69,7 +79,11 @@ jobs {
}
}
["print"] {
`runs-on` = new UbuntuLatest{}
`runs-on` = new Listing {
"macos-latest"
new WindowsLatest {}
}
needs = "hello_dependabot"
steps {
new {
name = "Hello World"
Expand Down
1 change: 1 addition & 0 deletions examples/UpdateWikiReadme.pkl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs {
}
new {
run = "go run wiki/generateWikiToc.go"
shell = "bash"
}
new {
name = "Commit changes"
Expand Down
27 changes: 24 additions & 3 deletions tests/GitHubAction.pkl-expected.pcf
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ examples {
default: Mona the Octocat
env:
PR_NUMBER: ${{github.event.number}}
SOME_INT: 3
SOME_FLOAT: 3.2
SOME_BOOL: true
concurrency:
group: example-group
cancel-in-progress: true
Expand All @@ -39,6 +42,11 @@ examples {
hello_dependabot:
runs-on: macos-latest
if: ${{ github.actor == 'dependabot[bot]' }}
needs:
- first_job
- second_job
environment:
name: production
steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -49,7 +57,10 @@ examples {
working-directory: src/
run: echo Dependabot!
print:
runs-on: ubuntu-latest
runs-on:
- macos-latest
- windows-latest
needs: hello_dependabot
steps:
- name: Hello World
run: echo Hello World
Expand Down Expand Up @@ -145,7 +156,8 @@ examples {
- uses: actions/setup-go@v5
with:
go-version: 1.17.7
- run: go run wiki/generateWikiToc.go
- shell: bash
run: go run wiki/generateWikiToc.go
- name: Commit changes
run: |-
git config --global user.email "[email protected]"
Expand All @@ -169,18 +181,27 @@ examples {
jobs:
matrixTest:
runs-on: ubuntu-latest
timeout-minutes: 5.5
strategy:
matrix:
os:
- ubuntu-latest
- macos-latest
- windows-latest
soemthingElse:
- os: ubuntu-latest
runtime: linux-x64
- os: windows-latest
runtime: windows-x64
- os: macos-latest
runtime: macos-x64
steps:
- name: Checkout
timeout-minutes: 2.3
uses: actions/checkout@v4
- name: Run on ${{ matrix.os }}
run: echo Hello World

"""
}
}

0 comments on commit dd9a971

Please sign in to comment.