Skip to content

Commit

Permalink
feat: Implement repository existence check and update GitHub commands…
Browse files Browse the repository at this point in the history
… to use it
  • Loading branch information
sldless committed Jan 27, 2025
1 parent 2b245be commit 0eb406c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
17 changes: 17 additions & 0 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,21 @@ module.exports = class Streamyfin extends Client {
];
}
};

async repoCheck(repoName) {
repoName = repoName.replace(/\s+/g, '-');
try {
const response = await axios.get(`https://api.github.com/repos/${this.repoOrg}/${repoName}`);
if (response.data && response.data?.id) {
return { exists: true, data: response.data };
} else {
return { exists: false };
}
} catch (error) {
console.error('Error checking repository:', error.message);
return { exists: false, error: error.message };
}
}


};
19 changes: 17 additions & 2 deletions commands/github/issue.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,33 @@ module.exports = {
data: new SlashCommandBuilder()
.setName('issue')
.setDescription('Get GitHub issue details.')
.addStringOption(option =>
option.setName('repo')
.setDescription("Repo name of your choice defaults to streamyfin")
.setRequired(false)
)
.addIntegerOption(option =>
option.setName('number')
.setDescription('The issue number')
.setRequired(false)
),
async run(interaction) {
const issueNumber = interaction.options.getInteger("number");
let repoName = interaction.options.getString("repo");

if (repoName) {
if (!issueNumber) return interaction.reply({ content: "Please provide an issue number.", ephemeral: true})
}

if (!repoName) repoName = interaction.client.repoName
let repoCheck = await interaction.client.repoCheck(repoName)
if (!repoCheck.exists) {
return interaction.reply({content: `${repoName} does not exist.`, ephemeral: true})
}
if (issueNumber) {
try {
const response = await axios.get(
`https://api.github.com/repos/${interaction.client.repoOrg}/${interaction.client.repoName}/issues/${issueNumber}`,
`https://api.github.com/repos/${interaction.client.repoOrg}/${repoName}/issues/${issueNumber}`,
{
headers: {
Authorization: `token ${interaction.client.githubToken}`,
Expand All @@ -29,7 +44,7 @@ module.exports = {
`🔗 **Issue #${issue.number}: ${issue.title}**\n${issue.html_url}`
);
} catch (error) {
await interaction.reply("❌ Issue not found or an error occurred.");
await interaction.reply(`❌ Issue not found for ${repoName} or an error occurred.`);
}
return;
}
Expand Down
22 changes: 11 additions & 11 deletions commands/github/repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,42 @@ module.exports = {

if (repoName) {
try {
repoName = repoName.replace(/\s+/g, '-');
const repo = await axios.get(`https://api.github.com/repos/${interaction.client.repoOrg}/${repoName}`).then(r => r.data)
if (!repo) return interaction.reply("Repo does not exist under Streamyfin")
const repo = await interaction.client.repoCheck(repoName)
if (!repo.exists) return interaction.reply("Repo does not exist under Streamyfin")
const embed = {
title: `Repository: ${repo.name}`,
title: `Repository: ${repo.data.name}`,
color: 0x6A0DAD,
description: repo.description || "No description.",
description: repo.data.description || "No description.",
thumbnail: {
url: repo.owner.avatar_url
url: repo.data.owner.avatar_url
},
fields: [
{
name: "Forks",
value: repo.forks_count.toLocaleString(),
value: repo.data.forks_count.toLocaleString(),
inline: true
},
{
name: "Open Issues",
value: repo.open_issues_count.toLocaleString(),
value: repo.data.open_issues_count.toLocaleString(),
inline: true
},
{
name: "Stars",
value: repo.stargazers_count.toLocaleString(),
value: repo.data.stargazers_count.toLocaleString(),
inline: true
},
{
name: "Language",
value: repo.language || "Not specified",
value: repo.data.language || "Not specified",
inline: true
},
],
url: repo.html_url
url: repo.data.html_url
};
await interaction.reply({ embeds: [embed] });
} catch (error) {
console.log(error)
await interaction.reply("❌ Repo not found or an error occurred.");
}
return;
Expand Down

0 comments on commit 0eb406c

Please sign in to comment.