sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/phony/phony.go (about) 1 /* 2 Copyright 2017 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 phony 18 19 import ( 20 "bytes" 21 "fmt" 22 "io" 23 "net/http" 24 25 "sigs.k8s.io/prow/pkg/github" 26 ) 27 28 // SendHook sends a GitHub event of type eventType to the provided address. 29 func SendHook(address, eventType string, payload, hmac []byte) error { 30 req, err := http.NewRequest(http.MethodPost, address, bytes.NewBuffer(payload)) 31 if err != nil { 32 return err 33 } 34 req.Header.Set("X-GitHub-Event", eventType) 35 req.Header.Set("X-GitHub-Delivery", "GUID") 36 req.Header.Set("X-Hub-Signature", github.PayloadSignature(payload, hmac)) 37 req.Header.Set("content-type", "application/json") 38 39 c := &http.Client{} 40 resp, err := c.Do(req) 41 if err != nil { 42 return err 43 } 44 defer resp.Body.Close() 45 rb, err := io.ReadAll(resp.Body) 46 if err != nil { 47 return err 48 } 49 if resp.StatusCode != 200 { 50 return fmt.Errorf("response from hook has status %d and body %s", resp.StatusCode, string(bytes.TrimSpace(rb))) 51 } 52 return nil 53 }