-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
entrypoint
executable file
·108 lines (82 loc) · 2.84 KB
/
entrypoint
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
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2020 Pier Luigi Fiorini <[email protected]>
#
# SPDX-License-Identifier: MIT
from github import Github
from pathlib import Path
import os
import re
import shutil
import subprocess
import sys
def issue_message(what, message, filename=None, line=None, col=None):
optional = []
if filename:
optional.append(f'file={filename}')
if line:
optional.append(f'line={line}')
if col:
optional.append(f'col={col}')
if optional:
middle = ' ' + ','.join(optional)
else:
middle = ''
print(f'::{what}{middle}::{message}')
def error(message, **kwargs):
# https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message
issue_message('error', message, **kwargs)
def find_qmllint():
exe = shutil.which("/usr/lib/qt6/bin/qmllint")
if exe is not None:
return exe
return None
def find_files(directory, pattern):
for root, dirs, files in os.walk(directory):
for basename in files:
print(basename)
if fnmatch.fnmatch(basename, pattern):
filename = os.path.join(root, basename)
print(filename)
yield filename
def lint(filename, qmllint_bin):
if qmllint_bin is None:
raise SystemExit('Cannot find qmllint executable')
result = subprocess.run([qmllint_bin, filename], capture_output=True)
if result.returncode != 0:
errmsg = result.stderr.decode().strip()
match = re.match(r'.+:(\d+) : (.+)', errmsg)
if match:
col, msg = match.groups()
error(msg, filename=filename, col=col)
return False
return True
def update_status(state, descr):
g = Github(os.environ['GITHUB_TOKEN'])
repo = g.get_repo(os.environ['GITHUB_REPOSITORY'])
repo.get_commit(sha=os.environ['GITHUB_SHA']).create_status(
state=state,
target_url='https://github.com/liri-infra/qmllint-action',
description=descr,
context='status-check/qmllint'
)
if __name__ == '__main__':
os.chdir(os.environ['GITHUB_WORKSPACE'])
# We don't update the status check from a workflow, the exit status is
# enough since it's interpreted as a status check
ci = os.environ.get('CI') is not None
failed = False
if ci is False:
update_status('pending', 'About to verify QML files')
qmllint_bin = find_qmllint()
for suffix in ('qml', 'js'):
for filename in Path('.').glob('**/*.' + suffix):
if lint(filename, qmllint_bin) is False:
failed = True
if failed is True:
if ci is False:
update_status('failure', 'QML files are not valid')
sys.exit(1)
else:
if ci is False:
update_status('success', 'QML files are valid')
sys.exit(0)