Skip to content
This repository was archived by the owner on Jun 8, 2023. It is now read-only.

Commit 111a329

Browse files
committed
github-commit-link: Add support for repo@<SHA> and user/repo@<SHA>
Based on similar parsing in github-issue-link.coffee. The main change in the logic is that the initial .* is now the non-greedy .*?, so it doesn't gobble up the repo-part of the regexp [1]. The user/repo portion is [^\s@]+, which matches one or more characters that are neither whitespace [2] nor @. The non-capturing (?:...) block [3] around the optional repo@ or user/repo@ qualifier allows us to group the qualifier with its @, even though we only need the repo or user/repo (without the @) in our internal logic. I use CoffeeScript's block regular expressions to comment the regexp [4], and CoffeeScript's existential operator '?' to check if 'msg.match[1]' is defined [5]. I also document the fact that lines matching 'commit/' are ignored. [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-questionmark [2]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-white-space [3]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-non-capturing-parentheses [4]: http://coffeescript.org/#regexes [5]: http://coffeescript.org/#operators
1 parent 712a884 commit 111a329

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/scripts/github-commit-link.coffee

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
# Configuration:
99
# HUBOT_GITHUB_REPO
1010
# The `user/repository` that you want to connect to. example: github/hubot-scripts
11+
# HUBOT_GITHUB_USER
12+
# The `user` that you want to connect to. example: github
1113
# HUBOT_GITHUB_TOKEN
1214
# You can retrieve your github token via:
1315
# curl -i https://api.github.com/authorizations -d '{"scopes":["repo"]}' -u "yourusername"
@@ -21,18 +23,33 @@
2123
# http[s]://yourdomain.com/api/v3/ for Enterprise installations.
2224
#
2325
# Commands:
24-
# Listens for <SHA> and links to the commit for your default repo on github
26+
# Listens for <SHA>s with at least seven characters:
27+
# <SHA> links to that commit in HUBOT_GITHUB_REPO
28+
# repo@<SHA> links to that commit in HUBOT_GITHUB_USER's repo
29+
# user/repo@<SHA> links to that commit in user/repo
30+
# Unless the string 'commit/' shows up in the line, in which case it
31+
# is ignored.
2532
#
2633
# Author:
2734
# achiu
2835

2936
module.exports = (robot) ->
3037
github = require("githubot")(robot)
31-
robot.hear /.*(\b[0-9a-f]{7,40}\b).*/i, (msg) ->
38+
robot.hear ///
39+
^.*? # non-greedy pre-commit-reference text
40+
\b # word boundary before the commit reference
41+
(?:([^\s@]+)@)? # optional repo@ or user/repo@ qualifier
42+
([0-9a-f]{7,40}) # commit hash (>= 7 and <= 40 hex digits long)
43+
\b # word boundary after the commit reference
44+
.*$ # post-commit-reference text
45+
///i, (msg) ->
3246
if process.env.HUBOT_GITHUB_REPO && process.env.HUBOT_GITHUB_TOKEN
3347
if !(msg.message.text.match(/commit\//))
34-
commit_sha = msg.match[1].replace /\b/, ""
35-
bot_github_repo = github.qualified_repo process.env.HUBOT_GITHUB_REPO
48+
commit_sha = msg.match[2]
49+
if msg.match[1]?
50+
bot_github_repo = github.qualified_repo msg.match[1]
51+
else
52+
bot_github_repo = github.qualified_repo process.env.HUBOT_GITHUB_REPO
3653
issue_title = ""
3754
base_url = process.env.HUBOT_GITHUB_API || 'https://api.github.com'
3855
github.get "#{base_url}/repos/#{bot_github_repo}/commits/" + commit_sha, (commit_obj) ->

0 commit comments

Comments
 (0)