From 156c4a764872cb35602045bd6989b672514b192b Mon Sep 17 00:00:00 2001 From: Dmytro Trotsko Date: Thu, 30 Nov 2023 20:37:07 +0200 Subject: [PATCH 1/2] OKRF23-54 Adde GitHub action to sync labeled issue to Jira board --- .../actions/sync-issue-with-jira/action.yaml | 120 ++++++++++++++++++ .github/workflows/sync-issue-with-jira.yml | 12 ++ 2 files changed, 132 insertions(+) create mode 100644 .github/actions/sync-issue-with-jira/action.yaml create mode 100644 .github/workflows/sync-issue-with-jira.yml diff --git a/.github/actions/sync-issue-with-jira/action.yaml b/.github/actions/sync-issue-with-jira/action.yaml new file mode 100644 index 000000000..924b8a701 --- /dev/null +++ b/.github/actions/sync-issue-with-jira/action.yaml @@ -0,0 +1,120 @@ +name: "Sync GitHub issue to Jira" +description: 'This GitHub action creates Jira issue from GitHub issue when GitHub issue is labeled with "jira" label.' +inputs: + webhook-url: + description: > + Jira integration webhook URL. + Store it as a secret as anyone who has access to it will be able to post to your Jira board. + required: true + label: + description: "Label which will trigger a Jira import." + required: true + default: "jira" + +runs: + using: "composite" + steps: + - name: restrict action to labelled issues + run: | + set -eux + + echo "NeedsJiraUpdate=false" >> $GITHUB_ENV + + if [ ${{ github.event_name }} != "issues" ]; then + echo "This action only work on issue events. Please use on: issues to use this action." + exit 1 + fi + + if [ ${{ github.event.issue.pull_request }} ]; then + echo "This action only work on issues, not pull requests." + exit 0 + fi + + # Issue creation with label will trigger 2 events and run twice: one create, one labelled. + # let just focus on labelling then for creating issues Jira-side. + if [ ${{ github.event_name }} == "issues" ] && [ ${{ github.event.action }} == "opened" ]; then + echo "Ignoring creation of issues as a label will trigger a second event." + exit 0 + fi + + # We only operate on labelled issues or issues that are just unlabeled with our desired label + ## check if one label of labels is our jira label + toconsider=${{ contains(github.event.issue.labels.*.name, inputs.label) }} + ## second chance, this has just been unlabeled and needs to be deleted on Jira + if [ ${{ github.event.action }} == "unlabeled" ] && [ ${{ github.event.label.name }} == ${{ inputs.label }} ]; then + toconsider=true + fi + if [ "${toconsider}" == false ]; then + echo "Our desired label not found on issue or not unlabeled, skipping" + exit 0 + fi + + # And finally, for the "labeled" event, we are only interested if the new added label is our desired one. + if [ ${{ github.event.action }} == "labeled" ] && [ ${{ github.event.label.name }} != ${{ inputs.label }} ]; then + echo "Not interested in this action, skipping" + exit 0 + fi + + # last one wins + echo "NeedsJiraUpdate=true" >> $GITHUB_ENV + shell: bash + + - name: "Update jira" + if: ${{ env.NeedsJiraUpdate == 'true' }} + env: + # ID is the html url to keep a link between systems as there is no way to force an ID on Jira side. + id: ${{ github.event.issue.html_url }} + title: ${{ github.event.issue.title }} + body: ${{ github.event.issue.body }} + author: ${{ github.event.issue.user.login }} + run: | + set -eux + + # Convert markdown to JIRA using mistletoe package which is available starting with impish. + # Since GH runners only have LTS versions it's safe to only check for focal which doesn't have the package. + if [ $(lsb_release -c -s) == "focal" ]; then + echo "Converting Markdown to JIRA is only possible starting with Ubuntu 22.04 (jammy). Pushing verbatim content to JIRA..." + else + TMPDIR=$(mktemp -d) + trap 'rm -rf -- "$TMPDIR"' EXIT + + sudo apt install -y python3-mistletoe + echo ${body} > $TMPDIR/body.md + body=$(PYTHONPATH=/usr/share/doc/python3-mistletoe mistletoe -r examples.jira_renderer.JIRARenderer $TMPDIR/body.md) + fi + + description="${body} + + + GitHub URL: ${id}. + + Opened by ${author}." + + # Choose Jira action based on event type and action. + action="" + if [ ${{ github.event_name }} == "issues" ]; then + action=Update + if [ ${{ github.event.action }} == "labeled" ]; then + action=Create + elif [ ${{ github.event.action }} == "reopened" ]; then + action=Reopen + elif [ ${{ github.event.action }} == "deleted" ] || [ ${{ github.event.action }} == "unlabeled" ]; then + # Note: deleting issue from GH is not supported ATM as there is no more label attached. unlabeled is supported. + action=Delete + elif [ ${{ github.event.action }} == "closed" ]; then + action=Close + fi + fi + + echo "PUSHING: $id $action $title $description" + + # Push to Jira as a json data format. + data=$(jq -n \ + --arg id "$id" \ + --arg action "$action" \ + --arg title "$title" \ + --arg description "$description" \ + '{data: {id: $id, action: $action, title: $title, description: $description}}') + curl -X POST -H 'Content-type: application/json' --data "${data}" '${{ inputs.webhook-url }}' + + shell: bash diff --git a/.github/workflows/sync-issue-with-jira.yml b/.github/workflows/sync-issue-with-jira.yml new file mode 100644 index 000000000..28ad8a2e1 --- /dev/null +++ b/.github/workflows/sync-issue-with-jira.yml @@ -0,0 +1,12 @@ +name: Sync GitHub issues to Jira +on: [issues] + +jobs: + sync-issues: + name: Sync issues to Jira + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: ./.github/actions/sync_with_jira/ + with: + webhook-url: ${{ secrets.JIRA_WEBHOOK }} \ No newline at end of file From 193a8389f534a4d3bdabea3b42b597d48e9f3fd5 Mon Sep 17 00:00:00 2001 From: Dmytro Trotsko Date: Wed, 6 Dec 2023 03:27:17 +0200 Subject: [PATCH 2/2] OKRF23-54 Updated sync-issue-to-jira action. --- .../actions/sync-issue-with-jira/action.yaml | 120 ------------------ .github/workflows/sync-issue-to-jira.yml | 83 ++++++++++++ .github/workflows/sync-issue-with-jira.yml | 12 -- 3 files changed, 83 insertions(+), 132 deletions(-) delete mode 100644 .github/actions/sync-issue-with-jira/action.yaml create mode 100644 .github/workflows/sync-issue-to-jira.yml delete mode 100644 .github/workflows/sync-issue-with-jira.yml diff --git a/.github/actions/sync-issue-with-jira/action.yaml b/.github/actions/sync-issue-with-jira/action.yaml deleted file mode 100644 index 924b8a701..000000000 --- a/.github/actions/sync-issue-with-jira/action.yaml +++ /dev/null @@ -1,120 +0,0 @@ -name: "Sync GitHub issue to Jira" -description: 'This GitHub action creates Jira issue from GitHub issue when GitHub issue is labeled with "jira" label.' -inputs: - webhook-url: - description: > - Jira integration webhook URL. - Store it as a secret as anyone who has access to it will be able to post to your Jira board. - required: true - label: - description: "Label which will trigger a Jira import." - required: true - default: "jira" - -runs: - using: "composite" - steps: - - name: restrict action to labelled issues - run: | - set -eux - - echo "NeedsJiraUpdate=false" >> $GITHUB_ENV - - if [ ${{ github.event_name }} != "issues" ]; then - echo "This action only work on issue events. Please use on: issues to use this action." - exit 1 - fi - - if [ ${{ github.event.issue.pull_request }} ]; then - echo "This action only work on issues, not pull requests." - exit 0 - fi - - # Issue creation with label will trigger 2 events and run twice: one create, one labelled. - # let just focus on labelling then for creating issues Jira-side. - if [ ${{ github.event_name }} == "issues" ] && [ ${{ github.event.action }} == "opened" ]; then - echo "Ignoring creation of issues as a label will trigger a second event." - exit 0 - fi - - # We only operate on labelled issues or issues that are just unlabeled with our desired label - ## check if one label of labels is our jira label - toconsider=${{ contains(github.event.issue.labels.*.name, inputs.label) }} - ## second chance, this has just been unlabeled and needs to be deleted on Jira - if [ ${{ github.event.action }} == "unlabeled" ] && [ ${{ github.event.label.name }} == ${{ inputs.label }} ]; then - toconsider=true - fi - if [ "${toconsider}" == false ]; then - echo "Our desired label not found on issue or not unlabeled, skipping" - exit 0 - fi - - # And finally, for the "labeled" event, we are only interested if the new added label is our desired one. - if [ ${{ github.event.action }} == "labeled" ] && [ ${{ github.event.label.name }} != ${{ inputs.label }} ]; then - echo "Not interested in this action, skipping" - exit 0 - fi - - # last one wins - echo "NeedsJiraUpdate=true" >> $GITHUB_ENV - shell: bash - - - name: "Update jira" - if: ${{ env.NeedsJiraUpdate == 'true' }} - env: - # ID is the html url to keep a link between systems as there is no way to force an ID on Jira side. - id: ${{ github.event.issue.html_url }} - title: ${{ github.event.issue.title }} - body: ${{ github.event.issue.body }} - author: ${{ github.event.issue.user.login }} - run: | - set -eux - - # Convert markdown to JIRA using mistletoe package which is available starting with impish. - # Since GH runners only have LTS versions it's safe to only check for focal which doesn't have the package. - if [ $(lsb_release -c -s) == "focal" ]; then - echo "Converting Markdown to JIRA is only possible starting with Ubuntu 22.04 (jammy). Pushing verbatim content to JIRA..." - else - TMPDIR=$(mktemp -d) - trap 'rm -rf -- "$TMPDIR"' EXIT - - sudo apt install -y python3-mistletoe - echo ${body} > $TMPDIR/body.md - body=$(PYTHONPATH=/usr/share/doc/python3-mistletoe mistletoe -r examples.jira_renderer.JIRARenderer $TMPDIR/body.md) - fi - - description="${body} - - - GitHub URL: ${id}. - - Opened by ${author}." - - # Choose Jira action based on event type and action. - action="" - if [ ${{ github.event_name }} == "issues" ]; then - action=Update - if [ ${{ github.event.action }} == "labeled" ]; then - action=Create - elif [ ${{ github.event.action }} == "reopened" ]; then - action=Reopen - elif [ ${{ github.event.action }} == "deleted" ] || [ ${{ github.event.action }} == "unlabeled" ]; then - # Note: deleting issue from GH is not supported ATM as there is no more label attached. unlabeled is supported. - action=Delete - elif [ ${{ github.event.action }} == "closed" ]; then - action=Close - fi - fi - - echo "PUSHING: $id $action $title $description" - - # Push to Jira as a json data format. - data=$(jq -n \ - --arg id "$id" \ - --arg action "$action" \ - --arg title "$title" \ - --arg description "$description" \ - '{data: {id: $id, action: $action, title: $title, description: $description}}') - curl -X POST -H 'Content-type: application/json' --data "${data}" '${{ inputs.webhook-url }}' - - shell: bash diff --git a/.github/workflows/sync-issue-to-jira.yml b/.github/workflows/sync-issue-to-jira.yml new file mode 100644 index 000000000..f311da905 --- /dev/null +++ b/.github/workflows/sync-issue-to-jira.yml @@ -0,0 +1,83 @@ +name: Sync GitHub issues to Jira +on: [issues] + +jobs: + sync-issues: + name: Sync issues to Jira + runs-on: ubuntu-latest + steps: + - name: Get the repository name + id: repo_name + run: echo "REPO_NAME=$(echo ${{ github.repository }} | cut -d '/' -f 2)" >> $GITHUB_OUTPUT + - name: Sync issues to Jira + env: + issue_id: ${{ github.event.issue.html_url }} + issue_title: ${{ github.event.issue.title }} + issue_body: ${{ github.event.issue.body }} + issue_author: ${{ github.event.issue.user.login }} + run: | + # Add your action code here + set -eux + + if [ ${{ github.event_name }} != "issues" ]; then + echo "This action only supports the issues event" + exit 1 + fi + if [ ${{ github.event.issue.pull_request }} ]; then + echo "This action does not support pull requests" + exit 0 + fi + # Issue creation with label will trigger 2 events and run twice: one create, one labelled. + # let just focus on labelling then for creating issues Jira-side. + if [ ${{ github.event_name }} == "issues" ] && [ ${{ github.event.action }} == "opened" ]; then + echo "Ignoring creation of issues as a label will trigger a second event." + exit 0 + fi + # We only operate on labelled issues or issues that are just unlabeled with our desired label which is 'jira' + ## check if one label of labels is our jira label + toconsider=${{ contains(github.event.issue.labels.*.name, 'jira') }} + if [ "${toconsider}" == false ]; then + echo "Our desired label not found on issue or not unlabeled, skipping" + exit 0 + fi + # And finally, for the "labeled" event, we are only interested if the new added label is our desired one. + if [ ${{ github.event.action }} == "labeled" ] && [ ${{ github.event.label.name }} != 'jira' ]; then + echo "Not interested in this action, skipping" + exit 0 + fi + + # Convert markdown to JIRA using mistletoe package which is available starting with impish. + # Since GH runners only have LTS versions it's safe to only check for focal which doesn't have the package. + if [ $(lsb_release -c -s) == "focal" ]; then + echo "Converting Markdown to JIRA is only possible starting with Ubuntu 22.04 (jammy). Pushing verbatim content to JIRA..." + else + TMPDIR=$(mktemp -d) + trap 'rm -rf -- "$TMPDIR"' EXIT + + sudo apt install -y python3-mistletoe + echo ${issue_body} > $TMPDIR/body.md + issue_body=$(PYTHONPATH=/usr/share/doc/python3-mistletoe mistletoe -r examples.jira_renderer.JIRARenderer $TMPDIR/body.md) + fi + + issue_description="*Issue created by ${issue_author}}* + + Description: ${issue_body} + + GitHub URL: ${issue_id}" + # Choose Jira action based on event type and action + action="" + if [ ${{ github.event.action }} == "labeled" ]; then + action=Create + fi + + echo "PUSHING: $issue_id $action $issue_title $issue_description" + # Push to Jira as a json data format + data=$(jq -n \ + --arg id "$issue_id" \ + --arg title "$issue_title" \ + --arg description "$issue_description" \ + --arg action "$action" \ + --arg project "${{ steps.repo_name.outputs.REPO_NAME }}" \ + '{data: {id: $id, title: $title, description: $description, action: $action, project: $project}}') + curl -X POST -H "Content-Type: application/json" -d "${data}" '${{ secrets.JIRA_WEBHOOK }}' + shell: bash diff --git a/.github/workflows/sync-issue-with-jira.yml b/.github/workflows/sync-issue-with-jira.yml deleted file mode 100644 index 28ad8a2e1..000000000 --- a/.github/workflows/sync-issue-with-jira.yml +++ /dev/null @@ -1,12 +0,0 @@ -name: Sync GitHub issues to Jira -on: [issues] - -jobs: - sync-issues: - name: Sync issues to Jira - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: ./.github/actions/sync_with_jira/ - with: - webhook-url: ${{ secrets.JIRA_WEBHOOK }} \ No newline at end of file