Add failed step logfile extraction
This commit is contained in:
parent
4c5f4a5312
commit
c3da1121ac
@ -2,6 +2,20 @@ from os import environ, getenv
|
||||
from datetime import datetime, UTC
|
||||
from discord_webhook import DiscordEmbed, DiscordWebhook
|
||||
from pprint import pprint
|
||||
from urllib.parse import urljoin, urlparse
|
||||
from requests import Session
|
||||
from requests.models import Response
|
||||
|
||||
|
||||
class APISession(Session):
|
||||
def __init__(self, base_url) -> None:
|
||||
self.base_url = base_url
|
||||
super().__init__()
|
||||
|
||||
def request(self, method: str | bytes, url: str | bytes, *args, **kwargs) -> Response:
|
||||
url = urljoin(self.base_url, url)
|
||||
return super().request(method, url, *args, **kwargs)
|
||||
|
||||
|
||||
webhook = DiscordWebhook(environ["PLUGIN_WEBHOOK_URL"])
|
||||
|
||||
@ -47,5 +61,33 @@ webhook.add_embed(DiscordEmbed(
|
||||
}]
|
||||
))
|
||||
|
||||
try:
|
||||
if not success:
|
||||
with APISession(urljoin(environ["PLUGIN_WOODPECKER_URL"], "/api")) as client:
|
||||
client.headers.setdefault("Authorization", f"Bearer {environ["PLUGIN_WOODPECKER_TOKAN"]}")
|
||||
pipeline_url = urlparse(environ["CI_PIPELINE_URL"]).path
|
||||
pipeline_info = client.get(pipeline_url).json()
|
||||
for workflow in pipeline_info["workflows"]:
|
||||
if workflow["name"] != environ["CI_WORKFLOW_NAME"]:
|
||||
continue
|
||||
for step in workflow["children"]:
|
||||
if step["state"] != "failure":
|
||||
continue
|
||||
logdata = client.get(urljoin(pipeline_url, step["id"]))
|
||||
webhook.add_file(logdata.content, f"{step['name']}.log")
|
||||
except KeyError:
|
||||
webhook.add_embed(DiscordEmbed(
|
||||
"API Error",
|
||||
"API access has not been configured.\n"\
|
||||
"As a result, Discord Notifier is unable to retrieve failed job logs to attach here.\n"\
|
||||
"Please use the `woodpecker_url` and `woodpecker_token` settings to allow API access.",
|
||||
color="ED4345",
|
||||
timestamp=datetime.now(UTC),
|
||||
footer={
|
||||
"text": "Discord Notifier",
|
||||
"icon_url": "https://dev.shielddagger.com/repo-avatars/273e88fa2afde290121dc7b5987dc366b88325f147bf1e5766bca26296bbc1f9"
|
||||
}
|
||||
))
|
||||
|
||||
pprint(webhook.json)
|
||||
webhook.execute()
|
||||
|
Loading…
x
Reference in New Issue
Block a user