Skip to content

Commit

Permalink
Merge pull request thelounge#22 from thelounge/commit-lookup
Browse files Browse the repository at this point in the history
Lookup commits by !gh <commit> command
  • Loading branch information
Max Leiter authored Feb 18, 2018
2 parents 58e7c38 + 1298343 commit 2cfc5c6
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 110 deletions.
120 changes: 57 additions & 63 deletions modules/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,80 +8,74 @@ var commands = function(bot, options, action) {
return;
}

let query;
let target = action.target;

// Only handle github commands in channels to prevent abuse
if (action.target === bot.user.nick) {
target = action.nick;
return;
}

if (action.message.startsWith(options.commandPrefix) || action.message.startsWith(options.botName)) {
if (action.message.startsWith(options.commandPrefix)) {
const message = action.message.split(" ");
const cmd = message.shift();

if (message[0] === "!github" || message[0] === "!gh") {
message.shift(); // remove the command
const arg = message[0];

if (message.length === 1) {
if (helper.stringIsPositiveInteger(arg)) {
query = helper.getIssueInformation({
user: options.githubUser,
repo: options.githubRepo,
issue: arg
});
} else {
if (arg === "search") {
query = "Search query cannot be empty";
} else {
query = action.nick + ": invalid issue/PR ID";
}
}
} else if (message.length >= 2) { // should be !gh search <query>
if (arg === "search") {
message.shift(); // remove 'search'
query = helper.searchGithub({
repo: options.githubRepo,
user: options.githubUser,
terms: message
});
} else {
query = action.nick + ": invalid command.";
}
}

if (query) {
// if it's returned as a Promise
if (typeof query.then === "function") {
query.then(function(m) {
return bot.say(target, m);
});
} else {
bot.say(target, query);
}
} else {
bot.say(target, "No result found for query");
}
if (cmd === "!github" || cmd === "!gh") {
handleSearchCommand(bot, options, action, message);
}
}

if (action.message.indexOf("#") > -1 && options.ignore.indexOf(action.nick) === -1) { // if the message contains # and isn't an ignored user
const issues = action.message.match(issueNumbersRegex);
if (issues) {
issues.forEach(function(issue) {
const issueNumber = issue.substr(1);
query = helper.getIssueInformation({
user: options.githubUser,
repo: options.githubRepo,
issue: issueNumber
});
query.then(function(m) {
return bot.say(target, m);
});
});
}
// if the message contains # and isn't an ignored user
if (action.message.indexOf("#") > -1 && options.ignore.indexOf(action.nick) === -1) {
handleIssueNumber(bot, options, action);
}
};

function handleSearchCommand(bot, options, action, message) {
const reply = (replyMsg) => bot.say(action.target, `${action.nick}: ${replyMsg}`);

if (message.length === 0) {
return reply("Usage: !gh search <query> or !gh <commit>");
}

const cmd = message.shift();

if (cmd === "search") {
if (message.length === 0) {
return reply("Search query cannot be empty.");
}

return helper.searchGithub({
repo: options.githubRepo,
user: options.githubUser,
terms: message,
}).then(reply);
}

helper.getCommitInformation({
user: options.githubUser,
repo: options.githubRepo,
commit: cmd,
}).then(reply);
}

function handleIssueNumber(bot, options, action) {
const issues = action.message.match(issueNumbersRegex);

if (issues) {
issues.forEach((issue) => {
const issueNumber = issue.substr(1);

helper.getIssueInformation({
user: options.githubUser,
repo: options.githubRepo,
issue: issueNumber,
}).then((message) => {
if (message) {
bot.say(action.target, message);
}
});
});
}
}

module.exports = {
commands
};
56 changes: 24 additions & 32 deletions modules/github_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,6 @@ fetch.Promise = require("bluebird");
const config = require("../config");
const c = require("irc-colors");

function stringIsPositiveInteger(string) {
var number = Number(string);

if (isNaN(number)) {
return false;
}

if (number === Infinity) {
return false;
}

if (number < 1) {
return false;
}

if (Math.floor(number) !== number) {
return false;
}

return true;
}

function getIssueInformation(options) {
const {repo = config.githubRepo, user = config.githubUser, issue} = options;
const url = format("https://api.github.com/repos/%s/%s/issues/%s", user, repo, issue);
Expand Down Expand Up @@ -70,23 +48,37 @@ function getIssueInformation(options) {
});
}

function getCommitInformation(options) {
const {repo = config.githubRepo, user = config.githubUser, commit} = options;
const url = format("https://api.github.com/repos/%s/%s/commits/%s", user, repo, commit);

return fetch(url)
.then((res) => res.json())
.then((res) => {
if (res.message === "Not Found") {
return "Commit not found.";
}

return `Commit by ${c.pink(res.commit.author.name)} on ${c.pink(res.commit.author.date)} - ${res.commit.message} ${res.html_url}`;
});
}

function searchGithub(options) {
const {repo = config.githubRepo, user = config.githubUser, terms} = options;
let status = null;
const url = `https://api.github.com/search/issues?q=repo:${user}/${repo}+${terms.join("+")}`;

return fetch(url)
.then((res) => res.json())
.then(function(res) {
if (res.items[0].state) {
status = res.items[0].state;
if (status === "closed") {
status = c.red(status);
} else {
status = c.green(status);
}
if (!res.items || res.items.length === 0) {
return "Nothing was found.";
}

let status = res.items[0].state;
if (status === "closed") {
status = c.red(status);
} else {
return "No issue found";
status = c.green(status);
}

const title = res.items[0].title;
Expand All @@ -110,6 +102,6 @@ function capitalizeFirstLetter(string) {

module.exports = {
getIssueInformation,
getCommitInformation,
searchGithub,
stringIsPositiveInteger
};
15 changes: 0 additions & 15 deletions tests/github.js

This file was deleted.

0 comments on commit 2cfc5c6

Please sign in to comment.