forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comment.go
132 lines (106 loc) · 2.94 KB
/
comment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfe
import (
"context"
"fmt"
"net/url"
)
// Compile-time proof of interface implementation.
var _ Comments = (*comments)(nil)
// Comments describes all the comment related methods that the
// Terraform Enterprise API supports.
//
// TFE API docs:
// https://developer.hashicorp.com/terraform/cloud-docs/api-docs/comments
type Comments interface {
// List all comments of the given run.
List(ctx context.Context, runID string) (*CommentList, error)
// Read a comment by its ID.
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)
}
// Comments implements Comments.
type comments struct {
client *Client
}
// CommentList represents a list of comments.
type CommentList struct {
*Pagination
Items []*Comment
}
// Comment represents a Terraform Enterprise comment.
type Comment struct {
ID string `jsonapi:"primary,comments"`
Body string `jsonapi:"attr,body"`
}
type CommentCreateOptions struct {
// Type is a public field utilized by JSON:API to
// set the resource type via the field tag.
// It is not a user-defined value and does not need to be set.
// https://jsonapi.org/format/#crud-creating
Type string `jsonapi:"primary,comments"`
// Required: Body of the comment.
Body string `jsonapi:"attr,body"`
}
// List all comments of the given run.
func (s *comments) List(ctx context.Context, runID string) (*CommentList, error) {
if !validStringID(&runID) {
return nil, ErrInvalidRunID
}
u := fmt.Sprintf("runs/%s/comments", url.QueryEscape(runID))
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, err
}
cl := &CommentList{}
err = req.Do(ctx, cl)
if err != nil {
return nil, err
}
return cl, nil
}
// Create a new comment with the given options.
func (s *comments) Create(ctx context.Context, runID string, options CommentCreateOptions) (*Comment, error) {
if err := options.valid(); err != nil {
return nil, err
}
if !validStringID(&runID) {
return nil, ErrInvalidRunID
}
u := fmt.Sprintf("runs/%s/comments", url.QueryEscape(runID))
req, err := s.client.NewRequest("POST", u, &options)
if err != nil {
return nil, err
}
comm := &Comment{}
err = req.Do(ctx, comm)
if err != nil {
return nil, err
}
return comm, err
}
// Read a comment by its ID.
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))
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, err
}
comm := &Comment{}
err = req.Do(ctx, comm)
if err != nil {
return nil, err
}
return comm, nil
}
func (o CommentCreateOptions) valid() error {
if !validString(&o.Body) {
return ErrInvalidCommentBody
}
return nil
}