Reword review messages and improve allowlist check

This commit is contained in:
Maciej Pędzich 2022-09-14 06:53:05 +02:00
parent 14cb3c6c24
commit 327da74f58

52
bot.ts
View File

@ -7,22 +7,16 @@ const bot: ApplicationFunction = (app) => {
const registryDirectoryPath = 'playlists/registry/'; const registryDirectoryPath = 'playlists/registry/';
const siQueryStart = '?si='; const siQueryStart = '?si=';
const loginAllowlist = ['mackorone', 'maciejpedzich'];
const repoAllowlist = ['spotify-playlist-archive', 'bot-testing-ground'];
const pull_number = context.payload.number; const pull_number = context.payload.number;
const repoData = { const currentRepoData = {
owner: context.payload.repository.owner.login, owner: context.payload.repository.owner.login,
repo: context.payload.repository.name repo: context.payload.repository.name
}; };
if ( const repoAllowlist = [
!( { owner: 'mackorone', repo: 'spotify-playlist-archive' },
loginAllowlist.includes(repoData.owner) && { owner: 'maciejpedzich', repo: 'bot-testing-ground' }
repoAllowlist.includes(repoData.repo) ];
)
)
return;
const removePathFromFilename = (filename: string) => const removePathFromFilename = (filename: string) =>
filename.replace(registryDirectoryPath, ''); filename.replace(registryDirectoryPath, '');
@ -30,24 +24,31 @@ const bot: ApplicationFunction = (app) => {
const upsertReview = async (body: string, review_id?: number) => { const upsertReview = async (body: string, review_id?: number) => {
if (review_id) { if (review_id) {
await context.octokit.pulls.updateReview({ await context.octokit.pulls.updateReview({
...repoData, ...currentRepoData,
pull_number, pull_number,
review_id, review_id,
body body
}); });
} else { } else {
await context.octokit.pulls.createReview({ await context.octokit.pulls.createReview({
...repoData, ...currentRepoData,
pull_number, pull_number,
event: 'REQUEST_CHANGES', event: 'REQUEST_CHANGES',
body body: `Hey there, thank you for taking interest in this project!\n${body}`
}); });
} }
}; };
try { try {
const isAllowlistedRepo = repoAllowlist.find(
({ owner, repo }) =>
currentRepoData.owner === owner && currentRepoData.repo === repo
);
if (!isAllowlistedRepo) return;
const { data: prFiles } = await context.octokit.pulls.listFiles({ const { data: prFiles } = await context.octokit.pulls.listFiles({
...repoData, ...currentRepoData,
pull_number pull_number
}); });
@ -79,7 +80,7 @@ const bot: ApplicationFunction = (app) => {
); );
const { data: priorReviews } = await context.octokit.pulls.listReviews({ const { data: priorReviews } = await context.octokit.pulls.listReviews({
...repoData, ...currentRepoData,
pull_number pull_number
}); });
@ -103,30 +104,31 @@ const bot: ApplicationFunction = (app) => {
}) })
.join('\n'); .join('\n');
const body = `Almost there! You just have to:\n${renameList}`; const body = `All new entries point to existing playlists, but you have to:\n${renameList}`;
await upsertReview(body, existingReview?.id); await upsertReview(body, existingReview?.id);
} else { } else {
if (existingReview) { if (existingReview) {
await context.octokit.pulls.dismissReview({ await context.octokit.pulls.dismissReview({
...repoData, ...currentRepoData,
pull_number, pull_number,
review_id: existingReview.id, review_id: existingReview.id,
message: 'All entries can now be accepted.' message: '🎉 Your entries can now be accepted! 🎉'
}); });
} }
await context.octokit.pulls.merge({ // TODO: Change successful validation handling
...repoData, // await context.octokit.pulls.merge({
pull_number // ...currentRepoData,
}); // pull_number
// });
} }
} catch (error) { } catch (error) {
await context.octokit.pulls.createReview({ await context.octokit.pulls.createReview({
...repoData, ...currentRepoData,
pull_number, pull_number,
event: 'COMMENT', event: 'COMMENT',
body: 'Something went wrong while verifying changes! @mackorone should handle it shortly.' body: 'Something went wrong while verifying your entries! @mackorone should handle it shortly.'
}); });
} }
} }