-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
198 lines (181 loc) · 7.17 KB
/
action.yml
File metadata and controls
198 lines (181 loc) · 7.17 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
# Actions docs: https://docs.github.com/en/actions/sharing-automations/creating-actions/metadata-syntax-for-github-actions
# XMLLint docs: http://xmlsoft.org/xmllint.html
name: 'XMLLint Validate'
author: 'Juliette Reinders Folmer'
description: >-
Validate an XML file for being well-formed.
# Icons available: https://feathericons.com/
branding:
icon: 'check-circle'
color: 'green'
inputs:
pattern:
description: 'Path to the file or glob pattern for the file(s) to validate with xmllint.'
required: true
xsd-file:
description: 'Path to a local file containing the XSD schema to validate against.'
required: false
default: ''
xsd-url:
description: 'URL to a remote file containing the XSD schema to validate against.'
required: false
default: ''
show-in-pr:
description: 'Show any errors from xmllint inline in PRs ?'
required: false
debug:
description: 'Whether to show verbose output for debugging the action'
required: false
runs:
using: "composite"
steps:
- name: 'Validate pattern input'
if: ${{ ! inputs.pattern }}
shell: bash
run: |
# Validate pattern input.
echo "::error title=XMLLint Validate::Missing required input 'pattern'. Please provide a path to a file or a glob pattern."
exit 1
- name: 'Set debug mode'
env:
DEBUG_INPUT: ${{ inputs.debug }}
shell: bash
run: |
# Determine debug mode.
if [[ "${{ env.DEBUG_INPUT }}" == "true" ]]; then
echo "DEBUG=true" >> "$GITHUB_ENV"
else
echo "DEBUG=false" >> "$GITHUB_ENV"
fi
- name: 'Validate local XSD file input'
id: valid_xsdfile
if: ${{ inputs.xsd-file }}
env:
XSD_FILE: ${{ inputs.xsd-file }}
shell: bash
run: |
# Validate local file input.
# Check for non-zero length file name.
if [[ -n "${{ env.XSD_FILE }}" ]]; then
# Check that file name ends on .xsd.
if [[ "${{ endsWith( env.XSD_FILE, '.xsd' ) }}" == "false" ]]; then
echo "::error title=XMLLint Validate::Local XSD file must use an '.xsd' file extension."
exit 1
# Check the file exists and has contents (file size greater than zero).
elif [[ -f "${{ env.XSD_FILE }}" && -s "${{ env.XSD_FILE }}" ]]; then
echo 'Local XSD file found.'
exit 0
else
echo "::error title=XMLLint Validate::Local XSD file must exist and have a file size greater than 0."
exit 1
fi
fi
- name: 'Notify about precedence'
if: ${{ inputs.xsd-file && inputs.xsd-url }}
shell: bash
# yamllint disable rule:line-length
run: |
echo "::warning title=XMLLint Validate::Both a local file as well as a remote URL passed for the XSD file. The local file takes precedence, the remote URL will be ignored."
# yamllint enable rule:line-length
- name: 'Set local file name'
if: ${{ ! inputs.xsd-file && inputs.xsd-url }}
shell: bash
run: |
# Set environment variable.
# Name which is unlikely to conflict with any existing files in a repo.
echo "DOWNLOADED_XSD_FILE=xmllint-validate-action-schema.xsd" >> "$GITHUB_ENV"
- name: 'Validate the URL and download the schema file'
id: valid_xsdurl
if: ${{ ! inputs.xsd-file && inputs.xsd-url }}
env:
XSD_URL: ${{ inputs.xsd-url }}
shell: bash
# yamllint disable rule:line-length
run: |
# Validate remote URL input and download file.
# Check for non-zero length URL input.
if [[ -n "${{ env.XSD_URL }}" ]]; then
# Check that URL uses http(s) protocol.
if [[ "${{ startsWith( env.XSD_URL, 'http://' ) || startsWith( env.XSD_URL, 'https://' ) || startsWith( env.XSD_URL, 'ftp://' ) }}" == "false" ]]; then
echo "::error title=XMLLint Validate::URL to the XSD file must be an 'http', 'https' or 'ftp' URL."
exit 1
# Check that URL ends on .xsd.
elif [[ "${{ endsWith( env.XSD_URL, '.xsd' ) }}" == "false" ]]; then
echo "::error title=XMLLint Validate::URL to the XSD file must point to a file using an '.xsd' file extension."
exit 1
# Try to download it.
else
if [[ "${{ env.DEBUG }}" == "false" ]]; then
wget -nc -nv "${{ env.XSD_URL }}" -O "${{ env.DOWNLOADED_XSD_FILE }}"
else
wget -nc "${{ env.XSD_URL }}" -O "${{ env.DOWNLOADED_XSD_FILE }}"
fi
if [[ -f "${{ env.DOWNLOADED_XSD_FILE }}" && -s "${{ env.DOWNLOADED_XSD_FILE }}" ]]; then
echo 'Download of the XSD file succesfull.'
exit 0
else
echo "::error title=XMLLint Validate::Remote XSD file must exist and have a file size greater than 0."
exit 1
fi
fi
fi
# yamllint enable rule:line-length
# Updating the lists can fail intermittently, typically after Microsoft has released a new package.
# This should not be blocking for this action, so ignore any errors from this step.
# Ref: https://github.com/dotnet/core/issues/4167
- name: 'Update the available packages list'
continue-on-error: true
shell: bash
run: |
# Update package list.
if [[ "${{ env.DEBUG }}" == "false" ]]; then
sudo apt-get -q update > /dev/null
else
sudo apt-get update
fi
- name: 'Install xmllint'
shell: bash
run: |
# Install xmllint.
if [[ "${{ env.DEBUG }}" == "false" ]]; then
sudo apt-get -q install --no-install-recommends -y libxml2-utils > /dev/null
else
sudo apt-get install --no-install-recommends -y libxml2-utils
xmllint --version
fi
# Show XML violations inline in the file diff.
# @link https://github.com/marketplace/actions/xmllint-problem-matcher
- name: 'Enable showing XML issues inline'
if: ${{ inputs.show-in-pr != 'false' }}
uses: korelstar/xmllint-problem-matcher@1bd292d642ddf3d369d02aaa8b262834d61198c0 # v1.2.0
- name: 'List files'
if: ${{ inputs.debug }}
env:
GLOB_PATTERN: ${{ inputs.pattern }}
shell: bash
run: |
# List files which will be examined.
ls -ah $GLOB_PATTERN
- name: 'Validate for well-formedness'
if: ${{ ! inputs.xsd-file && ! inputs.xsd-url }}
env:
GLOB_PATTERN: ${{ inputs.pattern }}
shell: bash
run: xmllint --noout $GLOB_PATTERN
- name: 'Validate against local schema'
if: ${{ inputs.xsd-file }}
env:
GLOB_PATTERN: ${{ inputs.pattern }}
XSD_FILE: ${{ inputs.xsd-file }}
shell: bash
run: xmllint --noout --schema "$XSD_FILE" $GLOB_PATTERN
- name: 'Validate against downloaded remote schema'
if: ${{ ! inputs.xsd-file && inputs.xsd-url }}
env:
GLOB_PATTERN: ${{ inputs.pattern }}
shell: bash
run: xmllint --noout --schema ${{ env.DOWNLOADED_XSD_FILE }} $GLOB_PATTERN
- name: 'Clean up downloaded file'
if: ${{ ! inputs.xsd-file && inputs.xsd-url }}
shell: bash
run: rm -f ${{ env.DOWNLOADED_XSD_FILE }}