Skip to content

Commit 2be2b26

Browse files
authored
Merge pull request #106 from sopel-irc/rip-git.io
formatting: remove `shorten_url()`
2 parents 42f5fbc + 7abc7d6 commit 2be2b26

File tree

2 files changed

+25
-35
lines changed

2 files changed

+25
-35
lines changed

sopel_modules/github/formatting.py

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import re
2121
import textwrap
2222

23-
import requests
24-
2523
from sopel.formatting import color
2624
from sopel import tools
2725

@@ -575,41 +573,32 @@ def fmt_release_message(payload=None):
575573
' (prerelease)' if payload['release']['prerelease'] else '')
576574

577575

578-
def shorten_url(url):
579-
try:
580-
res = requests.post('https://git.io', {'url': url})
581-
return res.headers['location']
582-
except:
583-
LOGGER.exception('Shortening link failed; using long URL.')
584-
return url
585-
586-
587576
def get_formatted_response(payload, row):
588577
global current_row, current_payload
589578
current_payload = payload
590579
current_row = row
591580

592581
messages = []
593582
if payload['event'] == 'push':
594-
messages.append(fmt_push_summary_message() + " " + fmt_url(shorten_url(get_push_summary_url())))
583+
messages.append(fmt_push_summary_message() + " " + fmt_url(get_push_summary_url()))
595584
for commit in get_distinct_commits():
596585
messages.append(fmt_commit_message(commit))
597586
elif payload['event'] == 'commit_comment':
598-
messages.append(fmt_commit_comment_summary() + " " + fmt_url(shorten_url(payload['comment']['html_url'])))
587+
messages.append(fmt_commit_comment_summary() + " " + fmt_url(payload['comment']['html_url']))
599588
elif payload['event'] == 'pull_request':
600589
if re.match('((re)?open|clos)ed', payload['action']) or payload['action'] in ['ready_for_review', 'converted_to_draft']:
601-
messages.append(fmt_pull_request_summary_message() + " " + fmt_url(shorten_url(payload['pull_request']['html_url'])))
590+
messages.append(fmt_pull_request_summary_message() + " " + fmt_url(payload['pull_request']['html_url']))
602591
elif payload['action'] == 'edited':
603592
if 'changes' in payload:
604593
if 'title' in payload['changes']:
605-
messages.append(fmt_pull_request_title_edit() + " " + fmt_url(shorten_url(payload['pull_request']['html_url'])))
594+
messages.append(fmt_pull_request_title_edit() + " " + fmt_url(payload['pull_request']['html_url']))
606595
elif re.match('(assigned|unassigned)', payload['action']):
607-
messages.append(fmt_issue_assignee_message() + " " + fmt_url(shorten_url(payload['pull_request']['html_url'])))
596+
messages.append(fmt_issue_assignee_message() + " " + fmt_url(payload['pull_request']['html_url']))
608597
elif re.match('(labeled|unlabeled)', payload['action']):
609598
if payload.get('label', None):
610599
# If a label is deleted, for example, we'll get a webhook payload with no details about the removed label.
611600
# We skip those; there's no reason to emit action messages to IRC with "unknown label" placeholders.
612-
messages.append(fmt_issue_label_message() + " " + fmt_url(shorten_url(payload['pull_request']['html_url'])))
601+
messages.append(fmt_issue_label_message() + " " + fmt_url(payload['pull_request']['html_url']))
613602
elif payload['event'] == 'pull_request_review':
614603
if payload['action'] == 'submitted' and payload['review']['state'] in ['approved', 'changes_requested', 'commented']:
615604
if payload['review']['state'] == 'commented' and payload['review']['body'] is None:
@@ -618,44 +607,44 @@ def get_formatted_response(payload, row):
618607
# Either way, an empty review must be accompanied by comments, which will get handled when their hook(s) fire(s).
619608
pass
620609
else:
621-
messages.append(fmt_pull_request_review_summary_message() + " " + fmt_url(shorten_url(payload['review']['html_url'])))
610+
messages.append(fmt_pull_request_review_summary_message() + " " + fmt_url(payload['review']['html_url']))
622611
elif payload['action'] == 'dismissed':
623-
messages.append(fmt_pull_request_review_dismissal_message() + " " + fmt_url(shorten_url(payload['review']['html_url'])))
612+
messages.append(fmt_pull_request_review_dismissal_message() + " " + fmt_url(payload['review']['html_url']))
624613
elif payload['event'] == 'pull_request_review_comment' and payload['action'] == 'created':
625-
messages.append(fmt_pull_request_review_comment_summary_message() + " " + fmt_url(shorten_url(payload['comment']['html_url'])))
614+
messages.append(fmt_pull_request_review_comment_summary_message() + " " + fmt_url(payload['comment']['html_url']))
626615
elif payload['event'] == 'issues':
627616
if re.match('((re)?open|clos)ed', payload['action']):
628617
if 'changes' in payload and all(k in payload['changes'] for k in ['old_repository', 'old_issue']):
629-
messages.append(fmt_issue_incoming_transfer_message() + " " + fmt_url(shorten_url(payload['issue']['html_url'])))
618+
messages.append(fmt_issue_incoming_transfer_message() + " " + fmt_url(payload['issue']['html_url']))
630619
else:
631-
messages.append(fmt_issue_summary_message() + " " + fmt_url(shorten_url(payload['issue']['html_url'])))
620+
messages.append(fmt_issue_summary_message() + " " + fmt_url(payload['issue']['html_url']))
632621
elif re.match('(assigned|unassigned)', payload['action']):
633-
messages.append(fmt_issue_assignee_message() + " " + fmt_url(shorten_url(payload['issue']['html_url'])))
622+
messages.append(fmt_issue_assignee_message() + " " + fmt_url(payload['issue']['html_url']))
634623
elif re.match('(labeled|unlabeled)', payload['action']):
635624
if payload.get('label', None):
636625
# If a label is deleted, for example, we'll get a webhook payload with no details about the removed label.
637626
# We skip those; there's no reason to emit action messages to IRC with "unknown label" placeholders.
638-
messages.append(fmt_issue_label_message() + " " + fmt_url(shorten_url(payload['issue']['html_url'])))
627+
messages.append(fmt_issue_label_message() + " " + fmt_url(payload['issue']['html_url']))
639628
elif re.match('(milestoned|demilestoned)', payload['action']):
640-
messages.append(fmt_issue_milestone_message() + " " + fmt_url(shorten_url(payload['issue']['html_url'])))
629+
messages.append(fmt_issue_milestone_message() + " " + fmt_url(payload['issue']['html_url']))
641630
elif payload['action'] == 'edited':
642631
if 'changes' in payload:
643632
if 'title' in payload['changes']:
644-
messages.append(fmt_issue_title_edit() + " " + fmt_url(shorten_url(payload['issue']['html_url'])))
633+
messages.append(fmt_issue_title_edit() + " " + fmt_url(payload['issue']['html_url']))
645634
elif payload['action'] == 'transferred':
646-
messages.append(fmt_issue_outgoing_transfer_message() + " " + fmt_url(shorten_url(payload['changes']['new_issue']['html_url'])))
635+
messages.append(fmt_issue_outgoing_transfer_message() + " " + fmt_url(payload['changes']['new_issue']['html_url']))
647636
elif payload['event'] == 'issue_comment' and payload['action'] == 'created':
648-
messages.append(fmt_issue_comment_summary_message() + " " + fmt_url(shorten_url(payload['comment']['html_url'])))
637+
messages.append(fmt_issue_comment_summary_message() + " " + fmt_url(payload['comment']['html_url']))
649638
elif payload['event'] == 'gollum':
650639
url = payload['pages'][0]['html_url'] if len(payload['pages']) else payload['repository']['url'] + '/wiki'
651-
messages.append(fmt_gollum_summary_message() + " " + fmt_url(shorten_url(url)))
640+
messages.append(fmt_gollum_summary_message() + " " + fmt_url(url))
652641
elif payload['event'] == 'watch':
653642
messages.append(fmt_watch_message())
654643
elif payload['event'] == 'status':
655644
messages.append(fmt_status_message())
656645
elif payload['event'] == 'release':
657646
if payload['action'] == 'published':
658647
# Currently the only possible action, but other events might eventually fire webhooks too
659-
messages.append(fmt_release_message() + " " + fmt_url(shorten_url(payload['release']['html_url'])))
648+
messages.append(fmt_release_message() + " " + fmt_url(payload['release']['html_url']))
660649

661650
return messages

sopel_modules/github/github.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from sopel.config.types import StaticSection, ValidatedAttribute
2020

2121
from . import formatting
22-
from .formatting import shorten_url, emojize
22+
from .formatting import emojize
2323
from .webhook import setup_webhook, shutdown_webhook
2424

2525
import operator
@@ -221,9 +221,8 @@ def issue_info(bot, trigger, match=None):
221221

222222
# append link, if not triggered by a link
223223
if not match:
224-
link = shorten_url(data['html_url'])
225224
response.append(bold(' | '))
226-
response.append(link)
225+
response.append(data['html_url'])
227226

228227
bot.say(''.join(response))
229228

@@ -516,13 +515,15 @@ def configure_repo_messages(bot, trigger):
516515
if not result:
517516
c.execute('''INSERT INTO gh_hooks (channel, repo_name, enabled) VALUES (?, ?, ?)''', (channel, repo_name, enabled))
518517
bot.say("Successfully enabled listening for {repo}'s events in {chan}.".format(chan=channel, repo=repo_name))
519-
bot.say('Great! Please allow me to create my webhook by authorizing via this link: ' + shorten_url(auth_url))
518+
bot.say('Great! Please allow me to create my webhook by authorizing via this link:')
519+
bot.say(auth_url, max_messages=10)
520520
bot.say('Once that webhook is successfully created, I\'ll post a message in here. Give me about a minute or so to set it up after you authorize. You can configure the colors that I use to display webhooks with {}gh-hook-color'.format(bot.config.core.help_prefix))
521521
else:
522522
c.execute('''UPDATE gh_hooks SET enabled = ? WHERE channel = ? AND repo_name = ?''', (enabled, channel, repo_name))
523523
bot.say("Successfully {state} the subscription to {repo}'s events".format(state='enabled' if enabled else 'disabled', repo=repo_name))
524524
if enabled:
525-
bot.say('Great! Please allow me to create my webhook by authorizing via this link: ' + shorten_url(auth_url))
525+
bot.say('Great! Please allow me to create my webhook by authorizing via this link:')
526+
bot.say(auth_url, max_messages=10)
526527
bot.say('Once that webhook is successfully created, I\'ll post a message in here. Give me about a minute or so to set it up after you authorize. You can configure the colors that I use to display webhooks with {}gh-hook-color'.format(bot.config.core.help_prefix))
527528
conn.commit()
528529
conn.close()

0 commit comments

Comments
 (0)