playlist-entry-validator/bot.ts

139 lines
4.4 KiB
TypeScript
Raw Normal View History

2022-09-12 12:45:19 +02:00
import { ApplicationFunction } from 'probot';
2022-09-13 10:03:48 +02:00
const bot: ApplicationFunction = (app) => {
app.on(
['pull_request.opened', 'pull_request.synchronize'],
async (context) => {
const registryDirectoryPath = 'playlists/registry/';
const siQueryStart = '?si=';
const pull_number = context.payload.number;
const currentRepoData = {
2022-09-13 10:03:48 +02:00
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name
};
const repoAllowlist = [
{ owner: 'mackorone', repo: 'spotify-playlist-archive' },
{ owner: 'maciejpedzich', repo: 'bot-testing-ground' }
];
const removePathFromFilename = (filename: string) =>
filename.replace(registryDirectoryPath, '');
const upsertReview = async (body: string, review_id?: number) => {
if (review_id) {
await context.octokit.pulls.updateReview({
...currentRepoData,
pull_number,
review_id,
body
});
} else {
await context.octokit.pulls.createReview({
...currentRepoData,
pull_number,
event: 'REQUEST_CHANGES',
body: `Hey there, thank you for taking interest in this project!\n${body}`
});
}
};
try {
const isAllowlistedRepo = repoAllowlist.find(
({ owner, repo }) =>
currentRepoData.owner === owner && currentRepoData.repo === repo
);
if (!isAllowlistedRepo) return;
const { data: prFiles } = await context.octokit.pulls.listFiles({
...currentRepoData,
pull_number
});
const filesToVerify = prFiles.filter(
({ status, filename }) =>
status === 'added' && filename.startsWith(registryDirectoryPath)
);
const filesWithSiQuery = filesToVerify.filter(({ filename }) =>
filename.includes(siQueryStart)
);
const playlistLookupResults = await Promise.all(
filesToVerify.map(async ({ filename }) => {
const filenameWithoutPath = removePathFromFilename(filename);
const spotifyResponse = await fetch(
`https://open.spotify.com/playlist/${filenameWithoutPath}`
);
return {
filename: removePathFromFilename(filename),
found: spotifyResponse.status === 200
};
})
);
const notFoundPlaylists = playlistLookupResults.filter(
({ found }) => !found
);
const { data: priorReviews } = await context.octokit.pulls.listReviews({
...currentRepoData,
pull_number
});
const [existingReview] = priorReviews;
if (notFoundPlaylists.length > 0) {
const renameList = notFoundPlaylists
.map(({ filename }) => `- ${filename}`)
.join('\n');
2022-09-13 10:03:48 +02:00
const body = `It looks like the following playlists don't exist:\n${renameList}`;
await upsertReview(body, existingReview?.id);
} else if (filesWithSiQuery.length > 0) {
const renameList = filesWithSiQuery
.map(({ filename }) => {
const filenameWithoutPath = removePathFromFilename(filename);
const [targetFilename] = filenameWithoutPath.split(siQueryStart);
return `- Rename ${filenameWithoutPath} to **${targetFilename}**`;
})
.join('\n');
2022-09-16 06:07:55 +02:00
const body = `All new files point to existing playlists, but you have to:\n${renameList}`;
await upsertReview(body, existingReview?.id);
} else {
2022-09-13 10:03:48 +02:00
if (existingReview) {
await context.octokit.pulls.dismissReview({
...currentRepoData,
2022-09-13 10:03:48 +02:00
pull_number,
review_id: existingReview.id,
2022-09-16 06:07:55 +02:00
message: '🎉 Your pull request can be merged! 🎉'
});
}
2022-09-13 10:03:48 +02:00
// TODO: Change successful validation handling
// await context.octokit.pulls.merge({
// ...currentRepoData,
// pull_number
// });
}
} catch (error) {
await context.octokit.pulls.createReview({
...currentRepoData,
pull_number,
event: 'COMMENT',
body: 'Something went wrong while verifying your entries! @mackorone should handle it shortly.'
});
}
}
);
2022-09-12 12:45:19 +02:00
};
2022-09-13 10:03:48 +02:00
export = bot;