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

feat(api-gitlab-v4): delete comment reaction #85

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
58 changes: 58 additions & 0 deletions packages/@vssue/api-gitlab-v4/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,4 +485,62 @@ describe('methods', () => {
expect(request.headers.Authorization).toBe(`Bearer ${mockToken}`);
expect(success).toBe(true);
});

describe('postCommentReaction', () => {
test('created', async () => {
const issueId = 1;
const commentId = fixtures.comment.id;
mock
.onPost(
new RegExp(
`projects/${encodedRepo}/issues/${issueId}/notes/${commentId}/award_emoji$`
)
)
.reply(201);
const success = await API.postCommentReaction({
issueId,
commentId,
accessToken: mockToken,
reaction: 'like',
});
expect(mock.history.post.length).toBe(1);
const request = mock.history.post[0];
expect(request.method).toBe('post');
expect(request.headers.Authorization).toBe(`Bearer ${mockToken}`);
expect(success).toBe(true);
});

test('deleted', async () => {
const issueId = 1;
const commentId = fixtures.comment.id;
const reactionId = fixtures.reactions[0].id;
mock
.onPost(
new RegExp(
`projects/${encodedRepo}/issues/${issueId}/notes/${commentId}/award_emoji$`
)
)
.reply(200, fixtures.reactions[0])
.onDelete(
new RegExp(
`projects/${encodedRepo}/issues/${issueId}/notes/${commentId}/award_emoji/${reactionId}$`
)
)
.reply(204);

const success = await API.postCommentReaction({
issueId,
commentId,
accessToken: mockToken,
reaction: 'like',
});
expect(mock.history.post.length).toBe(1);
expect(mock.history.delete.length).toBe(1);
const request = mock.history.delete[0];
expect(request.method).toBe('delete');
expect(request.headers.Authorization).toBe(`Bearer ${mockToken}`);

expect(success).toBe(true);
});
});
});
50 changes: 45 additions & 5 deletions packages/@vssue/api-gitlab-v4/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ export default class GitlabV4 implements VssueAPI.Instance {
/**
* Get reactions of a comment
*
* @see https://docs.gitlab.com/ce/api/award_emoji.html#list-an-awardables-award-emoji
* @see https://docs.gitlab.com/ce/api/award_emoji.html#list-a-comments-award-emoji
*/
async getCommentReactions({
accessToken,
Expand All @@ -420,13 +420,13 @@ export default class GitlabV4 implements VssueAPI.Instance {
/**
* Create a new reaction of a comment
*
* @see https://docs.gitlab.com/ce/api/award_emoji.html#award-a-new-emoji
* @see https://docs.gitlab.com/ce/api/award_emoji.html#award-a-new-emoji-on-a-comment
*/
async postCommentReaction({
accessToken,
issueId,
commentId,
reaction,
accessToken,
}: {
accessToken: VssueAPI.AccessToken;
issueId: string | number;
Expand All @@ -445,11 +445,24 @@ export default class GitlabV4 implements VssueAPI.Instance {
},
}
);

// 200 OK if the reaction is already token
// TODO: https://gitlab.com/gitlab-org/gitlab/-/issues/26060
if (response.status === 200) {
return this.deleteCommentReaction({
accessToken,
issueId,
commentId,
reactionId: response.data.id,
});
}

// 201 CREATED
return response.status === 201;
} catch (e) {
// it could be a bug of gitlab
// this is a bug of gitlab
// if a reaction (award emoji) has already existed, it returns a 404 response with a buggy message
// have submitted an issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/56147
// TODO: https://gitlab.com/gitlab-org/gitlab/-/issues/26060
/* istanbul ignore next */
if (e.response && e.response.status === 404) {
return false;
Expand All @@ -459,6 +472,33 @@ export default class GitlabV4 implements VssueAPI.Instance {
}
}

/**
* Delete a reaction of a comment
*
* @see https://docs.gitlab.com/ce/api/award_emoji.html#delete-an-award-emoji-from-a-comment
*/
async deleteCommentReaction({
accessToken,
issueId,
commentId,
reactionId,
}: {
accessToken: VssueAPI.AccessToken;
issueId: string | number;
commentId: string | number;
reactionId: string | number;
}): Promise<boolean> {
const response = await this.$http.delete(
`projects/${this._encodedRepo}/issues/${issueId}/notes/${commentId}/award_emoji/${reactionId}`,
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
);
return response.status === 204;
}

/**
* Get the parse HTML of markdown content
*
Expand Down