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