Skip to content

Commit 2ba5455

Browse files
committed
ci: PLT-725: add zd actions
1 parent ec7fe19 commit 2ba5455

File tree

4 files changed

+248
-0
lines changed

4 files changed

+248
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: "ZenDesk: Push an issue comment to zendesk ticket"
2+
3+
on:
4+
issue_comment:
5+
types:
6+
- created
7+
8+
jobs:
9+
issue_commented:
10+
name: Issue comment
11+
if: ${{ !github.event.issue.pull_request && github.event.comment.user.login != 'heidi-humansignal' }}
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: hmarr/[email protected]
15+
16+
- env:
17+
ZENDESK_HOST: ${{ vars.ZENDESK_HOST }}
18+
ZENDESK_USER: ${{ vars.ZENDESK_USER }}
19+
ZENDESK_TOKEN: ${{ secrets.ZENDESK_TOKEN }}
20+
ISSUE_URL: ${{ github.event.issue.html_url }}
21+
ISSUE_COMMENT_BODY: ${{ github.event.comment.body }}
22+
ISSUE_USER: ${{ github.event.comment.user.login }}
23+
WORKFLOW_RUN_LINK: "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
24+
run: |
25+
echo "Looking up ticket by issue: ${ISSUE_URL}"
26+
tickets=$(curl "https://${ZENDESK_HOST}/api/v2/search.json?query=external_id:${ISSUE_URL}" \
27+
--user "${ZENDESK_USER}/token:${ZENDESK_TOKEN}" \
28+
-H "Content-Type: application/json")
29+
ticket_id=$(echo $tickets | jq '.results[0].id')
30+
echo "Found Zendesk ticket ${ticket_id}"
31+
32+
echo "Looking up user by issuer: ${ISSUE_USER}"
33+
users=$(curl "https://labelstudio.zendesk.com/api/v2/users/[email protected]" \
34+
--user "${ZENDESK_USER}/token:${ZENDESK_TOKEN}" \
35+
--header "Content-Type: application/json")
36+
user_id=$(echo $users | jq '.users[0].id')
37+
if [[ "$user_id" == "null" ]]; then
38+
echo "Fall back to generic github user"
39+
user_id="388861316959"
40+
else
41+
echo "Found user ${user_id}"
42+
fi
43+
44+
body=$(jq -n --arg body "$ISSUE_COMMENT_BODY" '{body: $body}' | jq .body)
45+
echo "$body"
46+
47+
curl "https://${ZENDESK_HOST}/api/v2/tickets/${ticket_id}.json" \
48+
--request PUT \
49+
--user "${ZENDESK_USER}/token:${ZENDESK_TOKEN}" \
50+
--header "Content-Type: application/json" \
51+
--data-binary @- <<DATA
52+
{
53+
"ticket": {
54+
"comment": {
55+
"body": "[GITHUB_ISSUE_COMMENT]\n\n${body:1:-1}\n\nGITHUB ISSUE URL: ${ISSUE_URL}\nWORKFLOW RUN: ${WORKFLOW_RUN_LINK}",
56+
"author_id": $user_id
57+
}
58+
}
59+
}
60+
DATA
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: "ZenDesk: Create a zendesk ticket out of an issue"
2+
3+
on:
4+
issues:
5+
types:
6+
- opened
7+
8+
jobs:
9+
issue_created:
10+
name: Issue created
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: hmarr/[email protected]
14+
15+
- env:
16+
ZENDESK_HOST: ${{ vars.ZENDESK_HOST }}
17+
ZENDESK_USER: ${{ vars.ZENDESK_USER }}
18+
ZENDESK_TOKEN: ${{ secrets.ZENDESK_TOKEN }}
19+
ISSUE_TITLE: ${{ github.event.issue.title }}
20+
ISSUE_BODY: ${{ github.event.issue.body }}
21+
ISSUE_USER: ${{ github.event.issue.user.login }}
22+
ISSUE_URL: ${{ github.event.issue.html_url }}
23+
WORKFLOW_RUN_LINK: "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
24+
REPO_NAME: ${{ github.event.repository.name }}
25+
run: |
26+
body=$(jq -n --arg body "$ISSUE_BODY" '{body: $body}' | jq .body)
27+
echo "$body"
28+
29+
curl https://${ZENDESK_HOST}/api/v2/tickets \
30+
--request POST \
31+
--user "${ZENDESK_USER}/token:${ZENDESK_TOKEN}" \
32+
--header "Content-Type: application/json" \
33+
--data-binary @- <<DATA
34+
{
35+
"ticket": {
36+
"subject": "Github_Issue - ${REPO_NAME}: ${ISSUE_TITLE:0:35}",
37+
"comment": {
38+
"body": "[GITHUB_ISSUE_DESCRIPTION]\n\n${body:1:-1}\n\nGITHUB ISSUE URL: ${ISSUE_URL}\nWORKFLOW RUN: ${WORKFLOW_RUN_LINK}"
39+
},
40+
"tags": ["gh-issue"],
41+
"external_id": "$ISSUE_URL",
42+
"requester": {
43+
"locale_id": 1,
44+
"name": "$ISSUE_USER from Github",
45+
"email": "[email protected]"
46+
}
47+
}
48+
}
49+
DATA
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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/[email protected]
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+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: "ZenDesk: Close GitHub Issue on Zendesk Ticket Solved"
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
external_id:
7+
description: "GitHub issue url"
8+
required: true
9+
type: string
10+
11+
jobs:
12+
close_issue:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: hmarr/[email protected]
16+
17+
- uses: actions/github-script@v7
18+
with:
19+
github-token: ${{ secrets.GIT_PAT_HEIDI }}
20+
script: |
21+
// Extract issue details from the Zendesk external_id
22+
const parts = context.payload.inputs.external_id.split("/");
23+
const issue_number = parts[parts.length - 1];
24+
const issue_repo = parts[parts.length - 3];
25+
const issue_owner = parts[parts.length - 4];
26+
27+
// Close the GitHub issue
28+
const { data: issue } await github.rest.issues.update({
29+
owner: issue_owner,
30+
repo: issue_repo,
31+
issue_number: issue_number,
32+
state: "closed"
33+
});
34+
35+
core.info(`GitHub issue ${issue.html_url} closed successfully.`);

0 commit comments

Comments
 (0)