Skip to content

Instantly share code, notes, and snippets.

@spjwebster
Created October 13, 2016 15:00
Show Gist options
  • Save spjwebster/02bc2c5f2c5100db923d9769ecc10671 to your computer and use it in GitHub Desktop.
Save spjwebster/02bc2c5f2c5100db923d9769ecc10671 to your computer and use it in GitHub Desktop.
Codeship BitBar plugin
#!/usr/bin/env python
from contextlib import closing
import urllib2
import json
import sys
API_KEY = '<insert api key here>'
STATUS_EMOJI = {
'success': 'smiley',
'waiting': 'clock4',
'testing': 'hourglass',
'failed': 'poop',
'stopped': 'no_entry_sign',
'unknown': 'question'
}
def main():
print ":anchor:"
print "---"
cs = Codeship(API_KEY)
# Fetch a list of projects with builds
active_projects = [
p for p in cs.get_projects()
if p.builds
]
for project in active_projects:
status = project.latest_build.status
print ":{emoji}: {name} | href={url}".format(
name=project.repository_name,
emoji=STATUS_EMOJI.get(status, 'question'),
url=project.url,
)
for build in project.builds:
print "--:{emoji}: [{branch}] {message} | href={url}".format(
emoji=STATUS_EMOJI.get(build.status, 'question'),
branch=build.branch,
message=build.short_message,
url=build.url
)
class Codeship(object):
BASE_URL = 'https://codeship.com/api/v1'
PROJECTS_URL = BASE_URL + '/projects.json?api_key={api_key}'
PROJECT_URL = BASE_URL + '/projects/{project_id}.json?api_key={api_key}'
RESTART_BUILD_URL = BASE_URL + '/builds/{build_id}/restart.json?api_key={api_key}'
def __init__(self, api_key):
self.api_key = api_key
def get_projects(self):
response = self._req(
self.PROJECTS_URL.format(
api_key=self.api_key
)
)
if response:
return [
Project(p)
for p in response['projects']
]
def get_project(self, project_id):
response = self._req(
self.PROJECT_URL.format(
project_id=project_id,
api_key=self.api_key,
)
)
if response:
return Project(response)
def restart_build(self, build_id):
response = self._req(
self.RESTART_BUILD_URL.format(
build_id=build_id,
api_key=self.api_key,
),
data=''
)
return True if response else False
def _req(self, url, data=None):
try:
data_str = None
if data:
data_str = urllib.urlencode(data)
with closing(urllib2.urlopen(url, data=data_str)) as req:
response_body = req.read()
if response_body:
return json.loads(response_body)
except:
pass
class Project(object):
WEB_URL = 'https://codeship.com/projects/{project_id}'
def __init__(self, data):
self._data = data
@property
def url(self):
return self.WEB_URL.format(
project_id=self.id,
)
@property
def builds(self):
return [
Build(b)
for b in self._data['builds']
]
@property
def latest_build(self):
builds = self.builds
return builds[0] if builds else None
@property
def latest_completed_build(self):
builds = [b for b in self.builds if b.is_complete]
return builds[0] if builds else None
def __getattr__(self, name):
return self._data.get(name)
class Build(object):
WEB_URL = 'https://codeship.com/projects/{project_id}/builds/{build_id}'
INCOMPLETE_STATUSES = (
'waiting',
'stopped',
'testing'
)
def __init__(self, data):
self._data = data
@property
def is_complete(self):
return self.status not in self.INCOMPLETE_STATUSES
@property
def was_successful(self):
return self.status == 'success'
@property
def short_message(self):
return self.message.split("\n")[0]
@property
def finished_at(self):
return self._data['finished_at']
@property
def url(self):
return self.WEB_URL.format(
project_id=self.project_id,
build_id=self.id
)
def __getattr__(self, name):
if not name in self._data:
print self._data.keys()
return self._data.get(name)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment