Skip to content

Commit ce986dd

Browse files
authored
Add workflow to fill Jira fields with GitHub PR data
This workflow fills a Jira custom field with GitHub PR data by processing a provided CSV file containing Jira keys.
1 parent cb19135 commit ce986dd

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
name: Fill Jira Field with GitHub PR Data
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
details_csv:
7+
description: "Full CSV text (header + rows). Must include a 'key' column."
8+
required: true
9+
type: string
10+
secrets:
11+
jira_auth:
12+
description: "Plain 'email:api_token' for Jira Cloud Basic auth"
13+
required: true
14+
15+
jobs:
16+
fill_field:
17+
runs-on: ubuntu-latest
18+
env:
19+
BASE_URL: https://scylladb.atlassian.net
20+
TARGET_FIELD: customfield_10718
21+
PR_URL: ${{ github.event.pull_request.html_url }}
22+
23+
steps:
24+
- name: Save CSV
25+
id: save
26+
shell: bash
27+
run: |
28+
set -euo pipefail
29+
cat > details.csv <<'CSV_EOF'
30+
${{ inputs.details_csv }}
31+
CSV_EOF
32+
33+
echo "Saved details.csv"
34+
head -n 5 details.csv || true
35+
36+
- name: Extract Jira keys (case-insensitive 'key' column)
37+
id: extract
38+
shell: bash
39+
run: |
40+
set -euo pipefail
41+
42+
python3 - <<'PY'
43+
import csv, sys, os
44+
45+
if not os.path.exists("details.csv") or os.path.getsize("details.csv") == 0:
46+
print("CSV empty → no keys to process.")
47+
open("keys.txt", "w").close()
48+
sys.exit(0)
49+
50+
with open("details.csv", newline="", encoding="utf-8") as f:
51+
reader = csv.DictReader(f)
52+
if not reader.fieldnames:
53+
open("keys.txt", "w").close()
54+
sys.exit(0)
55+
56+
# Find the 'key' column
57+
fmap = {(h or "").strip().lower(): h for h in reader.fieldnames}
58+
hkey = fmap.get("key")
59+
if not hkey:
60+
print(f"ERROR: No 'key' column. Found: {reader.fieldnames}", file=sys.stderr)
61+
sys.exit(1)
62+
63+
keys = []
64+
for row in reader:
65+
val = (row.get(hkey) or "").strip()
66+
if val:
67+
keys.append(val)
68+
69+
seen = set()
70+
uniq = []
71+
for k in keys:
72+
if k not in seen:
73+
uniq.append(k)
74+
seen.add(k)
75+
76+
with open("keys.txt", "w") as out:
77+
for k in uniq:
78+
out.write(k + "\n")
79+
80+
print(f"Found {len(uniq)} Jira issues.")
81+
PY
82+
83+
echo "First few keys:"
84+
head -n 10 keys.txt || true
85+
86+
- name: Write PR data to Jira custom field
87+
shell: bash
88+
env:
89+
JIRA_AUTH: ${{ secrets.jira_auth }}
90+
PR_URL: ${{ env.PR_URL }}
91+
BASE_URL: ${{ env.BASE_URL }}
92+
FIELD_ID: ${{ env.TARGET_FIELD }}
93+
run: |
94+
set -euo pipefail
95+
96+
if [ ! -s keys.txt ]; then
97+
echo "No keys found. Done."
98+
exit 0
99+
fi
100+
101+
COMMENT_TEXT="This issue will be closed by ${PR_URL}"
102+
103+
while IFS= read -r key; do
104+
[ -n "$key" ] || continue
105+
106+
echo "Updating $key ..."
107+
108+
jq -n --arg msg "$COMMENT_TEXT" --arg field "$FIELD_ID" \
109+
'{fields: {($field): $msg}}' > payload.json
110+
111+
code=$(curl -sS -o resp.json -w "%{http_code}" \
112+
-X PUT "$BASE_URL/rest/api/3/issue/$key" \
113+
--user "$JIRA_AUTH" \
114+
-H "Content-Type: application/json" \
115+
--data @payload.json)
116+
117+
if [[ "$code" == "204" || "$code" == "200" ]]; then
118+
echo "OK $key ($code)"
119+
else
120+
echo "FAIL $key ($code)"
121+
head -c 200 resp.json || true
122+
echo
123+
exit 1
124+
fi
125+
126+
sleep 0.2
127+
done < keys.txt
128+
129+
echo "All Jira fields updated successfully."

0 commit comments

Comments
 (0)