Skip to content

Commit 4b8cdef

Browse files
authored
Switch to supporting looking up at multiple repo urls when retrieving the commit date. (#4866)
1 parent 428823a commit 4b8cdef

File tree

1 file changed

+37
-49
lines changed

1 file changed

+37
-49
lines changed

scripts/dotnet.py

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,9 @@ def get_repository(repository: str) -> Tuple[str, str]:
652652

653653
@tracer.start_as_current_span("dotnet_get_commit_date") # type: ignore
654654
def get_commit_date(
655-
framework: str,
656-
commit_sha: str,
657-
repository: Optional[str] = None
655+
framework: str,
656+
commit_sha: str,
657+
repository: Optional[str] = None
658658
) -> str:
659659
'''
660660
Gets the .NET Core committer date using the GitHub Web API from the
@@ -665,58 +665,46 @@ def get_commit_date(
665665
if not commit_sha:
666666
raise ValueError('.NET Commit sha was not defined.')
667667

668-
# Example URL: https://github.com/dotnet/runtime/commit/2d76178d5faa97be86fc8d049c7dbcbdf66dc497.patch
669-
url = None
670-
fallback_url = None
668+
build_timestamp = None
669+
sleep_time = 10 # Start with 10 second sleep timer
670+
671671
if repository is None:
672-
# The origin of the repo where the commit belongs to has changed
673-
# between release. Here we attempt to naively guess the repo.
674672
core_sdk_frameworks = ChannelMap.get_supported_frameworks()
675-
repo = 'sdk' if framework in core_sdk_frameworks else 'cli'
676-
url = f'https://github.com/dotnet/{repo}/commit/{commit_sha}.patch'
677-
fallback_repo = 'core-sdk' if framework in core_sdk_frameworks else 'cli'
678-
fallback_url = f'https://github.com/dotnet/{fallback_repo}/commit/{commit_sha}.patch'
673+
urls = []
674+
675+
if framework in core_sdk_frameworks:
676+
# Try dotnet/dotnet first, then dotnet/sdk, then dotnet/core-sdk
677+
urls.append(f'https://github.com/dotnet/dotnet/commit/{commit_sha}.patch')
678+
urls.append(f'https://github.com/dotnet/sdk/commit/{commit_sha}.patch')
679+
urls.append(f'https://github.com/dotnet/core-sdk/commit/{commit_sha}.patch')
680+
else:
681+
# Fallback to cli
682+
urls.append(f'https://github.com/dotnet/cli/commit/{commit_sha}.patch')
679683
else:
680684
owner, repo = get_repository(repository)
681-
url = f'https://github.com/{owner}/{repo}/commit/{commit_sha}.patch'
682-
fallback_url = url # We don't need to try a real fallback, just use the url
685+
urls = [f'https://github.com/{owner}/{repo}/commit/{commit_sha}.patch']
683686

684-
build_timestamp = None
685-
sleep_time = 10 # Start with 10 second sleep timer
686687
for retrycount in range(5):
687-
try:
688-
with urlopen(url) as response:
689-
getLogger().info("Commit: %s", url)
690-
patch = response.read().decode('utf-8')
691-
dateMatch = search(r'^Date: (.+)$', patch, MULTILINE)
692-
if dateMatch:
693-
build_timestamp = datetime.datetime.strptime(dateMatch.group(1), '%a, %d %b %Y %H:%M:%S %z').astimezone(datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
694-
getLogger().info(f"Got UTC timestamp {build_timestamp} from {dateMatch.group(1)}")
695-
break
696-
except URLError as error:
697-
getLogger().warning(f"URL Error trying to get commit date from {url}; Reason: {error.reason}; Attempt {retrycount}")
698-
# Try using the old core-sdk URL for sdk repo failures as the commits may be from before the switch
699-
if 'Not Found' in error.reason and repo == "sdk":
700-
try:
701-
getLogger().warning(f"Trying fallback URL {fallback_url}")
702-
with urlopen(fallback_url) as response:
703-
getLogger().info("Commit: %s", url)
704-
patch = response.read().decode('utf-8')
705-
dateMatch = search(r'^Date: (.+)$', patch, MULTILINE)
706-
if dateMatch:
707-
build_timestamp = datetime.datetime.strptime(dateMatch.group(1), '%a, %d %b %Y %H:%M:%S %z').astimezone(datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
708-
getLogger().info(f"Got UTC timestamp {build_timestamp} from {dateMatch.group(1)}")
709-
break
710-
except URLError as error_fallback:
711-
getLogger().warning(f"URL Error trying to get commit date from {fallback_url}; Reason: {error_fallback.reason}; Attempt {retrycount}")
712-
713-
sleep(sleep_time)
714-
sleep_time = sleep_time * 2
715-
716-
if not build_timestamp:
717-
raise RuntimeError(
718-
'Could not get timestamp for commit %s' % commit_sha)
719-
return build_timestamp
688+
for url in urls:
689+
try:
690+
with urlopen(url) as response:
691+
getLogger().info("Commit: %s", url)
692+
patch = response.read().decode('utf-8')
693+
dateMatch = search(r'^Date: (.+)$', patch, MULTILINE)
694+
if dateMatch:
695+
build_timestamp = datetime.datetime.strptime(
696+
dateMatch.group(1), '%a, %d %b %Y %H:%M:%S %z'
697+
).astimezone(datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
698+
getLogger().info(f"Got UTC timestamp {build_timestamp} from {dateMatch.group(1)}")
699+
return build_timestamp
700+
except URLError as error:
701+
getLogger().warning(f"URL Error trying to get commit date from {url}; Reason: {error.reason}; Attempt {retrycount}")
702+
except Exception as error:
703+
getLogger().warning(f"Error trying to get commit date from {url}; {type(error).__name__}: {error}; Attempt {retrycount}")
704+
sleep(sleep_time)
705+
sleep_time = sleep_time * 2
706+
707+
raise RuntimeError(f'Could not get timestamp for commit {commit_sha}')
720708

721709
def get_project_name(csproj_file: str) -> str:
722710
'''

0 commit comments

Comments
 (0)