@@ -121,6 +121,25 @@ export async function numberOfOpenClosedSessions(
121121 }
122122}
123123
124+ async function numberOfSessionBasedOnTitle ( octokit , githubConfig , prName ) {
125+ try {
126+ const query = `
127+ query($queryString: String!) {
128+ search(query: $queryString, type: ISSUE, last: 1) {
129+ issueCount
130+ }
131+ }
132+ ` ;
133+
134+ const response = await octokit . graphql ( query , {
135+ queryString : `repo:${ githubConfig . username } /${ githubConfig . repo } in:title '${ prName } ' is:pr` ,
136+ } ) ;
137+ return response . search . issueCount ;
138+ } catch ( error ) {
139+ return error ;
140+ }
141+ }
142+
124143export async function createSession ( octokit , githubConfig , prName ) {
125144 const { username : owner , repo } = githubConfig ;
126145 const username = ( await octokit . rest . users . getAuthenticated ( ) ) . data . login ;
@@ -238,10 +257,28 @@ export async function createSession(octokit, githubConfig, prName) {
238257 number : res . data . number ,
239258 } ;
240259 } catch ( error ) {
241- return {
242- text : error . message . replaceAll ( "Reference" , "Session" ) ,
243- status : "error" ,
244- } ;
260+ if ( error . status === 422 && error . response ?. url ?. includes ( "/refs" ) ) {
261+ const numberOfSession = await numberOfSessionBasedOnTitle (
262+ octokit ,
263+ githubConfig ,
264+ `${ prName } (` ,
265+ ) ;
266+
267+ if ( numberOfSession . message ) {
268+ return {
269+ text : numberOfSession . message ,
270+ status : "error" ,
271+ } ;
272+ } else {
273+ const newSessionName = `${ prName } (${ numberOfSession + 1 } )` ;
274+ return createSession ( octokit , githubConfig , newSessionName ) ;
275+ }
276+ } else {
277+ return {
278+ text : error . message ,
279+ status : "error" ,
280+ } ;
281+ }
245282 }
246283}
247284
0 commit comments