Skip to content

Instantly share code, notes, and snippets.

@kongo2002
Last active January 16, 2020 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kongo2002/3b971ada9ba394550e75c0a1f35e295e to your computer and use it in GitHub Desktop.
Save kongo2002/3b971ada9ba394550e75c0a1f35e295e to your computer and use it in GitHub Desktop.
" Vim script file
" Description: fetch JIRA issue information
" Author: Gregor Uhlenheuer
" Filename: vijira.vim
" Last Change: Thu 16 Jan 2020 11:53:16 PM CET
if exists('g:loaded_vijira')
finish
endif
let g:loaded_vijira = 1
if !exists('g:vijira_atlassian_domain')
echohl WarningMsg
echom 'missing configuration "g:vijira_atlassian_domain"'
echohl None
finish
endif
if !exists('g:vijira_atlassian_user_email')
echohl WarningMsg
echom 'missing configuration "g:vijira_atlassian_user_email"'
echohl None
finish
endif
if !exists('g:vijira_atlassian_api_token')
let g:vijira_atlassian_api_token=$VIJIRA_ATLASSIAN_API_TOKEN
if g:vijira_atlassian_api_token == ''
echohl WarningMsg
echom 'missing configuration "g:vijira_atlassian_api_token"'
echohl None
finish
endif
endif
if !has('python3')
echohl WarningMsg
echom 'no python3 support enabled'
echohl None
finish
endif
python3 << EOF
import requests
import vim
_domain = vim.vars['vijira_atlassian_domain']
_token = vim.vars['vijira_atlassian_api_token']
_user = vim.vars['vijira_atlassian_user_email']
class Issue(object):
def __init__(self, key, summary, status):
self.key = key
self.summary = summary
self.status = status
def format(self):
summary = self.summary.replace('"', '\\"')
return '{{"abbr":"{}","menu":"{}"}}'.format(self.key, summary)
def _request(path):
req_path = 'https://'+_domain+'.atlassian.net/rest/api/3'+path
resp = requests.get(req_path, auth=(_user, _token))
if resp.status_code != requests.codes.ok:
return {}
return resp.json()
def _extract_issues(response):
issues = []
for data in response.get('issues', []):
issue = _extract_issue(data)
if issue:
issues.append(issue)
return issues
def _extract_issue(data):
key = data.get('key', None)
if not key:
return None
fields = data.get('fields', {})
summary = fields.get('summary', '')
status = fields.get('status', {})
status_name = status.get('name', '')
return Issue(key, summary, status_name)
def open_issues():
query = '/search?jql=resolution=unresolved+and+assignee=currentUser()+order+by+key+asc&fields=status,summary'
issues = _extract_issues(_request(query))
return ','.join([x.format() for x in issues])
EOF
function! s:fetch_open_issues() abort
py3 vim.command('let issues=['+open_issues()+']')
return issues
endfunction
function! s:_complete(...) abort
let issues = s:fetch_open_issues()
"let format = 'v:val.abbr -> v:val.menu'
let format = 'v:val.abbr'
call map(issues, "extend(v:val, {'word': ".format.'})')
let lead = s:get_current_keyword()
call filter(issues, 'v:val.abbr =~ lead')
if !empty(issues)
call complete(col('.')-len(lead), issues)
endif
return ''
endfunction
function! s:get_current_keyword()
let c = col ('.')-1
let l = line('.')
let ll = getline(l)
let ll1 = strpart(ll,0,c)
let ll1 = matchstr(ll1,'\k*$')
if strlen(ll1) == 0
return ll1
else
let ll2 = strpart(ll,c,strlen(ll)-c+1)
let ll2 = matchstr(ll2,'^\k*')
return ll1.ll2
endif
endfunction
inoremap <silent> <Plug>ViJiraComplete <C-R>=<sid>_complete()<CR>
if !hasmapto('<Plug>ViJiraComplete', 'i')
imap <silent> <unique> <F5> <Plug>ViJiraComplete
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment