github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/cmd/hmac/fakeghhook/fakeghhook.go (about) 1 /* 2 Copyright 2020 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package fakeghhook 18 19 import ( 20 "errors" 21 "fmt" 22 23 "github.com/sirupsen/logrus" 24 25 "sigs.k8s.io/prow/pkg/github" 26 ) 27 28 // FakeClient is like client, but fake. 29 type FakeClient struct { 30 // Maps org name to the list of hooks 31 OrgHooks map[string][]github.Hook 32 // Maps repo name to the list of hooks 33 RepoHooks map[string][]github.Hook 34 UserRepoInvitations []github.UserRepoInvitation 35 UserOrgInvitations []github.UserOrgInvitation 36 } 37 38 func (f *FakeClient) ListOrgHooks(org string) ([]github.Hook, error) { 39 return listHooks(f.OrgHooks, org) 40 } 41 42 func (f *FakeClient) ListRepoHooks(org, repo string) ([]github.Hook, error) { 43 logrus.Infof("list hooks for %q", org+"/"+repo) 44 return listHooks(f.RepoHooks, org+"/"+repo) 45 } 46 47 func listHooks(m map[string][]github.Hook, key string) ([]github.Hook, error) { 48 return m[key], nil 49 } 50 51 func (f *FakeClient) CreateOrgHook(org string, req github.HookRequest) (int, error) { 52 if err := createHook(f.OrgHooks, org, req); err != nil { 53 return -1, err 54 } 55 return 0, nil 56 } 57 58 func (f *FakeClient) CreateRepoHook(org, repo string, req github.HookRequest) (int, error) { 59 if err := createHook(f.RepoHooks, org+"/"+repo, req); err != nil { 60 return -1, err 61 } 62 return 0, nil 63 } 64 65 func createHook(m map[string][]github.Hook, key string, req github.HookRequest) error { 66 var hooks []github.Hook 67 if _, ok := m[key]; ok { 68 hooks = m[key] 69 } 70 71 for _, hook := range hooks { 72 if hook.Config.URL == req.Config.URL { 73 return fmt.Errorf("error creating the hook as %q already exists", hook.Config.URL) 74 } 75 } 76 m[key] = append(hooks, github.Hook{ 77 ID: 0, 78 Name: "web", 79 Events: req.Events, 80 Active: *req.Active, 81 Config: *req.Config, 82 }) 83 return nil 84 } 85 86 func (f *FakeClient) EditOrgHook(org string, id int, req github.HookRequest) error { 87 return editHook(f.OrgHooks, org, req) 88 } 89 90 func (f *FakeClient) EditRepoHook(org, repo string, id int, req github.HookRequest) error { 91 return editHook(f.RepoHooks, org+"/"+repo, req) 92 } 93 94 func editHook(m map[string][]github.Hook, key string, req github.HookRequest) error { 95 if _, ok := m[key]; !ok { 96 return fmt.Errorf("no hooks exist for %q, cannot edit", key) 97 } 98 99 exists := false 100 hooks := m[key] 101 for i, hook := range hooks { 102 if hook.Config.URL == req.Config.URL { 103 hooks[i] = github.Hook{ 104 Name: "web", 105 Events: req.Events, 106 Active: *req.Active, 107 Config: *req.Config, 108 } 109 exists = true 110 } 111 } 112 113 if !exists { 114 return fmt.Errorf("no hook for %q, cannot edit", req.Config.URL) 115 } 116 117 m[key] = hooks 118 return nil 119 } 120 121 func (f *FakeClient) DeleteOrgHook(org string, id int, req github.HookRequest) error { 122 return deleteHook(f.OrgHooks, org, req) 123 } 124 125 func (f *FakeClient) DeleteRepoHook(org, repo string, id int, req github.HookRequest) error { 126 return deleteHook(f.RepoHooks, org+"/"+repo, req) 127 } 128 129 func (f *FakeClient) ListCurrentUserRepoInvitations() ([]github.UserRepoInvitation, error) { 130 return f.UserRepoInvitations, nil 131 } 132 133 func (f *FakeClient) AcceptUserRepoInvitation(invitationID int) error { 134 found := -1 135 for idx, iv := range f.UserRepoInvitations { 136 if iv.InvitationID == invitationID { 137 found = idx 138 break 139 } 140 } 141 if found == -1 { 142 return errors.New("invitation doesn't exist") 143 } 144 f.UserRepoInvitations = append(f.UserRepoInvitations[:found], f.UserRepoInvitations[found+1:]...) 145 return nil 146 } 147 148 func (f *FakeClient) ListCurrentUserOrgInvitations() ([]github.UserOrgInvitation, error) { 149 return f.UserOrgInvitations, nil 150 } 151 152 func (f *FakeClient) AcceptUserOrgInvitation(org string) error { 153 found := -1 154 for idx, iv := range f.UserOrgInvitations { 155 if iv.Org.Login == org { 156 found = idx 157 break 158 } 159 } 160 if found == -1 { 161 return errors.New("invitation doesn't exist") 162 } 163 f.UserOrgInvitations = append(f.UserOrgInvitations[:found], f.UserOrgInvitations[found+1:]...) 164 return nil 165 } 166 167 func deleteHook(m map[string][]github.Hook, key string, req github.HookRequest) error { 168 if _, ok := m[key]; !ok { 169 return nil 170 } 171 172 for i, hook := range m[key] { 173 if hook.Config.URL == req.Config.URL { 174 m[key] = append(m[key][:i], m[key][i+1:]...) 175 if len(m[key]) == 0 { 176 delete(m, key) 177 } 178 return nil 179 } 180 } 181 182 return fmt.Errorf("hook for %q does not exist, cannot delete", req.Config.URL) 183 }