Navigation Menu

Skip to content

Commit

Permalink
close issueの検索機能追加
Browse files Browse the repository at this point in the history
  • Loading branch information
akias committed Aug 30, 2017
1 parent 9bfa99a commit 89bb893
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 1 deletion.
Binary file modified Create issue.alfredworkflow
Binary file not shown.
115 changes: 114 additions & 1 deletion github.rb
Expand Up @@ -12,6 +12,7 @@ def initialize
@cache_file = '.repositoriescache'
@issue_cache_file = '.issuescache'
@current_repo_file = '.currentrepo'
@close_issue_cache_file = '.closeissuescache'
end

def store_token(token)
Expand Down Expand Up @@ -49,6 +50,15 @@ def search_issue(query)
results.uniq
end

def search_close_issue(query)
issues = load_and_cache_user_close_issues
results = issues.select do |issue|
issue['name'] =~ Regexp.new(query, 'i')
end
results += search_all_close_issues(query) if query =~ %r{\/}
results.uniq
end

def load_token
@token = File.read(@token_file).strip if File.exists?(@token_file)
end
Expand Down Expand Up @@ -87,6 +97,14 @@ def load_and_cache_user_issues
end
end

def load_and_cache_user_close_issues
if File.exists?(@close_issue_cache_file)
JSON.parse(File.read(@close_issue_cache_file))
else
cache_all_close_issues_for_repo
end
end

def cache_all_issues_for_repo
raise InvalidToken unless test_authentication
issues = []
Expand All @@ -98,6 +116,17 @@ def cache_all_issues_for_repo
issues
end

def cache_all_close_issues_for_repo
raise InvalidToken unless test_authentication
issues = []
issues += get_repo_close_issues
File.open(@close_issue_cache_file, 'w') do |f|
f.write issues.to_json
end

issues
end

def create(query)
load_token
load_current_repo
Expand Down Expand Up @@ -132,11 +161,16 @@ def rebuild_user_issues_cache
cache_all_issues_for_repo
end

def rebuild_user_close_issues_cache
File.delete(@close_issue_cache_file) if File.exists?(@close_issue_cache_file)
cache_all_close_issues_for_repo
end

def get_user_repos
res = get '/user/repos'
if res.is_a?(Array)
res.map do |repo|
{ 'name' => repo['full_fname'], 'url' => repo['html_url'] }
{ 'name' => repo['full_name'], 'url' => repo['html_url'] }
end
else
[]
Expand All @@ -154,6 +188,17 @@ def get_repo_issues
end
end

def get_repo_close_issues
res = get "/repos/#{load_current_repo}/issues", { 'state' => 'closed' }
if res.is_a?(Array)
res.map do |issue|
{ 'name' => issue['title'], 'url' => issue['html_url'] }
end
else
[]
end
end

def get_user_orgs
res = get '/user/orgs'
if res.is_a?(Array)
Expand Down Expand Up @@ -223,6 +268,17 @@ def get_org_issues(org)
end
end

def get_org_close_issues(org)
res = get "/orgs/#{load_current_repo}/issues?q=is%3Aclosed"
if res.is_a?(Array)
res.map do |repo|
{ 'name' => repo['title'], 'url' => repo['html_url'] }
end
else
[]
end
end

def search_all_issues(query)
return [] if !query || query.length == 0
raise InvalidToken unless test_authentication
Expand Down Expand Up @@ -259,10 +315,47 @@ def search_all_issues(query)
end
end

def search_all_close_issues(query)
return [] if !query || query.length == 0
raise InvalidToken unless test_authentication

parts = query.split('/', 2)

if parts.length == 1 and parts[0].length > 0
res = get "/orgs/#{load_current_repo}/issues", { 'state' => 'closed' }

if res.is_a?(Hash) and res.has_key?('items')
res['items'].map do |issue|
{ 'name' => issue['title'], 'url' => issue['html_url'] }
end
else
[]
end
elsif parts.length == 2 and parts[0].length > 0
user = parts[0]
user_query = parts[1]
res = get "/users/#{load_current_repo}/issues", { 'state' => 'closed' }

if res.is_a?(Array)
repos = res.select do |repo|
repo['name'] =~ Regexp.new(user_query, 'i')
end
repos.map do |issue|
{ 'name' => issue['title'], 'url' => issue['html_url'] }
end
else
[]
end
else
[]
end
end

def get(path, params = {})
params['per_page'] = 100
qs = params.map { |k, v| "#{CGI.escape k.to_s}=#{CGI.escape v.to_s}" }.join('&')
uri = URI("#{@base_uri}#{path}?#{qs}")
puts uri
json_all = []

begin
Expand Down Expand Up @@ -301,10 +394,13 @@ def get(path, params = {})
if query == '--update'
github.rebuild_user_repos_cache
github.rebuild_user_issues_cache
github.rebuild_user_close_issues_cache
elsif query == '--auth'
github.store_token(ARGV[1])
elsif query == '--repo'
github.store_current_repo(ARGV[1])
github.rebuild_user_issues_cache
github.rebuild_user_close_issues_cache
elsif query == '--create'
github.create(ARGV[1].tr('\\', ' '))
result = github.load_current_repo
Expand Down Expand Up @@ -343,6 +439,23 @@ def get(path, params = {})
end
end

puts output
elsif query == '--searchcloseissue'
results = github.search_close_issue(ARGV[1] || '')
output = XmlBuilder.build do |xml|
xml.items do
if results.length > 0
results.each do |repo|
xml.item Item.new(repo['url'], repo['url'], repo['name'], repo['url'], 'yes')
end
else
xml.item Item.new(
nil, query, 'Update the repository cache and try again.', 'Rebuilds your local cache from GitHub, then searches again; gh-update to rebuild anytime.', 'yes', 'FE3390F7-206C-45C4-94BB-5DD14DE23A1B.png'
)
end
end
end

puts output
else
result = github.load_current_repo
Expand Down

0 comments on commit 89bb893

Please sign in to comment.