Skip to content

Instantly share code, notes, and snippets.

@kols
Created March 3, 2015 09:38
Show Gist options
  • Save kols/c407cf210da76012842e to your computer and use it in GitHub Desktop.
Save kols/c407cf210da76012842e to your computer and use it in GitHub Desktop.
#!/usr/local/bin/python
import json
import sys
import keyring
import requests
APP_NAME = 'jira'
USERNAME = 'qilong.dou'
JIRA_SEARCH_API_ENDPOINT = 'http://jira.ele.to:8088/rest/api/2/search'
JIRA_ISSUE_DETAIL_URL = 'http://jira.ele.to:8088/browse/%s'
def get_password(app_name, username):
return keyring.get_password(app_name, username)
def get_issues():
query_str = 'jql=assignee=%s AND status!=closed&fields=summary' % USERNAME
url = JIRA_SEARCH_API_ENDPOINT + '?' + query_str
res = requests.get(url, auth=(USERNAME, get_password(APP_NAME, USERNAME)))
return res.json()['issues']
def _build_item(issue):
title = issue['fields']['summary']
key = issue['key']
return {
'title': title,
'subtitle': key,
'url': JIRA_ISSUE_DETAIL_URL % key,
'icon': 'jira_icon_0.png',
}
def get_res(issues):
issues = sorted(issues, key=lambda x: int(x['id']), reverse=True)
res = []
for item in issues:
res.append(_build_item(item))
return res
def search(keywords, issues):
res = []
for item in issues:
for kw in keywords:
if kw not in item['fields']['summary'] and \
kw not in item['key'].lower():
break
else:
res.append(item)
return res
def main():
if not sys.argv[1:]:
res = get_res(get_issues())
else:
res = get_res(
search([arg.decode('utf-8') for arg in sys.argv[1:]],
get_issues()))
print json.dumps(res)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment