Skip to content

Commit 2ca8ec5

Browse files
authored
feat(tasks): add support for task comments (#114)
1 parent e66f117 commit 2ca8ec5

File tree

4 files changed

+394
-0
lines changed

4 files changed

+394
-0
lines changed

crowdin/model/tasks.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,3 +980,45 @@ func (r *TaskSettingsTemplateAddRequest) Validate() error {
980980

981981
return nil
982982
}
983+
984+
// TaskComment represents a comment on a task.
985+
type TaskComment struct {
986+
ID int `json:"id"`
987+
UserID int `json:"userId"`
988+
TaskID int `json:"taskId"`
989+
Text string `json:"text"`
990+
TimeSpent int `json:"timeSpent"`
991+
CreatedAt string `json:"createdAt"`
992+
UpdatedAt string `json:"updatedAt"`
993+
}
994+
995+
// TaskCommentResponse defines the structure of the response
996+
// when getting a task comment.
997+
type TaskCommentResponse struct {
998+
Data *TaskComment `json:"data"`
999+
}
1000+
1001+
// TaskCommentsListResponse defines the structure of the response
1002+
// when getting a list of task comments.
1003+
type TaskCommentsListResponse struct {
1004+
Data []*TaskCommentResponse `json:"data"`
1005+
}
1006+
1007+
// TaskCommentAddRequest defines the structure of the request
1008+
// when adding a new comment to a task.
1009+
type TaskCommentAddRequest struct {
1010+
// Comment text
1011+
Text string `json:"text,omitempty"`
1012+
// Specifies the time spent on the task in seconds
1013+
TimeSpent int `json:"timeSpent,omitempty"`
1014+
}
1015+
1016+
// Validate checks if the request is valid.
1017+
// It implements the crowdin.Validator interface.
1018+
func (r *TaskCommentAddRequest) Validate() error {
1019+
if r == nil {
1020+
return ErrNilRequest
1021+
}
1022+
1023+
return nil
1024+
}

crowdin/model/tasks_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,3 +765,36 @@ func TestTasksService_Add_EnterprisePendingTaskCreateForm_WithRequestValidation(
765765
})
766766
}
767767
}
768+
769+
func TestTaskCommentAddRequestValidate(t *testing.T) {
770+
tests := []struct {
771+
name string
772+
req *TaskCommentAddRequest
773+
err string
774+
valid bool
775+
}{
776+
{
777+
name: "nil request",
778+
req: nil,
779+
err: "request cannot be nil",
780+
},
781+
{
782+
name: "valid request",
783+
req: &TaskCommentAddRequest{
784+
Text: "Work in task",
785+
TimeSpent: 3600,
786+
},
787+
valid: true,
788+
},
789+
}
790+
791+
for _, tt := range tests {
792+
t.Run(tt.name, func(t *testing.T) {
793+
if err := tt.req.Validate(); tt.valid {
794+
assert.NoError(t, err)
795+
} else {
796+
assert.EqualError(t, err, tt.err)
797+
}
798+
})
799+
}
800+
}

crowdin/tasks.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,67 @@ func (s *TasksService) EditSettingsTemplate(ctx context.Context, projectID, task
214214
func (s *TasksService) DeleteSettingsTemplate(ctx context.Context, projectID, taskSettingTemplateID int) (*Response, error) {
215215
return s.client.Delete(ctx, fmt.Sprintf("/api/v2/projects/%d/tasks/settings-templates/%d", projectID, taskSettingTemplateID), nil)
216216
}
217+
218+
// ListComments returns a list of task comments in a project.
219+
//
220+
// https://support.crowdin.com/developer/api/v2/#tag/Tasks/operation/api.projects.tasks.comments.getMany
221+
func (s *TasksService) ListComments(ctx context.Context, projectID, taskID int, opts *model.ListOptions) (
222+
[]*model.TaskComment, *Response, error,
223+
) {
224+
res := new(model.TaskCommentsListResponse)
225+
resp, err := s.client.Get(ctx, fmt.Sprintf("/api/v2/projects/%d/tasks/%d/comments", projectID, taskID), opts, res)
226+
if err != nil {
227+
return nil, resp, err
228+
}
229+
230+
list := make([]*model.TaskComment, 0, len(res.Data))
231+
for _, task := range res.Data {
232+
list = append(list, task.Data)
233+
}
234+
235+
return list, resp, err
236+
}
237+
238+
// GetComment returns a single task comment in a project by its identifier.
239+
//
240+
// https://support.crowdin.com/developer/api/v2/#tag/Tasks/operation/api.projects.tasks.comments.get
241+
func (s *TasksService) GetComment(ctx context.Context, projectID, taskID, commentID int) (
242+
*model.TaskComment, *Response, error,
243+
) {
244+
path := fmt.Sprintf("/api/v2/projects/%d/tasks/%d/comments/%d", projectID, taskID, commentID)
245+
res := new(model.TaskCommentResponse)
246+
resp, err := s.client.Get(ctx, path, nil, res)
247+
248+
return res.Data, resp, err
249+
}
250+
251+
// AddComment creates a new comment for a specific task in a project.
252+
//
253+
// https://support.crowdin.com/developer/api/v2/#tag/Tasks/operation/api.projects.tasks.comments.post
254+
func (s *TasksService) AddComment(ctx context.Context, projectID, taskID int, req *model.TaskCommentAddRequest) (
255+
*model.TaskComment, *Response, error,
256+
) {
257+
res := new(model.TaskCommentResponse)
258+
resp, err := s.client.Post(ctx, fmt.Sprintf("/api/v2/projects/%d/tasks/%d/comments", projectID, taskID), req, res)
259+
260+
return res.Data, resp, err
261+
}
262+
263+
// EditComment updates a comment for a specific task in a project by its identifier.
264+
//
265+
// https://support.crowdin.com/developer/api/v2/#tag/Tasks/operation/api.projects.tasks.comments.patch
266+
func (s *TasksService) EditComment(ctx context.Context, projectID, taskID, commentID int, req []*model.UpdateRequest) (
267+
*model.TaskComment, *Response, error,
268+
) {
269+
res := new(model.TaskCommentResponse)
270+
resp, err := s.client.Patch(ctx, fmt.Sprintf("/api/v2/projects/%d/tasks/%d/comments/%d", projectID, taskID, commentID), req, res)
271+
272+
return res.Data, resp, err
273+
}
274+
275+
// DeleteComment removes a comment from a specific task in a project by its identifier.
276+
//
277+
// https://support.crowdin.com/developer/api/v2/#tag/Tasks/operation/api.projects.tasks.comments.delete
278+
func (s *TasksService) DeleteComment(ctx context.Context, projectID, taskID, commentID int) (*Response, error) {
279+
return s.client.Delete(ctx, fmt.Sprintf("/api/v2/projects/%d/tasks/%d/comments/%d", projectID, taskID, commentID), nil)
280+
}

0 commit comments

Comments
 (0)