Skip to content

Instantly share code, notes, and snippets.

@spjwebster
Last active October 13, 2016 13:58
Show Gist options
  • Save spjwebster/d8501417bfbc739cf37a7188cc0c8051 to your computer and use it in GitHub Desktop.
Save spjwebster/d8501417bfbc739cf37a7188cc0c8051 to your computer and use it in GitHub Desktop.
Super simple standalone Codeship API wrapper for Python
# Super simple Codeship API wrapper for Python designed to be copy/pasted into
# BitBar plugins or other standalone scripts where installing Python packages
# from PyPI is impossible or undesirable.
#
# License: Public Domain
from contextlib import closing
import urllib2
import json
import sys
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment