Skip to content

Commit

Permalink
[CI] - GitHub actions master slack notifier (#4035)
Browse files Browse the repository at this point in the history
* 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
GuyAfik committed Feb 21, 2024
1 parent 43be044 commit 40fc9d7
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/on-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -448,3 +448,20 @@ jobs:
with:
artifacts-path-dir: content/test-lint
artifact-name: test-lint-artifacts

slack-notify-master-failure:
needs: [ pre-commit-checks, unit-tests, integration-tests, graph-tests, test-content-infrastructure, test-pre-commit-command, test-graph-commands, validate-files-old-validate, validate-files-new-validate, test-lint]
if: failure() && github.ref_name == 'master'
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Python Environment
uses: ./.github/actions/setup_environment
with:
python-version: '3.10'

- name: Notify Failed Jobs
run: |
poetry run python Utils/github_workflow_scripts/slack_notifier_master/slack_notifier.py --workflow-id ${{ github.run_id }} --slack-token ${{secrets.SLACK_TOKEN}} --github-token ${{ secrets.GITHUB_TOKEN }} --slack-channel ${{secrets.SLACK_CHANNEL}}
119 changes: 119 additions & 0 deletions Utils/github_workflow_scripts/slack_notifier_master/slack_notifier.py
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()

0 comments on commit 40fc9d7

Please sign in to comment.