2022-09-12 12:45:19 +02:00
|
|
|
import { ApplicationFunction } from 'probot';
|
2022-09-21 22:39:54 +02:00
|
|
|
import getMetaData from 'metadata-scraper';
|
2022-09-12 12:45:19 +02:00
|
|
|
|
2022-09-13 10:03:48 +02:00
|
|
|
const bot: ApplicationFunction = (app) => {
|
2022-09-12 20:58:06 +02:00
|
|
|
app.on(
|
|
|
|
['pull_request.opened', 'pull_request.synchronize'],
|
|
|
|
async (context) => {
|
|
|
|
const registryDirectoryPath = 'playlists/registry/';
|
|
|
|
const siQueryStart = '?si=';
|
2022-09-13 15:32:52 +02:00
|
|
|
|
2022-09-12 20:58:06 +02:00
|
|
|
const pull_number = context.payload.number;
|
2022-09-21 22:39:54 +02:00
|
|
|
const workingRepo = {
|
2022-09-13 10:03:48 +02:00
|
|
|
owner: context.payload.repository.owner.login,
|
|
|
|
repo: context.payload.repository.name
|
2022-09-12 20:58:06 +02:00
|
|
|
};
|
|
|
|
|
2022-09-14 06:53:05 +02:00
|
|
|
const repoAllowlist = [
|
|
|
|
{ owner: 'mackorone', repo: 'spotify-playlist-archive' },
|
|
|
|
{ owner: 'maciejpedzich', repo: 'bot-testing-ground' }
|
|
|
|
];
|
2022-09-13 15:32:52 +02:00
|
|
|
|
2022-09-12 20:58:06 +02:00
|
|
|
const removePathFromFilename = (filename: string) =>
|
|
|
|
filename.replace(registryDirectoryPath, '');
|
|
|
|
|
2022-09-21 22:39:54 +02:00
|
|
|
const upsertReview = async (
|
|
|
|
review_id: number | undefined,
|
|
|
|
body: string,
|
|
|
|
event: 'REQUEST_CHANGES' | 'COMMENT'
|
|
|
|
) => {
|
2022-09-12 20:58:06 +02:00
|
|
|
if (review_id) {
|
2022-09-22 06:14:31 +02:00
|
|
|
await context.octokit.pulls.updateReview({
|
2022-09-21 22:39:54 +02:00
|
|
|
...workingRepo,
|
2022-09-12 20:58:06 +02:00
|
|
|
pull_number,
|
|
|
|
review_id,
|
|
|
|
body
|
|
|
|
});
|
|
|
|
} else {
|
2022-09-22 06:14:31 +02:00
|
|
|
await context.octokit.pulls.createReview({
|
2022-09-21 22:39:54 +02:00
|
|
|
...workingRepo,
|
2022-09-12 20:58:06 +02:00
|
|
|
pull_number,
|
2022-09-21 22:39:54 +02:00
|
|
|
event,
|
|
|
|
body
|
2022-09-12 20:58:06 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2022-09-21 22:39:54 +02:00
|
|
|
const allowlistedRepo = repoAllowlist.find(
|
2022-09-14 06:53:05 +02:00
|
|
|
({ owner, repo }) =>
|
2022-09-21 22:39:54 +02:00
|
|
|
workingRepo.owner === owner && workingRepo.repo === repo
|
2022-09-14 06:53:05 +02:00
|
|
|
);
|
|
|
|
|
2022-09-21 22:39:54 +02:00
|
|
|
if (!allowlistedRepo) return;
|
2022-09-14 06:53:05 +02:00
|
|
|
|
2022-09-12 20:58:06 +02:00
|
|
|
const { data: prFiles } = await context.octokit.pulls.listFiles({
|
2022-09-21 22:39:54 +02:00
|
|
|
...workingRepo,
|
2022-09-12 20:58:06 +02:00
|
|
|
pull_number
|
|
|
|
});
|
|
|
|
|
|
|
|
const filesToVerify = prFiles.filter(
|
|
|
|
({ status, filename }) =>
|
|
|
|
status === 'added' && filename.startsWith(registryDirectoryPath)
|
|
|
|
);
|
|
|
|
|
|
|
|
const playlistLookupResults = await Promise.all(
|
|
|
|
filesToVerify.map(async ({ filename }) => {
|
|
|
|
const filenameWithoutPath = removePathFromFilename(filename);
|
2022-09-21 22:39:54 +02:00
|
|
|
const url = `https://open.spotify.com/playlist/${filenameWithoutPath}`;
|
|
|
|
|
|
|
|
const spotifyResponse = await fetch(url);
|
|
|
|
const found = spotifyResponse.status === 200;
|
2022-09-22 06:14:31 +02:00
|
|
|
|
2022-09-21 22:39:54 +02:00
|
|
|
let info: string | null = null;
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
const html = await spotifyResponse.text();
|
|
|
|
const { title, description } = await getMetaData({ html });
|
|
|
|
const playlistMeta = (description || '')
|
|
|
|
.split(' · ')
|
|
|
|
.filter((text) => text !== 'Playlist');
|
|
|
|
|
|
|
|
info = [title, ...playlistMeta].join(' · ');
|
|
|
|
}
|
2022-09-12 20:58:06 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
filename: removePathFromFilename(filename),
|
2022-09-21 22:39:54 +02:00
|
|
|
found,
|
|
|
|
info,
|
|
|
|
url
|
2022-09-12 20:58:06 +02:00
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2022-09-21 22:39:54 +02:00
|
|
|
const validEntries = playlistLookupResults.filter(
|
|
|
|
({ found, filename }) => found && !filename.includes(siQueryStart)
|
|
|
|
);
|
|
|
|
|
|
|
|
const entriesWithSiQuery = playlistLookupResults.filter(
|
|
|
|
({ found, filename }) => found && filename.includes(siQueryStart)
|
|
|
|
);
|
|
|
|
|
2022-09-12 20:58:06 +02:00
|
|
|
const notFoundPlaylists = playlistLookupResults.filter(
|
|
|
|
({ found }) => !found
|
|
|
|
);
|
|
|
|
|
|
|
|
const { data: priorReviews } = await context.octokit.pulls.listReviews({
|
2022-09-21 22:39:54 +02:00
|
|
|
...workingRepo,
|
2022-09-12 20:58:06 +02:00
|
|
|
pull_number
|
|
|
|
});
|
|
|
|
|
|
|
|
const [existingReview] = priorReviews;
|
2022-09-21 22:39:54 +02:00
|
|
|
|
|
|
|
let identifiedPlaylistsText = '';
|
|
|
|
let renameRequiredText = '';
|
|
|
|
let notFoundText = '';
|
2022-09-22 06:14:31 +02:00
|
|
|
let successText = `🎉 @${workingRepo.owner} can merge your pull request! 🎉`;
|
2022-09-21 22:39:54 +02:00
|
|
|
let reviewEvent: 'REQUEST_CHANGES' | 'COMMENT' = 'COMMENT';
|
|
|
|
|
|
|
|
if (validEntries.length > 0) {
|
|
|
|
const playlistLinks = validEntries
|
|
|
|
.map(({ url, info }) => `- [${info}](${url})`)
|
|
|
|
.join('\n');
|
|
|
|
|
|
|
|
identifiedPlaylistsText = `### ✅ These playlists were indentified:\n${playlistLinks}`;
|
|
|
|
}
|
2022-09-12 20:58:06 +02:00
|
|
|
|
|
|
|
if (notFoundPlaylists.length > 0) {
|
|
|
|
const renameList = notFoundPlaylists
|
|
|
|
.map(({ filename }) => `- ${filename}`)
|
|
|
|
.join('\n');
|
|
|
|
|
2022-09-21 22:39:54 +02:00
|
|
|
successText = '';
|
|
|
|
reviewEvent = 'REQUEST_CHANGES';
|
|
|
|
notFoundText = `### ❌ Playlists for these entries were not found:\n${renameList}`;
|
|
|
|
}
|
2022-09-13 10:03:48 +02:00
|
|
|
|
2022-09-21 22:39:54 +02:00
|
|
|
if (entriesWithSiQuery.length > 0) {
|
|
|
|
const renameList = entriesWithSiQuery
|
2022-09-13 10:03:48 +02:00
|
|
|
.map(({ filename }) => {
|
|
|
|
const filenameWithoutPath = removePathFromFilename(filename);
|
|
|
|
const [targetFilename] = filenameWithoutPath.split(siQueryStart);
|
|
|
|
|
2022-09-21 22:39:54 +02:00
|
|
|
return `- From ${filenameWithoutPath} to **${targetFilename}**`;
|
2022-09-13 10:03:48 +02:00
|
|
|
})
|
|
|
|
.join('\n');
|
|
|
|
|
2022-09-21 22:39:54 +02:00
|
|
|
successText = '';
|
|
|
|
reviewEvent = 'REQUEST_CHANGES';
|
|
|
|
renameRequiredText = `### ⚠️ These entries have to be renamed:\n${renameList}`;
|
|
|
|
}
|
2022-09-12 20:58:06 +02:00
|
|
|
|
2022-09-21 22:39:54 +02:00
|
|
|
const reviewBody = [
|
|
|
|
identifiedPlaylistsText,
|
|
|
|
renameRequiredText,
|
|
|
|
notFoundText,
|
|
|
|
successText
|
|
|
|
]
|
|
|
|
.filter(Boolean)
|
|
|
|
.join('\n\n');
|
|
|
|
|
|
|
|
await upsertReview(existingReview?.id, reviewBody, reviewEvent);
|
|
|
|
|
|
|
|
if (
|
|
|
|
renameRequiredText === '' &&
|
|
|
|
notFoundText === '' &&
|
|
|
|
existingReview?.id
|
|
|
|
) {
|
|
|
|
await context.octokit.pulls.dismissReview({
|
|
|
|
...workingRepo,
|
|
|
|
pull_number,
|
|
|
|
review_id: existingReview.id,
|
2022-09-22 06:14:31 +02:00
|
|
|
message: 'All new entries can be accepted!'
|
2022-09-21 22:39:54 +02:00
|
|
|
});
|
2022-09-12 20:58:06 +02:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2022-09-21 22:39:54 +02:00
|
|
|
console.error(error);
|
|
|
|
|
2022-09-12 20:58:06 +02:00
|
|
|
await context.octokit.pulls.createReview({
|
2022-09-21 22:39:54 +02:00
|
|
|
...workingRepo,
|
2022-09-12 20:58:06 +02:00
|
|
|
pull_number,
|
|
|
|
event: 'COMMENT',
|
2022-09-22 06:14:31 +02:00
|
|
|
body: `Something went wrong while validating new entries! @${workingRepo.owner} should handle it shortly...`
|
2022-09-12 20:58:06 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2022-09-12 12:45:19 +02:00
|
|
|
};
|
|
|
|
|
2022-09-13 10:03:48 +02:00
|
|
|
export = bot;
|