Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A #1281

Closed
wants to merge 7 commits into from
Closed

A #1281

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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