Skip to content

Commit

Permalink
gh: Now using pandoc to format issues
Browse files Browse the repository at this point in the history
GH markdown is very different than Jira's.  Luckily pandoc exists
and can be used to reformat issue and comment body text.

Signed-off-by: Michael Boquard <michael@redpanda.com>
  • Loading branch information
michael-redpanda committed May 15, 2024
1 parent 209f78d commit 674472a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 24 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/comment_added.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
env:
SCRIPT_DIR: ${{ github.workspace }}/.github/workflows/scripts
run: pip install -r ${SCRIPT_DIR}/requirements.txt
- name: install pandoc
run: sudo apt-get update && sudo apt-get install -y pandoc
- name: Add comment to JIRA Issue
if: ${{ !github.event.issue.pull_request }}
env:
Expand All @@ -27,4 +29,4 @@ jobs:
ISSUE_URL: ${{ github.event.issue.html_url }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ISSUE_COMMENT: ${{ github.event.comment.body }}
run: python ${SCRIPT_DIR}/jira_helper.py UPDATE_COMMENT --verbose
run: python ${SCRIPT_DIR}/jira_helper.py UPDATE_COMMENT --verbose -p CORE
4 changes: 3 additions & 1 deletion .github/workflows/jira_issue_manage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ jobs:
env:
SCRIPT_DIR: ${{ github.workspace }}/.github/workflows/scripts
run: pip install -r ${SCRIPT_DIR}/requirements.txt
- name: install pandoc
run: sudo apt-get update && sudo apt-get install -y pandoc
- name: Manage JIRA Issue
env:
SCRIPT_DIR: ${{ github.workspace }}/.github/workflows/scripts
Expand All @@ -36,4 +38,4 @@ jobs:
ISSUE_LABELS: ${{ join(github.event.issue.labels.*.name) }}
ISSUE_STATE: ${{ github.event.issue.state }}
EVENT_NAME: ${{ github.event.action }}
run: python ${SCRIPT_DIR}/jira_helper.py ISSUE --verbose
run: python ${SCRIPT_DIR}/jira_helper.py ISSUE --verbose -p CORE
57 changes: 35 additions & 22 deletions .github/workflows/scripts/jira_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import json
import logging
import os
import requests
from requests.auth import HTTPBasicAuth
import shutil
import subprocess
import sys
import tempfile
from typing import Tuple
from typing import Optional, Tuple


class NoJiraIssueFound(Exception):
Expand All @@ -37,25 +36,22 @@ def get_issue_state(state: str) -> IssueState:

class JiraHelper():
_base_url = 'https://redpandadata.atlassian.net'
_project_key = 'CORE'
_done_status_name = 'DONE'
_backlog_status_name = 'BACKLOG'
_bug_labels = ['kind/bug', 'ci-failure']

def __init__(self, command: Command, logger: logging.Logger):
def __init__(self, command: Command, logger: logging.Logger, project: str,
pandoc: Optional[str]):
self.logger: logging.Logger = logger
self.command: Command = command
self._pandoc = pandoc
self._project_key = project
self._check_env()
self._jira = Jira(url=self._base_url,
username=os.environ['JIRA_USER'],
password=os.environ['JIRA_TOKEN'],
cloud=True)

@staticmethod
def _get_auth() -> HTTPBasicAuth:
return HTTPBasicAuth(username=os.environ['JIRA_USER'],
password=os.environ['JIRA_TOKEN'])

def _check_env_val(self, val: str):
self.logger.debug(f'Checking environment for {val}')
try:
Expand Down Expand Up @@ -84,7 +80,20 @@ def _check_env(self):

def _get_gh_issue_comments(self, url):
return json.loads(
self._get_gh_output(f'gh issue view {url} --json comments'))
self._run_cmd_return_stdout(
f'gh issue view {url} --json comments'))

def _ghm_to_jira(self, ghm: str) -> str:
if self._pandoc is not None:
with tempfile.NamedTemporaryFile(delete=False) as tf:
tf.write(ghm.encode())
tf.flush()
tf.close()
jmd = self._run_cmd_return_stdout(
f'{self._pandoc} -f gfm -w jira {tf.name}')
os.unlink(tf.name)
return jmd
return ghm

def _issue_helper(self):
event_name = os.environ['EVENT_NAME']
Expand Down Expand Up @@ -126,7 +135,7 @@ def execute(self):
raise NotImplementedError(
f'Command {self.command} not yet implemented')

def _get_gh_output(self, command) -> str:
def _run_cmd_return_stdout(self, command) -> str:
return subprocess.check_output(command.split(' ')).decode()

def _transition_issue(self, issue_id, status_name):
Expand Down Expand Up @@ -193,15 +202,11 @@ def _create_issue(self):
labels = self._get_gh_issue_labels()
issue_type = self._get_issue_type(labels)

# Replace "```" with "{code}" because Jira's format is different than
# GitHubs
jira_issue_body = issue_body.replace("```", "{code}")
issue_body = self._ghm_to_jira(issue_body)

# When creating a bug, must use customfield_10083 as 'description' is not part of
# bug creation screen anymore
fields = {
"description" if issue_type == 'Task' else "customfield_10083":
f"{jira_issue_body}",
f"{issue_body}",
"summary": f"{issue_title}",
"issuetype": {
"name": f"{issue_type}"
Expand Down Expand Up @@ -243,7 +248,7 @@ def _create_issue(self):
return issue_id

def _add_comment_to_issue(self, issue_id, comment):
comment = comment.replace("```", "{code}")
comment = self._ghm_to_jira(comment)
self.logger.debug(
f'Updating issue {issue_id} with comment "{comment}"')
try:
Expand Down Expand Up @@ -318,16 +323,24 @@ def parse_args() -> Tuple[Command, bool]:
'--verbose',
action='store_true',
help='Increase verbosity')
parser.add_argument('-p',
'--project',
required=True,
help="The project to use")
args = parser.parse_args()
return (Command[args.command], args.verbose)
return (Command[args.command], args.verbose, args.project)


def find_program(prog) -> Optional[str]:
return shutil.which(prog)


def main():
logger = logging.getLogger(__name__)
(command, verbose) = parse_args()
(command, verbose, project) = parse_args()
logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO)
logger.info(f'Executing command {command}')
helper = JiraHelper(command, logger)
helper = JiraHelper(command, logger, project, find_program('pandoc'))
helper.execute()


Expand Down

0 comments on commit 674472a

Please sign in to comment.