-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·136 lines (108 loc) · 4.26 KB
/
main.py
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
#!/usr/bin/env python3
import argparse
import json
import git
import os
import subprocess
from pathlib import Path
def get_command_line_options(extra_options, enabled_rules, disabled_rules, config_file):
options = []
if extra_options:
options.append(extra_options)
if config_file == "true" and os.path.isfile("/github/workspace/.tflint.hcl"):
options.append("--config /github/workspace/.tflint.hcl")
if enabled_rules:
for enabled_rule in enabled_rules.split(" "):
options.append("--enable-rule " + enabled_rule)
if disabled_rules:
for disabled_rule in disabled_rules.split(" "):
options.append("--disable-rule " + disabled_rule)
return " ".join(options)
def get_file_list(changed_only, src, recurse):
files = []
if changed_only == "true":
event_path = os.environ.get("GITHUB_EVENT_PATH")
event_data = ""
with open(event_path) as f:
event_data = json.load(f)
if os.environ.get("GITHUB_EVENT_NAME") == "pull_request":
base = event_data["pull_request"]["base"]["sha"]
elif os.environ.get("GITHUB_EVENT_NAME") == "push":
base = event_data["before"]
else:
base = ""
repo = git.Repo(os.environ.get("GITHUB_WORKSPACE"))
for item in repo.index.diff(str(base)):
files.append(item.a_path)
elif recurse == "true":
for file in Path(src).rglob("*.tf"):
files.append(str(file.parent) + "/" + file.name)
return files
def parse_message(message):
error = ""
level = ""
filename = ""
line_number = ""
base_path = os.environ.get("GITHUB_WORKSPACE")
for line in message.splitlines():
if line.startswith(("Error", "Notice", "Warning")):
level = line.split(":")[0].lower()
if level == "warning":
level = "error"
elif level == "notice":
level = "warning"
error = line.split(":")[1].strip()
elif line.startswith(" on"):
filename = line.split(" ")[3]
filename = filename.replace(base_path, "")
line_number = line.split(" ")[5].replace(":", "")
elif line.startswith("Reference"):
error = error + " " + line
print("::%s file=%s,line=%s::%s" % (level, filename, line_number, error))
error = ""
def run_tflint(args):
if not os.path.isfile("/usr/local/bin/tflint"):
print("::debug::tflint is required to perform this action")
exit(1)
options = get_command_line_options(
args.extra_options, args.enabled_rules, args.disabled_rules, args.config_file
)
file_list = get_file_list(args.changed_only, args.path, args.recurse)
exit_code = 0
for file in file_list:
command_string = "/usr/local/bin/tflint " + options + " " + file
if args.debug == "true":
print(command_string)
result = subprocess.run(
[command_string], capture_output=True, text=True, shell=True
)
if result.stdout and args.tag_lines == "true":
parse_message(result.stdout)
if result.returncode > exit_code:
exit_code = result.returncode
exit(exit_code)
def main():
parser = argparse.ArgumentParser(description="TFLint github action")
parser.add_argument(
"--changed_only", default=os.environ.get("INPUT_TFLINT_CHANGED_ONLY")
)
parser.add_argument(
"--config_file", default=os.environ.get("INPUT_TFLINT_CONFIG_FILE")
)
parser.add_argument("--debug", default=os.environ.get("INPUT_TFLINT_DEBUG"))
parser.add_argument(
"--disabled_rules", default=os.environ.get("INPUT_TFLINT_DISABLED_RULES")
)
parser.add_argument(
"--enabled_rules", default=os.environ.get("INPUT_TFLINT_ENABLED_RULES")
)
parser.add_argument(
"--extra_options", default=os.environ.get("INPUT_TFLINT_EXTRA_OPTIONS")
)
parser.add_argument("--path", default=os.environ.get("INPUT_TFLINT_PATH"))
parser.add_argument("--recurse", default=os.environ.get("INPUT_TFLINT_RECURSE"))
parser.add_argument("--tag_lines", default=os.environ.get("INPUT_TFLINT_TAG_LINES"))
args = parser.parse_args()
run_tflint(args)
if __name__ == "__main__":
main()