From 131b14ac460b2beec496e7412e4a3e147856a51b Mon Sep 17 00:00:00 2001 From: James McMullan Date: Fri, 7 Jun 2024 15:13:11 -0400 Subject: [PATCH] HPCC4J-608 Merge fix version failure - Fixed jirabot merge add fix version code - Updated transition logic to be less brittle Signed-off-by: James McMullan James.McMullan@lexisnexis.com --- .github/workflows/Jirabot.yml | 29 ++++++++----------------- .github/workflows/JirabotMerge.yml | 34 ++++++++++++++---------------- 2 files changed, 25 insertions(+), 38 deletions(-) diff --git a/.github/workflows/Jirabot.yml b/.github/workflows/Jirabot.yml index 3e779dcac..966add686 100644 --- a/.github/workflows/Jirabot.yml +++ b/.github/workflows/Jirabot.yml @@ -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 @@ -47,24 +46,19 @@ jobs: import json from atlassian.jira import Jira - 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') @@ -141,17 +135,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 diff --git a/.github/workflows/JirabotMerge.yml b/.github/workflows/JirabotMerge.yml index 27f4cb449..2bfd15ba0 100644 --- a/.github/workflows/JirabotMerge.yml +++ b/.github/workflows/JirabotMerge.yml @@ -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 @@ -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 = [] @@ -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]: @@ -132,17 +132,19 @@ jobs: for fixVersion in fixVersions: alreadyHasFixVersion = False - versions = jira.get_project_versions('HPCC4J') + versions = jira.get_project_versions(projectName) for v in versions: if v['name'] == fixVersion: alreadyHasFixVersion = True if not alreadyHasFixVersion: - jira.add_version(fixVersion, 'HPCC4J', fixVersion) + project = jira.get_project(projectName) + projectId = project['id'] + jira.add_version(projectName, projectId, fixVersion) return fixVersions - def resolveIssue(jira, issue, fixVersions) -> str: + def resolveIssue(jira, projectName, issue, fixVersions) -> str: result = '' versionsToAdd = [] @@ -159,7 +161,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']}) @@ -184,8 +186,8 @@ jobs: statusName = str(issueFields['status']['name']) if statusName != 'Resolved': - transition = 'Accept Pull Request' - jira.issue_transition(issue_name, transition) + transitionId = jira.get_transition_id_to_status_name(issue_name, 'Resolved') + jira.set_issue_status_by_transition_id(issue_name, transitionId) result += "Workflow Transition: 'Resolve issue'\n" return result @@ -201,17 +203,13 @@ jobs: github_token = os.environ['GITHUB_TOKEN'] branch_name = os.environ['BRANCH_NAME'] comments_url = os.environ['COMMENTS_URL'] + project_name = 'HPCC4J' print("Attempting to close out Jira issue: %s %s %s" % (title, user, comments_url)) result = '' - issuem = re.search("(HPCC4J|JAPI)-[0-9]+", title) + issuem = re.search("(" + project_name + ")-[0-9]+", title) 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)