Convert playlist link filename into a valid URL

This commit is contained in:
Maciej Pędzich 2023-01-27 15:39:28 +01:00
parent 5e82956fcd
commit bd54932a5e

78
bot.ts
View File

@ -23,13 +23,13 @@ export const bot: ApplicationFunction = (app) => {
{ owner: 'maciejpedzich', repo: 'bot-testing-ground' } { owner: 'maciejpedzich', repo: 'bot-testing-ground' }
]; ];
const removePathFromFilename = (filename: string) => const removeRegistryPathFromFilename = (filename: string) =>
filename.replace(registryDirectoryPath, ''); filename.replace(registryDirectoryPath, '');
const upsertReview = async ( const upsertReview = async (
review_id: number | undefined, review_id: number | undefined,
body: string, event: ReviewEvent,
event: ReviewEvent body: string
) => { ) => {
if (review_id) { if (review_id) {
await octokit.pulls.updateReview({ await octokit.pulls.updateReview({
@ -70,11 +70,13 @@ export const bot: ApplicationFunction = (app) => {
const playlistLookupResults = await Promise.all( const playlistLookupResults = await Promise.all(
filesToVerify.map(async ({ filename }) => { filesToVerify.map(async ({ filename }) => {
const filenameWithoutPath = removePathFromFilename(filename); const filenameWithoutRegistryPath = removeRegistryPathFromFilename(
filename
).replace('https:/', 'https://');
const url = getPlaylistIdFromUrl(filename) const url = getPlaylistIdFromUrl(filenameWithoutRegistryPath)
? filename ? filename
: `https://open.spotify.com/playlist/${filenameWithoutPath}`; : `https://open.spotify.com/playlist/${filenameWithoutRegistryPath}`;
const spotifyResponse = await fetch(url); const spotifyResponse = await fetch(url);
const expectedStatusCodes = [200, 404]; const expectedStatusCodes = [200, 404];
@ -99,7 +101,7 @@ export const bot: ApplicationFunction = (app) => {
} }
return { return {
filename: removePathFromFilename(filename), filename: filenameWithoutRegistryPath,
found, found,
info, info,
url url
@ -107,33 +109,14 @@ export const bot: ApplicationFunction = (app) => {
}) })
); );
let successText = `🎉 @${workingRepo.owner} can merge your pull request! 🎉`;
let reviewEvent: ReviewEvent = 'APPROVE';
let identifiedPlaylistsText = '';
const validEntries = playlistLookupResults.filter( const validEntries = playlistLookupResults.filter(
({ found, filename }) => found && !filename.includes(siQueryStart) ({ found, filename }) => found && !filename.includes(siQueryStart)
); );
const entriesToRename = playlistLookupResults.filter(
({ found, filename }) =>
found &&
(filename.includes(siQueryStart) || getPlaylistIdFromUrl(filename))
);
const notFoundPlaylists = playlistLookupResults.filter(
({ found }) => !found
);
const { data: priorReviews } = await octokit.pulls.listReviews({
...workingRepo,
pull_number
});
const [existingReview] = priorReviews;
let identifiedPlaylistsText = '';
let renameRequiredText = '';
let notFoundText = '';
let successText = `🎉 @${workingRepo.owner} can merge your pull request! 🎉`;
let reviewEvent: ReviewEvent = 'APPROVE';
if (validEntries.length > 0) { if (validEntries.length > 0) {
const playlistLinks = validEntries const playlistLinks = validEntries
.map(({ url, info }) => `- [${info}](${url})`) .map(({ url, info }) => `- [${info}](${url})`)
@ -142,11 +125,19 @@ export const bot: ApplicationFunction = (app) => {
identifiedPlaylistsText = `### ✅ These playlists have been indentified:\n${playlistLinks}`; identifiedPlaylistsText = `### ✅ These playlists have been indentified:\n${playlistLinks}`;
} }
let renameRequiredText = '';
const entriesToRename = playlistLookupResults.filter(
({ found, filename }) =>
found &&
(filename.includes(siQueryStart) || getPlaylistIdFromUrl(filename))
);
if (entriesToRename.length > 0) { if (entriesToRename.length > 0) {
const renameList = entriesToRename const renameList = entriesToRename
.map(({ filename }) => { .map(({ filename }) => {
const playlistIdFromPossibleUrl = getPlaylistIdFromUrl(filename); const playlistIdFromPossibleUrl = getPlaylistIdFromUrl(filename);
const filenameWithoutPath = removePathFromFilename(filename); const filenameWithoutPath =
removeRegistryPathFromFilename(filename);
const targetFilename = const targetFilename =
playlistIdFromPossibleUrl || playlistIdFromPossibleUrl ||
@ -161,6 +152,11 @@ export const bot: ApplicationFunction = (app) => {
renameRequiredText = `### ⚠️ These entries have to be renamed:\n${renameList}`; renameRequiredText = `### ⚠️ These entries have to be renamed:\n${renameList}`;
} }
let notFoundText = '';
const notFoundPlaylists = playlistLookupResults.filter(
({ found }) => !found
);
if (notFoundPlaylists.length > 0) { if (notFoundPlaylists.length > 0) {
const renameList = notFoundPlaylists const renameList = notFoundPlaylists
.map(({ filename }) => `- ${filename}`) .map(({ filename }) => `- ${filename}`)
@ -180,16 +176,20 @@ export const bot: ApplicationFunction = (app) => {
.filter(Boolean) .filter(Boolean)
.join('\n\n'); .join('\n\n');
await upsertReview(existingReview?.id, reviewBody, reviewEvent); const { data: reviews } = await octokit.pulls.listReviews({
...workingRepo,
pull_number
});
const [existingReview] = reviews;
await upsertReview(existingReview?.id, reviewEvent, reviewBody);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
await upsertReview(
await octokit.pulls.createReview({ undefined,
...workingRepo, 'COMMENT',
pull_number, `Something went wrong while validating new entries! @${workingRepo.owner} should handle it shortly...`
event: 'COMMENT', );
body: `Something went wrong while validating new entries! @${workingRepo.owner} should handle it shortly...`
});
} }
} }
); );