|
| 1 | +name: "ZenDesk: Comment GitHub Issue on Zendesk Ticket Comment" |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + external_id: |
| 7 | + description: "GitHub issue url" |
| 8 | + required: true |
| 9 | + type: string |
| 10 | + custom_field: |
| 11 | + description: "Space separated list of labels" |
| 12 | + required: false |
| 13 | + default: "" |
| 14 | + type: string |
| 15 | + comment_body: |
| 16 | + description: "Zendesk comment body" |
| 17 | + required: true |
| 18 | + type: string |
| 19 | + author: |
| 20 | + description: "Zendesk comment author" |
| 21 | + required: false |
| 22 | + default: "" |
| 23 | + type: string |
| 24 | + |
| 25 | +jobs: |
| 26 | + process_comment_and_labels: |
| 27 | + runs-on: ubuntu-latest |
| 28 | + steps: |
| 29 | + - uses: hmarr/debug-action@v3.0.0 |
| 30 | + |
| 31 | + - uses: actions/github-script@v7 |
| 32 | + env: |
| 33 | + WORKFLOW_RUN_LINK: "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" |
| 34 | + with: |
| 35 | + github-token: ${{ secrets.GIT_PAT_HEIDI }} |
| 36 | + script: | |
| 37 | + // Extract issue details from the Zendesk external_id |
| 38 | + const parts = context.payload.inputs.external_id.split("/"); |
| 39 | + const issue_number = parts[parts.length - 1]; |
| 40 | + const issue_repo = parts[parts.length - 3]; |
| 41 | + const issue_owner = parts[parts.length - 4]; |
| 42 | +
|
| 43 | + // Extract comment details |
| 44 | + const comment_author = context.payload.inputs.author || "HumanSignal Support"; |
| 45 | + const comment_body = context.payload.inputs.comment_body; |
| 46 | + const formatted_comment_body = |
| 47 | + `${comment_body} |
| 48 | + |
| 49 | + > Comment by ${comment_author} |
| 50 | + > [Workflow Run](${process.env.WORKFLOW_RUN_LINK})`; |
| 51 | +
|
| 52 | + // Add a comment to the GitHub issue |
| 53 | + if (comment_body.startsWith('[GITHUB_ISSUE_')) { |
| 54 | + core.notice(`Skipping comment creation.`); |
| 55 | + } else { |
| 56 | + const { data: comment } = await github.rest.issues.createComment({ |
| 57 | + owner: issue_owner, |
| 58 | + repo: issue_repo, |
| 59 | + issue_number: issue_number, |
| 60 | + body: formatted_comment_body |
| 61 | + }); |
| 62 | + core.notice(`Comment created ${comment.html_url}`); |
| 63 | + } |
| 64 | +
|
| 65 | + // Extract labels from the custom_field |
| 66 | + let new_labels = []; |
| 67 | + if (context.payload.inputs.custom_field) { |
| 68 | + new_labels = context.payload.inputs.custom_field.split(" ").map(label => label.trim()); |
| 69 | + } |
| 70 | +
|
| 71 | + // Get the current labels on the GitHub issue |
| 72 | + const { data: current_labels } = await github.rest.issues.listLabelsOnIssue({ |
| 73 | + owner: issue_owner, |
| 74 | + repo: issue_repo, |
| 75 | + issue_number: issue_number |
| 76 | + }); |
| 77 | +
|
| 78 | + const current_label_names = current_labels.map(label => label.name); |
| 79 | +
|
| 80 | + // Labels to be added |
| 81 | + const labels_to_add = new_labels.filter(label => !current_label_names.includes(label)); |
| 82 | +
|
| 83 | + // Labels to be removed |
| 84 | + const labels_to_remove = current_label_names.filter(label => !new_labels.includes(label)); |
| 85 | +
|
| 86 | + // Remove labels that are not in the new labels list |
| 87 | + for (const label of labels_to_remove) { |
| 88 | + await github.rest.issues.removeLabel({ |
| 89 | + owner: issue_owner, |
| 90 | + repo: issue_repo, |
| 91 | + issue_number: issue_number, |
| 92 | + name: label |
| 93 | + }); |
| 94 | + } |
| 95 | +
|
| 96 | + // Add the new labels |
| 97 | + if (labels_to_add.length > 0) { |
| 98 | + await github.rest.issues.addLabels({ |
| 99 | + owner: issue_owner, |
| 100 | + repo: issue_repo, |
| 101 | + issue_number: issue_number, |
| 102 | + labels: labels_to_add |
| 103 | + }); |
| 104 | + } |
0 commit comments