From 678afb5a4e7956d21e5517f609408da52e4e94f0 Mon Sep 17 00:00:00 2001 From: Rodrigo Date: Mon, 24 Jul 2023 12:07:25 -0400 Subject: [PATCH] Create Jirabot.yml Signed-off-by: Rodrigo --- .github/workflows/Jirabot.yml | 101 ++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 .github/workflows/Jirabot.yml diff --git a/.github/workflows/Jirabot.yml b/.github/workflows/Jirabot.yml new file mode 100644 index 000000000..948bccbac --- /dev/null +++ b/.github/workflows/Jirabot.yml @@ -0,0 +1,101 @@ +# 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 + - 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 + from jira.client import JIRA + + 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'] + comments_url = os.environ['COMMENTS_URL'] + pull_url = os.environ['PULL_URL'] + github_token = os.environ['GITHUB_TOKEN'] + + print("%s %s %s" % (title, user, comments_url)) + status = '' + issuem = re.search("(HPCC4J|JAPI)-[0-9]+", title) + if issuem: + issue_name = issuem.group() + if user == 'kunalaswani': + user = 'kunal.aswani' + if user == 'timothyklemm': + user = 'klemti01' + if user == 'jpmcmu': + user = 'mcmuja01' + if user == 'asselitx': + user = 'terrenceasselin' + if user == 'jeclrsg': + user = 'clemje01' + if user == 'jackdelv': + user = 'delvecja' + options = { + 'server': jira_url + } + jira = JIRA(options=options, basic_auth=(jirabot_user, jirabot_pass)) + issue = jira.issue(issue_name) + status = jira_url + '/browse/' + issue_name + '\\n' + if False and issue.fields.status.name != 'Active' and issue.fields.status.name != 'Open' and issue.fields.status.name != 'New' and issue.fields.status.name != 'Discussing' and issue.fields.status.name != 'Awaiting Information': + status += 'Jira not updated (state was not active or new)' + elif issue.fields.customfield_10010 != None: + if issue.fields.customfield_10010 != pull_url: + status += 'Jira not updated (pull request "%s" already registered)' % issue.fields.customfield_10010 + else: + status += 'This pull request is already registered' + elif issue.fields.assignee is not None and issue.fields.assignee.name.lower() != user.lower(): + status += 'Jira not updated (user does not match)' + else: + if issue.fields.assignee is None: + jira.assign_issue(issue, user) + issue.update(fields={'customfield_10010': pull_url}) + issue = jira.issue(issue_name) + try: + transitions = jira.transitions(issue) + jira.transition_issue(issue, '291') # Attach Pull Request + except: + status += 'Failed to set to merge pending: transitions=%s' % transitions + status += 'Jira updated' + print('curl -X POST %s -H "Content-Type: application/json" -H "Authorization: token %s" --data \'{ "body": "%s" }\'' % ( comments_url, github_token, status )) + os.system('curl -X POST %s -H "Content-Type: application/json" -H "Authorization: token %s" --data \'{ "body": "%s" }\'' % ( comments_url, github_token, status )) + + print(status) + shell: python