github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/github/yaml/perms.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 ( 20 Permissions struct { 21 Actions string `yaml:"actions,omitempty"` 22 Checks string `yaml:"checks,omitempty"` 23 Contents string `yaml:"contents,omitempty"` 24 Deployments string `yaml:"deployments,omitempty"` 25 IDToken string `yaml:"id-token,omitempty"` 26 Issues string `yaml:"issues,omitempty"` 27 Discussions string `yaml:"discussions,omitempty"` 28 Packages string `yaml:"packages,omitempty"` 29 Pages string `yaml:"pages,omitempty"` 30 PullRequests string `yaml:"pull-requests,omitempty"` 31 RepositoryProjects string `yaml:"repository-projects,omitempty"` 32 SecurityEvents string `yaml:"security-events,omitempty"` 33 Statuses string `yaml:"statuses,omitempty"` 34 ReadAll bool `yaml:"-"` 35 WriteAll bool `yaml:"-"` 36 } 37 38 // temporary structure used to marshal and 39 // unmarshal permissions. 40 permissions struct { 41 Actions string `yaml:"actions,omitempty"` 42 Checks string `yaml:"checks,omitempty"` 43 Contents string `yaml:"contents,omitempty"` 44 Deployments string `yaml:"deployments,omitempty"` 45 IDToken string `yaml:"id-token,omitempty"` 46 Issues string `yaml:"issues,omitempty"` 47 Discussions string `yaml:"discussions,omitempty"` 48 Packages string `yaml:"packages,omitempty"` 49 Pages string `yaml:"pages,omitempty"` 50 PullRequests string `yaml:"pull-requests,omitempty"` 51 RepositoryProjects string `yaml:"repository-projects,omitempty"` 52 SecurityEvents string `yaml:"security-events,omitempty"` 53 Statuses string `yaml:"statuses,omitempty"` 54 ReadAll bool `yaml:"-"` 55 WriteAll bool `yaml:"-"` 56 } 57 ) 58 59 // UnmarshalYAML implements the unmarshal interface for WorkflowTriggers. 60 func (v *Permissions) UnmarshalYAML(unmarshal func(interface{}) error) error { 61 var out1 string 62 var out2 permissions 63 64 if err := unmarshal(&out1); err == nil { 65 switch out1 { 66 case "read-all": 67 v.ReadAll = true 68 case "write-all": 69 v.WriteAll = true 70 } 71 return nil 72 } 73 if err := unmarshal(&out2); err == nil { 74 *v = (Permissions)(out2) 75 return nil 76 } 77 return errors.New("failed to unmarshal permissions") 78 } 79 80 // MarshalYAML implements the marshal interface. 81 func (v *Permissions) MarshalYAML() (interface{}, error) { 82 switch { 83 case v.ReadAll: 84 return "read-all", nil 85 case v.WriteAll: 86 return "write-all", nil 87 default: 88 return permissions(*v), nil 89 } 90 }