github.com/bndr/gojenkins@v1.1.0/fingerprint.go (about) 1 // Copyright 2015 Vadim Kravcenko 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 // not use this file except in compliance with the License. You may obtain 5 // 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, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations 13 // under the License. 14 15 package gojenkins 16 17 import ( 18 "context" 19 "errors" 20 "fmt" 21 ) 22 23 type FingerPrint struct { 24 Jenkins *Jenkins 25 Base string 26 Id string 27 Raw *FingerPrintResponse 28 } 29 30 type FingerPrintResponse struct { 31 FileName string `json:"fileName"` 32 Hash string `json:"hash"` 33 Original struct { 34 Name string 35 Number int64 36 } `json:"original"` 37 Timestamp int64 `json:"timestamp"` 38 Usage []struct { 39 Name string `json:"name"` 40 Ranges struct { 41 Ranges []struct { 42 End int64 `json:"end"` 43 Start int64 `json:"start"` 44 } `json:"ranges"` 45 } `json:"ranges"` 46 } `json:"usage"` 47 } 48 49 func (f FingerPrint) Valid(ctx context.Context) (bool, error) { 50 status, err := f.Poll(ctx) 51 52 if err != nil { 53 return false, err 54 } 55 56 if status != 200 || f.Raw.Hash != f.Id { 57 return false, fmt.Errorf("Jenkins says %s is Invalid or the Status is unknown", f.Id) 58 } 59 return true, nil 60 } 61 62 func (f FingerPrint) ValidateForBuild(ctx context.Context, filename string, build *Build) (bool, error) { 63 valid, err := f.Valid(ctx) 64 if err != nil { 65 return false, err 66 } 67 68 if valid { 69 return true, nil 70 } 71 72 if f.Raw.FileName != filename { 73 return false, errors.New("Filename does not Match") 74 } 75 if build != nil && f.Raw.Original.Name == build.Job.GetName() && 76 f.Raw.Original.Number == build.GetBuildNumber() { 77 return true, nil 78 } 79 return false, nil 80 } 81 82 func (f FingerPrint) GetInfo(ctx context.Context) (*FingerPrintResponse, error) { 83 _, err := f.Poll(ctx) 84 if err != nil { 85 return nil, err 86 } 87 return f.Raw, nil 88 } 89 90 func (f FingerPrint) Poll(ctx context.Context) (int, error) { 91 response, err := f.Jenkins.Requester.GetJSON(ctx, f.Base+f.Id, f.Raw, nil) 92 if err != nil { 93 return 0, err 94 } 95 return response.StatusCode, nil 96 }