Skip to content

Commit

Permalink
add ListedIssueEvent API
Browse files Browse the repository at this point in the history
  • Loading branch information
jstrachan committed Jul 8, 2019
1 parent 284f1ac commit 57883e8
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 12 deletions.
4 changes: 4 additions & 0 deletions scm/driver/bitbucket/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ type issueService struct {
client *wrapper
}

func (s *issueService) ListEvents(context.Context, string, int, scm.ListOptions) ([]*scm.ListedIssueEvent, *scm.Response, error) {
panic("implement me")
}

func (s *issueService) ListLabels(context.Context, string, int, scm.ListOptions) ([]*scm.Label, *scm.Response, error) {
panic("implement me")
}
Expand Down
10 changes: 5 additions & 5 deletions scm/driver/fake/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ type Data struct {
PullRequestChanges map[int][]*scm.Change
PullRequestComments map[int][]*scm.Comment
ReviewID int
Reviews map[int][]scm.Review
Reviews map[int][]*scm.Review
// TODO
//CombinedStatuses map[string]*github.CombinedStatus
CreatedStatuses map[string][]scm.Status
// TODO
// IssueEvents map[int][]github.ListedIssueEvent
Commits map[string]scm.CommitTree
IssueEvents map[int][]*scm.ListedIssueEvent
Commits map[string]scm.CommitTree

//All Labels That Exist In The Repo
RepoLabelsExisting []string
Expand Down Expand Up @@ -65,8 +64,9 @@ func NewData() *Data {
PullRequests: map[int]*scm.PullRequest{},
PullRequestChanges: map[int][]*scm.Change{},
PullRequestComments: map[int][]*scm.Comment{},
Reviews: map[int][]scm.Review{},
Reviews: map[int][]*scm.Review{},
CreatedStatuses: map[string][]scm.Status{},
IssueEvents: map[int][]*scm.ListedIssueEvent{},
Commits: map[string]scm.CommitTree{},
MilestoneMap: map[string]int{},
CommitMap: map[string][]scm.Commit{},
Expand Down
4 changes: 2 additions & 2 deletions scm/driver/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ func NewDefault() (*scm.Client, *Data) {

client.Repositories = &repositoryService{client: client, data: data}
client.Issues = &issueService{client: client, data: data}
client.PullRequests = &pullService{client: client, data: data}
client.Reviews = &reviewService{client: client, data: data}

// TODO
/*
client.Contents = &contentService{client}
client.Git = &gitService{client}
client.Organizations = &organizationService{client}
client.PullRequests = &pullService{&issueService{client}}
client.Reviews = &reviewService{client}
client.Users = &userService{client}
client.Webhooks = &webhookService{client}
*/
Expand Down
26 changes: 21 additions & 5 deletions scm/driver/fake/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ type issueService struct {

const botName = "k8s-ci-robot"

func (s *issueService) ListEvents(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.ListedIssueEvent, *scm.Response, error) {
f := s.data
return append([]*scm.ListedIssueEvent{}, f.IssueEvents[number]...), nil, nil
}

func (s *issueService) Find(ctx context.Context, repo string, number int) (*scm.Issue, *scm.Response, error) {
f := s.data
for _, slice := range f.Issues {
Expand Down Expand Up @@ -122,8 +127,9 @@ func (s *issueService) List(context.Context, string, scm.IssueListOptions) ([]*s
panic("implement me")
}

func (s *issueService) ListComments(context.Context, string, int, scm.ListOptions) ([]*scm.Comment, *scm.Response, error) {
panic("implement me")
func (s *issueService) ListComments(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Comment, *scm.Response, error) {
f := s.data
return append([]*scm.Comment{}, f.IssueComments[number]...), nil, nil
}

func (s *issueService) Create(context.Context, string, *scm.IssueInput) (*scm.Issue, *scm.Response, error) {
Expand All @@ -132,7 +138,7 @@ func (s *issueService) Create(context.Context, string, *scm.IssueInput) (*scm.Is

func (s *issueService) CreateComment(ctx context.Context, repo string, number int, comment *scm.CommentInput) (*scm.Comment, *scm.Response, error) {
f := s.data
f.IssueCommentsAdded = append(f.IssueCommentsAdded, fmt.Sprintf("%s#%d:%s", repo, number, comment))
f.IssueCommentsAdded = append(f.IssueCommentsAdded, fmt.Sprintf("%s#%d:%s", repo, number, comment.Body))
answer := &scm.Comment{
ID: f.IssueCommentID,
Body: comment.Body,
Expand All @@ -143,8 +149,18 @@ func (s *issueService) CreateComment(ctx context.Context, repo string, number in
return answer, nil, nil
}

func (s *issueService) DeleteComment(context.Context, string, int, int) (*scm.Response, error) {
panic("implement me")
func (s *issueService) DeleteComment(ctx context.Context, repo string, number int, id int) (*scm.Response, error) {
f := s.data
f.IssueCommentsDeleted = append(f.IssueCommentsDeleted, fmt.Sprintf("%s#%d", repo, id))
for num, ics := range f.IssueComments {
for i, ic := range ics {
if ic.ID == id {
f.IssueComments[num] = append(ics[:i], ics[i+1:]...)
return nil, nil
}
}
}
return nil, fmt.Errorf("could not find issue comment %d", id)
}

func (s *issueService) Close(context.Context, string, int) (*scm.Response, error) {
Expand Down
50 changes: 50 additions & 0 deletions scm/driver/fake/pr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package fake

import (
"context"

"github.com/jenkins-x/go-scm/scm"
)

type pullService struct {
client *wrapper
data *Data
}

func (s *pullService) Find(context.Context, string, int) (*scm.PullRequest, *scm.Response, error) {
panic("implement me")
}

func (s *pullService) FindComment(context.Context, string, int, int) (*scm.Comment, *scm.Response, error) {
panic("implement me")
}

func (s *pullService) List(context.Context, string, scm.PullRequestListOptions) ([]*scm.PullRequest, *scm.Response, error) {
panic("implement me")
}

func (s *pullService) ListChanges(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
f := s.data
return f.PullRequestChanges[number], nil, nil
}

func (s *pullService) ListComments(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Comment, *scm.Response, error) {
f := s.data
return append([]*scm.Comment{}, f.PullRequestComments[number]...), nil, nil
}

func (s *pullService) Merge(context.Context, string, int) (*scm.Response, error) {
panic("implement me")
}

func (s *pullService) Close(context.Context, string, int) (*scm.Response, error) {
panic("implement me")
}

func (s *pullService) CreateComment(context.Context, string, int, *scm.CommentInput) (*scm.Comment, *scm.Response, error) {
panic("implement me")
}

func (s *pullService) DeleteComment(context.Context, string, int, int) (*scm.Response, error) {
panic("implement me")
}
46 changes: 46 additions & 0 deletions scm/driver/fake/review.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package fake

import (
"context"

"github.com/jenkins-x/go-scm/scm"
)

type reviewService struct {
client *wrapper
data *Data
}

func (s *reviewService) Find(ctx context.Context, repo string, number int, reviewID int) (*scm.Review, *scm.Response, error) {
reviews, r, err := s.List(ctx, repo, number, scm.ListOptions{})
if err != nil {
return nil, r, err
}
for _, review := range reviews {
if review.ID == reviewID {
return review, nil, nil
}
}
return nil, r, err
}

func (s *reviewService) List(ctx context.Context, repo string, number int, opt scm.ListOptions) ([]*scm.Review, *scm.Response, error) {
f := s.data
return append([]*scm.Review{}, f.Reviews[number]...), nil, nil
}

func (s *reviewService) Create(ctx context.Context, repo string, number int, input *scm.ReviewInput) (*scm.Review, *scm.Response, error) {
f := s.data
review := &scm.Review{
ID: f.ReviewID,
Author: scm.User{Login: botName},
Body: input.Body,
}
f.Reviews[number] = append(f.Reviews[number], review)
f.ReviewID++
return review, nil, nil
}

func (s *reviewService) Delete(context.Context, string, int, int) (*scm.Response, error) {
panic("implement me")
}
4 changes: 4 additions & 0 deletions scm/driver/gitea/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ type issueService struct {
client *wrapper
}

func (s *issueService) ListEvents(context.Context, string, int, scm.ListOptions) ([]*scm.ListedIssueEvent, *scm.Response, error) {
panic("implement me")
}

func (s *issueService) ListLabels(context.Context, string, int, scm.ListOptions) ([]*scm.Label, *scm.Response, error) {
panic("implement me")
}
Expand Down
33 changes: 33 additions & 0 deletions scm/driver/github/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ func (s *issueService) ListLabels(ctx context.Context, repo string, number int,
return convertLabelObjects(out), res, err
}

func (s *issueService) ListEvents(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.ListedIssueEvent, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/issues/%d/events?%s", repo, number, encodeListOptions(opts))
out := []*listedIssueEvent{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertListedIssueEvents(out), res, err
}

func (s *issueService) AddLabel(ctx context.Context, repo string, number int, label string) (*scm.Response, error) {
path := fmt.Sprintf("repos/%s/issues/%d/labels", repo, number)
in := []string{label}
Expand Down Expand Up @@ -153,6 +160,15 @@ type issueCommentInput struct {
Body string `json:"body"`
}

// listedIssueEvent represents an issue event from the events API (not from a webhook payload).
// https://developer.github.com/v3/issues/events/
type listedIssueEvent struct {
Event string `json:"event"` // This is the same as IssueEvent.Action.
Actor user `json:"actor"`
Label label `json:"label"`
Created time.Time `json:"created_at"`
}

// helper function to convert from the gogs issue list to
// the common issue structure.
func convertIssueList(from []*issue) []*scm.Issue {
Expand Down Expand Up @@ -231,3 +247,20 @@ func convertLabelObjects(from []*label) []*scm.Label {
}
return labels
}

func convertListedIssueEvents(src []*listedIssueEvent) []*scm.ListedIssueEvent {
var answer []*scm.ListedIssueEvent
for _, from := range src {
answer = append(answer, convertListedIssueEvent(from))
}
return answer
}

func convertListedIssueEvent(from *listedIssueEvent) *scm.ListedIssueEvent {
return &scm.ListedIssueEvent{
Event: from.Event,
Actor: *convertUser(&from.Actor),
Label: convertLabel(from.Label),
Created: from.Created,
}
}
4 changes: 4 additions & 0 deletions scm/driver/gitlab/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ type issueService struct {
client *wrapper
}

func (s *issueService) ListEvents(context.Context, string, int, scm.ListOptions) ([]*scm.ListedIssueEvent, *scm.Response, error) {
panic("implement me")
}

func (s *issueService) ListLabels(context.Context, string, int, scm.ListOptions) ([]*scm.Label, *scm.Response, error) {
panic("implement me")
}
Expand Down
4 changes: 4 additions & 0 deletions scm/driver/gogs/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ type issueService struct {
client *wrapper
}

func (s *issueService) ListEvents(context.Context, string, int, scm.ListOptions) ([]*scm.ListedIssueEvent, *scm.Response, error) {
panic("implement me")
}

func (s *issueService) ListLabels(context.Context, string, int, scm.ListOptions) ([]*scm.Label, *scm.Response, error) {
panic("implement me")
}
Expand Down
4 changes: 4 additions & 0 deletions scm/driver/stash/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ type issueService struct {
client *wrapper
}

func (s *issueService) ListEvents(context.Context, string, int, scm.ListOptions) ([]*scm.ListedIssueEvent, *scm.Response, error) {
panic("implement me")
}

func (s *issueService) ListLabels(context.Context, string, int, scm.ListOptions) ([]*scm.Label, *scm.Response, error) {
panic("implement me")
}
Expand Down
11 changes: 11 additions & 0 deletions scm/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ type (
Body string
}

// ListedIssueEvent for listing events on an issue
ListedIssueEvent struct {
Event string
Actor User
Label Label
Created time.Time
}

// IssueService provides access to issue resources.
IssueService interface {
// Find returns the issue by number.
Expand All @@ -76,6 +84,9 @@ type (
// ListLabels returns the labels on an issue
ListLabels(context.Context, string, int, ListOptions) ([]*Label, *Response, error)

// ListEvents returns the labels on an issue
ListEvents(context.Context, string, int, ListOptions) ([]*ListedIssueEvent, *Response, error)

// Create creates a new issue.
Create(context.Context, string, *IssueInput) (*Issue, *Response, error)

Expand Down

0 comments on commit 57883e8

Please sign in to comment.