-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CI] - GitHub actions master slack notifier (#4035)
* bla * try rohammosalli/slack-action@master * create script to notify failed jobs on github * add slack-notify-master-failure job * update: * update user * remove comments * slack notifier to workflow scripts * test * if failure() run only if there is a failing job * check log info * logger * veirfy=False * run only on master * update branch for test * without branch * revert failing test * run only in master * change step name * pre-commit * use typer instead of argparse * fix types * test * fix failed test * test * update args * run only in master * logs * pre-commit
- Loading branch information
Showing
2 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
Utils/github_workflow_scripts/slack_notifier_master/slack_notifier.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
from typing import Collection, Dict, List, Optional | ||
|
||
import typer | ||
from github import Github, WorkflowRun | ||
from slack_sdk import WebClient | ||
|
||
from demisto_sdk.commands.common.logger import logger | ||
|
||
DEFAULT_SLACK_CHANNEL = "dmst-build-test" | ||
|
||
|
||
def get_failed_jobs(workflow_run: WorkflowRun) -> List[str]: | ||
jobs = [job for job in workflow_run.jobs() if job.conclusion == "failure"] | ||
|
||
failed_jobs = [] | ||
|
||
for job in jobs: | ||
failed_steps = [] | ||
for step in job.steps: | ||
if step.conclusion == "failure": | ||
failed_steps.append(step.name) | ||
|
||
job_name = job.name | ||
failed_jobs.append( | ||
f'{job_name}[{", ".join(failed_steps)}]' if failed_steps else job_name | ||
) | ||
|
||
return failed_jobs | ||
|
||
|
||
def construct_slack_message(summary_url: str, failed_jobs: List[str]) -> List[Dict]: | ||
def construct_slack_section(_section_title: str, _failed_entities: Collection[str]): | ||
""" | ||
Construct a single section in the slack body message. | ||
Args: | ||
_section_title (str): title of the section. | ||
_failed_entities (Collection[str]): failed jobs | ||
Returns: | ||
dict: a format a single section in the slack body message. | ||
""" | ||
return { | ||
"title": f"{_section_title} - ({len(_failed_entities)})", | ||
"value": "\n".join(_failed_entities), | ||
"short": False, | ||
} | ||
|
||
slack_body_message = [] | ||
if failed_jobs: | ||
slack_body_message.append( | ||
construct_slack_section( | ||
_section_title="Failed Github-Actions Jobs", | ||
_failed_entities=failed_jobs, | ||
) | ||
) | ||
|
||
title = "Demisto SDK Master-Failure" | ||
|
||
if slack_body_message: | ||
return [ | ||
{ | ||
"fallback": title, | ||
"color": "danger", | ||
"title": title, | ||
"title_link": summary_url, | ||
"fields": slack_body_message, | ||
} | ||
] | ||
return [] | ||
|
||
|
||
main = typer.Typer(pretty_exceptions_enable=False) | ||
|
||
|
||
@main.command() | ||
def slack_notifier( | ||
ctx: typer.Context, | ||
workflow_id: int = typer.Option( | ||
"", | ||
"--workflow-id", | ||
help="The workflow id triggered by the PR", | ||
), | ||
slack_token: Optional[str] = typer.Option( | ||
None, | ||
"--slack-token", | ||
help="The token for slack api", | ||
), | ||
github_token: Optional[str] = typer.Option( | ||
None, "--github-token", "-n", help="The token for Github-Api" | ||
), | ||
slack_channel: str = typer.Option( | ||
DEFAULT_SLACK_CHANNEL, | ||
"--slack-channel", | ||
help="The slack channel to send the summary", | ||
), | ||
): | ||
gh_client = Github(login_or_token=github_token) | ||
repo = gh_client.get_repo("demisto/demisto-sdk") | ||
workflow_run: WorkflowRun = repo.get_workflow_run(workflow_id) | ||
|
||
failed_jobs = get_failed_jobs(workflow_run) | ||
summary_url = workflow_run.html_url | ||
|
||
slack_message = construct_slack_message(summary_url, failed_jobs=failed_jobs) | ||
if slack_message: | ||
slack_client = WebClient(token=slack_token) | ||
slack_client.chat_postMessage( | ||
channel=slack_channel, | ||
attachments=slack_message, | ||
username="Demisto-SDK Github-Actions", | ||
) | ||
logger.info("Successfully reported failed jobs to slack") | ||
else: | ||
logger.info("There are not any failed jobs to report to slack") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |