Skip to content

Commit

Permalink
Merge pull request #1810 from cathales/dashboard
Browse files Browse the repository at this point in the history
ci: notification from Dashboard
  • Loading branch information
JeanRochCoulon committed Apr 24, 2023
2 parents dda6b8a + 75467ea commit 320e46f
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/dashboard-done.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
on:
workflow_dispatch:
inputs:
pr_number:
description: 'ID of the PR to comment'
required: true
type: string
success:
description: 'Is the workflow successful?'
required: true
type: boolean

permissions:
pull-requests: write

jobs:
welcome:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v6
with:
script: |
const inputs = context.payload.inputs
const pr = inputs.pr_number
const success = inputs.success == 'true'
const status_text = success ? ":heavy_check_mark: successful" : ":x: failed"
const url = `https://riscv-ci.pages.thales-invia.fr/dashboard/dashboard_core-v-verif_${pr}.html`
await github.rest.issues.createComment({
issue_number: pr,
owner: context.repo.owner,
repo: context.repo.repo,
body: `${status_text} run, report available [here](${url}).`
})
63 changes: 63 additions & 0 deletions .gitlab-ci/scripts/github_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""
This module makes it possible to trigger GitHub workflows.
"""

from os import environ as env
import requests

def api_url(owner, repo):
"Build API url for a given repository"
return f"https://api.github.com/repos/{owner}/{repo}"

def pulls(owner, repo):
"Get (public) pull requests from a given repository"
url = api_url(owner, repo) + '/pulls'
headers = {}
if 'GH_TOKEN' in env:
headers["Authorization"] = f"Token {env['GH_TOKEN']}"
response = requests.get(url, headers=headers)
assert response.status_code == 200
return response.json()

class Workflow:
"GitHub Workflow that can be triggered on a dispatch event"
def __init__(self, owner, repo, workflow_id, ref):
dispatches = f"/actions/workflows/{workflow_id}/dispatches"
self.url = api_url(owner, repo) + dispatches
self.ref = ref

def _trigger(self, inputs):
"Trigger the workflow"
data = {
'ref': self.ref,
'inputs': inputs,
}
token = env['GH_TOKEN']
headers = {
'Accept': 'application/vnd.github+json',
'Authorization': f"Bearer {token}",
'X-GitHub-Api-Version': '2022-11-28',
}
return requests.post(url=self.url, json=data, headers=headers)

class DashboardDone(Workflow):
"`dashboard-done.yml` GitHub workflow"
def __init__(self, owner, repo, ref):
workflow_id = 'dashboard-done.yml'
Workflow.__init__(self, owner, repo, workflow_id, ref)

def send(self, pr, success):
"Send success or failure message"
inputs = {
'pr_number': str(pr),
'success': success,
}
return self._trigger(inputs)

def send_success(self, pr):
"Send message stating that job is successful"
return self.send(pr, True)

def send_failure(self, pr):
"Send message stating that job is failed"
return self.send(pr, False)
20 changes: 20 additions & 0 deletions .gitlab-ci/scripts/merge_job_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import datetime
import sys
import subprocess
import github_integration as gh

# arguments: inputdir outputfile

Expand Down Expand Up @@ -105,12 +106,14 @@
'jobs': []
}

success = True
dir_list = os.listdir(sys.argv[1])
for f in dir_list:
with open(sys.argv[1] + "/" + f, 'r') as job_report:
report = safe_load(job_report)
pipeline["jobs"].append(report)
if report['status'] != 'pass':
success = False
pipeline["status"] = 'fail'
pipeline["label"] = 'FAIL'

Expand Down Expand Up @@ -139,3 +142,20 @@
''', shell=True))
except subprocess.CalledProcessError as e:
print(f"Error: {e.output}")

def find_pr(branch, prs):
match = re.search(r'(.*)_PR_([a-zA-Z0-9](?:[a-zA-Z0-9]|[-_](?=[a-zA-Z0-9])){0,38})', branch)
if match:
label = f'{match.group(2)}:{match.group(1)}'
for pr in prs:
if label == pr['head']['label']:
return pr
return None

pulls = gh.pulls('openhwgroup', workflow_repo)
pr = find_pr(workflow_commit_ref_name, pulls)
if pr is not None:
ref_branch = pr['base']['ref']
wf = gh.DashboardDone('openhwgroup', workflow_repo, ref_branch)
response = wf.send(pr['number'], success)
print(response.text)

0 comments on commit 320e46f

Please sign in to comment.