feat: implement gitlab client and surround with try catch

This commit is contained in:
Andrea Lamparelli 2024-04-09 22:35:14 +02:00
parent aa4b8f5488
commit 3dd1a842af
5 changed files with 107 additions and 42 deletions

29
dist/cli/index.js vendored
View file

@ -948,6 +948,8 @@ class GitHubClient {
return data.html_url; return data.html_url;
} }
async createPullRequestComment(prUrl, comment) { async createPullRequestComment(prUrl, comment) {
let commentUrl = undefined;
try {
const { owner, project, id } = this.extractPullRequestData(prUrl); const { owner, project, id } = this.extractPullRequestData(prUrl);
const { data } = await this.octokit.issues.createComment({ const { data } = await this.octokit.issues.createComment({
owner: owner, owner: owner,
@ -958,7 +960,12 @@ class GitHubClient {
if (!data) { if (!data) {
throw new Error("Pull request comment creation failed"); throw new Error("Pull request comment creation failed");
} }
return data.url; commentUrl = data.url;
}
catch (error) {
this.logger.error(`Error creating comment on pull request ${prUrl}: ${error}`);
}
return commentUrl;
} }
// UTILS // UTILS
/** /**
@ -1201,9 +1208,23 @@ class GitLabClient {
await Promise.all(promises); await Promise.all(promises);
return mr.web_url; return mr.web_url;
} }
// TODO: implement createPullRequestComment // https://docs.gitlab.com/ee/api/notes.html#create-new-issue-note
async createPullRequestComment(prUrl, comment) { async createPullRequestComment(mrUrl, comment) {
throw new Error("Method not implemented."); 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 // UTILS
/** /**

29
dist/gha/index.js vendored
View file

@ -913,6 +913,8 @@ class GitHubClient {
return data.html_url; return data.html_url;
} }
async createPullRequestComment(prUrl, comment) { async createPullRequestComment(prUrl, comment) {
let commentUrl = undefined;
try {
const { owner, project, id } = this.extractPullRequestData(prUrl); const { owner, project, id } = this.extractPullRequestData(prUrl);
const { data } = await this.octokit.issues.createComment({ const { data } = await this.octokit.issues.createComment({
owner: owner, owner: owner,
@ -923,7 +925,12 @@ class GitHubClient {
if (!data) { if (!data) {
throw new Error("Pull request comment creation failed"); throw new Error("Pull request comment creation failed");
} }
return data.url; commentUrl = data.url;
}
catch (error) {
this.logger.error(`Error creating comment on pull request ${prUrl}: ${error}`);
}
return commentUrl;
} }
// UTILS // UTILS
/** /**
@ -1166,9 +1173,23 @@ class GitLabClient {
await Promise.all(promises); await Promise.all(promises);
return mr.web_url; return mr.web_url;
} }
// TODO: implement createPullRequestComment // https://docs.gitlab.com/ee/api/notes.html#create-new-issue-note
async createPullRequestComment(prUrl, comment) { async createPullRequestComment(mrUrl, comment) {
throw new Error("Method not implemented."); 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 // UTILS
/** /**

View file

@ -49,6 +49,6 @@ import { BackportPullRequest, GitClientType, GitPullRequest } from "@bp/service/
* @param prUrl pull request's URL * @param prUrl pull request's URL
* @param comment comment body * @param comment comment body
*/ */
createPullRequestComment(prUrl: string, comment: string): Promise<string>; createPullRequestComment(prUrl: string, comment: string): Promise<string | undefined>;
} }

View file

@ -158,7 +158,9 @@ export default class GitHubClient implements GitClient {
return data.html_url; return data.html_url;
} }
async createPullRequestComment(prUrl: string, comment: string): Promise<string> { async createPullRequestComment(prUrl: string, comment: string): Promise<string | undefined> {
let commentUrl: string | undefined = undefined;
try {
const { owner, project, id } = this.extractPullRequestData(prUrl); const { owner, project, id } = this.extractPullRequestData(prUrl);
const { data } = await this.octokit.issues.createComment({ const { data } = await this.octokit.issues.createComment({
owner: owner, owner: owner,
@ -171,7 +173,12 @@ export default class GitHubClient implements GitClient {
throw new Error("Pull request comment creation failed"); throw new Error("Pull request comment creation failed");
} }
return data.url; commentUrl = data.url;
} catch (error) {
this.logger.error(`Error creating comment on pull request ${prUrl}: ${error}`);
}
return commentUrl;
} }
// UTILS // UTILS

View file

@ -162,9 +162,25 @@ export default class GitLabClient implements GitClient {
return mr.web_url; return mr.web_url;
} }
// TODO: implement createPullRequestComment // https://docs.gitlab.com/ee/api/notes.html#create-new-issue-note
async createPullRequestComment(prUrl: string, comment: string): Promise<string> { async createPullRequestComment(mrUrl: string, comment: string): Promise<string | undefined> {
throw new Error("Method not implemented."); 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 // UTILS