Skip to content

Commit

Permalink
HPCC4J-608 Merge fix version failure
Browse files Browse the repository at this point in the history
- Fixed jirabot merge add fix version code
- Updated transition logic to be less brittle

Signed-off-by: James McMullan [email protected]
  • Loading branch information
jpmcmu committed Jul 9, 2024
1 parent f83ba85 commit f277694
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 48 deletions.
29 changes: 9 additions & 20 deletions .github/workflows/Jirabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GHUB_JIRA_USER_MAP: ${{ vars.GHUB_JIRA_USER_MAP }}
JIRA_ISSUE_PROPERTY_MAP: ${{ vars.JIRA_ISSUE_PROPERTY_MAP }}
JIRA_ISSUE_TRANSITION_MAP: ${{ vars.JIRA_ISSUE_TRANSITION_MAP }}
run: |
import os
import re
Expand All @@ -56,24 +55,19 @@ jobs:
else:
return ''
def updateIssue(jira, issue, prAuthor : str, transitionMap: dict, propertyMap: dict, pull_url: str) -> str:
def updateIssue(jira, issue, prAuthor : str, propertyMap: dict, pull_url: str) -> str:
result = ''
issueName = issue['key']
issueFields = issue['fields']
statusName = str(issueFields['status']['name'])
transition = transitionMap.get(statusName, None)
if transition == None:
print('Error: Unable to find transition for status: ' + statusName)
elif transition != '':
try:
jira.issue_transition(issueName, transition)
result += 'Workflow Transition: ' + transition + '\n'
except Exception as error:
transitions = jira.get_issue_transitions(issueName)
result += 'Error: Transition: "' + transition + '" failed with: "' + str(error) + '" Valid transitions=' + str(transitions) + '\n'
try:
transitionId = jira.get_transition_id_to_status_name(issue_name, desiredStatus)
jira.set_issue_status_by_transition_id(issue_name, transitionId)
result += 'Workflow Transition To: ' + desiredStatus + '\n'
except Exception as error:
transitions = jira.get_issue_transitions(issueName)
result += 'Error: Transitioning to: "' + desiredStatus + '" failed with: "' + str(error) + '" Valid transitions=' + str(transitions) + '\n'
prFieldName = propertyMap.get('pullRequestFieldName', 'customfield_10010')
Expand Down Expand Up @@ -153,17 +147,12 @@ jobs:
result = 'Jirabot Action Result:\n'
transitionMap = json.loads(os.environ['JIRA_ISSUE_TRANSITION_MAP'])
if not isinstance(transitionMap, dict):
print('Error: JIRA_ISSUE_TRANSITION_MAP is not a valid JSON object, ignoring.')
transitionMap = {}
jiraIssuePropertyMap = json.loads(os.environ['JIRA_ISSUE_PROPERTY_MAP'])
if not isinstance(jiraIssuePropertyMap, dict):
print('Error: JIRA_ISSUE_PROPERTY_MAP is not a valid JSON object, ignoring.')
jiraIssuePropertyMap = {}
result += updateIssue(jira, issue, jiraUser, transitionMap, jiraIssuePropertyMap, pull_url)
result += updateIssue(jira, issue, jiraUser, jiraIssuePropertyMap, pull_url)
jira.issue_add_comment(issue_name, result)
result = 'Jira Issue: ' + jira_url + '/browse/' + issue_name + '\n\n' + result
Expand Down
69 changes: 41 additions & 28 deletions .github/workflows/JirabotMerge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ jobs:
COMMENTS_URL: ${{ github.event.pull_request.comments_url }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BRANCH_NAME: ${{ github.ref_name }}

run: |
import os
import re
Expand Down Expand Up @@ -95,9 +94,9 @@ jobs:
major, minor, point = map(int, version)
return f"{major}.{minor}.{point}"
def generateFixVersionList(jira, branchName):
latestVersion = getTagVersionForCmd("git tag --list 'hpcc4j_*-release' --sort=-v:refname | head -n 1")
def generateFixVersionList(jira, projectName, branchName):
cmd = "git tag --list 'hpcc4j_*-release' --sort=-v:refname | head -n 1"
latestVersion = getTagVersionForCmd(cmd)
# If we are merging into master we assume it is going into the next minor release
fixVersions = []
Expand All @@ -116,7 +115,8 @@ jobs:
curMajor = branchVersion[0]
latestMajor = latestVersion[0]
while curMajor <= latestMajor:
latestVersionInMajor = getTagVersionForCmd("git tag --list 'hpcc4j_" + str(curMajor) + "*-release' --sort=-v:refname | head -n 1")
cmd = "git tag --list 'hpcc4j_" + str(curMajor) + "*-release' --sort=-v:refname | head -n 1"
latestVersionInMajor = getTagVersionForCmd(cmd)
curMinor = 0
if curMajor == branchVersion[0]:
Expand All @@ -131,18 +131,24 @@ jobs:
curMajor += 1
for fixVersion in fixVersions:
alreadyHasFixVersion = False
versions = jira.get_project_versions('HPCC4J')
for v in versions:
if v['name'] == fixVersion:
alreadyHasFixVersion = True
if not alreadyHasFixVersion:
jira.add_version(fixVersion, 'HPCC4J', fixVersion)
try:
alreadyHasFixVersion = False
versions = jira.get_project_versions(projectName)
for v in versions:
if v['name'] == fixVersion:
alreadyHasFixVersion = True
if not alreadyHasFixVersion:
project = jira.get_project(projectName)
projectId = project['id']
jira.add_version(projectName, projectId, fixVersion)
catch Exception as error:
print('Error: Unable to add fix version: ' + fixVersion + ' with: ' + str(error))
sys.exit(1)
return fixVersions
def resolveIssue(jira, issue, fixVersions) -> str:
def resolveIssue(jira, projectName, issue, fixVersions) -> str:
result = ''
versionsToAdd = []
Expand All @@ -159,7 +165,7 @@ jobs:
if not alreadyHasFixVersion:
versionsToAdd.append(addedVersion)
versions = jira.get_project_versions('HPCC4J')
versions = jira.get_project_versions(projectName)
updatedVersionList = []
for v in issueFields['fixVersions']:
updatedVersionList.append({'id' : v['id']})
Expand All @@ -178,15 +184,21 @@ jobs:
result += "Error: Unable to find fix version: " + fixVersionName + "\n"
if len(versionsToAdd) > 0:
jira.update_issue_field(issueName, {'fixVersions': updatedVersionList})
try:
jira.update_issue_field(issueName, {'fixVersions': updatedVersionList})
except Exception as error:
result += 'Error: Updating fix versions failed with: "' + str(error) + '\n'
else:
result += "Fix versions already added.\n"
statusName = str(issueFields['status']['name'])
if statusName != 'Resolved':
transition = 'Accept Pull Request'
jira.issue_transition(issue_name, transition)
result += "Workflow Transition: 'Resolve issue'\n"
try:
transitionId = jira.get_transition_id_to_status_name(issueName, 'Resolved')
jira.set_issue_status_by_transition_id(issueName, transitionId)
result += "Workflow Transition: 'Resolve issue'\n"
except Exception as error:
result += 'Error: Transitioning to: "Resolved" failed with: "' + str(error) + '\n'
return result
Expand All @@ -201,16 +213,12 @@ jobs:
github_token = os.environ['GITHUB_TOKEN']
branch_name = os.environ['BRANCH_NAME']
comments_url = os.environ['COMMENTS_URL']
project_name = 'HPCC4J'
result = ''
issuem = re.search("(HPCC4J|JAPI)-[0-9]+", title)
issuem = re.search("(" + project_name + ")-[0-9]+", title, re.IGNORECASE)
if issuem:
nameCorrectionPattern = re.compile("hpcc4j", re.IGNORECASE)
issue_name = nameCorrectionPattern.sub("HPCC4J",issuem.group())
options = {
'server': jira_url
}
issue_name = issuem.group()
jira = Jira(url=jira_url, username=jirabot_user, password=jirabot_pass, cloud=True)
Expand All @@ -221,9 +229,14 @@ jobs:
result = 'Jirabot Action Result:\n'
fixVersions = generateFixVersionList(jira, branch_name)
result += resolveIssue(jira, issue, fixVersions)
fixVersions = generateFixVersionList(jira, project_name, branch_name)
result += resolveIssue(jira, project_name, issue, fixVersions)
jira.issue_add_comment(issue_name, result)
# Escape the result for JSON
result = json.dumps(result)
subprocess.run(['curl', '-X', 'POST', comments_url, '-H', 'Content-Type: application/json', '-H', f'Authorization: token {github_token}', '--data', f'{{ "body": {result} }}'], check=True)
else:
print('Unable to find Jira issue name in title')
Expand Down

0 comments on commit f277694

Please sign in to comment.