github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/github/fakegithub/fakegithub.go (about) 1 /* 2 Copyright 2016 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 fakegithub 18 19 import ( 20 "fmt" 21 "regexp" 22 23 "k8s.io/test-infra/prow/github" 24 ) 25 26 type FakeClient struct { 27 Issues []github.Issue 28 OrgMembers []string 29 IssueComments map[int][]github.IssueComment 30 IssueCommentID int 31 PullRequests map[int]*github.PullRequest 32 PullRequestChanges map[int][]github.PullRequestChange 33 CombinedStatuses map[string]*github.CombinedStatus 34 35 //All Labels That Exist In The Repo 36 ExistingLabels []string 37 // org/repo#number:label 38 LabelsAdded []string 39 LabelsRemoved []string 40 41 // org/repo#number:body 42 IssueCommentsAdded []string 43 // org/repo#issuecommentid 44 IssueCommentsDeleted []string 45 46 // org/repo#issuecommentid:reaction 47 IssueReactionsAdded []string 48 CommentReactionsAdded []string 49 50 // org/repo#number:assignee 51 AssigneesAdded []string 52 53 // Fake remote git storage. File name are keys 54 // and values map SHA to content 55 RemoteFiles map[string]map[string]string 56 } 57 58 func (f *FakeClient) BotName() (string, error) { 59 return "k8s-ci-robot", nil 60 } 61 62 func (f *FakeClient) IsMember(org, user string) (bool, error) { 63 for _, m := range f.OrgMembers { 64 if m == user { 65 return true, nil 66 } 67 } 68 return false, nil 69 } 70 71 func (f *FakeClient) ListIssueComments(owner, repo string, number int) ([]github.IssueComment, error) { 72 return append([]github.IssueComment{}, f.IssueComments[number]...), nil 73 } 74 75 func (f *FakeClient) CreateComment(owner, repo string, number int, comment string) error { 76 f.IssueCommentsAdded = append(f.IssueCommentsAdded, fmt.Sprintf("%s/%s#%d:%s", owner, repo, number, comment)) 77 f.IssueComments[number] = append(f.IssueComments[number], github.IssueComment{ 78 ID: f.IssueCommentID, 79 Body: comment, 80 }) 81 f.IssueCommentID++ 82 return nil 83 } 84 85 func (f *FakeClient) CreateCommentReaction(org, repo string, ID int, reaction string) error { 86 f.CommentReactionsAdded = append(f.CommentReactionsAdded, fmt.Sprintf("%s/%s#%d:%s", org, repo, ID, reaction)) 87 return nil 88 } 89 90 func (f *FakeClient) CreateIssueReaction(org, repo string, ID int, reaction string) error { 91 f.IssueReactionsAdded = append(f.IssueReactionsAdded, fmt.Sprintf("%s/%s#%d:%s", org, repo, ID, reaction)) 92 return nil 93 } 94 95 func (f *FakeClient) DeleteComment(owner, repo string, ID int) error { 96 f.IssueCommentsDeleted = append(f.IssueCommentsDeleted, fmt.Sprintf("%s/%s#%d", owner, repo, ID)) 97 for num, ics := range f.IssueComments { 98 for i, ic := range ics { 99 if ic.ID == ID { 100 f.IssueComments[num] = append(ics[:i], ics[i+1:]...) 101 return nil 102 } 103 } 104 } 105 return fmt.Errorf("could not find issue comment %d", ID) 106 } 107 108 func (f *FakeClient) DeleteStaleComments(org, repo string, number int, comments []github.IssueComment, isStale func(github.IssueComment) bool) error { 109 if comments == nil { 110 comments, _ = f.ListIssueComments(org, repo, number) 111 } 112 for _, comment := range comments { 113 if isStale(comment) { 114 if err := f.DeleteComment(org, repo, comment.ID); err != nil { 115 return fmt.Errorf("failed to delete stale comment with ID '%d'", comment.ID) 116 } 117 } 118 } 119 return nil 120 } 121 122 func (f *FakeClient) GetPullRequest(owner, repo string, number int) (*github.PullRequest, error) { 123 return f.PullRequests[number], nil 124 } 125 126 func (f *FakeClient) GetPullRequestChanges(org, repo string, number int) ([]github.PullRequestChange, error) { 127 return f.PullRequestChanges[number], nil 128 } 129 130 func (f *FakeClient) GetRef(owner, repo, ref string) (string, error) { 131 return "abcde", nil 132 } 133 134 func (f *FakeClient) CreateStatus(owner, repo, ref string, s github.Status) error { 135 return nil 136 } 137 138 func (f *FakeClient) GetCombinedStatus(owner, repo, ref string) (*github.CombinedStatus, error) { 139 return f.CombinedStatuses[ref], nil 140 } 141 142 func (f *FakeClient) GetRepoLabels(owner, repo string) ([]github.Label, error) { 143 la := []github.Label{} 144 for _, l := range f.ExistingLabels { 145 la = append(la, github.Label{Name: l}) 146 } 147 return la, nil 148 } 149 150 func (f *FakeClient) GetIssueLabels(owner, repo string, number int) ([]github.Label, error) { 151 // Only labels added to an issue are considered. Removals are ignored by this fake. 152 re := regexp.MustCompile(fmt.Sprintf(`^%s/%s#%d:(.*)$`, owner, repo, number)) 153 la := []github.Label{} 154 for _, l := range f.LabelsAdded { 155 groups := re.FindStringSubmatch(l) 156 if groups != nil { 157 la = append(la, github.Label{Name: groups[1]}) 158 } 159 } 160 return la, nil 161 } 162 163 func (f *FakeClient) AddLabel(owner, repo string, number int, label string) error { 164 if f.ExistingLabels == nil { 165 f.LabelsAdded = append(f.LabelsAdded, fmt.Sprintf("%s/%s#%d:%s", owner, repo, number, label)) 166 return nil 167 } 168 for _, l := range f.ExistingLabels { 169 if label == l { 170 f.LabelsAdded = append(f.LabelsAdded, fmt.Sprintf("%s/%s#%d:%s", owner, repo, number, label)) 171 return nil 172 } 173 } 174 return fmt.Errorf("cannot add %v to %s/%s/#%d", label, owner, repo, number) 175 } 176 177 func (f *FakeClient) RemoveLabel(owner, repo string, number int, label string) error { 178 f.LabelsRemoved = append(f.LabelsRemoved, fmt.Sprintf("%s/%s#%d:%s", owner, repo, number, label)) 179 return nil 180 } 181 182 // FindIssues returns f.Issues 183 func (f *FakeClient) FindIssues(query, sort string, asc bool) ([]github.Issue, error) { 184 return f.Issues, nil 185 } 186 187 func (f *FakeClient) AssignIssue(owner, repo string, number int, assignees []string) error { 188 var m github.MissingUsers 189 for _, a := range assignees { 190 if a == "not-in-the-org" { 191 m.Users = append(m.Users, a) 192 continue 193 } 194 f.AssigneesAdded = append(f.AssigneesAdded, fmt.Sprintf("%s/%s#%d:%s", owner, repo, number, a)) 195 } 196 if m.Users == nil { 197 return nil 198 } 199 return m 200 } 201 202 func (f *FakeClient) GetFile(org, repo, file, commit string) ([]byte, error) { 203 contents, ok := f.RemoteFiles[file] 204 if !ok { 205 return nil, fmt.Errorf("could not find file %s", file) 206 } 207 if commit == "" { 208 if master, ok := contents["master"]; ok { 209 return []byte(master), nil 210 } 211 212 return nil, fmt.Errorf("could not find file %s in master", file) 213 } 214 215 if content, ok := contents[commit]; ok { 216 return []byte(content), nil 217 } 218 219 return nil, fmt.Errorf("could not find file %s with ref %s", file, commit) 220 } 221 222 // ListTeamMembers return a fake team with a single "sig-lead" Github teammember 223 func (f *FakeClient) ListTeamMembers(teamID int) ([]github.TeamMember, error) { 224 return []github.TeamMember{{Login: "sig-lead"}}, nil 225 }