skill-gitlab-pipelines/__init__.py
2024-06-13 10:39:11 +07:00

73 lines
4.0 KiB
Python

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['sites'][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['sites'][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']}\n"
f"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 sites - returns avaiable gitlab sites\n"
return_text = f"{return_text} gitlab list projects - Returns project keywords and urls\n"
return_text = f"{return_text} gitlab <project> pipeline last - Returns name, status and id for last pipeline in specific project\n"
return_text = f"{return_text}```"
# return_text = f"{return_text}```gitlab <project> update inventory <id> - Updates inventory sources for inventory in specific project```\n"
# return_text = f"{return_text}```gitlab <project> list running jobs - Returns information about running jobs for specific project```\n"
# return_text = f"{return_text}```gitlab <project> list failed jobs - Returns information about last 5 failed jobs for specific project```\n"
# return_text = f"{return_text}```gitlab <project> list failed jobs yesterday - Returns information about last 24 hours of failed jobs for specific project```\n"
# return_text = f"{return_text}```gitlab <project> list failed jobs num:<#> - Returns information about last # failed jobs for specific project```\n"
# return_text = f"{return_text}```gitlab <project> list scheduled jobs - Returns information about next 5 scheduled jobs for specific project```\n"
# return_text = f"{return_text}```gitlab <project> list scheduled jobs num:<#> - Returns information about next # scheduled jobs for specific project```\n"
# return_text = f"{return_text}```gitlab <project> list scheduled jobs past - Returns information about scheduled jobs with next_run in the past```\n"
# return_text = f"{return_text}```gitlab <project> list projects - Returns information about project for specific project```\n"
# return_text = f"{return_text}```gitlab <project> update project <id> - 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}")
@match_regex(r"^gitlab sites$")
async def list_gitlab_sites(self, message):
sites_print = [i for i in self.config['sites']]
await message.respond(f"{sites_print}")
@match_regex(r"ping")
async def ping(self, event):
await event.respond('World')