Skip to content

Commit

Permalink
Merge pull request #1278 from SwitchbladeBot/fix/commandsource-fallba…
Browse files Browse the repository at this point in the history
…ck-info

added fallback commandsource stuff when there is no local repository
  • Loading branch information
Doges authored Jan 28, 2021
2 parents a717f55 + 3a04326 commit 9954e05
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ OWLBOT_KEY=73892rbg8egy6t574bytybg8ted6tg5g3e87
LANGUAGELAYER_API_KEY=1a5fdsc2e83dddd4ad1e12a59fa585
DEBUG=false
MERRIAM_WEBSTER_API_KEY=1293a33-4731-34cb-69a3-178a39b7c23
GITHUB_USER=SwitchbladeBot
GITHUB_REPOSITORY=switchblade
GITHUB_BRANCH=master
21 changes: 11 additions & 10 deletions src/commands/bot/commandsource.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { Command, CommandError, SwitchbladeEmbed, GitUtils } = require('../../')
const { Command, SwitchbladeEmbed, GitUtils } = require('../../')
const { sep } = require('path')
const moment = require('moment')

const REPOSITORY_URL = 'https://github.com/SwitchbladeBot/switchblade'
const REPOSITORY_URL = (user, repository) => `https://github.com/${user}/${repository}`

module.exports = class CommandSource extends Command {
constructor (client) {
Expand All @@ -22,27 +22,28 @@ module.exports = class CommandSource extends Command {

async run ({ channel, author, language, t }, command) {
channel.startTyping()
const branchOrHash = await GitUtils.getHashOrBranch()
if (branchOrHash === null) {
await channel.stopTyping(true)
throw new CommandError(t('commands:commandsource.noRepositoryOrHEAD'))
}

const org = process.env.GITHUB_USER || 'SwitchbladeBot'
const repository = process.env.GITHUB_REPOSITORY || 'switchblade'
const fallbackBranch = process.env.GITHUB_BRANCH || 'master'

const branchOrHash = await GitUtils.getHashOrBranch(org, repository, fallbackBranch)

moment.locale(language)

const path = command.path.split(sep).join('/')
const { date, user } = await GitUtils.getLatestCommitInfo()
const { date, user } = await GitUtils.getLatestCommitInfo(org, repository, fallbackBranch)

channel.send(new SwitchbladeEmbed(author)
.setTitle(command.fullName)
.setDescriptionFromBlockArray([
[
!branchOrHash ? `**${t('commands:commandsource.branchNotUpToDate')}**` : '',
`[${path}](${REPOSITORY_URL}/blob/${branchOrHash || 'master'}/${path})`
`[${path}](${REPOSITORY_URL(org, repository)}/blob/${branchOrHash || 'master'}/${path})`
],
[
`${t('commands:commandsource.lastEdited', { ago: moment(date).fromNow(), user })}`,
`[\`${branchOrHash || 'master'}\`](${REPOSITORY_URL}/tree/${branchOrHash || 'master'})`
`[\`${branchOrHash || 'master'}\`](${REPOSITORY_URL(org, repository)}/tree/${branchOrHash || 'master'})`
]
])
).then(() => channel.stopTyping(true))
Expand Down
19 changes: 14 additions & 5 deletions src/utils/GitUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require('fs')
const child = require('child_process')

module.exports = class GitUtils {
static async getHashOrBranch () {
static async getHashOrBranch (user, repository, fallbackBranch = 'master') {
try {
const rev = fs.readFileSync('.git/HEAD').toString().trim()

Expand All @@ -13,7 +13,7 @@ module.exports = class GitUtils {

let res
try {
res = await axios.get(`https://api.github.com/repos/SwitchbladeBot/switchblade/commits/${branch}`)
res = await axios.get(`https://api.github.com/repos/${user}/${repository}/commits/${branch}`)
} catch (_) {
return false
}
Expand All @@ -29,12 +29,21 @@ module.exports = class GitUtils {
return rev
}
} catch (_) {
return null
let res
try {
res = await axios.get(`https://api.github.com/repos/${user}/${repository}/commits/${fallbackBranch}`)
} catch (__) {
return false
}

const sha = Array.isArray(res.data) ? res.data[0].sha : res.data.sha

return sha.length > 7 ? sha.slice(0, 7) : sha
}
}

static async getLatestCommitInfo () {
const branchOrHash = await GitUtils.getHashOrBranch()
static async getLatestCommitInfo (user, repository, fallbackBranch) {
const branchOrHash = await GitUtils.getHashOrBranch(user, repository, fallbackBranch)
if (!branchOrHash && branchOrHash !== null) {
const data = child.execSync('git log --pretty=format:"%cn | %cd"').toString()
const [user, ...date] = data.split('\n')[0].split('|')
Expand Down

0 comments on commit 9954e05

Please sign in to comment.