-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HPCC4J-532 Jirabot python script fixes
- Added new jirabot to resolve tickets on merge Signed-off-by: James McMullan [email protected]
- Loading branch information
Showing
1 changed file
with
222 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
name: Jirabot - Merge | ||
|
||
on: | ||
push: | ||
branches: | ||
- "master" | ||
- "candidate-*" | ||
|
||
jobs: | ||
jirabot: | ||
runs-on: ubuntu-latest | ||
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 | ||
- name: "Run" | ||
env: | ||
JIRABOT_USERNAME : ${{ secrets.JIRABOT_USERNAME }} | ||
JIRABOT_PASSWORD : ${{ secrets.JIRABOT_PASSWORD }} | ||
JIRA_URL : ${{ secrets.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 | ||
import subprocess | ||
import time | ||
from jira.client import JIRA | ||
def compareVersions(verA, verB) -> int: | ||
aParts = verA.split('.') | ||
bParts = verB.split('.') | ||
if len(aParts) != 3 or len(bParts) != 3: | ||
print('Invalid version comparison: ' + verA + ' <=> ' + verB) | ||
sys.exit(1) | ||
if aParts[2].lower() == 'x': | ||
aParts[2] = 10000 | ||
if bParts[2].lower() == 'x': | ||
bParts[2] = 10000 | ||
aMajor, aMinor, aPoint = map(int, aParts) | ||
bMajor, bMinor, bPoint = map(int, bParts) | ||
if aMajor < bMajor: | ||
return -1 | ||
elif aMajor > bMajor: | ||
return 1 | ||
if aMinor < bMinor: | ||
return -1 | ||
elif aMinor > bMinor: | ||
return 1 | ||
if aPoint < bPoint: | ||
return -1 | ||
elif aPoint > bPoint: | ||
return 1 | ||
return 0 | ||
def findLatestMinorForMajor(majorVer, versions) -> str: | ||
latestMinor = 0 | ||
latestPoint = 0 | ||
latestVer = None | ||
for v in versions: | ||
parts = v.name.split('.') | ||
if len(parts) != 3: | ||
continue | ||
if int(parts[0]) != majorVer: | ||
continue | ||
if int(parts[1]) >= latestMinor and (parts[2].lower() == 'x' or int(parts[2]) > latestPoint): | ||
latestVer = v.name | ||
latestMinor = int(parts[1]) | ||
if parts[2].lower() != 'x': | ||
latestPoint = int(parts[2]) | ||
return latestVer | ||
def incrementPointVersion(version) -> str: | ||
parts = version.split('.') | ||
if len(parts) != 3: | ||
print('Invalid version: ' + version) | ||
sys.exit(1) | ||
major, minor, point = map(int, parts) | ||
incPoint = point + 2 | ||
return f"{major}.{minor}.{incPoint}" | ||
def generateFixVersionList(jira): | ||
gitTagProcess = subprocess.Popen('git describe --tags `git rev-list --tags --max-count=1`', stdout=subprocess.PIPE, shell=True) | ||
(output, err) = gitTagProcess.communicate() | ||
gitTagProcessStatus = gitTagProcess.wait() | ||
if gitTagProcessStatus != 0: | ||
print('Unable to retrieve latest git tag.') | ||
sys.exit(1) | ||
latestGitTag = str(output) | ||
tagVersionIsRelease = "-release" in latestGitTag | ||
versionPattern = re.compile(r".*([0-9]+\.[0-9]+\.[0-9]+).*") | ||
versionMatch = versionPattern.match(latestGitTag) | ||
if versionMatch: | ||
tagVersion = versionMatch.group(1) | ||
else: | ||
print('Unable to extract version from git tag.') | ||
sys.exit(2) | ||
fixVersion = tagVersion | ||
if tagVersionIsRelease: | ||
fixVersion = incrementPointVersion(fixVersion) | ||
alreadyHasFixVersion = False | ||
versions = jira.project_versions('JAPI') | ||
for v in versions: | ||
if v.name == fixVersion: | ||
alreadyHasFixVersion = True | ||
if not alreadyHasFixVersion: | ||
jira.create_version(name=fixVersion, project='JAPI', description=fixVersion) | ||
parts = fixVersion.split('.') | ||
majorVer = int(parts[0]) | ||
fixVersions = [fixVersion] | ||
latestMinor = findLatestMinorForMajor(majorVer, versions) | ||
if latestMinor != None and compareVersions(fixVersion, latestMinor) < 0: | ||
fixVersions.append(latestMinor) | ||
return fixVersions | ||
def resolveIssue(jira, issue, fixVersions) -> str: | ||
result = '' | ||
versionsToAdd = [] | ||
for addedVersion in fixVersions: | ||
alreadyHasFixVersion = False | ||
for v in issue.fields.fixVersions: | ||
if v.name == addedVersion: | ||
alreadyHasFixVersion = True | ||
break | ||
if not alreadyHasFixVersion: | ||
versionsToAdd.append(addedVersion) | ||
versions = jira.project_versions('JAPI') | ||
updatedVersionList = [] | ||
for v in issue.fields.fixVersions: | ||
updatedVersionList.append({'id' : v.id}) | ||
for fixVersionName in versionsToAdd: | ||
fixVersion = None | ||
for v in versions: | ||
if v.name == fixVersionName: | ||
fixVersion = v | ||
break | ||
if fixVersion: | ||
updatedVersionList.append({'id' : fixVersion.id}) | ||
result += "Added fix version: " + fixVersionName + "\n" | ||
else: | ||
result += "Error: Unable to find fix version: " + fixVersionName + "\n" | ||
if len(versionsToAdd) > 0: | ||
issue.update(fields={'fixVersions': updatedVersionList}) | ||
else: | ||
result += "Fix versions already added.\n" | ||
statusName = str(issue.fields.status) | ||
if statusName != 'Resolved': | ||
transition = 'Resolve Issue' | ||
jira.transition_issue(issue, transition) | ||
result += "Workflow Transition: 'Resolve issue'\n" | ||
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'] | ||
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()) | ||
jira = JIRA(options=options, basic_auth=(jirabot_user, jirabot_pass)) | ||
issue = jira.issue(issue_name) | ||
if issue is None: | ||
print('Unable to find issue with name: ' + issue_name) | ||
sys.exit(1) | ||
result = 'Jirabot Action Result:\n' | ||
fixVersions = generateFixVersionList(jira) | ||
result += resolveIssue(jira, issue, fixVersions) | ||
jira.add_comment(issue, result) | ||
else: | ||
print('Unable to find Jira issue name in title') | ||
print(result) | ||
shell: python |