Skip to content

Commit 55799bb

Browse files
authored
Merge pull request #201 from fosslight/git
Add params for cloning private git
2 parents 2dfda86 + 5bffd9d commit 55799bb

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/fosslight_util/download.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ def parse_src_link(src_link):
9393

9494

9595
def cli_download_and_extract(link: str, target_dir: str, log_dir: str, checkout_to: str = "",
96-
compressed_only: bool = False, ssh_key: str = "") -> Tuple[bool, str, str, str]:
96+
compressed_only: bool = False, ssh_key: str = "",
97+
id: str = "", git_token: str = "") -> Tuple[bool, str, str, str]:
9798
global logger
9899

99100
success = True
@@ -121,7 +122,8 @@ def cli_download_and_extract(link: str, target_dir: str, log_dir: str, checkout_
121122
is_rubygems = src_info.get("rubygems", False)
122123

123124
# General download (git clone, wget)
124-
success_git, msg, oss_name, oss_version = download_git_clone(link, target_dir, checkout_to, tag, branch, ssh_key)
125+
success_git, msg, oss_name, oss_version = download_git_clone(link, target_dir, checkout_to,
126+
tag, branch, ssh_key, id, git_token)
125127
link = change_ssh_link_to_https(link)
126128
if (not is_rubygems) and (not success_git):
127129
if os.path.isfile(target_dir):
@@ -203,6 +205,8 @@ def download_git_repository(refs_to_checkout, git_url, target_dir, tag):
203205
success = False
204206
oss_version = ""
205207
clone_default_branch_flag = False
208+
209+
logger.info(f"Download git url :{git_url}")
206210
if refs_to_checkout:
207211
try:
208212
# gitPython uses the branch argument the same whether you check out to a branch or a tag.
@@ -224,7 +228,7 @@ def download_git_repository(refs_to_checkout, git_url, target_dir, tag):
224228
return success, oss_version
225229

226230

227-
def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch="", ssh_key=""):
231+
def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch="", ssh_key="", id="", git_token=""):
228232
oss_name = get_github_ossname(git_url)
229233
refs_to_checkout = decide_checkout(checkout_to, tag, branch)
230234
msg = ""
@@ -250,6 +254,14 @@ def download_git_clone(git_url, target_dir, checkout_to="", tag="", branch="", s
250254
with Git().custom_environment(GIT_SSH_COMMAND=git_ssh_cmd):
251255
success, oss_version = download_git_repository(refs_to_checkout, git_url, target_dir, tag)
252256
else:
257+
if id and git_token:
258+
try:
259+
m = re.match(r"^(ht|f)tp(s?)\:\/\/", git_url)
260+
protocol = m.group()
261+
if protocol:
262+
git_url = git_url.replace(protocol, f"{protocol}{id}:{git_token}@")
263+
except Exception as error:
264+
logger.info(f"Failed to insert id, token to git url:{error}")
253265
success, oss_version = download_git_repository(refs_to_checkout, git_url, target_dir, tag)
254266

255267
logger.info(f"git checkout: {oss_version}")

0 commit comments

Comments
 (0)