Skip to content

Commit

Permalink
plugins: Move hard coded messages out of code
Browse files Browse the repository at this point in the history
This begins to move the large chunks of hard coded messages out of
code and into markdown template files.

Closes #257
  • Loading branch information
andrewda committed Dec 6, 2017
1 parent f3ba1ec commit 11ce167
Show file tree
Hide file tree
Showing 26 changed files with 135 additions and 172 deletions.
5 changes: 5 additions & 0 deletions .coafile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ bears = LineLengthBear
[all.links]
bears = InvalidLinkBear

[jinja2]
files = plugins/templates/**/*.jinja2.md
bears = Jinja2Bear
default_actions = Jinja2Bear: ApplyPatchAction

[commit]
bears = GitCommitBear
shortlog_regex = ([^:]*|[^:]+[^ ]: [A-Z0-9*].*)
Expand Down
108 changes: 25 additions & 83 deletions plugins/explain.py
Original file line number Diff line number Diff line change
@@ -1,103 +1,45 @@
import re
import glob
import os.path

from errbot import BotPlugin, re_botcmd
from errbot.templating import tenv


class Explain(BotPlugin):
"""
Explain various terms
"""

MSGS = {
'review': 'After creating your `Pull Request`, it is under the review '
'process. This can be deduced from the `process/pending '
'review` label. Now you have to wait for the reviewers to '
'review your PR. You should *not* ask for reviews on our '
'Gitter channel - we review those PRs continuously.\n\n'
'We\'re usually swamped with reviews, while you are waiting '
'**please review other people\'s PRs** at [coala.io/review]'
'(https://coala.io/review): that helps you and will make '
'your review happen faster as well. As a rule of thumb, '
'*for every review you receive, give at least one review '
'to someone else!*\n\nFor a good review, look at every '
'commit on its own and place `ack <sha>`(commit is ready) or '
'`unack <sha>(commit needs work) needs work` comments on the '
'pull request, be sure to remove other spacing like tabs. If '
'you\'re done with a pull request, you can use '
'`{bot_prefix} mark wip <pull URL>` to mark it *work in '
'progress* finally.',
'closes': 'We use bug prediction in coala which relies on the `Fixes` '
'keyword in commit messages. To get good results from that '
'we need to use `Closes` for normal issues instead of `Fixes`'
' which should only be used for real bugs. (See also [the '
'commit message docs](https://coala.io/commit).) To change '
'your message you just use `git commit --amend` and then '
'`git push --force` the new commit to replace the old one.',
'fixes': 'We use bug prediction in coala which relies on the `Fixes` '
'keyword in commit messages. To get good results from that '
'we need to use `Fixes` for bugfix issues instead of '
'`Closes`. (See also [the commit message docs]'
'(https://coala.io/commit).) To change your message you '
'just use `git commit --amend` and then `git push --force` '
'the new commit to replace the old one.',
'commit message': 'To change your message you just use `git commit '
'--amend` and then `git push --force` the new '
'commit to replace the old one.\n\nIf you\'re just '
'looking to fix an issue very quickly and not '
'interested in contributing to coala long term, we '
'can fix up the message for you - just tell us :).',
'rebase': 'It looks like your PR is out of date and needs a rebase.'
'\n\n[This page](https://coala.io/rebase) may help you to get'
' started on this. We also have [a quick video tutorial on '
'how to rebase](https://asciinema.org/a/78683). That should '
'help you understand the basics of how it works and what you'
'should be doing.\n\nIf you\'re just looking to fix an issue '
'very quickly and not interested in contributing to coala '
'long term, we can fix it up for you - just tell us :).',
'cep': 'At coala we\'re using [cEP\'s (coala Enhancement Proposals)]'
'(http://coala.io/cep) to define major design decisions - '
'they\'re a bit like PEP\'s but not quite as extensive and '
'obviously written with a lower case c.',
'gitlab': 'We are currently evaluating on if we want to use GitLab for'
'code hosting. That\'s why some repositories are already on '
'GitLab, if you want to participate in the migration '
'discussion, please add information [at our GitLab wiki page]'
'(https://github.com/coala/coala/wiki/GitLab).',
'google': 'Hey. This message was triggered because someone was too '
'lazy to type this *again*. Don\'t take it personally. '
'Please.\n\nWe all got to learn this: *use google*. Or '
'duckduckgo. Anything. The search engine that earned your '
'trust. You got a build error? Search for the first red '
'thing and google it. You got an exception? *Read the '
'message.* Search it. *Think.*\n\nKeep this in mind: *You*'
'are sitting in front of the problem, not us. You will have '
'a much easier time solving it. That\'s why you should try '
'doing it first.',
'promotion': 'To become part of the coala developers team, there '
'are a few steps you need to complete. The newcomer '
'process is as follows:\nYou will start as a newcomer, '
'which is kind of a trial. If you complete the following '
'tasks, you will become a developer at coala:\n\n- run '
'coala on a project of yours\n- merge a difficulty/'
'newcomer Pull Request\n- review at least a difficulty/'
'newcomer Pull Request\n- merge a difficulty/low Pull '
'Request\n- review at least a difficulty/low or higher '
'Pull Request'
}
files = glob.glob('plugins/templates/explain/*.jinja2.md')
KNOWN_KEYS = []
for fname in files:
KNOWN_KEYS.append(fname.replace(
'plugins/templates/explain/', ''
).replace('.jinja2.md', ''))

ERROR_MSG = (
'Sorry, I only know about these things:\n- ' +
'\n- '.join(MSGS.keys())
'\n- '.join(KNOWN_KEYS)
)

@re_botcmd(pattern=r'^explain\s+(\w+)(?:\s+to\s+@?([\w-]+))?$',
re_cmd_name_help='explain <term>',
flags=re.IGNORECASE)
def explain(self, msg, match):
"""Explain various terms.""" # Ignore QuotesBear
return ('{}'.format('@{}: \n'.format(match.group(2))
if match.group(2) else '') +
self.MSGS.get(
match.group(1).lower(),
self.ERROR_MSG
).format(bot_prefix=self.bot_config.BOT_PREFIX))
user = msg.frm.nick
response = ''
filename = 'explain/{}.jinja2.md'.format(match.group(1).lower())
if match.group(1).lower() in self.KNOWN_KEYS:
if match.group(2):
response += '@{}: \n'.format(match.group(2))
response += tenv().get_template(filename).render(
username=user,
target=match.group(2),
bot_prefix=self.bot_config.BOT_PREFIX,
)
else:
response = self.ERROR_MSG

return response
89 changes: 41 additions & 48 deletions plugins/labhub.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from IGitt.GitHub.GitHub import GitHub, GitHubToken
from IGitt.GitLab.GitLab import GitLab, GitLabPrivateToken
from errbot import BotPlugin, re_botcmd
from errbot.templating import tenv

from plugins import constants
from utils.backends import message_link
Expand All @@ -16,31 +17,6 @@
class LabHub(BotPlugin):
"""GitHub and GitLab utilities""" # Ignore QuotesBear

INVITE_SUCCESS = {
'newcomers': 'Welcome @{}! :tada:\n\nWe\'ve just sent you an invite'
' via email. Please accept it before proceeding forward.'
'\nTo get started, please follow our [newcomers guide]'
'(https://coala.io/newcomer). Most '
'issues will be explained there and in linked pages - it '
'will save you a lot of time, just read it. *Really.*\n\n'
'*Do not take an issue if you don\'t understand it on '
'your own.*Especially if you are new you have to be aware '
'that getting started with an open source community is '
'not trivial: you will have to work hard and most likely '
'become a better coder than you are now just as we all '
'did.\n\nDon\'t get us wrong: we are *very* glad to have '
'you with us on this journey into open source! We will '
'also be there for you at all times to help you with '
'actual problems. :)',
'developers': ' Wow @{}, you are a part of developers team now! :tada: '
'Welcome to our community! You were a newcomer before, '
'and we\'d like to know what could\'ve been better, '
'please fill http://coala.io/newform',
'maintainers': ' @{} you seem to be awesome! You are now a maintainer! '
':tada: Please go through '
'https://github.com/coala/coala/wiki/Membership'
}

GH_ORG_NAME = constants.GH_ORG_NAME
GL_ORG_NAME = constants.GL_ORG_NAME

Expand Down Expand Up @@ -108,7 +84,12 @@ def invite_cmd(self, msg, match):

if invitee == 'me':
user = msg.frm.nick
self.send(msg.frm, self.INVITE_SUCCESS['newcomers'].format(user))
response = tenv().get_template(
'labhub/promotions/newcomers.jinja2.md'
).render(
username=user,
)
self.send(msg.frm, response)
self.TEAMS[self.GH_ORG_NAME + ' newcomers'].invite(user)
self.invited_users.add(user)
return
Expand All @@ -129,10 +110,18 @@ def invite_cmd(self, msg, match):

# send the invite
self.TEAMS[team_mapping[team.lower()]].invite(invitee)
return self.INVITE_SUCCESS[team.lower()].format(invitee)
return tenv().get_template(
'labhub/promotions/{}.jinja2.md'.format(team.lower())
).render(
target=invitee,
)
else:
return ('@{}, you are not a maintainer, only maintainers can invite'
' other people. Nice try :poop:'.format(inviter))
return tenv().get_template(
'labhub/errors/not-maintainer.jinja2.md'
).render(
action='invite other people',
target=invitee,
)

def callback_message(self, msg):
"""Invite the user whose message includes the holy 'hello world'"""
Expand All @@ -141,8 +130,12 @@ def callback_message(self, msg):
if (not self.TEAMS[self.GH_ORG_NAME + ' newcomers'].is_member(user)
and user not in self.invited_users):
# send the invite
self.send(msg.frm,
self.INVITE_SUCCESS['newcomers'].format(user))
response = tenv().get_template(
'labhub/promotions/newcomers.jinja2.md'
).render(
target=user,
)
self.send(msg.frm, response)
self.TEAMS[self.GH_ORG_NAME + ' newcomers'].invite(user)
self.invited_users.add(user)

Expand All @@ -151,11 +144,12 @@ def callback_message(self, msg):
flags=re.IGNORECASE)
def create_issue_cmd(self, msg, match):
"""Create issues on GitHub and GitLab repositories.""" # Ignore QuotesBear, LineLengthBear, PyCodeStyleBear
user = msg.frm.nick
repo_name = match.group(1)
iss_title = match.group(2)
iss_description = match.group(3) if match.group(3) is not None else ''
extra_msg = '\nOpened by @{username} at [{backend}]({msg_link})'.format(
username=msg.frm.nick,
username=user,
backend=self.bot_config.BACKEND,
msg_link=message_link(self, msg)
)
Expand All @@ -165,9 +159,11 @@ def create_issue_cmd(self, msg, match):
iss = repo.create_issue(iss_title, iss_description + extra_msg)
return 'Here you go: {}'.format(iss.url)
else:
return ('Can\'t create an issue for a repository that does not '
'exist. Please ensure that the repository is available '
'and owned by the org.')
return tenv().get_template(
'labhub/errors/no-repository.jinja2.md'
).render(
target=user,
)

@re_botcmd(pattern=r'^unassign\s+https://(github|gitlab)\.com/([^/]+)/([^/]+)/issues/(\d+)', # Ignore LineLengthBear, PyCodeStyleBear
re_cmd_name_help='unassign <complete-issue-URL>',
Expand Down Expand Up @@ -312,15 +308,6 @@ def eligible(user, iss):
return False
return True

eligility_conditions = [
'- You must be a member of {} org to be assigned an issue '
'If you are not a member yet, just type Hello World and '
'corobo will invite you.'.format(self.GH_ORG_NAME),
'- A newcomer cannot be assigned to an issue with a difficulty '
'level higher than newcomer or low difficulty.',
'- A newcomer cannot be assigned to unlabelled issues.'
]

try:
iss = self.REPOS[repo_name].get_issue(int(iss_number))
except KeyError:
Expand All @@ -333,13 +320,19 @@ def eligible(user, iss):
'issue. :tada:')
else:
yield 'You are not eligible to be assigned to this issue.'
yield '\n'.join(eligility_conditions)
yield tenv().get_template(
'labhub/errors/not-eligible.jinja2.md'
).render(
organization=self.GH_ORG_NAME,
)
elif user in iss.assignees:
yield ('The issue is already assigned to you.')
else:
yield ('The issue is already assigned to someone. Please '
'check if the assignee is still working on the issue, '
'if not, you should ask for reassignment.')
yield tenv().get_template(
'labhub/errors/already-assigned.jinja2.md'
).render(
username=user,
)

@staticmethod
def community_state(pr_count: dict):
Expand Down
18 changes: 3 additions & 15 deletions plugins/lmgtfy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,9 @@ class Lmgtfy(BotPlugin):
question rather than search it for themselves.
"""

MSG = (
'Hey! You seem to have asked a question which could have been solved '
'by other means as well. Click [this]({}). When you ask a question, be '
'sure to try out a few things first - you\'re in a much better position'
' to help yourself than we are. Try googling, thinking, searching on '
'GitHub or [git grep](https://git-scm.com/docs/git-grep) if you are '
'looking for source code.'
'[StackOverflow](http://stackoverflow.com/help/how-to-ask) '
'has a good guide on how to ask questions in a way that allows us to '
'help you even faster!'
)

@re_botcmd(pattern=r'lmgtfy\s+(.+)',
re_cmd_name_help='lmgtfy <search-string>')
re_cmd_name_help='lmgtfy <search-string>',
template='lmgtfy.jinja2')
def lmgtfy(self, msg, match):
"""I'm lazy, please google for me.""" # Ignore QuotesBear
link = 'https://www.lmgtfy.com/?q=' + match.group(1)
return self.MSG.format(link)
return {'query': match.group(1)}
1 change: 1 addition & 0 deletions plugins/templates/explain/cep.jinja2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
At coala we're using [cEP's (coala Enhancement Proposals)](http://coala.io/cep) to define major design decisions - they're a bit like PEP's but not quite as extensive and obviously written with a lower case c.
1 change: 1 addition & 0 deletions plugins/templates/explain/closes.jinja2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
We use bug prediction in coala which relies on the `Fixes` keyword in commit messages. To get good results from that we need to use `Closes` for normal issues instead of `Fixes` which should only be used for real bugs. (See also [the commit message docs](https://coala.io/commit).) To change your message you just use `git commit --amend` and then `git push --force` the new commit to replace the old one.
3 changes: 3 additions & 0 deletions plugins/templates/explain/commit.jinja2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
To change your message you just use `git commit --amend` and then `git push --force` the new commit to replace the old one.

If you're just looking to fix an issue very quickly and not interested in contributing to coala long term, we can fix up the message for you - just tell us :).
1 change: 1 addition & 0 deletions plugins/templates/explain/fixes.jinja2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
We use bug prediction in coala which relies on the `Fixes` keyword in commit messages. To get good results from that we need to use `Fixes` for bugfix issues instead of `Closes`. (See also [the commit message docs](https://coala.io/commit).) To change your message you just use `git commit --amend` and then `git push --force` the new commit to replace the old one.
1 change: 1 addition & 0 deletions plugins/templates/explain/gitlab.jinja2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
We are currently evaluating on if we want to use GitLab for code hosting. That's why some repositories are already on GitLab, if you want to participate in the migration discussion, please add information [at our GitLab wiki page](https://github.com/coala/coala/wiki/GitLab).
5 changes: 5 additions & 0 deletions plugins/templates/explain/google.jinja2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Hey. This message was triggered because someone was too lazy to type this *again*. Don't take it personally. Please.

We all got to learn this: *use google*. Or duckduckgo. Anything. The search engine that earned your trust. You got a build error? Search for the first red thing and google it. You got an exception? *Read the message.* Search it. *Think.*

Keep this in mind: *You* are sitting in front of the problem, not us. You will have a much easier time solving it. That's why you should try doing it first.
8 changes: 8 additions & 0 deletions plugins/templates/explain/promotion.jinja2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
To become part of the coala developers team, there are a few steps you need to complete. The newcomer process is as follows:
You will start as a newcomer, which is kind of a trial. If you complete the following tasks, you will become a developer at coala:

- run coala on a project of yours
- merge a difficulty/newcomer Pull Request
- review at least a difficulty/newcomer Pull Request
- merge a difficulty/low Pull Request
- review at least a difficulty/low or higher Pull Request
5 changes: 5 additions & 0 deletions plugins/templates/explain/rebase.jinja2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
It looks like your PR is out of date and needs a rebase.

[This page](https://coala.io/rebase) may help you to get started on this. We also have [a quick video tutorial on how to rebase](https://asciinema.org/a/78683). That should help you understand the basics of how it works and what you should be doing.

If you're just looking to fix an issue very quickly and not interested in contributing to coala long term, we can fix it up for you - just tell us :).
5 changes: 5 additions & 0 deletions plugins/templates/explain/review.jinja2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
After creating your `Pull Request`, it is under the review process. This can be deduced from the `process/pending review` label. Now you have to wait for the reviewers to review your PR. You should *not* ask for reviews on our Gitter channel - we review those PRs continuously.

We're usually swamped with reviews, while you are waiting **please review other people's PRs** at [coala.io/review](https://coala.io/review): that helps you and will make your review happen faster as well. As a rule of thumb, *for every review you receive, give at least one review to someone else!*

For a good review, look at every commit on its own and place `ack <sha>` (commit is ready) or `unack <sha>` (commit needs work) comments on the pull request, be sure to remove other spacing like tabs. If you're done with a pull request, you can use `{bot_prefix} mark wip <pull URL>` to mark it *work in progress* finally.
Loading

0 comments on commit 11ce167

Please sign in to comment.