Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add comments object #355

Merged
merged 4 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
changed due to some patterns established for the upcoming 1.0 release
  • Loading branch information
alex-ikse committed Mar 16, 2022
commit 8414b93fbbd881ab43128560fd41c7e52d6ff56d
29 changes: 17 additions & 12 deletions comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Comments interface {
List(ctx context.Context, runID string) (*CommentList, error)

// Read a comment by its ID.
Read(ctx context.Context, CommentID string) (*Comment, error)
Read(ctx context.Context, commentID string) (*Comment, error)

// Create a new comment with the given options.
Create(ctx context.Context, runID string, options CommentCreateOptions) (*Comment, error)
Expand Down Expand Up @@ -50,10 +50,7 @@ type CommentCreateOptions struct {
Type string `jsonapi:"primary,comments"`

// Required: Body of the comment.
Body *string `jsonapi:"attr,body"`

// Optional: Run where the comment is attached
Run *Run `jsonapi:"relation,run"`
Body string `jsonapi:"attr,body"`
}

// List all comments of the given run.
Expand All @@ -79,12 +76,12 @@ func (s *comments) List(ctx context.Context, runID string) (*CommentList, error)

// Create a new comment with the given options.
func (s *comments) Create(ctx context.Context, runID string, options CommentCreateOptions) (*Comment, error) {
if !validStringID(&runID) {
return nil, ErrInvalidRunID
if err := options.valid(); err != nil {
return nil, err
}

if !validString(options.Body) {
return nil, ErrCommentBody
if !validStringID(&runID) {
return nil, ErrInvalidRunID
}

u := fmt.Sprintf("runs/%s/comments", url.QueryEscape(runID))
Expand All @@ -103,12 +100,12 @@ func (s *comments) Create(ctx context.Context, runID string, options CommentCrea
}

// Read a comment by its ID.
func (s *comments) Read(ctx context.Context, CommentID string) (*Comment, error) {
if !validStringID(&CommentID) {
func (s *comments) Read(ctx context.Context, commentID string) (*Comment, error) {
if !validStringID(&commentID) {
return nil, ErrInvalidCommentID
}

u := fmt.Sprintf("comments/%s", url.QueryEscape(CommentID))
u := fmt.Sprintf("comments/%s", url.QueryEscape(commentID))
req, err := s.client.newRequest("GET", u, nil)
if err != nil {
return nil, err
Expand All @@ -122,3 +119,11 @@ func (s *comments) Read(ctx context.Context, CommentID string) (*Comment, error)

return comm, nil
}

func (o CommentCreateOptions) valid() error {
if !validString(&o.Body) {
return ErrInvalidCommentBody
}

return nil
}
4 changes: 2 additions & 2 deletions comment_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestCommentsList(t *testing.T) {

t.Run("create a comment", func(t *testing.T) {
options := CommentCreateOptions{
Body: String(commentBody1),
Body: commentBody1,
}
cl, err := client.Comments.Create(ctx, rTest.ID, options)
require.NoError(t, err)
Expand All @@ -48,7 +48,7 @@ func TestCommentsList(t *testing.T) {

t.Run("create 2nd comment", func(t *testing.T) {
options := CommentCreateOptions{
Body: String(commentBody2),
Body: commentBody2,
}
cl, err := client.Comments.Create(ctx, rTest.ID, options)
require.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ var (
ErrInvalidNotificationTrigger = errors.New("invalid value for notification trigger")

ErrInvalidCommentID = errors.New("invalid value for comment ID")

ErrInvalidCommentBody = errors.New("invalid value for comment body")
)

// Missing values for required field/option
Expand Down