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

47
dist/cli/index.js vendored
View file

@ -948,17 +948,24 @@ class GitHubClient {
return data.html_url; return data.html_url;
} }
async createPullRequestComment(prUrl, comment) { async createPullRequestComment(prUrl, comment) {
const { owner, project, id } = this.extractPullRequestData(prUrl); let commentUrl = undefined;
const { data } = await this.octokit.issues.createComment({ try {
owner: owner, const { owner, project, id } = this.extractPullRequestData(prUrl);
repo: project, const { data } = await this.octokit.issues.createComment({
issue_number: id, owner: owner,
body: comment repo: project,
}); issue_number: id,
if (!data) { body: comment
throw new Error("Pull request comment creation failed"); });
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 // 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
/** /**

47
dist/gha/index.js vendored
View file

@ -913,17 +913,24 @@ class GitHubClient {
return data.html_url; return data.html_url;
} }
async createPullRequestComment(prUrl, comment) { async createPullRequestComment(prUrl, comment) {
const { owner, project, id } = this.extractPullRequestData(prUrl); let commentUrl = undefined;
const { data } = await this.octokit.issues.createComment({ try {
owner: owner, const { owner, project, id } = this.extractPullRequestData(prUrl);
repo: project, const { data } = await this.octokit.issues.createComment({
issue_number: id, owner: owner,
body: comment repo: project,
}); issue_number: id,
if (!data) { body: comment
throw new Error("Pull request comment creation failed"); });
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 // 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,20 +158,27 @@ 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> {
const { owner, project, id } = this.extractPullRequestData(prUrl); let commentUrl: string | undefined = undefined;
const { data } = await this.octokit.issues.createComment({ try {
owner: owner, const { owner, project, id } = this.extractPullRequestData(prUrl);
repo: project, const { data } = await this.octokit.issues.createComment({
issue_number: id, owner: owner,
body: comment repo: project,
}); issue_number: id,
body: comment
});
if (!data) { if (!data) {
throw new Error("Pull request comment creation failed"); 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 // 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