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 watcher that is another user as this looks not implemented on Add() #291

Merged
merged 6 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 8 additions & 4 deletions jira/internal/watcher_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func (w *WatcherService) Gets(ctx context.Context, issueKeyOrID string) (*model.
// POST /rest/api/{2-3}/issue/{issueKeyOrID}/watchers
//
// https://docs.go-atlassian.io/jira-software-cloud/issues/watcher#add-watcher
func (w *WatcherService) Add(ctx context.Context, issueKeyOrID string) (*model.ResponseScheme, error) {
return w.internalClient.Add(ctx, issueKeyOrID)
func (w *WatcherService) Add(ctx context.Context, issueKeyOrID string, accountID ...string) (*model.ResponseScheme, error) {
return w.internalClient.Add(ctx, issueKeyOrID, accountID...)
}

// Delete deletes a user as a watcher of an issue.
Expand Down Expand Up @@ -82,15 +82,19 @@ func (i *internalWatcherImpl) Gets(ctx context.Context, issueKeyOrID string) (*m
return watchers, response, nil
}

func (i *internalWatcherImpl) Add(ctx context.Context, issueKeyOrID string) (*model.ResponseScheme, error) {
func (i *internalWatcherImpl) Add(ctx context.Context, issueKeyOrID string, accountID ...string) (*model.ResponseScheme, error) {

if issueKeyOrID == "" {
return nil, model.ErrNoIssueKeyOrIDError
}

endpoint := fmt.Sprintf("rest/api/%v/issue/%v/watchers", i.version, issueKeyOrID)

request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", nil)
var payload []byte = nil // add self user
if len(accountID) > 0 {
payload = []byte(accountID[0]) // add another user
}
request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload)
if err != nil {
return nil, err
}
Expand Down
71 changes: 69 additions & 2 deletions jira/internal/watcher_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func Test_internalWatcherImpl_Add(t *testing.T) {
type args struct {
ctx context.Context
issueKeyOrID string
accountID []string
}

testCases := []struct {
Expand All @@ -187,6 +188,7 @@ func Test_internalWatcherImpl_Add(t *testing.T) {
args: args{
ctx: context.Background(),
issueKeyOrID: "DUMMY-5",
accountID: nil,
},
on: func(fields *fields) {

Expand All @@ -197,7 +199,38 @@ func Test_internalWatcherImpl_Add(t *testing.T) {
http.MethodPost,
"rest/api/3/issue/DUMMY-5/watchers",
"",
[]byte(nil)).
Return(&http.Request{}, nil)

client.On("Call",
&http.Request{},
nil).
Return(&model.ResponseScheme{}, nil)

fields.c = client
},
wantErr: false,
Err: nil,
},

{
name: "when the api version is v3 and the watcher accountId is set",
fields: fields{version: "3"},
args: args{
ctx: context.Background(),
issueKeyOrID: "DUMMY-5",
accountID: []string{"someAccountID"},
},
on: func(fields *fields) {

client := mocks.NewConnector(t)

client.On("NewRequest",
context.Background(),
http.MethodPost,
"rest/api/3/issue/DUMMY-5/watchers",
"",
[]byte("someAccountID")).
Return(&http.Request{}, nil)

client.On("Call",
Expand All @@ -217,6 +250,7 @@ func Test_internalWatcherImpl_Add(t *testing.T) {
args: args{
ctx: context.Background(),
issueKeyOrID: "DUMMY-5",
accountID: nil,
},
on: func(fields *fields) {

Expand All @@ -227,7 +261,39 @@ func Test_internalWatcherImpl_Add(t *testing.T) {
http.MethodPost,
"rest/api/2/issue/DUMMY-5/watchers",
"",
[]byte(nil),
).
Return(&http.Request{}, nil)

client.On("Call",
&http.Request{},
nil).
Return(&model.ResponseScheme{}, nil)

fields.c = client
},
wantErr: false,
Err: nil,
},

{
name: "when the api version is v2 and the watcher accountId is set",
fields: fields{version: "2"},
args: args{
ctx: context.Background(),
issueKeyOrID: "DUMMY-5",
accountID: []string{"someAccountID"},
},
on: func(fields *fields) {

client := mocks.NewConnector(t)

client.On("NewRequest",
context.Background(),
http.MethodPost,
"rest/api/2/issue/DUMMY-5/watchers",
"",
[]byte("someAccountID")).
Return(&http.Request{}, nil)

client.On("Call",
Expand Down Expand Up @@ -258,6 +324,7 @@ func Test_internalWatcherImpl_Add(t *testing.T) {
args: args{
ctx: context.Background(),
issueKeyOrID: "DUMMY-5",
accountID: nil,
},
on: func(fields *fields) {

Expand All @@ -268,7 +335,7 @@ func Test_internalWatcherImpl_Add(t *testing.T) {
http.MethodPost,
"rest/api/3/issue/DUMMY-5/watchers",
"",
nil).
[]byte(nil)).
Return(&http.Request{}, errors.New("error, unable to create the http request"))

fields.c = client
Expand All @@ -288,7 +355,7 @@ func Test_internalWatcherImpl_Add(t *testing.T) {
newService, err := NewWatcherService(testCase.fields.c, testCase.fields.version)
assert.NoError(t, err)

gotResponse, err := newService.Add(testCase.args.ctx, testCase.args.issueKeyOrID)
gotResponse, err := newService.Add(testCase.args.ctx, testCase.args.issueKeyOrID, testCase.args.accountID...)

if testCase.wantErr {

Expand Down
6 changes: 3 additions & 3 deletions service/jira/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type WatcherConnector interface {
// GET /rest/api/{2-3}/issue/{issueIdOrKey}/watchers
//
// https://docs.go-atlassian.io/jira-software-cloud/issues/watcher#get-issue-watchers
Gets(ctx context.Context, issueKeyOrId string) (*model.IssueWatcherScheme, *model.ResponseScheme, error)
Gets(ctx context.Context, issueKeyOrID string) (*model.IssueWatcherScheme, *model.ResponseScheme, error)

// Add adds a user as a watcher of an issue by passing the account ID of the user.
//
Expand All @@ -23,12 +23,12 @@ type WatcherConnector interface {
// POST /rest/api/{2-3}/issue/{issueIdOrKey}/watchers
//
// https://docs.go-atlassian.io/jira-software-cloud/issues/watcher#add-watcher
Add(ctx context.Context, issueKeyOrId string) (*model.ResponseScheme, error)
Add(ctx context.Context, issueKeyOrID string, accountID ...string) (*model.ResponseScheme, error)

// Delete deletes a user as a watcher of an issue.
//
// DELETE /rest/api/{2-3}/issue/{issueIdOrKey}/watchers
//
// https://docs.go-atlassian.io/jira-software-cloud/issues/watcher#delete-watcher
Delete(ctx context.Context, issueKeyOrId, accountId string) (*model.ResponseScheme, error)
Delete(ctx context.Context, issueKeyOrID, accountID string) (*model.ResponseScheme, error)
}
Loading