Skip to content

Commit 502bc3a

Browse files
committed
Add function for downloading git with ssh_key
1 parent f8e339c commit 502bc3a

File tree

1 file changed

+49
-27
lines changed

1 file changed

+49
-27
lines changed

src/fosslight_util/download.py

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import logging
1111
import argparse
1212
import shutil
13-
from git import Repo, GitCommandError
13+
from git import Repo, GitCommandError, Git
1414
import bz2
1515
import contextlib
1616
from datetime import datetime
@@ -124,7 +124,7 @@ def main():
124124

125125

126126
def cli_download_and_extract(link: str, target_dir: str, log_dir: str, checkout_to: str = "",
127-
compressed_only: bool = False) -> Tuple[bool, str, str, str]:
127+
compressed_only: bool = False, ssh_key: str = "") -> Tuple[bool, str, str, str]:
128128
global logger
129129

130130
success = True
@@ -152,7 +152,7 @@ def cli_download_and_extract(link: str, target_dir: str, log_dir: str, checkout_
152152
is_rubygems = src_info.get("rubygems", False)
153153

154154
# General download (git clone, wget)
155-
success_git, msg, oss_name, oss_version = download_git_clone(link, target_dir, checkout_to, tag, branch)
155+
success_git, msg, oss_name, oss_version = download_git_clone(link, target_dir, checkout_to, tag, branch, ssh_key)
156156
if (not is_rubygems) and (not success_git):
157157
if os.path.isfile(target_dir):
158158
shutil.rmtree(target_dir)
@@ -229,11 +229,37 @@ def get_github_token(git_url):
229229
return github_token
230230

231231

232-
def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch=""):
232+
def download_git_repository(refs_to_checkout, git_url, target_dir, tag):
233+
success = False
234+
oss_version = ""
235+
236+
if refs_to_checkout:
237+
try:
238+
# gitPython uses the branch argument the same whether you check out to a branch or a tag.
239+
repo = Repo.clone_from(git_url, target_dir, branch=refs_to_checkout)
240+
success = True
241+
except GitCommandError as error:
242+
logger.debug(f"Git checkout error:{error}")
243+
success = False
244+
245+
if not success:
246+
repo = Repo.clone_from(git_url, target_dir)
247+
clone_default_branch_flag = True
248+
success = True
249+
250+
if refs_to_checkout != tag or clone_default_branch_flag:
251+
oss_version = repo.active_branch.name
252+
else:
253+
oss_version = repo.git.describe('--tags')
254+
return success, oss_version
255+
256+
257+
def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch="", ssh_key=""):
233258
oss_name = get_github_ossname(git_url)
234259
refs_to_checkout = decide_checkout(checkout_to, tag, branch)
235260
clone_default_branch_flag = False
236261
msg = ""
262+
success = True
237263

238264
try:
239265
if platform.system() != "Windows":
@@ -244,36 +270,32 @@ def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch=""):
244270
alarm.start()
245271

246272
Path(target_dir).mkdir(parents=True, exist_ok=True)
247-
if refs_to_checkout != "":
248-
try:
249-
# gitPython uses the branch argument the same whether you check out to a branch or a tag.
250-
repo = Repo.clone_from(git_url, target_dir, branch=refs_to_checkout)
251-
except GitCommandError as error:
252-
error_msg = error.args[2].decode("utf-8")
253-
if "Remote branch " + refs_to_checkout + " not found in upstream origin" in error_msg:
254-
# clone default branch, when non-existent branch or tag entered
255-
repo = Repo.clone_from(git_url, target_dir)
256-
clone_default_branch_flag = True
257-
else:
258-
repo = Repo.clone_from(git_url, target_dir)
259-
clone_default_branch_flag = True
260273

261-
if refs_to_checkout != tag or clone_default_branch_flag:
262-
oss_version = repo.active_branch.name
274+
if git_url.startswith("ssh:") and not ssh_key:
275+
msg = "Private git needs ssh_key"
276+
success = False
263277
else:
264-
oss_version = repo.git.describe('--tags')
265-
logger.info(f"git checkout: {oss_version}")
278+
if ssh_key:
279+
logger.info(f"Download git with ssh_key")
280+
git_ssh_cmd = f'ssh -i {ssh_key}'
281+
with Git().custom_environment(GIT_SSH_COMMAND=git_ssh_cmd):
282+
success, oss_version = download_git_repository(refs_to_checkout, git_url, target_dir, tag)
283+
else:
284+
success, oss_version = download_git_repository(refs_to_checkout, git_url, target_dir, tag)
266285

267-
if platform.system() != "Windows":
268-
signal.alarm(0)
269-
else:
270-
del alarm
286+
logger.info(f"git checkout: {oss_version}")
287+
refs_to_checkout = oss_version
288+
289+
if platform.system() != "Windows":
290+
signal.alarm(0)
291+
else:
292+
del alarm
271293
except Exception as error:
294+
success = False
272295
logger.warning(f"git clone - failed: {error}")
273296
msg = str(error)
274-
return False, msg, oss_name, refs_to_checkout
275297

276-
return True, msg, oss_name, oss_version
298+
return success, msg, oss_name, refs_to_checkout
277299

278300

279301
def download_wget(link, target_dir, compressed_only):

0 commit comments

Comments
 (0)