From 3dd1a842afd6121ad7685e503f8da6b160006b03 Mon Sep 17 00:00:00 2001 From: Andrea Lamparelli Date: Tue, 9 Apr 2024 22:35:14 +0200 Subject: [PATCH] feat: implement gitlab client and surround with try catch --- dist/cli/index.js | 47 ++++++++++++++++++------- dist/gha/index.js | 47 ++++++++++++++++++------- src/service/git/git-client.ts | 2 +- src/service/git/github/github-client.ts | 31 +++++++++------- src/service/git/gitlab/gitlab-client.ts | 22 ++++++++++-- 5 files changed, 107 insertions(+), 42 deletions(-) diff --git a/dist/cli/index.js b/dist/cli/index.js index 6f6ab5c..a68d73c 100755 --- a/dist/cli/index.js +++ b/dist/cli/index.js @@ -948,17 +948,24 @@ class GitHubClient { return data.html_url; } async createPullRequestComment(prUrl, comment) { - const { owner, project, id } = this.extractPullRequestData(prUrl); - const { data } = await this.octokit.issues.createComment({ - owner: owner, - repo: project, - issue_number: id, - body: comment - }); - if (!data) { - throw new Error("Pull request comment creation failed"); + let commentUrl = undefined; + try { + const { owner, project, id } = this.extractPullRequestData(prUrl); + const { data } = await this.octokit.issues.createComment({ + owner: owner, + repo: project, + issue_number: id, + body: comment + }); + if (!data) { + throw new Error("Pull request comment creation failed"); + } + commentUrl = data.url; } - return data.url; + catch (error) { + this.logger.error(`Error creating comment on pull request ${prUrl}: ${error}`); + } + return commentUrl; } // UTILS /** @@ -1201,9 +1208,23 @@ class GitLabClient { await Promise.all(promises); return mr.web_url; } - // TODO: implement createPullRequestComment - async createPullRequestComment(prUrl, comment) { - throw new Error("Method not implemented."); + // https://docs.gitlab.com/ee/api/notes.html#create-new-issue-note + async createPullRequestComment(mrUrl, comment) { + const commentUrl = undefined; + try { + const { namespace, project, id } = this.extractMergeRequestData(mrUrl); + const projectId = this.getProjectId(namespace, project); + const { data } = await this.client.post(`/projects/${projectId}/issues/${id}/notes`, { + body: comment, + }); + if (!data) { + throw new Error("Merge request comment creation failed"); + } + } + catch (error) { + this.logger.error(`Error creating comment on merge request ${mrUrl}: ${error}`); + } + return commentUrl; } // UTILS /** diff --git a/dist/gha/index.js b/dist/gha/index.js index 209d2fb..2cfc0b1 100755 --- a/dist/gha/index.js +++ b/dist/gha/index.js @@ -913,17 +913,24 @@ class GitHubClient { return data.html_url; } async createPullRequestComment(prUrl, comment) { - const { owner, project, id } = this.extractPullRequestData(prUrl); - const { data } = await this.octokit.issues.createComment({ - owner: owner, - repo: project, - issue_number: id, - body: comment - }); - if (!data) { - throw new Error("Pull request comment creation failed"); + let commentUrl = undefined; + try { + const { owner, project, id } = this.extractPullRequestData(prUrl); + const { data } = await this.octokit.issues.createComment({ + owner: owner, + repo: project, + issue_number: id, + body: comment + }); + if (!data) { + throw new Error("Pull request comment creation failed"); + } + commentUrl = data.url; } - return data.url; + catch (error) { + this.logger.error(`Error creating comment on pull request ${prUrl}: ${error}`); + } + return commentUrl; } // UTILS /** @@ -1166,9 +1173,23 @@ class GitLabClient { await Promise.all(promises); return mr.web_url; } - // TODO: implement createPullRequestComment - async createPullRequestComment(prUrl, comment) { - throw new Error("Method not implemented."); + // https://docs.gitlab.com/ee/api/notes.html#create-new-issue-note + async createPullRequestComment(mrUrl, comment) { + const commentUrl = undefined; + try { + const { namespace, project, id } = this.extractMergeRequestData(mrUrl); + const projectId = this.getProjectId(namespace, project); + const { data } = await this.client.post(`/projects/${projectId}/issues/${id}/notes`, { + body: comment, + }); + if (!data) { + throw new Error("Merge request comment creation failed"); + } + } + catch (error) { + this.logger.error(`Error creating comment on merge request ${mrUrl}: ${error}`); + } + return commentUrl; } // UTILS /** diff --git a/src/service/git/git-client.ts b/src/service/git/git-client.ts index 3174636..93190d9 100644 --- a/src/service/git/git-client.ts +++ b/src/service/git/git-client.ts @@ -49,6 +49,6 @@ import { BackportPullRequest, GitClientType, GitPullRequest } from "@bp/service/ * @param prUrl pull request's URL * @param comment comment body */ - createPullRequestComment(prUrl: string, comment: string): Promise; + createPullRequestComment(prUrl: string, comment: string): Promise; } \ No newline at end of file diff --git a/src/service/git/github/github-client.ts b/src/service/git/github/github-client.ts index 4e8277d..6548429 100644 --- a/src/service/git/github/github-client.ts +++ b/src/service/git/github/github-client.ts @@ -158,20 +158,27 @@ export default class GitHubClient implements GitClient { return data.html_url; } - async createPullRequestComment(prUrl: string, comment: string): Promise { - const { owner, project, id } = this.extractPullRequestData(prUrl); - const { data } = await this.octokit.issues.createComment({ - owner: owner, - repo: project, - issue_number: id, - body: comment - }); - - if (!data) { - throw new Error("Pull request comment creation failed"); + async createPullRequestComment(prUrl: string, comment: string): Promise { + let commentUrl: string | undefined = undefined; + try { + const { owner, project, id } = this.extractPullRequestData(prUrl); + const { data } = await this.octokit.issues.createComment({ + owner: owner, + repo: project, + issue_number: id, + body: comment + }); + + if (!data) { + throw new Error("Pull request comment creation failed"); + } + + commentUrl = data.url; + } catch (error) { + this.logger.error(`Error creating comment on pull request ${prUrl}: ${error}`); } - return data.url; + return commentUrl; } // UTILS diff --git a/src/service/git/gitlab/gitlab-client.ts b/src/service/git/gitlab/gitlab-client.ts index 28a92ba..da26010 100644 --- a/src/service/git/gitlab/gitlab-client.ts +++ b/src/service/git/gitlab/gitlab-client.ts @@ -162,9 +162,25 @@ export default class GitLabClient implements GitClient { return mr.web_url; } - // TODO: implement createPullRequestComment - async createPullRequestComment(prUrl: string, comment: string): Promise { - throw new Error("Method not implemented."); + // https://docs.gitlab.com/ee/api/notes.html#create-new-issue-note + async createPullRequestComment(mrUrl: string, comment: string): Promise { + const commentUrl: string | undefined = undefined; + try{ + const { namespace, project, id } = this.extractMergeRequestData(mrUrl); + const projectId = this.getProjectId(namespace, project); + + const { data } = await this.client.post(`/projects/${projectId}/issues/${id}/notes`, { + body: comment, + }); + + if (!data) { + throw new Error("Merge request comment creation failed"); + } + } catch(error) { + this.logger.error(`Error creating comment on merge request ${mrUrl}: ${error}`); + } + + return commentUrl; } // UTILS