-
Notifications
You must be signed in to change notification settings - Fork 13
/
git.py
99 lines (86 loc) · 3.52 KB
/
git.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
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from .base import Processor, NotConfiguredException
from git import Git, Repo
import os
import tempfile
class GitProcessor(Processor):
"""
Clones Git repositories via HTTPS or SSH.
Args:
url (str): Repository URL to download.
branch (str, optional): Branch to check out.
directory (str, optional): Directory to clone into.
depth (int, optional): Depth to check out.
"""
def get_default_config_key():
return 'git'
def process(self, output_var='git'):
if 'url' not in self.config:
raise NotConfiguredException('No URL configured!')
url = self._jinja_expand_string(self.config['url'], 'url')
self._init_tempdir()
if 'directory' in self.config:
directory = self._jinja_expand_string(self.config['directory'],
'url')
if directory and not os.path.exists(directory):
self.logger.debug(
'Creating directory under temporary directory: %s' %
(directory))
os.makedirs(directory, exist_ok=True)
else:
directory = './'
clone_args = {}
if 'depth' in self.config:
clone_args['depth'] = self._jinja_expand_int(
self.config['depth'], 'depth')
self.logger.info('Cloning %s to %s' % (url, directory),
extra={
**{
'url': url,
'directory': directory
},
**clone_args
})
if 'privateKey' in self.config:
private_key = self._jinja_expand_dict(self.config['privateKey'],
'private_key')
key_file = tempfile.TemporaryFile()
key_file.write(private_key['key'].encode('utf-8'))
git_ssh_cmd = 'ssh -i %s' % (key_file.name)
with Git().custom_environment(GIT_SSH_COMMAND=git_ssh_cmd):
repo = Repo.clone_from(url, directory, **clone_args)
else:
repo = Repo.clone_from(url, directory, **clone_args)
if 'branch' in self.config:
branch = self._jinja_expand_string(self.config['branch'], 'branch')
repo.git.checkout(branch)
output = {
'branch': repo.active_branch.name,
'commit': {
'id':
repo.head.commit.hexsha,
'author':
"%s <%s>" % (repo.head.commit.author.name,
repo.head.commit.author.email),
'timestamp':
repo.head.commit.committed_datetime.isoformat(),
'message':
repo.head.commit.message,
},
'directory': directory,
}
return {
output_var: output,
}