Skip to content
This repository was archived by the owner on Aug 3, 2024. It is now read-only.

Commit ba2d8e4

Browse files
committed
feat: delete comment from database and search for another one
1 parent 4068a38 commit ba2d8e4

File tree

4 files changed

+85
-32
lines changed

4 files changed

+85
-32
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
},
7373
"license": "MPL-2.0",
7474
"dependencies": {
75+
"@octokit/request-error": "^2.0.5",
7576
"@octokit/rest": "^18.1.0",
7677
"@sentry/node": "^6.1.0",
7778
"@types/imap": "^0.8.33",

src/deploy.ts

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import dns from '@src/dns';
77
import comments from '@src/comments';
88
import data, { emailData } from '@src/data';
99
import Message, { MessagePlatform } from './modeles/Message';
10-
import messages from '@root/test/messages';
10+
import { RequestError } from '@octokit/request-error';
1111

1212
const afterComment = (
1313
configBlock: string,
@@ -75,6 +75,63 @@ const afterComment = (
7575
.catch((error: Error) => logger.error(error, prInfos, emailInfos));
7676
};
7777

78+
const findMessageAndProceed = (
79+
prId: number,
80+
emailInfos: emailData,
81+
prInfos: {
82+
sha: string;
83+
ref: string;
84+
cloneUrl: string;
85+
repoSlug: string;
86+
},
87+
configBlock: string
88+
) => {
89+
Message.forPr(prId, emailInfos.repoName).then((messages) => {
90+
if (messages.length === 0) {
91+
logger.debug('No comment found, posting one.');
92+
github
93+
.createComment(
94+
emailInfos.prId || 0,
95+
prInfos.repoSlug,
96+
comments.getPendingComment(emailInfos.commentId || 0, prInfos.ref, prInfos.sha)
97+
)
98+
.then((deployComment) =>
99+
afterComment(configBlock, emailInfos, deployComment.data.id, {
100+
sha: prInfos.sha,
101+
ref: prInfos.ref,
102+
cloneUrl: prInfos.repoSlug,
103+
repoSlug: prInfos.repoSlug,
104+
})
105+
)
106+
.catch((error: Error) => logger.error(error, prInfos, emailInfos));
107+
return;
108+
}
109+
logger.debug('Comment found, using it.');
110+
const lastMessage = messages[messages.length - 1];
111+
github
112+
.getComment(lastMessage.getCommentId(), emailInfos.repoName)
113+
.then((commentData) => {
114+
afterComment(configBlock, emailInfos, commentData.data.id, {
115+
sha: prInfos.sha,
116+
ref: prInfos.ref,
117+
cloneUrl: prInfos.repoSlug,
118+
repoSlug: prInfos.repoSlug,
119+
});
120+
})
121+
.catch((error: RequestError) => {
122+
if (error.status && error.status === 404) {
123+
logger.debug('Deleting the comment from the database.');
124+
Message.delete(lastMessage.getId()).then(() => {
125+
logger.debug('Searching for a new comment.');
126+
findMessageAndProceed(prId, emailInfos, prInfos, configBlock);
127+
});
128+
return;
129+
}
130+
logger.error(error, prInfos, emailInfos);
131+
});
132+
});
133+
};
134+
78135
export default {
79136
deploy: (emailInfos: emailData, configBlock: string) => {
80137
if (emailInfos.prId === null || emailInfos.commentId === null) {
@@ -84,41 +141,19 @@ export default {
84141
const prId = emailInfos.prId || 0;
85142
github
86143
.getPrInfos(emailInfos.prId || 0, emailInfos.repoName)
87-
.then((prInfos) => {
88-
Message.forPr(prId, emailInfos.repoName).then((messages) => {
89-
if (messages.length === 0) {
90-
logger.debug('No comment found, posting one.');
91-
github
92-
.createComment(
93-
emailInfos.prId || 0,
94-
prInfos.data.base.repo.full_name,
95-
comments.getPendingComment(
96-
emailInfos.commentId || 0,
97-
prInfos.data.head.ref,
98-
prInfos.data.head.sha
99-
)
100-
)
101-
.then((deployComment) =>
102-
afterComment(configBlock, emailInfos, deployComment.data.id, {
103-
sha: prInfos.data.head.sha,
104-
ref: prInfos.data.head.ref,
105-
cloneUrl: prInfos.data.head.repo.clone_url,
106-
repoSlug: prInfos.data.base.repo.full_name,
107-
})
108-
)
109-
.catch((error: Error) => logger.error(error, prInfos, emailInfos));
110-
return;
111-
}
112-
logger.debug('Comment found, using it.');
113-
const lastMessage = messages[messages.length - 1];
114-
afterComment(configBlock, emailInfos, lastMessage.getCommentId(), {
144+
.then((prInfos) =>
145+
findMessageAndProceed(
146+
prId,
147+
emailInfos,
148+
{
115149
sha: prInfos.data.head.sha,
116150
ref: prInfos.data.head.ref,
117151
cloneUrl: prInfos.data.head.repo.clone_url,
118152
repoSlug: prInfos.data.base.repo.full_name,
119-
});
120-
});
121-
})
153+
},
154+
configBlock
155+
)
156+
)
122157
.catch((error: Error) => logger.error(error, emailInfos));
123158
},
124159
};

src/modeles/Message.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ export default class Message {
104104
});
105105
}
106106

107+
public static delete(id: number): Promise<void> {
108+
return knex.getConnection().from('messages').where('id', id).delete();
109+
}
110+
107111
public static all(): Promise<Message[]> {
108112
return knex
109113
.getConnection()
@@ -123,6 +127,10 @@ export default class Message {
123127
return this.platform;
124128
}
125129

130+
public getId(): number {
131+
return this.id as number;
132+
}
133+
126134
public getCommentId(): number {
127135
return this.comment_id;
128136
}

yarn.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,15 @@
774774
deprecation "^2.0.0"
775775
once "^1.4.0"
776776

777+
"@octokit/request-error@^2.0.5":
778+
version "2.0.5"
779+
resolved "https://registry.yarnpkg.com/@octokit/request-error/-/request-error-2.0.5.tgz#72cc91edc870281ad583a42619256b380c600143"
780+
integrity sha512-T/2wcCFyM7SkXzNoyVNWjyVlUwBvW3igM3Btr/eKYiPmucXTtkxt2RBsf6gn3LTzaLSLTQtNmvg+dGsOxQrjZg==
781+
dependencies:
782+
"@octokit/types" "^6.0.3"
783+
deprecation "^2.0.0"
784+
once "^1.4.0"
785+
777786
"@octokit/request@^5.3.0":
778787
version "5.3.2"
779788
resolved "https://registry.yarnpkg.com/@octokit/request/-/request-5.3.2.tgz#1ca8b90a407772a1ee1ab758e7e0aced213b9883"

0 commit comments

Comments
 (0)