from opsdroid.matchers import match_regex, match_webhook from opsdroid.message import Message from opsdroid.skill import Skill import logging import aiohttp import json import random logger = logging.getLogger(__name__) class GitlabPipelineSkill(Skill): def __init__(self, opsdroid, config): super(GitlabPipelineSkill, self).__init__(opsdroid, config) # do some setup stuff here async def _rest_call(self, site, api_url, call_method): token = self.config["gitlab"][site]["access_token"], headers = {f"Authorization: Bearer {token}"} timeout = aiohttp.ClientTimeout(total=60) async with aiohttp.ClientSession(headers=headers, timeout=timeout) as session: if call_method == "get": async with session.get(api_url) as resp: data = await resp.json() return data else: async with session.post(api_url) as resp: data = await resp.json() return data async def get_latest_pipeline(self, site, project_id): api_url = f"{self.config["gitlab"][site]["url"]}/api/v4/projects/{project_id}/pipelines/latest" data = await self._rest_call(site, api_url, "get") return_text = f"*For site {site} latest - Pipline*\n" for i in data["results"]: return_text = f"{return_text}```Pipeline ID: {i['id']} Project ID: {i['project_id']} Name: {i['name']}` Status: {i['status']} ``\n" return return_text async def _get_help(self): return_text = f"*Help*\n" return_text = f"{return_text}```/gitlab help - returns this help screen```\n" return_text = f"{return_text}```/gitlab list projects - Returns project keywords and urls```\n" return_text = f"{return_text}```/gitlab pipeline last - Returns name, status and id for last pipeline in specific project```\n" # return_text = f"{return_text}```gitlab update inventory - Updates inventory sources for inventory in specific project```\n" # return_text = f"{return_text}```gitlab list running jobs - Returns information about running jobs for specific project```\n" # return_text = f"{return_text}```gitlab list failed jobs - Returns information about last 5 failed jobs for specific project```\n" # return_text = f"{return_text}```gitlab list failed jobs yesterday - Returns information about last 24 hours of failed jobs for specific project```\n" # return_text = f"{return_text}```gitlab list failed jobs num:<#> - Returns information about last # failed jobs for specific project```\n" # return_text = f"{return_text}```gitlab list scheduled jobs - Returns information about next 5 scheduled jobs for specific project```\n" # return_text = f"{return_text}```gitlab list scheduled jobs num:<#> - Returns information about next # scheduled jobs for specific project```\n" # return_text = f"{return_text}```gitlab list scheduled jobs past - Returns information about scheduled jobs with next_run in the past```\n" # return_text = f"{return_text}```gitlab list projects - Returns information about project for specific project```\n" # return_text = f"{return_text}```gitlab update project - Updated a project by id```\n" return return_text @match_regex(r"^/gitlab help$") async def list_help(self, message): help_print = await self._get_help() await message.respond(f"{help_print}")