Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 1 | package image_repositories |
| 2 | |
| 3 | import ( |
Mohammed Naser | 6d6b2c9 | 2022-09-06 15:24:23 -0400 | [diff] [blame] | 4 | "fmt" |
Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 5 | "io" |
| 6 | |
| 7 | "github.com/go-git/go-billy/v5" |
| 8 | "github.com/goccy/go-yaml" |
| 9 | ) |
| 10 | |
| 11 | type GithubWorkflow struct { |
Mohammed Naser | 6d6b2c9 | 2022-09-06 15:24:23 -0400 | [diff] [blame] | 12 | Name string `yaml:"name"` |
| 13 | Concurrency GithubWorkflowConcurrency `yaml:"concurrency"` |
| 14 | On GithubWorkflowTrigger `yaml:"on"` |
| 15 | Jobs map[string]GithubWorkflowJob `yaml:"jobs"` |
Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | type GithubWorkflowTrigger struct { |
Mohammed Naser | 6d6b2c9 | 2022-09-06 15:24:23 -0400 | [diff] [blame] | 19 | PullRequest GithubWorkflowPullRequest `yaml:"pull_request,omitempty"` |
| 20 | Push GithubWorkflowPush `yaml:"push,omitempty"` |
| 21 | Schedule []GithubWorkflowSchedule `yaml:"schedule,omitempty"` |
| 22 | WorkflowDispatch GithubWorkflowDispatch `yaml:"workflow_dispatch,omitempty"` |
Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | type GithubWorkflowPullRequest struct { |
Mohammed Naser | 6d6b2c9 | 2022-09-06 15:24:23 -0400 | [diff] [blame] | 26 | Types []string `yaml:"types,omitempty"` |
Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | type GithubWorkflowPush struct { |
| 30 | Branches []string `yaml:"branches"` |
| 31 | } |
| 32 | |
Mohammed Naser | 6d6b2c9 | 2022-09-06 15:24:23 -0400 | [diff] [blame] | 33 | type GithubWorkflowSchedule struct { |
| 34 | Cron string `yaml:"cron"` |
| 35 | } |
| 36 | |
| 37 | type GithubWorkflowDispatch struct { |
| 38 | Inputs map[string]GithubWorkflowDispatchInput `yaml:"inputs,omitempty"` |
| 39 | } |
| 40 | |
| 41 | type GithubWorkflowDispatchInput struct { |
| 42 | Description string `yaml:"description"` |
| 43 | Required bool `yaml:"required"` |
| 44 | Default string `yaml:"default"` |
| 45 | } |
| 46 | |
| 47 | type GithubWorkflowConcurrency struct { |
| 48 | Group string `yaml:"group"` |
| 49 | CancelInProgress bool `yaml:"cancel-in-progress"` |
| 50 | } |
| 51 | |
Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 52 | type GithubWorkflowJob struct { |
Oleksandr Kozachenko | d363883 | 2023-10-31 18:54:56 +0100 | [diff] [blame] | 53 | Permissions map[string]string `yaml:"permissions,omitempty"` |
| 54 | RunsOn string `yaml:"runs-on"` |
| 55 | Strategy GithubWorkflowStrategy `yaml:"strategy,omitempty"` |
| 56 | Steps []GithubWorkflowStep `yaml:"steps"` |
Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 57 | } |
| 58 | |
Mohammed Naser | 0c42887 | 2023-09-21 12:59:20 +0000 | [diff] [blame] | 59 | func (j GithubWorkflowJob) DeepCopy() GithubWorkflowJob { |
| 60 | job := GithubWorkflowJob{} |
| 61 | job.RunsOn = j.RunsOn |
| 62 | job.Strategy = j.Strategy.DeepCopy() |
| 63 | |
| 64 | job.Steps = make([]GithubWorkflowStep, len(j.Steps)) |
| 65 | for i, step := range j.Steps { |
| 66 | job.Steps[i] = step.DeepCopy() |
| 67 | } |
| 68 | |
| 69 | return job |
| 70 | } |
| 71 | |
Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 72 | type GithubWorkflowStrategy struct { |
Mohammed Naser | 67abe6a | 2022-12-03 14:24:12 -0500 | [diff] [blame] | 73 | Matrix map[string]interface{} `yaml:"matrix"` |
Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 74 | } |
| 75 | |
Mohammed Naser | 0c42887 | 2023-09-21 12:59:20 +0000 | [diff] [blame] | 76 | func (s *GithubWorkflowStrategy) DeepCopy() GithubWorkflowStrategy { |
| 77 | strategy := *s |
| 78 | strategy.Matrix = make(map[string]interface{}) |
| 79 | for k, v := range s.Matrix { |
| 80 | strategy.Matrix[k] = v |
| 81 | } |
| 82 | |
| 83 | return strategy |
| 84 | } |
| 85 | |
Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 86 | type GithubWorkflowStep struct { |
Mohammed Naser | 2a6c424 | 2022-09-06 15:31:31 -0400 | [diff] [blame] | 87 | Name string `yaml:"name"` |
Oleksandr Kozachenko | ca40dce | 2023-11-03 14:07:54 +0100 | [diff] [blame] | 88 | Id string `yaml:"id,omitempty"` |
Mohammed Naser | 2a6c424 | 2022-09-06 15:31:31 -0400 | [diff] [blame] | 89 | Run string `yaml:"run,omitempty"` |
| 90 | Uses string `yaml:"uses,omitempty"` |
| 91 | If string `yaml:"if,omitempty"` |
| 92 | With map[string]string `yaml:"with,omitempty"` |
| 93 | Environment map[string]string `yaml:"env,omitempty"` |
Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Mohammed Naser | 0c42887 | 2023-09-21 12:59:20 +0000 | [diff] [blame] | 96 | func (s *GithubWorkflowStep) DeepCopy() GithubWorkflowStep { |
| 97 | step := *s |
| 98 | step.With = make(map[string]string) |
| 99 | for k, v := range s.With { |
| 100 | step.With[k] = v |
| 101 | } |
| 102 | |
| 103 | step.Environment = make(map[string]string) |
| 104 | for k, v := range s.Environment { |
| 105 | step.Environment[k] = v |
| 106 | } |
| 107 | |
| 108 | return step |
| 109 | } |
| 110 | |
Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 111 | func (g *GithubWorkflow) Write(wr io.Writer) error { |
| 112 | bytes, err := yaml.Marshal(g) |
| 113 | if err != nil { |
| 114 | return err |
| 115 | } |
| 116 | |
| 117 | _, err = wr.Write(bytes) |
| 118 | return err |
| 119 | } |
| 120 | |
| 121 | func (g *GithubWorkflow) WriteFile(fs billy.Filesystem) error { |
Mohammed Naser | 6d6b2c9 | 2022-09-06 15:24:23 -0400 | [diff] [blame] | 122 | file := fmt.Sprintf(".github/workflows/%s.yml", g.Name) |
| 123 | |
| 124 | f, err := fs.Create(file) |
Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 125 | if err != nil { |
| 126 | return err |
| 127 | } |
| 128 | defer f.Close() |
| 129 | |
| 130 | return g.Write(f) |
| 131 | } |