14 from tempfile
import TemporaryDirectory
15 from typing
import Union
17 from .ApiConfig
import ApiConfig, Runner
18 from .CliOptions
import CliOptions
23 Setup temp_dir using the diff or current MR
25 :ivar temp_dir: Temporary directory location for storing MR changes.
26 :ivar allowlist: Allow list from JSON
27 :ivar api_config: ApiConfig
30 def __init__(self, cli_options: CliOptions, api_config: ApiConfig):
34 :param cli_options: CliOptions object to get allow list from
35 :param api_config: API configuration for the CI
37 self.temp_dir: Union[TemporaryDirectory[str], TemporaryDirectory[bytes]] \
38 = TemporaryDirectory()
39 self.allowlist: dict[str, list[str]] = cli_options.allowlist
40 self.api_config: ApiConfig = api_config
44 Clean the created temp dir
46 self.temp_dir.cleanup()
50 Check if the path is allow listed
52 The function used fnmatch to check if the path is in allow list or not.
54 :param path: path to check
55 :return: True if the path is in allow list, False otherwise
57 path_is_excluded =
False
58 for pattern
in self.allowlist[
'exclude']:
59 if fnmatch.fnmatchcase(path, pattern):
60 path_is_excluded =
True
62 return path_is_excluded
66 Populate temp dir using the gitlab API `merge_requests`
68 :return: temp dir path
70 if self.api_config.running_on == Runner.GITLAB:
71 api_req_url = f
"{self.api_config.api_url}/projects/" \
72 f
"{self.api_config.project_id}/merge_requests/" \
73 f
"{self.api_config.mr_iid}/changes"
74 headers = {
'Private-Token': self.api_config.api_token}
77 elif self.api_config.running_on == Runner.GITHUB:
78 api_req_url = f
"{self.api_config.api_url}/repos/" \
79 f
"{self.api_config.github_repo_slug}/pulls/" + \
80 f
"{self.api_config.github_pull_request}/files"
82 "Authorization": f
"Bearer {self.api_config.api_token}",
83 "X-GitHub-Api-Version":
"2022-11-28",
84 "Accept":
"application/vnd.github+json"
89 api_req_url =
"https://api.github.com/repos/" \
90 f
"{self.api_config.travis_repo_slug}/pulls/" \
91 f
"{self.api_config.travis_pull_request}/files"
96 context = ssl.create_default_context()
97 context.check_hostname =
False
98 context.verify_mode = ssl.CERT_NONE
100 req = urllib.request.Request(api_req_url, headers=headers)
102 with urllib.request.urlopen(req, context=context)
as response:
103 change_response = response.read()
104 except Exception
as e:
105 print(f
"Unable to get URL {api_req_url}")
108 change_response = json.loads(change_response)
109 if self.api_config.running_on == Runner.GITLAB:
110 changes = change_response[
'changes']
112 changes = change_response
114 remove_diff_regex = re.compile(
r"^([ +-])(.*)$", re.MULTILINE)
116 for change
in changes:
117 if path_key
in change
and change_key
in change:
119 if path_to_be_excluded
is False:
120 curr_file = os.path.join(self.temp_dir.name, change[path_key])
121 curr_dir = os.path.dirname(curr_file)
122 if curr_dir != self.temp_dir.name:
123 os.makedirs(name=curr_dir, exist_ok=
True)
124 curr_file = open(file=curr_file, mode=
'w+', encoding=
'UTF-8')
125 print(re.sub(remove_diff_regex,
r"\2", change[change_key]),
128 return self.temp_dir.name
bool __is_excluded_path(self, str path)
def __init__(self, CliOptions cli_options, ApiConfig api_config)