HCCP4J-571 Github Action: Jirabot Merge fails #48
Workflow file for this run
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
# JiraBot github action | |
# ===================== | |
# | |
name: jirabot | |
on: | |
pull_request_target: | |
types: [opened, reopened] | |
branches: | |
- "master" | |
- "candidate-*" | |
jobs: | |
jirabot: | |
runs-on: ubuntu-20.04 | |
steps: | |
- uses: "actions/setup-python@v2" | |
with: | |
python-version: "3.8" | |
- name: "Install dependencies" | |
run: | | |
set -xe | |
python -VV | |
python -m site | |
python -m pip install --upgrade pip setuptools wheel | |
python -m pip install --upgrade jira | |
python -m pip --version | |
python -m pip freeze | grep jira | |
- name: "Run" | |
env: | |
JIRABOT_USERNAME : ${{ secrets.JIRABOT_USERNAME }} | |
JIRABOT_PASSWORD : ${{ secrets.JIRABOT_PASSWORD }} | |
JIRA_URL : ${{ vars.JIRA_URL }} | |
PULL_REQUEST_NUMBER : ${{ github.event.pull_request.number }} | |
PULL_REQUEST_TITLE : ${{ github.event.pull_request.title }} | |
PULL_REQUEST_AUTHOR_NAME : ${{ github.event.pull_request.user.login }} | |
PULL_URL: ${{ github.event.pull_request.html_url }} | |
COMMENTS_URL: ${{ github.event.pull_request.comments_url }} | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
import os | |
import re | |
from jira.client import JIRA | |
def updateIssue(jira, issue, user: str, pull_url: str) -> str: | |
result = '' | |
statusName = str(issue.fields.status) | |
if statusName == 'Open': | |
transition = 'Start Progress' | |
elif statusName == 'In Progress': | |
transition = '' | |
elif statusName == 'Resolved': | |
transition = 'Reopen Issue' | |
elif statusName == 'Closed': | |
transition = 'Reopen Issue' | |
else: | |
transition = '' | |
if transition != '': | |
try: | |
jira.transition_issue(issue, transition) | |
result += 'Workflow Transition: ' + transition + '\n' | |
except Exception as error: | |
transitions = jira.transitions(issue) | |
result += 'Error: Transition: "' + transition + '" failed with: "' + str(error) + '" Valid transitions=' + str(transitions) + '\n' | |
if issue.fields.customfield_10010 is None: | |
issue.update(fields={'customfield_10010': pull_url}) | |
result += 'Updated PR\n' | |
elif issue.fields.customfield_10010 is not None and issue.fields.customfield_10010 != pull_url: | |
result += 'Additional PR: ' + pull_url + '\n' | |
if issue.fields.assignee is None: | |
jira.assign_issue(issue, user) | |
result += 'Assigning user: ' + user + '\n' | |
elif issue.fields.assignee is not None and issue.fields.assignee.name.lower() != user.lower(): | |
result += 'Changing assignee from: ' + issue.fields.assignee.name + ' to: ' + user + '\n' | |
jira.assign_issue(issue, user) | |
return result | |
jirabot_user = os.environ['JIRABOT_USERNAME'] | |
jirabot_pass = os.environ['JIRABOT_PASSWORD'] | |
jira_url = os.environ['JIRA_URL'] | |
pr = os.environ['PULL_REQUEST_NUMBER'] | |
title = os.environ['PULL_REQUEST_TITLE'] | |
user = os.environ['PULL_REQUEST_AUTHOR_NAME'] | |
pull_url = os.environ['PULL_URL'] | |
github_token = os.environ['GITHUB_TOKEN'] | |
comments_url = os.environ['COMMENTS_URL'] | |
print("%s %s %s" % (title, user, comments_url)) | |
result = '' | |
issuem = re.search("(HPCC4J|JAPI)-[0-9]+", title) | |
if issuem: | |
nameCorrectionPattern = re.compile("hpcc4j", re.IGNORECASE) | |
issue_name = nameCorrectionPattern.sub("JAPI",issuem.group()) | |
userDict = { | |
'kunalaswani': 'kunal.aswani', | |
'timothyklemm': 'klemti01', | |
'jpmcmu': 'mcmuja01', | |
'asselitx': 'terrenceasselin', | |
'jeclrsg': 'clemje01', | |
'jackdelv': 'delvecja', | |
} | |
user = userDict.get(user, user) | |
options = { | |
'server': jira_url | |
} | |
jira = JIRA(options=options, basic_auth=(jirabot_user, jirabot_pass)) | |
issue = jira.issue(issue_name) | |
result = 'Jirabot Action Result:\n' | |
result += updateIssue(jira, issue, user, pull_url) | |
jira.add_comment(issue, result) | |
else: | |
print('Unable to find Jira issue name in title') | |
print(result) | |
shell: python |