Skip to content

Commit

Permalink
Run upgrades in the background, prepare release v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewaite committed Oct 20, 2017
1 parent b2294ed commit f66d5d3
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 35 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [v1.1.0] (2017-10-20)
* Run updates in the background

## [v1.0.2] (2017-10-20)
* Bump alfred-workflow to 1.28.1 to fix issues

Expand All @@ -15,7 +18,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

* Initial implementaiton of alfred-gitlab workflow

[Unreleased]: https://github.com/lukewaite/alfred-gitlab/compare/v1.0.2...HEAD
[Unreleased]: https://github.com/lukewaite/alfred-gitlab/compare/v1.1.0...HEAD
[v1.1.2]: https://github.com/lukewaite/alfred-gitlab/compare/v1.0.2...v1.1.0
[v1.0.2]: https://github.com/lukewaite/alfred-gitlab/compare/v1.0.1...v1.0.2
[v1.0.1]: https://github.com/lukewaite/alfred-gitlab/compare/v1.0.0...v1.0.1
[v1.0.0]: https://github.com/lukewaite/alfred-gitlab/compare/90b63639ac1d06f9a52c37afd3f9c1da37d6ebd2...v1.0.0
Binary file not shown.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ By default, we will only show projects which you are a member of.

## TODOs
* Optionally, allow you to search for non-membership repos
* Background the API Fetch updates, so you don't have to wait for them after initial fetch
* Add alfred-workflow updater notifications
* Clean up

Expand Down
49 changes: 16 additions & 33 deletions src/gitlab.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,13 @@
# encoding: utf-8
import sys
import argparse
from workflow import Workflow, ICON_WEB, ICON_WARNING, web, PasswordNotFound
from workflow import Workflow, ICON_WEB, ICON_WARNING, ICON_INFO, web, PasswordNotFound
from workflow.background import run_in_background, is_running

__version__ = '1.0.2'
__version__ = '1.1.0'

log = None

def get_projects(api_key, url):
"""
Parse all pages of projects
:return: list
"""
return get_project_page(api_key, url, 1, [])

def get_project_page(api_key, url, page, list):
log.info("Calling API page {page}".format(page=page))
params = dict(private_token=api_key, per_page=100, page=page, membership='true')
r = web.get(url, params)

# throw an error if request failed
# Workflow will catch this and show it to the user
r.raise_for_status()

# Parse the JSON returned by GitLab and extract the projects
result = list + r.json()

nextpage = r.headers.get('X-Next-Page')
if nextpage:
result = get_project_page(api_key, url, nextpage, result)

return result


def search_for_project(project):
"""Generate a string search key for a project"""
elements = []
Expand Down Expand Up @@ -73,7 +48,7 @@ def main(wf):
####################################################################

try:
api_key = wf.get_password('gitlab_api_key')
wf.get_password('gitlab_api_key')
except PasswordNotFound: # API key has not yet been set
wf.add_item('No API key set.',
'Please use glsetkey to set your GitLab API key.',
Expand All @@ -100,13 +75,21 @@ def main(wf):

query = args.query

def wrapper():
return get_projects(api_key, api_url)
projects = wf.cached_data('projects', None, max_age=0)

projects = wf.cached_data('projects', wrapper, max_age=3600)
# Start update script if cached data is too old (or doesn't exist)
if not wf.cached_data_fresh('projects', max_age=600):
cmd = ['/usr/bin/python', wf.workflowfile('update.py')]
run_in_background('update', cmd)

# Notify the user if the cache is being updated
if is_running('update'):
wf.add_item('Updating project list via GitLab',
valid=False,
icon=ICON_INFO)

# If script was passed a query, use it to filter projects
if query:
if query and projects:
projects = wf.filter(query, projects, key=search_for_project, min_score=20)

if not projects: # we have no data to show, so show a warning and stop
Expand Down
53 changes: 53 additions & 0 deletions src/update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# encoding: utf-8

from workflow import web, Workflow, PasswordNotFound

def get_projects(api_key, url):
"""
Parse all pages of projects
:return: list
"""
return get_project_page(api_key, url, 1, [])

def get_project_page(api_key, url, page, list):
log.info("Calling API page {page}".format(page=page))
params = dict(private_token=api_key, per_page=100, page=page, membership='true')
r = web.get(url, params)

# throw an error if request failed
# Workflow will catch this and show it to the user
r.raise_for_status()

# Parse the JSON returned by GitLab and extract the projects
result = list + r.json()

nextpage = r.headers.get('X-Next-Page')
if nextpage:
result = get_project_page(api_key, url, nextpage, result)

return result

def main(wf):
try:
# Get API key from Keychain
api_key = wf.get_password('gitlab_api_key')
api_url = wf.settings.get('api_url', None)

# Retrieve projects from cache if available and no more than 600
# seconds old
def wrapper():
return get_projects(api_key, api_url)

projects = wf.cached_data('projects', wrapper, max_age=3600)

# Record our progress in the log file
log.debug('{} gitlab projects cached'.format(len(projects)))

except PasswordNotFound: # API key has not yet been set
# Nothing we can do about this, so just log it
wf.logger.error('No API key saved')

if __name__ == u"__main__":
wf = Workflow()
log = wf.logger
wf.run(main)

0 comments on commit f66d5d3

Please sign in to comment.