This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
forked from LambdaLabs/docsy-lambda-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zd_sync
executable file
·130 lines (116 loc) · 3.19 KB
/
zd_sync
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash
#
# Publish a docs site[1] article to the Zendesk Help Center[2].
#
# **Warning** This script doesn't have much error handling and hasn't been
# tested much. Use at your own risk.
#
# Usage: ./zd_sync URL SECTION
#
# URL is the docs site article URL. For example:
# https://docs.lambdalabs.com/cloud/faq/nmi-received-for-unknown-reason/
#
# SECTION is the Help Center section in which to publish the article.
# SECTION must be one of the following:
# - cloud
# - linux
# - servers
# - tensorbook
# - windows
# - workstations
#
# [1] https://docs.lambdalabs.com/
# [2] https://support.lambdalabs.com/hc/en-us
#
set -e
# Tip: If you use 1Password to store your API credentials, you can install the
# 1Password CLI[3] and set the below API variables to, for example:
#
# $(op read 'op://Private/Lambda Zendesk API key/username')
#
# This is safer than setting the variables in plain text.
#
# [3] https://1password.com/downloads/command-line/
#
readonly API_EMAIL=""
readonly API_TOKEN=""
readonly API_URL=""
clean_up() {
echo "Deleting temporary files…"
rm -f "${ARTICLE_FILE}" "${ARTICLE_JSON_FILE}" "${PUBL_ARTICLE_JSON_FILE}"
echo "Done."
}
if [[ "$#" -eq 0 ]]; then
echo "No URL or section supplied. Aborting." >&2
clean_up
exit 1
fi
if [[ "$#" -eq 1 ]]; then
echo "No section supplied. Aborting." >&2
clean_up
exit 1
fi
readonly ARTICLE_FILE="$(mktemp)"
if ! curl --silent "$1" > "${ARTICLE_FILE}"; then
echo "Unable to fetch article. Aborting." >&2
clean_up
exit 1
fi
case "$2" in
"cloud")
SECTION=9050648839821
;;
"linux")
SECTION=9050769494541
;;
"servers")
SECTION=9050593366285
;;
"tensorbook")
SECTION=4738754483597
;;
"windows")
SECTION=4734490996109
;;
"workstations")
SECTION=9050588728205
;;
*)
echo "Invalid section. Aborting." >&2
clean_up
exit 1
;;
esac
readonly ARTICLE_TITLE="$(xmllint --html --xpath "string(//h1[@id='zd-article-title'])" \
- < "${ARTICLE_FILE}" 2> /dev/null)"
readonly ARTICLE_BODY="$(xmllint --html --xpath "//div[@id='zd-article-body']" \
- < "${ARTICLE_FILE}" 2> /dev/null)"
if [[ -z "${ARTICLE_TITLE}" ]] || [[ -z "${ARTICLE_BODY}" ]]; then
echo "URL isn't a docs site article. Aborting." >&2
clean_up
exit 1
fi
readonly ARTICLE_JSON_FILE="$(mktemp)"
jq --null-input --arg title "${ARTICLE_TITLE}" --arg body "${ARTICLE_BODY}" \
'{
"article": {
"title": $title,
"body": $body,
"locale": "en-us",
"permission_group_id": 2813452,
"user_segment_id": null
},
"notify_subscribers": false
}' > "${ARTICLE_JSON_FILE}"
# TODO: Improve so that if the below command is successful, echo the URL of
# the published article.
readonly PUBL_ARTICLE_JSON_FILE="$(mktemp)"
curl "${API_URL}/sections/${SECTION}/articles.json" \
--silent \
--data @"${ARTICLE_JSON_FILE}" \
--header "Content-Type: application/json" \
--user "${API_EMAIL}/token:${API_TOKEN}" > "${PUBL_ARTICLE_JSON_FILE}"
echo -e "Article published successfully.\n"
jq . < "${PUBL_ARTICLE_JSON_FILE}"
echo -e "\n"
clean_up