|
| 1 | +name: "Sync GitHub issue to Jira" |
| 2 | +description: 'This GitHub action creates Jira issue from GitHub issue when GitHub issue is labeled with "jira" label.' |
| 3 | +inputs: |
| 4 | + webhook-url: |
| 5 | + description: > |
| 6 | + Jira integration webhook URL. |
| 7 | + Store it as a secret as anyone who has access to it will be able to post to your Jira board. |
| 8 | + required: true |
| 9 | + label: |
| 10 | + description: "Label which will trigger a Jira import." |
| 11 | + required: true |
| 12 | + default: "jira" |
| 13 | + |
| 14 | +runs: |
| 15 | + using: "composite" |
| 16 | + steps: |
| 17 | + - name: restrict action to labelled issues |
| 18 | + run: | |
| 19 | + set -eux |
| 20 | +
|
| 21 | + echo "NeedsJiraUpdate=false" >> $GITHUB_ENV |
| 22 | +
|
| 23 | + if [ ${{ github.event_name }} != "issues" ]; then |
| 24 | + echo "This action only work on issue events. Please use on: issues to use this action." |
| 25 | + exit 1 |
| 26 | + fi |
| 27 | +
|
| 28 | + if [ ${{ github.event.issue.pull_request }} ]; then |
| 29 | + echo "This action only work on issues, not pull requests." |
| 30 | + exit 0 |
| 31 | + fi |
| 32 | +
|
| 33 | + # Issue creation with label will trigger 2 events and run twice: one create, one labelled. |
| 34 | + # let just focus on labelling then for creating issues Jira-side. |
| 35 | + if [ ${{ github.event_name }} == "issues" ] && [ ${{ github.event.action }} == "opened" ]; then |
| 36 | + echo "Ignoring creation of issues as a label will trigger a second event." |
| 37 | + exit 0 |
| 38 | + fi |
| 39 | +
|
| 40 | + # We only operate on labelled issues or issues that are just unlabeled with our desired label |
| 41 | + ## check if one label of labels is our jira label |
| 42 | + toconsider=${{ contains(github.event.issue.labels.*.name, inputs.label) }} |
| 43 | + ## second chance, this has just been unlabeled and needs to be deleted on Jira |
| 44 | + if [ ${{ github.event.action }} == "unlabeled" ] && [ ${{ github.event.label.name }} == ${{ inputs.label }} ]; then |
| 45 | + toconsider=true |
| 46 | + fi |
| 47 | + if [ "${toconsider}" == false ]; then |
| 48 | + echo "Our desired label not found on issue or not unlabeled, skipping" |
| 49 | + exit 0 |
| 50 | + fi |
| 51 | +
|
| 52 | + # And finally, for the "labeled" event, we are only interested if the new added label is our desired one. |
| 53 | + if [ ${{ github.event.action }} == "labeled" ] && [ ${{ github.event.label.name }} != ${{ inputs.label }} ]; then |
| 54 | + echo "Not interested in this action, skipping" |
| 55 | + exit 0 |
| 56 | + fi |
| 57 | +
|
| 58 | + # last one wins |
| 59 | + echo "NeedsJiraUpdate=true" >> $GITHUB_ENV |
| 60 | + shell: bash |
| 61 | + |
| 62 | + - name: "Update jira" |
| 63 | + if: ${{ env.NeedsJiraUpdate == 'true' }} |
| 64 | + env: |
| 65 | + # ID is the html url to keep a link between systems as there is no way to force an ID on Jira side. |
| 66 | + id: ${{ github.event.issue.html_url }} |
| 67 | + title: ${{ github.event.issue.title }} |
| 68 | + body: ${{ github.event.issue.body }} |
| 69 | + author: ${{ github.event.issue.user.login }} |
| 70 | + run: | |
| 71 | + set -eux |
| 72 | +
|
| 73 | + # Convert markdown to JIRA using mistletoe package which is available starting with impish. |
| 74 | + # Since GH runners only have LTS versions it's safe to only check for focal which doesn't have the package. |
| 75 | + if [ $(lsb_release -c -s) == "focal" ]; then |
| 76 | + echo "Converting Markdown to JIRA is only possible starting with Ubuntu 22.04 (jammy). Pushing verbatim content to JIRA..." |
| 77 | + else |
| 78 | + TMPDIR=$(mktemp -d) |
| 79 | + trap 'rm -rf -- "$TMPDIR"' EXIT |
| 80 | +
|
| 81 | + sudo apt install -y python3-mistletoe |
| 82 | + echo ${body} > $TMPDIR/body.md |
| 83 | + body=$(PYTHONPATH=/usr/share/doc/python3-mistletoe mistletoe -r examples.jira_renderer.JIRARenderer $TMPDIR/body.md) |
| 84 | + fi |
| 85 | +
|
| 86 | + description="${body} |
| 87 | +
|
| 88 | +
|
| 89 | + GitHub URL: ${id}. |
| 90 | +
|
| 91 | + Opened by ${author}." |
| 92 | +
|
| 93 | + # Choose Jira action based on event type and action. |
| 94 | + action="" |
| 95 | + if [ ${{ github.event_name }} == "issues" ]; then |
| 96 | + action=Update |
| 97 | + if [ ${{ github.event.action }} == "labeled" ]; then |
| 98 | + action=Create |
| 99 | + elif [ ${{ github.event.action }} == "reopened" ]; then |
| 100 | + action=Reopen |
| 101 | + elif [ ${{ github.event.action }} == "deleted" ] || [ ${{ github.event.action }} == "unlabeled" ]; then |
| 102 | + # Note: deleting issue from GH is not supported ATM as there is no more label attached. unlabeled is supported. |
| 103 | + action=Delete |
| 104 | + elif [ ${{ github.event.action }} == "closed" ]; then |
| 105 | + action=Close |
| 106 | + fi |
| 107 | + fi |
| 108 | +
|
| 109 | + echo "PUSHING: $id $action $title $description" |
| 110 | +
|
| 111 | + # Push to Jira as a json data format. |
| 112 | + data=$(jq -n \ |
| 113 | + --arg id "$id" \ |
| 114 | + --arg action "$action" \ |
| 115 | + --arg title "$title" \ |
| 116 | + --arg description "$description" \ |
| 117 | + '{data: {id: $id, action: $action, title: $title, description: $description}}') |
| 118 | + curl -X POST -H 'Content-type: application/json' --data "${data}" '${{ inputs.webhook-url }}' |
| 119 | +
|
| 120 | + shell: bash |
0 commit comments