-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
116 lines (106 loc) · 4.2 KB
/
action.yml
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
name: Rich Workflow Run
description: Enrich workflow run event with jobs, logs and artifacts information
inputs:
jobs-url:
description: "Jobs URL"
required: false
logs-url:
description: "Logs URL"
required: false
artifacts-url:
description: "Artifacts URL"
required: false
artifact-names:
description: "A comma-separated list of artifact names to download"
required: false
default: ''
outputs:
jobs:
description: "Jobs"
value: ${{ steps.jobs.outputs.jobs || '{}' }}
logs:
description: "Logs"
value: ${{ steps.logs.outputs.logs || '{}' }}
artifacts:
description: "Artifacts"
value: ${{ steps.artifacts.outputs.artifacts || '{}' }}
runs:
using: "composite"
steps:
- id: jobs
if: inputs.jobs-url != ''
name: Download jobs information
env:
GH_TOKEN: ${{ github.token }}
JOBS_URL: ${{ inputs.jobs-url }}
run: |
eof="EOF$RANDOM"
jobs="$(gh api "$JOBS_URL")"
echo "jobs<<$eof" >> $GITHUB_OUTPUT
jq -c '.jobs | map({"key": .name, "value": (. | .steps = (.steps | map({"key": .name, "value": .}) | from_entries))}) | from_entries' <<< "$jobs" >> $GITHUB_OUTPUT
echo "$eof" >> $GITHUB_OUTPUT
shell: bash
- id: logs
if: inputs.logs-url != ''
name: Download logs information
env:
GH_TOKEN: ${{ github.token }}
LOGS_URL: ${{ inputs.logs-url }}
run: |
eof="EOF$RANDOM"
logs="{}"
zip="$(mktemp)"
dir="$(mktemp -d)"
gh api "$LOGS_URL" > "$zip"
unzip "$zip" -d "$dir"
while read file; do
if [[ -z "$file" ]]; then
continue
fi
key="$(jq -nr '$file | sub($dir;"") | sub("/\\d+_";"/") | sub("^/";"") | sub("\\.txt";"")' --arg dir "$dir" --arg file "$file")"
value="$(jq -Rs 'split("\n") | map(select(. == "" | not)) | map(split(" ")) | map({"time": .[0], "message": .[1:] | join(" ")})' "$file")"
logs="$(jq -n '$logs | .[$key] = $value' --arg key "$key" --argjson value "$value" --argjson logs "$logs")"
done <<< "$(find "$dir" -name "*.txt")"
rm -rf "$dir" "$zip"
echo "logs<<$eof" >> $GITHUB_OUTPUT
jq -c '.' <<< "$logs" >> $GITHUB_OUTPUT
echo "$eof" >> $GITHUB_OUTPUT
shell: bash
- id: artifacts
if: inputs.artifacts-url != ''
name: Download artifacts information
env:
GH_TOKEN: ${{ github.token }}
ARTIFACTS_URL: ${{ inputs.artifacts-url }}
ARTIFACT_NAMES: ${{ inputs.artifact-names }}
run: |
eof="EOF$RANDOM"
raw_artifacts="$(gh api "$ARTIFACTS_URL")"
echo "$raw_artifacts" | jq -c '.'
artifacts="{}"
while read name; do
if [[ -z "$name" ]]; then
continue
fi
artifact="$(jq -n '$artifacts | .artifacts | map(select(.name == $name)) | .[0]' --arg name "$name" --argjson artifacts "$raw_artifacts")"
url="$(jq -nr '$artifact | select(.name as $name | $names | split(",") | index($name)) | .archive_download_url' --argjson artifact "$artifact" --arg names "$ARTIFACT_NAMES")"
if [[ -n "$url" ]]; then
zip="$(mktemp)"
dir="$(mktemp -d)"
gh api "$url" > "$zip"
unzip "$zip" -d "$dir"
files="{}"
while read file; do
key="$(jq -nr '$file | sub($dir;"") | sub("^/";"")' --arg dir "$dir" --arg file "$file")"
value="$(jq '.' "$file" 2> /dev/null || jq -Rs '.' "$file")"
files="$(jq -n '$files | .[$key] = $value' --arg key "$key" --argjson value "$value" --argjson files "$files")"
done <<< "$(find "$dir" -type f)"
rm -rf "$dir" "$zip"
artifact="$(jq -n '$artifact | .files = $files' --argjson files "$files" --argjson artifact "$artifact")"
fi
artifacts="$(jq -n '$artifacts | .[$artifact | .name] = $artifact' --argjson artifact "$artifact" --argjson artifacts "$artifacts")"
done <<< "$(jq -r '.artifacts[] | .name' <<< "$raw_artifacts")"
echo "artifacts<<$eof" >> $GITHUB_OUTPUT
jq -c '.' <<< "$artifacts" >> $GITHUB_OUTPUT
echo "$eof" >> $GITHUB_OUTPUT
shell: bash