Merge pull request '[#306] Removed Travis CI integration' (#324) from 306/remove-travisci into master

Reviewed-on: platypush/platypush#324
This commit is contained in:
Fabio Manganiello 2023-09-20 23:35:38 +02:00
commit 4fa3d7b311
8 changed files with 0 additions and 190 deletions

View file

@ -50,7 +50,6 @@ Backends
platypush/backend/stt.picovoice.speech.rst
platypush/backend/tcp.rst
platypush/backend/todoist.rst
platypush/backend/travisci.rst
platypush/backend/trello.rst
platypush/backend/weather.buienradar.rst
platypush/backend/weather.darksky.rst

View file

@ -1,5 +0,0 @@
``travisci``
==============================
.. automodule:: platypush.backend.travisci
:members:

View file

@ -1,5 +0,0 @@
``travisci``
==============================
.. automodule:: platypush.plugins.travisci
:members:

View file

@ -129,7 +129,6 @@ Plugins
platypush/plugins/tensorflow.rst
platypush/plugins/todoist.rst
platypush/plugins/torrent.rst
platypush/plugins/travisci.rst
platypush/plugins/trello.rst
platypush/plugins/tts.rst
platypush/plugins/tts.google.rst

View file

@ -1,105 +0,0 @@
import datetime
from typing import Optional
from platypush.backend import Backend
from platypush.context import get_plugin
from platypush.message.event.travisci import TravisciBuildPassedEvent, TravisciBuildFailedEvent
class TravisciBackend(Backend):
"""
This backend polls for new builds on a `Travis-Ci <https://travis-ci.org>`_ account and triggers an event
whenever a new build is completed.
Requires:
* The :class:`platypush.plugins.foursquare.FoursquarePlugin` plugin configured and enabled.
Triggers:
- :class:`platypush.message.event.travisci.TravisciBuildPassedEvent` when the build of a project owned by
the user passes.
- :class:`platypush.message.event.travisci.TravisciBuildFailedEvent` when the build of a project owned by
the user fails.
"""
_last_build_finished_at_varname = '_travisci_last_build_finished_at'
def __init__(self, poll_seconds: Optional[float] = 60.0, *args, **kwargs):
"""
:param poll_seconds: How often the backend should check for new builds (default: one minute).
"""
super().__init__(*args, poll_seconds=poll_seconds, **kwargs)
self._last_build_finished_at = None
@staticmethod
def _convert_iso_date(d):
if not d:
return
try:
return float(d)
except ValueError:
pass
if isinstance(d, str):
if d.endswith('Z'):
d = d[:-1] + '+00:00'
d = datetime.datetime.fromisoformat(d)
assert isinstance(d, datetime.datetime), 'Unexpected datetime type: ' + type(d)
return d.timestamp()
def __enter__(self):
self._last_build_finished_at = self._convert_iso_date(
get_plugin('variable').get(self._last_build_finished_at_varname).
output.get(self._last_build_finished_at_varname) or 0)
self.logger.info('Started Travis-CI backend')
def loop(self):
builds = get_plugin('travisci').builds(limit=1).output
if not builds:
return
last_build = builds[0]
last_build_finished_at = self._convert_iso_date(last_build.get('finished_at', 0))
if not last_build_finished_at:
return
if self._last_build_finished_at and last_build_finished_at <= self._last_build_finished_at:
return
if last_build.get('state') == 'passed':
evt_type = TravisciBuildPassedEvent
elif last_build.get('state') == 'failed':
evt_type = TravisciBuildFailedEvent
else:
return
evt = evt_type(repository_id=last_build.get('repository', {}).get('id'),
repository_name=last_build.get('repository', {}).get('name'),
repository_slug=last_build.get('repository').get('slug'),
build_id=int(last_build.get('id')),
build_number=int(last_build.get('number')),
duration=last_build.get('duration'),
previous_state=last_build.get('previous_state'),
private=last_build.get('private'),
tag=last_build.get('tag'),
branch=last_build.get('branch', {}).get('name'),
commit_id=last_build.get('commit', {}).get('id'),
commit_sha=last_build.get('commit', {}).get('sha'),
commit_message=last_build.get('commit', {}).get('message'),
committed_at=self._convert_iso_date(
last_build.get('commit', {}).get('committed_at')),
created_by=last_build.get('created_by', {}).get('login'),
started_at=self._convert_iso_date(last_build.get('started_at')),
finished_at=self._convert_iso_date(last_build.get('finished_at')))
self.bus.post(evt)
self._last_build_finished_at = last_build_finished_at
get_plugin('variable').set(**{self._last_build_finished_at_varname: self._last_build_finished_at})
# vim:sw=4:ts=4:et:

View file

@ -1,10 +0,0 @@
manifest:
events:
platypush.message.event.travisci.TravisciBuildFailedEvent: when the build of a
project owned bythe user fails.
platypush.message.event.travisci.TravisciBuildPassedEvent: when the build of a
project owned bythe user passes.
install:
pip: []
package: platypush.backend.travisci
type: backend

View file

@ -1,57 +0,0 @@
import requests
from typing import Callable, Dict, Any, List
from platypush.plugins import Plugin, action
class TravisciPlugin(Plugin):
"""
`Travis-Ci <https://travis-ci.org/>`_ continuous integration plugin.
Setup:
- Get your API token from your `Travis-Ci account settings page <https://travis-ci.org/account/preferences>`_.
"""
api_base_url = 'https://api.travis-ci.org/'
def __init__(self, token: str, **kwargs):
super().__init__(**kwargs)
self.headers = {
'Travis-API-Version': '3',
'Authorization': 'token ' + token,
}
def _make_request(self, method: Callable, endpoint: str, **kwargs):
url = self.api_base_url + endpoint
response = method(url, headers=self.headers, **kwargs).json()
if response.get('@type') == 'error':
raise AssertionError('{type}: {message}'.
format(type=response.get('error_type'), message=response.get('error_message')))
return response
@action
def repos(self) -> Dict[str, Dict[str, Any]]:
"""
Get the repos owned by current user.
:return: Repo name -> Repo attributes mapping.
"""
return {
repo['name']: repo
for repo in self._make_request(requests.get, endpoint='repos').get('repositories', [])
}
@action
def builds(self, limit: int = 100) -> Dict[str, List[Dict[str, Any]]]:
"""
Get the list of builds triggered on the owned repositories
:param limit: Maximum number of builds to be retrieved (default: 100).
:return: Repo name -> List of builds
"""
return self._make_request(requests.get, endpoint='builds').get('builds', [])[:limit]
# vim:sw=4:ts=4:et:

View file

@ -1,6 +0,0 @@
manifest:
events: {}
install:
pip: []
package: platypush.plugins.travisci
type: plugin