github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/github/yaml/on.go (about) 1 // Copyright 2022 Harness, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package yaml 16 17 import "errors" 18 19 type On struct { 20 BranchProtectionRule *Event `yaml:"branch_protection_rule,omitempty"` 21 CheckRun *Event `yaml:"check_run,omitempty"` 22 CheckSuite *Event `yaml:"check_suite,omitempty"` 23 Create *struct{} `yaml:"create,omitempty"` 24 Delete *struct{} `yaml:"delete,omitempty"` 25 Deployment *struct{} `yaml:"deployment,omitempty"` 26 DeploymentStatus *struct{} `yaml:"deployment_status,omitempty"` 27 Discussion *Event `yaml:"discussion,omitempty"` 28 DiscussionComment *Event `yaml:"discussion_comment,omitempty"` 29 Fork *struct{} `yaml:"fork,omitempty"` 30 Gollum *struct{} `yaml:"gollum,omitempty"` 31 IssueComment *Event `yaml:"issue_comment,omitempty"` 32 Issues *Event `yaml:"issues,omitempty"` 33 Label *Event `yaml:"label,omitempty"` 34 Member *Event `yaml:"member,omitempty"` 35 MergeGroup *Event `yaml:"merge_group,omitempty"` 36 Milestone *Event `yaml:"milestone,omitempty"` 37 PageBuild *struct{} `yaml:"page_build,omitempty"` 38 Project *Event `yaml:"project,omitempty"` 39 ProjectCard *Event `yaml:"project_card,omitempty"` 40 ProjectColumn *Event `yaml:"project_column,omitempty"` 41 Public *struct{} `yaml:"public,omitempty"` 42 PullRequest *PullRequest `yaml:"pull_request,omitempty"` 43 PullRequestReview *Event `yaml:"pull_request_review,omitempty"` 44 PullRequestReviewComment *Event `yaml:"pull_request_review_comment,omitempty"` 45 PullRequestTarget *PullRequestTarget `yaml:"pull_request_target,omitempty"` 46 Push *Push `yaml:"push,omitempty"` 47 RegistryPackage *Event `yaml:"registry_package,omitempty"` 48 RepositoryDispatch *Event `yaml:"repository_dispatch,omitempty"` 49 Release *Event `yaml:"release,omitempty"` 50 Schedule *Schedule `yaml:"schedule,omitempty"` 51 Status *struct{} `yaml:"status,omitempty"` 52 Watch *Event `yaml:"watch,omitempty"` 53 WorkflowCall *WorkflowCall `yaml:"workflow_call,omitempty"` 54 WorkflowDispatch *WorkflowDispatch `yaml:"workflow_dispatch,omitempty"` 55 WorkflowRun *WorkflowRun `yaml:"workflow_run,omitempty"` 56 } 57 58 // UnmarshalYAML implements the unmarshal interface for WorkflowTriggers. 59 func (v *On) UnmarshalYAML(unmarshal func(interface{}) error) error { 60 var out1 string 61 var out2 []string 62 63 if err := unmarshal(&out1); err == nil { 64 v.setEvent(out1) 65 return nil 66 } 67 if err := unmarshal(&out2); err == nil { 68 for _, s := range out2 { 69 v.setEvent(s) 70 } 71 return nil 72 } 73 74 out3 := struct { 75 BranchProtectionRule *Event `yaml:"branch_protection_rule,omitempty"` 76 CheckRun *Event `yaml:"check_run,omitempty"` 77 CheckSuite *Event `yaml:"check_suite,omitempty"` 78 Create *struct{} `yaml:"create,omitempty"` 79 Delete *struct{} `yaml:"delete,omitempty"` 80 Deployment *struct{} `yaml:"deployment,omitempty"` 81 DeploymentStatus *struct{} `yaml:"deployment_status,omitempty"` 82 Discussion *Event `yaml:"discussion,omitempty"` 83 DiscussionComment *Event `yaml:"discussion_comment,omitempty"` 84 Fork *struct{} `yaml:"fork,omitempty"` 85 Gollum *struct{} `yaml:"gollum,omitempty"` 86 IssueComment *Event `yaml:"issue_comment,omitempty"` 87 Issues *Event `yaml:"issues,omitempty"` 88 Label *Event `yaml:"label,omitempty"` 89 Member *Event `yaml:"member,omitempty"` 90 MergeGroup *Event `yaml:"merge_group,omitempty"` 91 Milestone *Event `yaml:"milestone,omitempty"` 92 PageBuild *struct{} `yaml:"page_build,omitempty"` 93 Project *Event `yaml:"project,omitempty"` 94 ProjectCard *Event `yaml:"project_card,omitempty"` 95 ProjectColumn *Event `yaml:"project_column,omitempty"` 96 Public *struct{} `yaml:"public,omitempty"` 97 PullRequest *PullRequest `yaml:"pull_request,omitempty"` 98 PullRequestReview *Event `yaml:"pull_request_review,omitempty"` 99 PullRequestReviewComment *Event `yaml:"pull_request_review_comment,omitempty"` 100 PullRequestTarget *PullRequestTarget `yaml:"pull_request_target,omitempty"` 101 Push *Push `yaml:"push,omitempty"` 102 RegistryPackage *Event `yaml:"registry_package,omitempty"` 103 RepositoryDispatch *Event `yaml:"repository_dispatch,omitempty"` 104 Release *Event `yaml:"release,omitempty"` 105 Schedule *Schedule `yaml:"schedule,omitempty"` 106 Status *struct{} `yaml:"status,omitempty"` 107 Watch *Event `yaml:"watch,omitempty"` 108 WorkflowCall *WorkflowCall `yaml:"workflow_call,omitempty"` 109 WorkflowDispatch *WorkflowDispatch `yaml:"workflow_dispatch,omitempty"` 110 WorkflowRun *WorkflowRun `yaml:"workflow_run,omitempty"` 111 }{} 112 if err := unmarshal(&out3); err == nil { 113 *v = out3 114 return nil 115 } 116 117 return errors.New("failed to unmarshal on") 118 } 119 120 func (v *On) setEvent(event string) { 121 switch event { 122 case "branch_protection_rule": 123 v.BranchProtectionRule = new(Event) 124 case "check_run": 125 v.CheckRun = new(Event) 126 case "check_suite": 127 v.CheckSuite = new(Event) 128 case "create": 129 v.Create = new(struct{}) 130 case "delete": 131 v.Delete = new(struct{}) 132 case "deployment": 133 v.Deployment = new(struct{}) 134 case "deployment_status": 135 v.DeploymentStatus = new(struct{}) 136 case "discussion": 137 v.Discussion = new(Event) 138 case "discussion_comment": 139 v.DiscussionComment = new(Event) 140 case "fork": 141 v.Fork = new(struct{}) 142 case "gollum": 143 v.Gollum = new(struct{}) 144 case "issue_comment": 145 v.IssueComment = new(Event) 146 case "issues": 147 v.Issues = new(Event) 148 case "label": 149 v.Label = new(Event) 150 case "member": 151 v.Member = new(Event) 152 case "merge_group": 153 v.MergeGroup = new(Event) 154 case "milestone": 155 v.Milestone = new(Event) 156 case "page_build": 157 v.PageBuild = new(struct{}) 158 case "project": 159 v.Project = new(Event) 160 case "project_card": 161 v.ProjectCard = new(Event) 162 case "project_column": 163 v.ProjectColumn = new(Event) 164 case "public": 165 v.Public = new(struct{}) 166 case "pull_request": 167 v.PullRequest = new(PullRequest) 168 case "pull_request_review": 169 v.PullRequestReview = new(Event) 170 case "pull_request_review_comment": 171 v.PullRequestReviewComment = new(Event) 172 case "pull_request_target": 173 v.PullRequestTarget = new(PullRequestTarget) 174 case "push": 175 v.Push = new(Push) 176 case "registry_package": 177 v.RegistryPackage = new(Event) 178 case "repository_dispatch": 179 v.RepositoryDispatch = new(Event) 180 case "release": 181 v.Release = new(Event) 182 case "schedule": 183 v.Schedule = new(Schedule) 184 case "status": 185 v.Status = new(struct{}) 186 case "watch": 187 v.Watch = new(Event) 188 case "workflow_call": 189 v.WorkflowCall = new(WorkflowCall) 190 case "workflow_dispatch": 191 v.WorkflowDispatch = new(WorkflowDispatch) 192 case "workflow_run": 193 v.WorkflowRun = new(WorkflowRun) 194 } 195 } 196 197 // // ApplyDefaults is a helper funciton to apply default actions 198 // // to events if the event is non-nil but empty. 199 // func (v *On) ApplyDefaults() { 200 // set := func(event *Event, actions []string) { 201 // if event != nil && len(event.Types) == 0 { 202 // event.Types = append(event.Types, actions...) 203 // } 204 // } 205 206 // set(v.BranchProtectionRule, []string{"created", "edited", "deleted"}) 207 // set(v.CheckRun, []string{"created", "rerequested", "completed", "requested_action"}) 208 // set(v.CheckSuite, []string{"completed", "requested", "rerequested"}) 209 // set(v.Discussion, []string{"created", "edited", "deleted", "transferred", "pinned", "unpinned", "labeled", "unlabeled", "locked", "unlocked", "category_changed", "answered", "unanswered"}) 210 // set(v.DiscussionComment, []string{"created", "edited", "deleted"}) 211 // set(v.IssueComment, []string{"created", "edited", "deleted"}) 212 // set(v.Issues, []string{"opened", "edited", "deleted", "transferred", "pinned", "unpinned", "closed", "reopened", "assigned", "unassigned", "labeled", "unlabeled", "locked", "unlocked", "milestoned", "demilestoned"}) 213 // set(v.Label, []string{"created", "edited", "deleted"}) 214 // set(v.Member, []string{"added", "edited", "deleted"}) 215 // set(v.MergeGroup, []string{"checks_requested"}) 216 // set(v.Milestone, []string{"created", "closed", "opened", "edited", "deleted"}) 217 // set(v.Project, []string{"created", "updated", "closed", "reopened", "edited", "deleted"}) 218 // set(v.ProjectCard, []string{"created", "moved", "converted", "edited", "deleted"}) 219 // set(v.ProjectColumn, []string{"created", "updated", "moved", "deleted"}) 220 // set(v.PullRequestReview, []string{"submitted", "edited", "dismissed"}) 221 // set(v.PullRequestReviewComment, []string{"created", "edited", "deleted"}) 222 // set(v.RegistryPackage, []string{"published", "updated"}) 223 // set(v.Release, []string{"published", "unpublished", "created", "edited", "deleted", "prereleased", "released"}) 224 // set(v.Watch, []string{"started"}) 225 226 // if v.PullRequest != nil && len(v.PullRequest.Types) != 0 { 227 // v.PullRequest.Types = []string{"opened", "synchronize", "reopened"} 228 // } 229 // if v.PullRequestTarget != nil && len(v.PullRequestTarget.Types) != 0 { 230 // v.PullRequestTarget.Types = []string{"opened", "synchronize", "reopened"} 231 // } 232 // }