Skip to content
Snippets Groups Projects
Commit 43358c40 authored by Zhang, Chen's avatar Zhang, Chen
Browse files

Merge branch 'PL138_add_mantid_report' into 'main'

[PL138] add mantid report to slack bot

See merge request !7
parents ee35779e 558651dc
No related branches found
No related tags found
1 merge request!7[PL138] add mantid report to slack bot
Pipeline #321055 passed
......@@ -41,27 +41,16 @@ def legend():
"failure": ":x:",
"other error": ":ghost:",
}
_legend = "\t:white_small_square:".join([f"{status}: {emoji}" for status, emoji in emojis.items()])
_legend = "\t:white_small_square:\t".join(
[f"{status}: {emoji}" for status, emoji in emojis.items()]
)
block = [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "LEGEND",
"emoji": True
}
},
{
"type": "section",
"text":
{
"type": "mrkdwn",
"text": _legend
}
},
{
"type": "divider"
"text": {"type": "plain_text", "text": "LEGEND", "emoji": True},
},
{"type": "section", "text": {"type": "mrkdwn", "text": _legend}},
{"type": "divider"},
]
return block
......@@ -73,10 +62,13 @@ def github_status_blocks():
"text": {
"type": "plain_text",
"text": "PROJECTS IN GITHUB:",
"emoji": True
}
"emoji": True,
},
},
]
headers = {
"Authorization": f"token {os.environ['GITHUB_TOKEN']}" # GITHUB_TOKEN needs to be set in the environment
}
# Organization, Repository, Workflow-Name
WORKFLOW_ANY = "*" # any workflow is fine for testing
query_github = [
......@@ -93,38 +85,33 @@ def github_status_blocks():
branch_reports = list()
url = f"https://api.github.com/repos/{owner}/{project}/actions/runs"
for branch in ["next", "qa", "main"]:
response = requests.get(url, params={"branch": branch})
response = requests.get(url, params={"branch": branch}, headers=headers)
run_status = "error" # default branch report
if response.ok:
for run in response.json()["workflow_runs"]:
if workflow_name in (WORKFLOW_ANY, run["name"]):
# run["conclusion"] is None for uncompleted jobs
run_status = run["conclusion"] if run["conclusion"] is not None else run["status"]
run_status = (
run["conclusion"]
if run["conclusion"] is not None
else run["status"]
)
break
else:
logger.error(f"Request GET failed for {owner}/{project}?parameter=branch={branch}")
branch_reports.append(f"*{branch}* {status2emoji.get(run_status, ':ghost:')}")
project_report = "\t:white_small_square:".join(branch_reports)
logger.error(
f"Request GET failed for {owner}/{project}?parameter=branch={branch}"
)
branch_reports.append(
f"*{branch}* {status2emoji.get(run_status, ':ghost:')}"
)
project_report = "\t:white_small_square:\t".join(branch_reports)
blocks += [
{
"type": "header",
"text": {
"type": "plain_text",
"text": f"{project}",
"emoji": True
}
},
{
"type": "section",
"text":
{
"type": "mrkdwn",
"text": project_report
}
},
{
"type": "divider"
"text": {"type": "plain_text", "text": f"{project}", "emoji": True},
},
{"type": "section", "text": {"type": "mrkdwn", "text": project_report}},
{"type": "divider"},
]
return blocks
......@@ -136,8 +123,8 @@ def gitlab_status_blocks():
"text": {
"type": "plain_text",
"text": "PROJECTS IN GITLAB:",
"emoji": True
}
"emoji": True,
},
},
]
......@@ -146,7 +133,7 @@ def gitlab_status_blocks():
"drtSANS": 4955,
"Web Monitor Test AMQ Message Generator": 10770,
"Web_Reflectivity": 5380,
"live-run-monitor": 10186
"live-run-monitor": 10186,
}
for project, project_id in projects.items():
......@@ -174,42 +161,90 @@ def gitlab_status_blocks():
print(status)
branches_status[branch] = status_emoji
# build the block
status_report = "\t:white_small_square:".join([f"*{branch}* {branches_status[branch]}"
for branch in ["next", "qa", "main"]])
status_report = "\t:white_small_square:\t".join(
[
f"*{branch}* {branches_status[branch]}"
for branch in ["next", "qa", "main"]
]
)
blocks += [
{
"type": "header",
"text": {
"type": "plain_text",
"text": f"{project}",
"emoji": True
}
"text": {"type": "plain_text", "text": f"{project}", "emoji": True},
},
{
"type": "section",
"text":
{
"type": "mrkdwn",
"text": status_report
}
{"type": "section", "text": {"type": "mrkdwn", "text": status_report}},
{"type": "divider"},
]
return blocks
def mantid_block():
"""Mantid block
Mantid project does not follow ORNL branch conventions, therefore it needs
a special block.
"""
owner = "mantidproject"
project = "mantid"
url = f"https://api.github.com/repos/{owner}/{project}/actions/runs"
headers = {
"Authorization": f"token {os.environ['GITHUB_TOKEN']}" # GITHUB_TOKEN needs to be set in the environment
}
#
branches_status = {
"main": "error",
"ornl-next": "error",
}
for branch in branches_status.keys():
response = requests.get(url, params={"branch": branch}, headers=headers)
if response.ok:
workflow_runs = response.json()["workflow_runs"]
if len(workflow_runs) > 0:
conclusion = workflow_runs[0].get("conclusion", "error")
branches_status[branch] = status2emoji[conclusion]
#
status_report = "\t:white_small_square:\t".join(
[f"*{branch}* {status}" for branch, status in branches_status.items()]
)
#
blocks = [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "Mantid Project",
"emoji": True,
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": status_report,
},
]
},
{"type": "divider"},
]
return blocks
def get_status_blocks():
blocks = legend()
# Gitlab general project following ORNL branch conventions
blocks += gitlab_status_blocks()
# Gitlab projects with special branch conventions
# None at the moment
# Github projects following ORNL branch conventions
blocks += github_status_blocks()
# Github projects with special branch conventions
blocks += mantid_block()
return blocks
# Install the Slack app and get xoxb- token in advance
app = App(token=os.environ["SLACK_BOT_TOKEN"], name="Amaterasu")
@app.event("app_mention")
def greetings(ack, say):
ack()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment