forked from shykes/gha
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkflow.go
More file actions
144 lines (125 loc) · 6.13 KB
/
workflow.go
File metadata and controls
144 lines (125 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"encoding/json"
"github.com/shykes/gha/internal/dagger"
"gopkg.in/yaml.v3"
)
const (
genHeader = "# This file was generated. See https://daggerverse.dev/mod/github.com/shykes/gha"
)
type Workflow struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
On WorkflowTriggers `json:"on" yaml:"on"`
Concurrency *WorkflowConcurrency `json:"concurrency,omitempty" yaml:"concurrency,omitempty"`
Jobs map[string]Job `json:"jobs" yaml:"jobs"`
Env map[string]string `json:"env,omitempty" yaml:"env,omitempty"`
}
// Generate an overlay config directory for this workflow
func (w Workflow) Config(
// Filename of the workflow file under .github/workflows/
filename string,
// Encode the workflow as JSON, which is valid YAML
asJson bool,
) *dagger.Directory {
var (
contents []byte
err error
)
if asJson {
contents, err = json.MarshalIndent(w, "", " ")
} else {
contents, err = yaml.Marshal(w)
}
if err != nil {
panic(err)
}
return dag.
Directory().
WithNewFile(".github/workflows/"+filename, genHeader+"\n"+string(contents))
}
type WorkflowConcurrency struct {
Group string `json:"group,omitempty" yaml:"group,omitempty"`
CancelInProgress bool `json:"cancel-in-progress,omitempty" yaml:"cancel-in-progress,omitempty"`
}
type WorkflowTriggers struct {
Push *PushEvent `json:"push,omitempty" yaml:"push,omitempty"`
PullRequest *PullRequestEvent `json:"pull_request,omitempty" yaml:"pull_request,omitempty"`
Schedule []ScheduledEvent `json:"schedule,omitempty" yaml:"schedule,omitempty"`
WorkflowDispatch *WorkflowDispatchEvent `json:"workflow_dispatch,omitempty" yaml:"workflow_dispatch,omitempty"`
IssueComment *IssueCommentEvent `json:"issue_comment,omitempty" yaml:"issue_comment,omitempty"`
}
type PushEvent struct {
Branches []string `json:"branches,omitempty" yaml:"branches,omitempty"`
Tags []string `json:"tags,omitempty" yaml:"tags,omitempty"`
Paths []string `json:"paths,omitempty" yaml:"paths,omitempty"`
}
type PullRequestEvent struct {
Types []string `json:"types,omitempty" yaml:"types,omitempty"`
Branches []string `json:"branches,omitempty" yaml:"branches,omitempty"`
Paths []string `json:"paths,omitempty" yaml:"paths,omitempty"`
}
type ScheduledEvent struct {
Cron string `json:"cron" yaml:"cron"`
}
type WorkflowDispatchEvent struct {
// FIXME: The Dagger API can't serialize maps
// Inputs map[string]DispatchInput `json:"inputs,omitempty" yaml:"inputs,omitempty"`
}
type IssueCommentEvent struct {
Types []string `json:"types,omitempty" yaml:"types,omitempty"`
}
type DispatchInput struct {
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Required bool `json:"required,omitempty" yaml:"required,omitempty"`
Default string `json:"default,omitempty" yaml:"default,omitempty"`
}
type Job struct {
RunsOn []string `json:"runs-on" yaml:"runs-on"`
Permissions *JobPermissions `json:"permissions,omitempty" yaml:"permissions,omitempty"`
Name string `json:"name" yaml:"name"`
Needs []string `json:"needs,omitempty" yaml:"needs,omitempty"`
Steps []JobStep `json:"steps" yaml:"steps"`
Env map[string]string `json:"env,omitempty" yaml:"env,omitempty"`
Strategy *Strategy `json:"strategy,omitempty" yaml:"strategy,omitempty"`
TimeoutMinutes int `json:"timeout-minutes,omitempty" yaml:"timeout-minutes,omitempty"`
Outputs map[string]string `json:"outputs,omitempty" yaml:"outputs,omitempty"`
}
type JobStep struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
ID string `json:"id,omitempty" yaml:"id,omitempty"`
Uses string `json:"uses,omitempty" yaml:"uses,omitempty"`
Run string `json:"run,omitempty" yaml:"run,omitempty"`
With map[string]string `json:"with,omitempty" yaml:"with,omitempty"`
Env map[string]string `json:"env,omitempty" yaml:"env,omitempty"`
TimeoutMinutes int `json:"timeout-minutes,omitempty" yaml:"timeout-minutes,omitempty"`
Shell string `json:"shell,omitempty" yaml:"shell,omitempty"`
// Other step-specific fields can be added here...
}
type Strategy struct {
Matrix map[string][]string `json:"matrix,omitempty" yaml:"matrix,omitempty"`
MaxParallel int `json:"max-parallel,omitempty" yaml:"max-parallel,omitempty"`
FailFast bool `json:"fail-fast,omitempty" yaml:"fail-fast,omitempty"`
}
// PermissionLevel represents the possible levels of permissions in a job.
type PermissionLevel string
const (
PermissionRead PermissionLevel = "read"
PermissionWrite PermissionLevel = "write"
PermissionNone PermissionLevel = "none"
)
// Permissions defines the permission levels for various scopes in a job.
type JobPermissions struct {
Contents PermissionLevel `json:"contents,omitempty" yaml:"contents,omitempty"`
Issues PermissionLevel `json:"issues,omitempty" yaml:"issues,omitempty"`
Actions PermissionLevel `json:"actions,omitempty" yaml:"actions,omitempty"`
Packages PermissionLevel `json:"packages,omitempty" yaml:"packages,omitempty"`
Deployments PermissionLevel `json:"deployments,omitempty" yaml:"deployments,omitempty"`
PullRequests PermissionLevel `json:"pull-requests,omitempty" yaml:"pull-requests,omitempty"`
Pages PermissionLevel `json:"pages,omitempty" yaml:"pages,omitempty"`
IdToken PermissionLevel `json:"id-token,omitempty" yaml:"id-token,omitempty"`
RepositoryProjects PermissionLevel `json:"repository-projects,omitempty" yaml:"repository-projects,omitempty"`
Statuses PermissionLevel `json:"statuses,omitempty" yaml:"statuses,omitempty"`
Metadata PermissionLevel `json:"metadata,omitempty" yaml:"metadata,omitempty"`
Checks PermissionLevel `json:"checks,omitempty" yaml:"checks,omitempty"`
Discussions PermissionLevel `json:"discussions,omitempty" yaml:"discussions,omitempty"`
}