From 8db331f9cc60361ab8a5d97645fb18411abf9aa2 Mon Sep 17 00:00:00 2001 From: Andrew Bayer Date: Fri, 17 Jan 2020 14:06:16 -0500 Subject: [PATCH 1/2] chore: Fake PR apis should behave like fake issue APIs Ran into problems with this trying to get Lighthouse updated. Signed-off-by: Andrew Bayer --- scm/driver/fake/data.go | 36 +++++++++++---------- scm/driver/fake/pr.go | 72 +++++++++++++++++++++++++++-------------- 2 files changed, 66 insertions(+), 42 deletions(-) diff --git a/scm/driver/fake/data.go b/scm/driver/fake/data.go index eba05568..2d845286 100644 --- a/scm/driver/fake/data.go +++ b/scm/driver/fake/data.go @@ -3,23 +3,25 @@ package fake import "github.com/jenkins-x/go-scm/scm" type Data struct { - Issues map[int][]*scm.Issue - OrgMembers map[string][]string - Collaborators []string - IssueComments map[int][]*scm.Comment - IssueCommentID int - PullRequests map[int]*scm.PullRequest - PullRequestChanges map[int][]*scm.Change - PullRequestComments map[int][]*scm.Comment - PullRequestLabelsAdded []string - PullRequestLabelsRemoved []string - PullRequestLabelsExisting []string - ReviewID int - Reviews map[int][]*scm.Review - Statuses map[string][]*scm.Status - IssueEvents map[int][]*scm.ListedIssueEvent - Commits map[string]*scm.Commit - TestRef string + Issues map[int][]*scm.Issue + OrgMembers map[string][]string + Collaborators []string + IssueComments map[int][]*scm.Comment + IssueCommentID int + PullRequests map[int]*scm.PullRequest + PullRequestChanges map[int][]*scm.Change + PullRequestComments map[int][]*scm.Comment + PullRequestCommentsAdded []string + PullRequestCommentsDeleted []string + PullRequestLabelsAdded []string + PullRequestLabelsRemoved []string + PullRequestLabelsExisting []string + ReviewID int + Reviews map[int][]*scm.Review + Statuses map[string][]*scm.Status + IssueEvents map[int][]*scm.ListedIssueEvent + Commits map[string]*scm.Commit + TestRef string //All Labels That Exist In The Repo RepoLabelsExisting []string diff --git a/scm/driver/fake/pr.go b/scm/driver/fake/pr.go index f39a521f..fed7426f 100644 --- a/scm/driver/fake/pr.go +++ b/scm/driver/fake/pr.go @@ -3,8 +3,10 @@ package fake import ( "context" "fmt" + "regexp" "github.com/jenkins-x/go-scm/scm" + "k8s.io/apimachinery/pkg/util/sets" ) type pullService struct { @@ -42,33 +44,50 @@ func (s *pullService) ListComments(ctx context.Context, repo string, number int, return append([]*scm.Comment{}, f.PullRequestComments[number]...), nil, nil } -func (s *pullService) ListLabels(context.Context, string, int, scm.ListOptions) ([]*scm.Label, *scm.Response, error) { - r := []*scm.Label{} - for _, l := range s.data.PullRequestLabelsExisting { - r = append(r, &scm.Label{ - Name: l, - }) +func (s *pullService) ListLabels(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Label, *scm.Response, error) { + f := s.data + re := regexp.MustCompile(fmt.Sprintf(`^%s#%d:(.*)$`, repo, number)) + la := []*scm.Label{} + allLabels := sets.NewString(f.PullRequestLabelsExisting...) + allLabels.Insert(f.PullRequestLabelsAdded...) + allLabels.Delete(f.PullRequestLabelsRemoved...) + for _, l := range allLabels.List() { + groups := re.FindStringSubmatch(l) + if groups != nil { + la = append(la, &scm.Label{Name: groups[1]}) + } } - return r, nil, nil + return la, nil, nil } func (s *pullService) AddLabel(ctx context.Context, repo string, number int, label string) (*scm.Response, error) { - s.data.PullRequestLabelsAdded = append(s.data.PullRequestLabelsAdded, label) - s.data.PullRequestLabelsExisting = append(s.data.PullRequestLabelsExisting, label) - return nil, nil + f := s.data + labelString := fmt.Sprintf("%s#%d:%s", repo, number, label) + if sets.NewString(f.PullRequestLabelsAdded...).Has(labelString) { + return nil, fmt.Errorf("cannot add %v to %s/#%d", label, repo, number) + } + if f.RepoLabelsExisting == nil { + f.PullRequestLabelsAdded = append(f.PullRequestLabelsAdded, labelString) + return nil, nil + } + for _, l := range f.RepoLabelsExisting { + if label == l { + f.PullRequestLabelsAdded = append(f.PullRequestLabelsAdded, labelString) + return nil, nil + } + } + return nil, fmt.Errorf("cannot add %v to %s/#%d", label, repo, number) } +// DeleteLabel removes a label func (s *pullService) DeleteLabel(ctx context.Context, repo string, number int, label string) (*scm.Response, error) { - s.data.PullRequestLabelsRemoved = append(s.data.PullRequestLabelsRemoved, label) - - left := []string{} - for _, l := range s.data.PullRequestLabelsExisting { - if l != label { - left = append(left, l) - } + f := s.data + labelString := fmt.Sprintf("%s#%d:%s", repo, number, label) + if !sets.NewString(f.PullRequestLabelsRemoved...).Has(labelString) { + f.PullRequestLabelsRemoved = append(f.PullRequestLabelsRemoved, labelString) + return nil, nil } - s.data.PullRequestLabelsExisting = left - return nil, nil + return nil, fmt.Errorf("cannot remove %v from %s/#%d", label, repo, number) } func (s *pullService) Merge(context.Context, string, int) (*scm.Response, error) { @@ -81,6 +100,7 @@ func (s *pullService) Close(context.Context, string, int) (*scm.Response, error) func (s *pullService) CreateComment(ctx context.Context, repo string, number int, comment *scm.CommentInput) (*scm.Comment, *scm.Response, error) { f := s.data + f.PullRequestCommentsAdded = append(f.PullRequestCommentsAdded, fmt.Sprintf("%s#%d:%s", repo, number, comment.Body)) answer := &scm.Comment{ ID: f.IssueCommentID, Body: comment.Body, @@ -93,12 +113,14 @@ func (s *pullService) CreateComment(ctx context.Context, repo string, number int func (s *pullService) DeleteComment(ctx context.Context, repo string, number int, id int) (*scm.Response, error) { f := s.data - newComments := []*scm.Comment{} - for _, c := range f.PullRequestComments[number] { - if c.ID != id { - newComments = append(newComments, c) + f.PullRequestCommentsDeleted = append(f.PullRequestCommentsDeleted, fmt.Sprintf("%s#%d", repo, id)) + for num, ics := range f.PullRequestComments { + for i, ic := range ics { + if ic.ID == id { + f.PullRequestComments[num] = append(ics[:i], ics[i+1:]...) + return nil, nil + } } } - f.PullRequestComments[number] = newComments - return nil, nil + return nil, fmt.Errorf("could not find issue comment %d", id) } From 92155b2b110975a6dfd9ce164994979d1506ab74 Mon Sep 17 00:00:00 2001 From: Andrew Bayer Date: Fri, 17 Jan 2020 15:01:58 -0500 Subject: [PATCH 2/2] chore: remove some no-longer-relevant OWNERS Signed-off-by: Andrew Bayer --- OWNERS | 6 ------ 1 file changed, 6 deletions(-) diff --git a/OWNERS b/OWNERS index 2d4c3337..f69a535d 100644 --- a/OWNERS +++ b/OWNERS @@ -1,7 +1,6 @@ approvers: - rawlingsj - jstrachan -- rajdavies - ccojocar - garethjevans - pmuir @@ -9,11 +8,9 @@ approvers: - daveconde - hferentschik - abayer -- dwnusbaum - warrenbailey - cagiti - dgozalo -- joseblas - macox - dlorenc - wlynch @@ -21,7 +18,6 @@ approvers: reviewers: - rawlingsj - jstrachan -- rajdavies - ccojocar - garethjevans - pmuir @@ -29,11 +25,9 @@ reviewers: - daveconde - hferentschik - abayer -- dwnusbaum - warrenbailey - cagiti - dgozalo -- joseblas - macox - dlorenc - wlynch