Skip to content

Commit

Permalink
Merge pull request #257 from jstrachan/changes
Browse files Browse the repository at this point in the history
fix: refactor the label comment code
  • Loading branch information
jenkins-x-bot-test authored Mar 23, 2021
2 parents 74d1a5c + 30161c0 commit ee723d0
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 63 deletions.
21 changes: 16 additions & 5 deletions scm/driver/bitbucket/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package bitbucket
import (
"context"

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

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

Expand All @@ -30,17 +32,26 @@ func (s *issueService) ListEvents(context.Context, string, int, scm.ListOptions)
return nil, nil, scm.ErrNotSupported
}

func (s *issueService) ListLabels(context.Context, string, int, scm.ListOptions) ([]*scm.Label, *scm.Response, error) {
// TODO
return nil, nil, nil
func (s *issueService) ListLabels(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Label, *scm.Response, error) {
// Get all comments, parse out labels (removing and added based off time)
cs, res, err := s.ListComments(ctx, repo, number, opts)
if err == nil {
l, err := labels.ConvertLabelComments(cs)
return l, res, err
}
return nil, res, err
}

func (s *issueService) AddLabel(ctx context.Context, repo string, number int, label string) (*scm.Response, error) {
return nil, scm.ErrNotSupported
input := labels.CreateLabelAddComment(label)
_, res, err := s.CreateComment(ctx, repo, number, input)
return res, err
}

func (s *issueService) DeleteLabel(ctx context.Context, repo string, number int, label string) (*scm.Response, error) {
return nil, scm.ErrNotSupported
input := labels.CreateLabelRemoveComment(label)
_, res, err := s.CreateComment(ctx, repo, number, input)
return res, err
}

func (s *issueService) Find(ctx context.Context, repo string, number int) (*scm.Issue, *scm.Response, error) {
Expand Down
27 changes: 21 additions & 6 deletions scm/driver/bitbucket/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"strings"
"time"

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

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

Expand Down Expand Up @@ -55,17 +57,30 @@ func (s *pullService) ListChanges(ctx context.Context, repo string, number int,
return convertDiffstats(out), res, err
}

func (s *pullService) ListLabels(context.Context, string, int, scm.ListOptions) ([]*scm.Label, *scm.Response, error) {
// TODO
return nil, nil, nil
func (s *pullService) ListLabels(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Label, *scm.Response, error) {
// Get all comments, parse out labels (removing and added based off time)
cs, res, err := s.ListComments(ctx, repo, number, opts)
if err == nil {
l, err := labels.ConvertLabelComments(cs)
return l, res, err
}
return nil, res, err
}

func (s *pullService) ListEvents(context.Context, string, int, scm.ListOptions) ([]*scm.ListedIssueEvent, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
func (s *pullService) AddLabel(ctx context.Context, repo string, number int, label string) (*scm.Response, error) {
input := labels.CreateLabelAddComment(label)
_, res, err := s.CreateComment(ctx, repo, number, input)
return res, err
}

func (s *pullService) DeleteLabel(ctx context.Context, repo string, number int, label string) (*scm.Response, error) {
return nil, scm.ErrNotSupported
input := labels.CreateLabelRemoveComment(label)
_, res, err := s.CreateComment(ctx, repo, number, input)
return res, err
}

func (s *pullService) ListEvents(context.Context, string, int, scm.ListOptions) ([]*scm.ListedIssueEvent, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}

func (s *pullService) Merge(ctx context.Context, repo string, number int, options *scm.PullRequestMergeOptions) (*scm.Response, error) {
Expand Down
14 changes: 5 additions & 9 deletions scm/driver/stash/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"context"
"fmt"

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

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

Expand Down Expand Up @@ -35,26 +37,20 @@ func (s *issueService) ListLabels(ctx context.Context, repo string, number int,
// Get all comments, parse out labels (removing and added based off time)
cs, res, err := s.ListComments(ctx, repo, number, opts)
if err == nil {
l, err := convertLabelComments(cs)
l, err := labels.ConvertLabelComments(cs)
return l, res, err
}
return nil, res, err
}

func (s *issueService) AddLabel(ctx context.Context, repo string, number int, label string) (*scm.Response, error) {
// Add a comment with /jx-label <name>
input := &scm.CommentInput{
Body: fmt.Sprintf("%s%s", addLabel, label),
}
input := labels.CreateLabelAddComment(label)
_, res, err := s.CreateComment(ctx, repo, number, input)
return res, err
}

func (s *issueService) DeleteLabel(ctx context.Context, repo string, number int, label string) (*scm.Response, error) {
// Add a comment with /jx-label <name> remove
input := &scm.CommentInput{
Body: fmt.Sprintf("%s%s%s", addLabel, label, removeLabel),
}
input := labels.CreateLabelRemoveComment(label)
_, res, err := s.CreateComment(ctx, repo, number, input)
return res, err
}
Expand Down
52 changes: 9 additions & 43 deletions scm/driver/stash/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,18 @@ import (
"fmt"
"net/http"
"net/url"
"sort"
"strings"
"time"

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

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

type pullService struct {
client *wrapper
}

const addLabel = "/jx-label "
const removeLabel = " remove"
const emptyString = ""

func (s *pullService) Find(ctx context.Context, repo string, number int) (*scm.PullRequest, *scm.Response, error) {
namespace, name := scm.Split(repo)
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/pull-requests/%d", namespace, name, number)
Expand Down Expand Up @@ -64,63 +61,32 @@ func (s *pullService) ListChanges(ctx context.Context, repo string, number int,
return convertDiffstats(out), res, err
}

func convertLabelComments(cs []*scm.Comment) ([]*scm.Label, error) {
sort.SliceStable(cs, func(i, j int) bool {
return cs[i].Created.UnixNano() < cs[j].Created.UnixNano()
})
m := make(map[string]bool)
for _, com := range cs {
if strings.HasPrefix(com.Body, addLabel) {
t := strings.ReplaceAll(com.Body, addLabel, emptyString)
if strings.HasSuffix(t, removeLabel) {
t = strings.ReplaceAll(t, removeLabel, emptyString)
m[t] = true
} else {
m[t] = false
}
}
}
var ls []*scm.Label
for l, i := range m {
if i == false {
ls = append(ls, &scm.Label{Name: l})
}
}
return ls, nil
}

func (s *pullService) ListLabels(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Label, *scm.Response, error) {
// Get all comments, parse out labels (removing and added based off time)
cs, res, err := s.ListComments(ctx, repo, number, opts)
if err == nil {
l, err := convertLabelComments(cs)
l, err := labels.ConvertLabelComments(cs)
return l, res, err
}
return nil, res, err
}

func (s *pullService) ListEvents(context.Context, string, int, scm.ListOptions) ([]*scm.ListedIssueEvent, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}

func (s *pullService) AddLabel(ctx context.Context, repo string, number int, label string) (*scm.Response, error) {
// Add a comment with /jx-label <name>
input := &scm.CommentInput{
Body: fmt.Sprintf("%s%s", addLabel, label),
}
input := labels.CreateLabelAddComment(label)
_, res, err := s.CreateComment(ctx, repo, number, input)
return res, err
}

func (s *pullService) DeleteLabel(ctx context.Context, repo string, number int, label string) (*scm.Response, error) {
// Add a comment with /jx-label <name> remove
input := &scm.CommentInput{
Body: fmt.Sprintf("%s%s%s", addLabel, label, removeLabel),
}
input := labels.CreateLabelRemoveComment(label)
_, res, err := s.CreateComment(ctx, repo, number, input)
return res, err
}

func (s *pullService) ListEvents(context.Context, string, int, scm.ListOptions) ([]*scm.ListedIssueEvent, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}

func (s *pullService) ListComments(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Comment, *scm.Response, error) {
// TODO(bradrydzewski) the challenge with comments is that we need to use
// the activities endpoint, which returns entries that may or may not be
Expand Down
56 changes: 56 additions & 0 deletions scm/labels/labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package labels

import (
"fmt"
"sort"
"strings"

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

const (
addLabel = "/jx-label "
removeLabel = " remove"
emptyString = ""
)

// ConvertLabelComments converts comments to labels for git providers which don't support native labels
func ConvertLabelComments(cs []*scm.Comment) ([]*scm.Label, error) {
sort.SliceStable(cs, func(i, j int) bool {
return cs[i].Created.UnixNano() < cs[j].Created.UnixNano()
})
m := make(map[string]bool)
for _, com := range cs {
if strings.HasPrefix(com.Body, addLabel) {
t := strings.ReplaceAll(com.Body, addLabel, emptyString)
if strings.HasSuffix(t, removeLabel) {
t = strings.ReplaceAll(t, removeLabel, emptyString)
m[t] = true
} else {
m[t] = false
}
}
}
var ls []*scm.Label
for l, i := range m {
if i == false {
ls = append(ls, &scm.Label{Name: l})
}
}
return ls, nil
}

// CreateLabelAddComment creates a label comment for git providers which don't support labels with comment with /jx-label <name>
func CreateLabelAddComment(label string) *scm.CommentInput {
return &scm.CommentInput{
Body: fmt.Sprintf("%s%s", addLabel, label),
}
}

// CreateLabelRemoveComment adds a comment with /jx-label <name> remove
func CreateLabelRemoveComment(label string) *scm.CommentInput {
input := &scm.CommentInput{
Body: fmt.Sprintf("%s%s%s", addLabel, label, removeLabel),
}
return input
}

0 comments on commit ee723d0

Please sign in to comment.