@@ -7,7 +7,7 @@ import dns from '@src/dns';
77import comments from '@src/comments' ;
88import data , { emailData } from '@src/data' ;
99import Message , { MessagePlatform } from './modeles/Message' ;
10- import messages from '@root/test/messages ' ;
10+ import { RequestError } from '@octokit/request-error ' ;
1111
1212const 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+
78135export 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} ;
0 commit comments