Skip to content

Commit

Permalink
DAOS-7821 gha: Tweak jira reporting script. (#9671)
Browse files Browse the repository at this point in the history
Load labels from github to avoid errors on removal.
Add components for il and mercury.
Experiment with pull_request_target.
Use denylist for ticket status

Signed-off-by: Ashley Pittman <ashley.m.pittman@intel.com>
Co-authored-by: Phil Henderson <phillip.henderson@intel.com>
  • Loading branch information
ashleypittman and phender authored Jul 18, 2022
1 parent cf1e3b0 commit f540d6c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/pr-metadata.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
name: Jira Report

on:
# This should probably be pull_request_target.
pull_request:
# Having this be pull_request_target rather than pull_request means it runs in the context of the
# target branch rather than the PR, which in turn means the checkout is of the target.
pull_request_target:

jobs:
example_comment_pr:
runs-on: ubuntu-latest
name: Report Jira data to PR comment
env:
PR_TITLE: ${{ github.event.pull_request.title }}
PR_NUMBER: ${{ github.event.pull_request.number }}
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -28,7 +30,7 @@ jobs:
${{ steps.jira-data.outputs.message }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Set labels
if: always() && ${{ steps.jira-data.outputs.label }}
if: ${{ always() && steps.jira-data.outputs.label != '' }}
uses: actions-ecosystem/action-add-labels@v1
with:
labels: ${{ steps.jira-data.outputs.label }}
Expand Down
44 changes: 33 additions & 11 deletions ci/jira_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import os
import sys
import json
import urllib
import jira

# Script to improve interaction with Jenkins, GitHub and Jira. This is intended to work in several
Expand All @@ -22,15 +24,18 @@
# Expected components from the commit message, and directory in src/, src/client or utils/ is also
# valid. We've never checked/enforced these before so there have been a lot of values used in the
# past.
VALID_COMPONENTS = ('build', 'ci', 'doc', 'gha', 'test')
VALID_COMPONENTS = ('build', 'ci', 'doc', 'gha', 'il', 'mercury', 'test')

# Expected ticket prefix.
VALID_TICKET_PREFIX = ('DAOS', 'CORCI', 'SRE')

# 10044 is "Approved to Merge"
# 10045 is "Required for Version"
FIELDS = 'summary,status,labels,customfield_10044,customfield_10045'

# Expected values for Status. Tickets which are closed should not be being worked on, and tickets
# Excluded values for Status. Tickets which are closed should not be being worked on, and tickets
# which are Open or Reopened should be set to In Progress when being worked on.
STATUS_VALUES_ALLOWED = ('In Review', 'In Progress')
STATUS_VALUES_NOT_ALLOWED = ('Open', 'Reopened', 'To Do', 'Resolved')

# Labels in GitHub which this script will set/clear based on the logic below.
MANAGED_LABELS = ('release-2.2', 'release-2.4', 'priority')
Expand All @@ -41,7 +46,7 @@ def set_output(key, value):

clean_value = value.replace('\n', '%0A')
print(f'::set-output name={key}::{clean_value}')
print(value)
print(f'{key}:{value}')


# pylint: disable=too-many-branches
Expand Down Expand Up @@ -86,7 +91,7 @@ def main():

# Check format of ticket_number.
parts = ticket_number.split('-', maxsplit=1)
if parts[0] not in ('DAOS', 'CORCI'):
if parts[0] not in VALID_TICKET_PREFIX:
errors.append('Ticket number prefix incorrect')
try:
int(parts[1])
Expand All @@ -103,7 +108,7 @@ def main():
return
print(ticket.fields.summary)
print(ticket.fields.status)
if str(ticket.fields.status) not in STATUS_VALUES_ALLOWED:
if str(ticket.fields.status) in STATUS_VALUES_NOT_ALLOWED:
errors.append('Ticket status value not as expected')

# Highest priority, tickets with "Approved to Merge" set.
Expand Down Expand Up @@ -162,11 +167,28 @@ def main():
if gh_label:
set_output('label', '\n'.join(gh_label))

to_remove = list(MANAGED_LABELS)
for label in gh_label:
to_remove.remove(label)
if to_remove:
set_output('label-clear', '\n'.join(to_remove))
github_repo = os.getenv('GITHUB_REPOSITORY')

if github_repo:

pr_number = os.getenv('PR_NUMBER')

gh_url = f'https://api.github.com/repos/{github_repo}/issues/{pr_number}/labels'
print(gh_url)
with urllib.request.urlopen(gh_url) as gh_label_data: # nosec
gh_labels = json.loads(gh_label_data.read())

# Remove all managed labels which are not to be set.
to_remove = []
for label in gh_labels:
name = label['name']
if name in MANAGED_LABELS and name not in gh_label:
to_remove.append(name)
if to_remove:
set_output('label-clear', '\n'.join(to_remove))

# Could possibly query/verify more data using this URL however no use-case for this yet.
# gh_url = f'https://api.github.com/repos/{github_repo}/pulls/{pr_number}'

if errors:
sys.exit(1)
Expand Down

0 comments on commit f540d6c

Please sign in to comment.