-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.sh
More file actions
212 lines (164 loc) · 5.35 KB
/
Copy pathentrypoint.sh
File metadata and controls
212 lines (164 loc) · 5.35 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env bash
set -euo pipefail
DOCS_PATH="${MARK_DOCS_PATH:-.}"
CHANGED_ONLY="${MARK_CHANGED_ONLY:-false}"
DRY_RUN="${MARK_DRY_RUN:-false}"
EXCLUDE_DIRS="${MARK_EXCLUDE_DIRS:-}"
CONFLUENCE_URL="${MARK_URL:-}"
SPACE="${MARK_SPACE:-}"
PARENT="${MARK_PARENT:-}"
MARK_USERNAME="${MARK_USERNAME:-}"
MARK_PASSWORD="${MARK_PASSWORD:-}"
DROP_H1="${MARK_DROP_H1:-false}"
STRIP_LINEBREAKS="${MARK_STRIP_LINEBREAKS:-false}"
TITLE_FROM_H1="${MARK_TITLE_FROM_H1:-false}"
TITLE_FROM_FILENAME="${MARK_TITLE_FROM_FILENAME:-false}"
LOG_LEVEL="${MARK_LOG_LEVEL:-info}"
echo "========================================="
echo "DOCS_PATH : ${DOCS_PATH}"
echo "CHANGED_ONLY : ${CHANGED_ONLY}"
echo "DRY_RUN : ${DRY_RUN}"
echo "EXCLUDE_DIRS : ${EXCLUDE_DIRS:-<none>}"
echo "CONFLUENCE_URL : ${CONFLUENCE_URL}"
echo "SPACE : ${SPACE:-<from markdown>}"
echo "PARENT : ${PARENT:-<from markdown>}"
echo "DROP_H1 : ${DROP_H1}"
echo "STRIP_LINEBREAKS : ${STRIP_LINEBREAKS}"
echo "TITLE_FROM_H1 : ${TITLE_FROM_H1}"
echo "TITLE_FROM_FILENAME : ${TITLE_FROM_FILENAME}"
echo "LOG_LEVEL : ${LOG_LEVEL}"
echo "========================================="
# Validate mark binary
if ! command -v mark >/dev/null 2>&1; then
echo "ERROR: mark binary not found"
exit 1
fi
# Validate required variables
required_vars=(
MARK_PASSWORD
CONFLUENCE_URL
MARK_USERNAME
)
for var in "${required_vars[@]}"; do
if [[ -z "${!var:-}" ]]; then
echo "ERROR: Missing required variable: ${var}"
exit 1
fi
done
# Parse exclude directories once
declare -a EXCLUDE_ARRAY=()
if [[ -n "${EXCLUDE_DIRS}" ]]; then
IFS=',' read -ra EXCLUDE_ARRAY <<< "${EXCLUDE_DIRS}"
fi
# Check whether a file should be excluded
should_exclude() {
local file="$1"
for dir in "${EXCLUDE_ARRAY[@]}"; do
local normalized_dir="${dir#/}"
normalized_dir="${normalized_dir%/}"
[[ -z "${normalized_dir}" ]] && continue
if [[ "${file}" == "${DOCS_PATH}/${normalized_dir}/"* ]]; then
return 0
fi
done
return 1
}
sync_file() {
local file="$1"
echo "::group::Syncing ${file}"
# Parse metadata from HTML comments in the markdown file
local file_space=""
local file_parent=""
local file_title=""
if [[ -f "${file}" ]]; then
file_space=$(sed -n 's/.*<!-- Space: \([^-]*\) -->.*/\1/p' "${file}" | head -1 || true)
file_parent=$(sed -n 's/.*<!-- Parent: \([^-]*\) -->.*/\1/p' "${file}" | head -1 || true)
file_title=$(sed -n 's/.*<!-- Title: \([^-]*\) -->.*/\1/p' "${file}" | head -1 || true)
fi
local mark_args=(
-p "${MARK_PASSWORD}"
-b "${CONFLUENCE_URL}"
-u "${MARK_USERNAME}"
--log-level "${LOG_LEVEL^^}"
)
# Use environment variable if set, otherwise fall back to file metadata
local space_to_use="${SPACE:-${file_space}}"
local parent_to_use="${PARENT:-${file_parent}}"
[[ -n "${space_to_use}" ]] && mark_args+=(--space "${space_to_use}")
[[ -n "${parent_to_use}" ]] && mark_args+=(--parents "${parent_to_use}")
[[ "${DRY_RUN}" == "true" ]] && mark_args+=(--dry-run)
# Formatting options
[[ "${DROP_H1}" == "true" ]] && mark_args+=(--drop-h1)
[[ "${STRIP_LINEBREAKS}" == "true" ]] && mark_args+=(-L)
[[ "${TITLE_FROM_H1}" == "true" ]] && mark_args+=(--title-from-h1)
[[ "${TITLE_FROM_FILENAME}" == "true" ]] && mark_args+=(--title-from-filename)
echo "Running: mark ${file}"
local retries=3
local attempt=1
until mark "${mark_args[@]}" -f "${file}"; do
if [[ "${attempt}" -ge "${retries}" ]]; then
echo "ERROR: Failed syncing ${file} after ${retries} attempts"
echo "::endgroup::"
return 1
fi
echo "Retry ${attempt}/${retries} failed. Retrying in 5 seconds..."
sleep 5
((attempt++))
done
echo "::endgroup::"
}
build_find_command() {
local -a cmd=(
find
"${DOCS_PATH}"
-type f
-name "*.md"
)
for dir in "${EXCLUDE_ARRAY[@]}"; do
local normalized_dir="${dir#/}"
normalized_dir="${normalized_dir%/}"
[[ -z "${normalized_dir}" ]] && continue
cmd+=(
-not
-path
"${DOCS_PATH}/${normalized_dir}/*"
)
done
"${cmd[@]}"
}
get_changed_files() {
# Handle shallow clone / first commit gracefully
if git rev-parse HEAD~1 >/dev/null 2>&1; then
git diff --name-only HEAD~1 HEAD
else
git ls-files '*.md'
fi | grep '\.md$' || true
}
echo "Discovering markdown files..."
if [[ "${CHANGED_ONLY}" == "true" ]]; then
echo "Running in changed-only mode"
FILES="$(
while IFS= read -r file; do
[[ -z "${file}" ]] && continue
# Skip deleted files
[[ ! -f "${file}" ]] && continue
if ! should_exclude "${file}"; then
echo "${file}"
fi
done <<< "$(get_changed_files)"
)"
else
FILES="$(build_find_command)"
fi
if [[ -z "${FILES}" ]]; then
echo "No markdown files found"
exit 0
fi
echo "Files to sync:"
echo "${FILES}"
echo "========================================="
while IFS= read -r file; do
[[ -z "${file}" ]] && continue
sync_file "${file}"
done <<< "${FILES}"
echo "Sync completed successfully"