Skip to content

Commit

Permalink
Re-adding URL shortening via Bit.ly.
Browse files Browse the repository at this point in the history
  • Loading branch information
coddingtonbear committed Mar 5, 2014
1 parent 09685d6 commit 179a4c4
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 20 deletions.
34 changes: 33 additions & 1 deletion bugwarrior/db.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import copy
import os
import re
import warnings

import bitlyapi
import dogpile.cache
import six
from twiggy import log
from taskw import TaskWarriorShellout
from taskw.utils import get_annotation_value

from bugwarrior.config import asbool, NoOptionError
from bugwarrior.notifications import send_notification
Expand All @@ -14,6 +16,36 @@
MARKUP = "(bw)"


DOGPILE_CACHE_PATH = os.path.expanduser('~/.cache/bitly.dbm')
if not os.path.isdir(os.path.dirname(DOGPILE_CACHE_PATH)):
os.mkdirs(os.path.dirname(DOGPILE_CACHE_PATH))
CACHE_REGION = dogpile.cache.make_region().configure(
"dogpile.cache.dbm",
arguments=dict(filename=DOGPILE_CACHE_PATH),
)


class URLShortener(object):
_instance = None

def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(URLShortener, cls).__new__(
cls, *args, **kwargs
)
return cls._instance

def __init__(self, bitly_user, bitly_key):
self.bitly_user = bitly_user
self.bitly_key = bitly_key

self.bitly = bitlyapi.BitLy(bitly_user, bitly_key)

@CACHE_REGION.cache_on_arguments()
def shorten(self, url):
return self.bitly.shorten(longUrl=url)['url']


class NotFound(Exception):
pass

Expand Down
50 changes: 41 additions & 9 deletions bugwarrior/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import six
from twiggy import log

from bugwarrior.db import MARKUP
from bugwarrior.db import MARKUP, URLShortener


# Sentinels for process completion status
Expand All @@ -16,30 +16,41 @@

class IssueService(object):
""" Abstract base class for each service """
# Which class should this service instantiate for holding these issues?
ISSUE_CLASS = None
# What prefix should we use for this service's configuration values
CONFIG_PREFIX = ''

def __init__(self, config, target):
self.config = config
self.target = target

self.desc_len = 35
if config.has_option('general', 'description_length'):
self.desc_len = self.config.getint('general', 'description_length')
else:
self.desc_len = 35

self.anno_len = 45
if config.has_option('general', 'annotation_length'):
self.anno_len = self.config.getint('general', 'annotation_length')
else:
self.anno_len = 45

self.bitly_api_user = None
if config.has_option('general', 'bitly.api_user'):
self.bitly_api_user = config.get('general', 'bitly.api_user')

self.bitly_api_key = None
if config.has_option('general', 'bitly.api_key'):
self.bitly_api_key = config.get('general', 'bitly.api_key')

self.description_template = None
if config.has_option(self.target, 'description_template'):
self.description_template = self.config.get(
self.target, 'description_template'
)
else:
self.description_template = None

self.default_priority = 'M'
if config.has_option(self.target, 'default_priority'):
self.default_priority = config.get(self.target, 'default_priority')
else:
self.default_priority = 'M'

log.name(target).info("Working on [{0}]", self.target)

def config_get_default(self, key, default=None):
Expand Down Expand Up @@ -67,6 +78,8 @@ def get_issue_for_record(self, record, extra=None):
'description_length': self.desc_len,
'description_template': self.description_template,
'target': self.target,
'bitly_api_key': self.bitly_api_key,
'bitly_api_user': self.bitly_api_user,
}
origin.update(self.get_service_metadata())
return self.ISSUE_CLASS(record, origin=origin, extra=extra)
Expand Down Expand Up @@ -205,6 +218,25 @@ def get_priority(self):
self.origin['default_priority']
)

def get_processed_url(self, url):
""" Returns a URL with conditional processing.
If the following config keys are set:
- [general]bitly.api_user
- [general]bitly.api_key
returns a shortened URL; otherwise returns the URL unaltered.
"""
if (self.origin['bitly_api_user'] and self.origin['bitly_api_key']):
shortener = URLShortener(
self.origin['bitly_api_user'],
self.origin['bitly_api_key'],
)
return shortener.shorten(url)
return url

def parse_date(self, date):
if date:
return parse_date(date)
Expand Down
2 changes: 1 addition & 1 deletion bugwarrior/services/activecollab2.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def get_default_description(self):
if self.record['name']
else self.record['body']
),
url=self.record['permalink'],
url=self.get_processed_url(self.record['permalink']),
number=self.record['ticket_id'],
cls=self.record['type'].lower(),
)
Expand Down
2 changes: 1 addition & 1 deletion bugwarrior/services/activecollab3.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def get_default_description(self):
if self.record['name']
else self.record['body']
),
url=self.record['permalink'],
url=self.get_processed_url(self.record['permalink']),
number=self.record['id'],
cls=self.record['type'],
)
Expand Down
2 changes: 1 addition & 1 deletion bugwarrior/services/bitbucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def to_taskwarrior(self):
def get_default_description(self):
return self.build_default_description(
title=self.record['title'],
url=self.extra['url'],
url=self.get_processed_url(self.extra['url']),
number=self.record['local_id'],
cls='issue'
)
Expand Down
2 changes: 1 addition & 1 deletion bugwarrior/services/bz.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def to_taskwarrior(self):
def get_default_description(self):
return self.build_default_description(
title=self.record['summary'],
url=self.extra['url'],
url=self.get_processed_url(self.extra['url']),
number=self.record['id'],
cls='issue',
)
Expand Down
2 changes: 1 addition & 1 deletion bugwarrior/services/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def to_taskwarrior(self):
def get_default_description(self):
return self.build_default_description(
title=self.record['title'],
url=self.record['html_url'],
url=self.get_processed_url(self.record['html_url']),
number=self.record['number'],
cls=self.extra['type'],
)
Expand Down
2 changes: 1 addition & 1 deletion bugwarrior/services/jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def get_priority(self):
def get_default_description(self):
return self.build_default_description(
title=self.get_summary(),
url=self.get_url(),
url=self.get_processed_url(self.get_url()),
number=self.get_number(),
cls='issue',
)
Expand Down
2 changes: 1 addition & 1 deletion bugwarrior/services/mplan.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def get_project(self):
def get_default_description(self):
return self.build_default_description(
title=self.get_issue_title(),
url=self.get_issue_url(),
url=self.get_processed_url(self.get_issue_url()),
number=self.record['Id'],
cls='issue',
)
Expand Down
2 changes: 1 addition & 1 deletion bugwarrior/services/redmine.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def get_project_name(self):
def get_default_description(self):
return self.build_default_description(
title=self.record['subject'],
url=self.record['url'],
url=self.get_processed_url(self.record['url']),
number=self.record['id'],
cls='issue',
)
Expand Down
2 changes: 1 addition & 1 deletion bugwarrior/services/teamlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def to_taskwarrior(self):
def get_default_description(self):
return self.build_default_description(
title=self.record['title'],
url=self.get_issue_url(),
url=self.get_processed_url(self.get_issue_url()),
number=self.record['id'],
cls='issue',
)
Expand Down
2 changes: 1 addition & 1 deletion bugwarrior/services/trac.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def to_taskwarrior(self):
def get_default_description(self):
return self.build_default_description(
title=self.record['summary'],
url=self.record['url'],
url=self.get_processed_url(self.record['url']),
number=self.record['number'],
cls='issue'
)
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
"six",
"jinja2>=2.7.2",
"pycurl",
"bitlyapi>=0.1.1",
"dogpile.cache>=0.5.3",
],
tests_require=[
"Mock",
Expand Down

0 comments on commit 179a4c4

Please sign in to comment.