Skip to content

Commit a411c8b

Browse files
authored
Feat: Handle duplicate PR titles (#150)
* feat: added duplicate pr increment with number * feat: added duplicate pr titlke based on number of existing session * chore: remove title from graphql query
1 parent 730bfcf commit a411c8b

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

src/api/session.js

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
124143
export 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

Comments
 (0)