Example #1
0
class CircleCI(IntervalModule):
    """
    Get current status of circleci builds
    Requires `circleci` `dateutil.parser`

    Formatters:

    * `{repo_slug}`  - repository owner/repository name
    * `{repo_status}` - repository status
    * `{repo_name}` - repository name
    * `{repo_owner}` - repository owner
    * `{last_build_started}` - date of the last finished started
    * `{last_build_duration}` - duration of the last build, not populated with workflows(yet)


    Examples

    .. code-block:: python

        status_color_map = {
            'passed': '#00FF00',
            'failed': '#FF0000',
            'errored': '#FFAA00',
            'cancelled': '#EEEEEE',
            'started': '#0000AA',

        }

    .. code-block:: python

        repo_status_map={
            'success': '<span color="#00af00">success</span>',
            'running': '<span color="#0000af">running</span>',
            'failed': '<span color="#af0000">failed</span>',
        }

    """

    settings = (
        'format',
        ('circleci_token', 'circleci access token'),
        ('repo_slug', 'repository identifier eg. "enkore/i3pystatus"'),
        ('time_format', 'passed directly to .strftime() for `last_build_started`'),
        ('repo_status_map', 'map representing how to display status'),
        ('duration_format', '`last_build_duration` format string'),
        ('status_color_map', 'color for all text based on status'),
        ('color', 'color for all text not otherwise colored'),
        ('workflow_name', '[WORKFLOWS_ONLY] if specified, monitor this workflows status. if not specified this module '
                          'will default to reporting the status of your last build'),
        ('workflow_branch', '[WORKFLOWS_ONLY] if specified, monitor the workflows in this branch'))

    required = ('circleci_token', 'repo_slug')

    format = '{repo_owner}/{repo_name}-{repo_status} [({last_build_started}({last_build_duration}))]'
    short_format = '{repo_name}-{repo_status}'
    time_format = '%m/%d'
    duration_format = '%m:%S'
    status_color_map = None
    repo_slug = None
    circleci_token = None
    repo_status_map = None
    color = '#DDDDDD'
    workflow_name = None
    workflow_branch = None

    circleci = None

    on_leftclick = 'open_build_webpage'

    def init(self):
        self.repo_status = None
        self.last_build_duration = None
        self.last_build_started = None
        self.repo_owner, self.repo_name = self.repo_slug.split('/')

        self.workflows = self.workflow_name is not None or self.workflow_branch is not None

    def _format_time(self, time):
        _datetime = dateutil.parser.parse(time)
        return _datetime.strftime(self.time_format)

    @require(internet)
    def run(self):
        if self.circleci is None:
            self.circleci = Api(self.circleci_token)

        if self.workflows:
            if self.workflow_branch and not self.workflow_name:
                self.output = dict(
                    full_text='workflow_name must be specified!'
                )
                return

            project = {p['reponame']: p for p in self.circleci.get_projects()}.get(self.repo_name)
            if not self.workflow_branch:
                self.workflow_branch = project.get('default_branch')

            workflow_info = project['branches'].get(self.workflow_branch)['latest_workflows'].get(self.workflow_name)

            self.last_build_started = self._format_time(workflow_info.get('created_at'))
            self.repo_status = workflow_info.get('status')

            self.last_build_duration = ''  # TODO: gather this information once circleCI exposes it

        else:
            self.repo_summary = self.circleci.get_project_build_summary(
                self.repo_owner,
                self.repo_name,
                limit=1)
            if len(self.repo_summary) != 1:
                return
            self.repo_summary = self.repo_summary[0]

            self.repo_status = self.repo_summary.get('status')

            self.last_build_started = self._format_time(self.repo_summary.get('queued_at'))
            try:
                self.last_build_duration = TimeWrapper(
                    self.repo_summary.get('build_time_millis') / 1000,
                    default_format=self.duration_format)
            except TypeError:
                self.last_build_duration = 0

        if self.repo_status_map:
            self.repo_status = self.repo_status_map.get(self.repo_status, self.repo_status)

        self.output = dict(
            full_text=formatp(self.format, **vars(self)),
            short_text=self.short_format.format(**vars(self)),
        )
        if self.status_color_map:
            self.output['color'] = self.status_color_map.get(self.repo_status, self.color)
        else:
            self.output['color'] = self.color

    def open_build_webpage(self):
        if self.repo_summary.get('workflows'):
            url_format = 'workflow-run/{}'.format(self.repo_summary['workflows']['workflow_id'])
        else:
            url_format = 'gh/{repo_owner}/{repo_name}/{job_number}'

        os.popen('xdg-open https:/circleci.com/{} > /dev/null'
                 .format(url_format))
Example #2
0
class TestCircleCIApi(unittest.TestCase):

    def setUp(self):
        self.c = Api(os.getenv('CIRCLE_TOKEN'))

    def loadMock(self, filename):
        """helper function to open mock responses"""
        filename = 'tests/mocks/{0}'.format(filename)

        with open(filename, 'r') as f:
            self.c._request = MagicMock(return_value=f.read())

    def test_bad_verb(self):

        with self.assertRaises(BadVerbError) as e:
            self.c._request('BAD', 'dummy')

        self.assertEqual('BAD', e.exception.argument)
        self.assertIn('DELETE', e.exception.message)

    def test_get_user_info(self):
        self.loadMock('mock_user_info_response')
        resp = json.loads(self.c.get_user_info())

        self.assertEqual(resp["selected_email"], '*****@*****.**')

    def test_get_projects(self):
        self.loadMock('mock_get_projects_response')
        resp = json.loads(self.c.get_projects())

        self.assertEqual(resp[0]['vcs_url'], 'MOCK+https://ghe-dev.circleci.com/ccie-tester/testing')

    def test_follow_project(self):
        self.loadMock('mock_follow_project_response')
        resp = json.loads(self.c.follow_project('ccie-tester', 'testing'))

        self.assertEqual(resp["mock+following"], True)

    def test_get_project_build_summary(self):
        self.loadMock('mock_project_build_summary_response')
        resp = json.loads(self.c.get_project_build_summary('ccie-tester', 'testing'))

        self.assertEqual(len(resp), 6)
        self.assertEqual(resp[0]['username'], 'MOCK+ccie-tester')

        # with invalid status filter
        with self.assertRaises(InvalidFilterError) as e:
            json.loads(self.c.get_project_build_summary('ccie-tester', 'testing', status_filter='dummy'))

        self.assertEqual('dummy', e.exception.argument)
        self.assertIn('running', e.exception.message)

        # with branch
        resp = json.loads(self.c.get_project_build_summary('ccie-tester', 'testing', branch='master'))

        self.assertEqual(len(resp), 6)
        self.assertEqual(resp[0]['username'], 'MOCK+ccie-tester')

    def test_get_recent_builds(self):
        self.loadMock('mock_get_recent_builds_response')
        resp = json.loads(self.c.get_recent_builds())

        self.assertEqual(len(resp), 7)
        self.assertEqual(resp[0]['reponame'], 'MOCK+testing')

    def test_get_build_info(self):
        self.loadMock('mock_get_build_info_response')
        resp = json.loads(self.c.get_build_info('ccie-tester', 'testing', '1'))

        self.assertEqual(resp['reponame'], 'MOCK+testing')

    def test_get_artifacts(self):
        self.loadMock('mock_get_artifacts_response')
        resp = json.loads(self.c.get_artifacts('ccie-tester', 'testing', '1'))

        self.assertEqual(resp[0]['path'], 'MOCK+raw-test-output/go-test-report.xml')

    def test_retry_build(self):
        self.loadMock('mock_retry_build_response')
        resp = json.loads(self.c.retry_build('ccie-tester', 'testing', '1'))

        self.assertEqual(resp['reponame'], 'MOCK+testing')

        # with SSH
        resp = json.loads(self.c.retry_build('ccie-tester', 'testing', '1', ssh=True))

        self.assertEqual(resp['reponame'], 'MOCK+testing')

    def test_cancel_build(self):
        self.loadMock('mock_cancel_build_response')
        resp = json.loads(self.c.cancel_build('ccie-tester', 'testing', '11'))

        self.assertEqual(resp['reponame'], 'MOCK+testing')
        self.assertEqual(resp['build_num'], 11)
        self.assertTrue(resp['canceled'])

    def test_add_ssh_user(self):
        self.loadMock('mock_add_ssh_user_response')
        resp = json.loads(self.c.add_ssh_user('ccie-tester', 'testing', '11'))

        self.assertEqual(resp['reponame'], 'MOCK+testing')
        self.assertEqual(resp['ssh_users'][0]['login'], 'ccie-tester')

    def test_trigger_build(self):
        self.loadMock('mock_trigger_build_response')
        resp = json.loads(self.c.trigger_build('ccie-tester', 'testing'))

        self.assertEqual(resp['reponame'], 'MOCK+testing')

    def test_list_checkout_keys(self):
        self.loadMock('mock_list_checkout_keys_response')
        resp = json.loads(self.c.list_checkout_keys('levlaz', 'circleci-sandbox'))

        self.assertEqual(resp[0]['type'], 'deploy-key')
        self.assertIn('public_key', resp[0])

    def test_create_checkout_key(self):

        with self.assertRaises(BadKeyError) as e:
            self.c.create_checkout_key('levlaz', 'test', 'bad')

        self.assertEqual('bad', e.exception.argument)
        self.assertIn('deploy-key', e.exception.message)

        self.loadMock('mock_create_checkout_key_response')
        resp = json.loads(self.c.create_checkout_key('levlaz', 'test', 'deploy-key'))

        self.assertEqual(resp['type'], 'deploy-key')
        self.assertIn('public_key', resp)

    def test_get_checkout_key(self):

        self.loadMock('mock_get_checkout_key_response')
        resp = json.loads(self.c.get_checkout_key('levlaz', 'circleci-sandbox', '94:19:ab:a9:f4:2b:21:1c:a5:87:dd:ee:3d:c2:90:4e'))

        self.assertEqual(resp['type'], 'deploy-key')
        self.assertIn('public_key', resp)

    def test_delete_checkout_key(self):
        self.loadMock('mock_delete_checkout_key_response')
        resp = json.loads(self.c.delete_checkout_key('levlaz', 'circleci-sandbox', '94:19:ab:a9:f4:2b:21:1c:a5:87:dd:ee:3d:c2:90:4e'))

        self.assertEqual(resp['message'], 'ok')

    def test_clear_cache(self):
        self.loadMock('mock_clear_cache_response')
        resp = json.loads(self.c.clear_cache('levlaz', 'circleci-sandbox'))

        self.assertEqual('build dependency caches deleted', resp['status'])

    def test_get_test_metadata(self):
        self.loadMock('mock_get_test_metadata_response')
        resp = json.loads(self.c.get_test_metadata('levlaz', 'circleci-demo-javascript-express', 127))

        self.assertEqual(len(resp), 2)
        self.assertIn('tests', resp)

    def test_list_envvars(self):
        self.loadMock('mock_list_envvars_response')
        resp = json.loads(self.c.list_envvars('levlaz', 'circleci-sandbox'))

        self.assertEqual(len(resp), 4)
        self.assertEqual(resp[0]['name'], 'BAR')

    def test_add_envvar(self):
        self.loadMock('mock_add_envvar_response')
        resp = json.loads(self.c.add_envvar('levlaz', 'circleci-sandbox', 'foo', 'bar'))

        self.assertEqual(resp['name'], 'foo')
        self.assertNotEqual(resp['value'], 'bar')

    def test_get_envvar(self):
        self.loadMock('mock_get_envvar_response')
        resp = json.loads(self.c.get_envvar('levlaz', 'circleci-sandbox', 'foo'))

        self.assertEqual(resp['name'], 'foo')
        self.assertNotEqual(resp['value'], 'bar')

    def test_delete_envvar(self):
        self.loadMock('mock_delete_envvar_response')
        resp = json.loads(self.c.delete_envvar('levlaz', 'circleci-sandbox', 'foo'))

        self.assertEqual(resp['message'], 'ok')

    def test_get_latest_artifact(self):
        self.loadMock('mock_get_latest_artifacts_response')
        resp = json.loads(self.c.get_latest_artifact('levlaz', 'circleci-sandbox'))

        self.assertEqual(resp[0]['path'],'circleci-docs/index.html')

        resp = json.loads(self.c.get_latest_artifact('levlaz', 'circleci-sandbox', 'master'))
        self.assertEqual(resp[0]['path'],'circleci-docs/index.html')

        with self.assertRaises(InvalidFilterError):
            self.c.get_latest_artifact('levlaz', 'circleci-sandbox', 'master', 'invalid')
Example #3
0
class CircleCIHelper:
    def __init__(self, token):
        try:
            self.ci = Api(token)
            self.username = self.ci.get_user_info()['login']
            app.logger.info("Initialised connection to Circleci with user: %s",
                            self.username)
            return None

        except Exception as e:
            app.logger.error(
                "Unable to initialise connection to CircleCi api: %s", repr(e))
            return None

    def get_own_projects(self, include_branches=False):
        try:
            projects = []

            for project in self.ci.get_projects():

                if (include_branches):
                    branches = []
                    for branch in project['branches']:
                        branches.append(branch)

                    projects.append({
                        "name":
                        project['username'] + "/" + project['reponame'],
                        "branches":
                        branches
                    })
                else:
                    projects.append({
                        "name":
                        project['username'] + "/" + project['reponame']
                    })

            return projects
        except Exception as e:
            app.logger.error("Unable to get user own projects: %s", repr(e))
            return []

    def get_latest_project_builds(self, username, project):
        try:
            recent_builds = []

            for build in self.ci.get_project_build_summary(username, project):
                recent_builds.append({
                    "build_num":
                    build['build_num'],
                    "branch":
                    build['branch'],
                    "status":
                    build['status'],
                    "build_time_ms":
                    build['build_time_millis'],
                    "start_time":
                    build['start_time']
                })

            return recent_builds

        except Exception as e:
            app.logger.error(
                "Unable to get user '%s' latest project '%s' builds: %s",
                username, project, repr(e))
            return []