diff --git a/.github/workflows/comment_added.yml b/.github/workflows/comment_added.yml index c8c2854149fbe..b0a4f219d3a86 100644 --- a/.github/workflows/comment_added.yml +++ b/.github/workflows/comment_added.yml @@ -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: @@ -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 diff --git a/.github/workflows/jira_issue_manage.yml b/.github/workflows/jira_issue_manage.yml index a5787c7abef76..74ab5df12cf40 100644 --- a/.github/workflows/jira_issue_manage.yml +++ b/.github/workflows/jira_issue_manage.yml @@ -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 @@ -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 diff --git a/.github/workflows/scripts/jira_helper.py b/.github/workflows/scripts/jira_helper.py index 9c4636c57deec..e3bbd948c2499 100644 --- a/.github/workflows/scripts/jira_helper.py +++ b/.github/workflows/scripts/jira_helper.py @@ -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): @@ -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: @@ -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'] @@ -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): @@ -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}" @@ -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: @@ -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()