Skip to content

Commit 22d90f4

Browse files
committed
obtain PR/commit diff via GitHub API rather than downloading *.diff file via github.com
1 parent 250d36a commit 22d90f4

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

easybuild/tools/github.py

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
from easybuild.framework.easyconfig.parser import EasyConfigParser
5555
from easybuild.tools import LooseVersion
5656
from easybuild.tools.build_log import EasyBuildError, EasyBuildExit, print_msg, print_warning
57-
from easybuild.tools.config import DEFAULT_DOWNLOAD_MAX_ATTEMPTS, build_option
57+
from easybuild.tools.config import build_option
5858
from easybuild.tools.filetools import apply_patch, copy_dir, copy_easyblocks, copy_file, copy_framework_files
5959
from easybuild.tools.filetools import det_patched_files, download_file, extract_file
6060
from easybuild.tools.filetools import get_easyblock_class_name, mkdir, read_file, symlink, which, write_file
@@ -548,15 +548,22 @@ def fetch_files_from_pr(pr, path=None, github_user=None, github_account=None, gi
548548
diff_url = pr_data['diff_url']
549549
diff_fn = os.path.basename(diff_url)
550550
diff_filepath = os.path.join(path, diff_fn)
551-
# max. 6 attempts + initial wait time of 10sec -> max. 10 * (2^6) = 640sec (~10min) before giving up on download
552-
# see also https://github.com/easybuilders/easybuild-framework/issues/4869
553-
max_attempts = DEFAULT_DOWNLOAD_MAX_ATTEMPTS
554-
download_file(diff_fn, diff_url, diff_filepath, forced=True, trace=False,
555-
max_attempts=max_attempts)
556-
if not os.path.exists(diff_filepath):
557-
raise EasyBuildError(f"Failed to download {diff_url}, even after {max_attempts} attempts and being patient...")
558-
diff_txt = read_file(diff_filepath)
559-
_log.debug("Diff for PR #%s:\n%s", pr, diff_txt)
551+
552+
def pr_request_fn(gh):
553+
return gh.repos[github_account][github_repo].pulls[pr]
554+
555+
# see also https://docs.github.com/en/rest/pulls/pulls#get-a-pull-request,
556+
# in particular part about media types
557+
accept_diff = {'Accept': 'application/vnd.github.diff'}
558+
status, data = github_api_get_request(pr_request_fn, github_user=github_user, headers=accept_diff)
559+
if status == HTTP_STATUS_OK:
560+
# decode from bytes to text
561+
diff_txt = data.decode()
562+
_log.debug("Diff for PR #%s:\n%s", pr, diff_txt)
563+
write_file(diff_filepath, diff_txt)
564+
else:
565+
error_msg = f"Failed to download diff for {github_account}/{github_repo} PR #{pr}! (HTTP status code: {status})"
566+
raise EasyBuildError(error_msg)
560567

561568
patched_files = det_patched_files(txt=diff_txt, omit_ab_prefix=True, github=True, filter_deleted=True)
562569
_log.debug("List of patched files for PR #%s: %s", pr, patched_files)
@@ -703,24 +710,25 @@ def fetch_files_from_commit(commit, files=None, path=None, github_account=None,
703710

704711
# if no files are specified, determine which files are touched in commit
705712
if not files:
706-
diff_url = os.path.join(GITHUB_URL, github_account, github_repo, 'commit', commit + '.diff')
707-
diff_fn = os.path.basename(diff_url)
708-
diff_filepath = os.path.join(path, diff_fn)
709-
# max. 6 attempts + initial wait time of 10sec -> max. 10 * (2^6) = 640sec (~10min) before giving up on download
710-
# see also https://github.com/easybuilders/easybuild-framework/issues/4869
711-
max_attempts = DEFAULT_DOWNLOAD_MAX_ATTEMPTS
712-
download_file(diff_fn, diff_url, diff_filepath, forced=True, trace=False,
713-
max_attempts=max_attempts)
714-
if os.path.exists(diff_filepath):
715-
diff_txt = read_file(diff_filepath)
713+
github_user = build_option('github_user')
714+
715+
def pr_request_fn(gh):
716+
return gh.repos[github_account][github_repo].pulls[pr]
717+
718+
# see also https://docs.github.com/en/rest/commits/commits#get-a-commit,
719+
# in particular part about media types
720+
accept_diff = {'Accept': 'application/vnd.github.diff'}
721+
status, data = github_api_get_request(pr_request_fn, github_user=github_user, headers=accept_diff)
722+
if status == HTTP_STATUS_OK:
723+
# decode from bytes to text
724+
diff_txt = data.decode()
716725
_log.debug("Diff for commit %s:\n%s", commit, diff_txt)
717726

718727
files = det_patched_files(txt=diff_txt, omit_ab_prefix=True, github=True, filter_deleted=True)
719728
_log.debug("List of patched files for commit %s: %s", commit, files)
720729
else:
721-
msg = f"Failed to download diff for commit {commit} of {github_account}/{github_repo} "
722-
msg += " (after {max_attempts} attempts)"
723-
raise EasyBuildError(msg, exit_code=EasyBuildExit.FAIL_GITHUB)
730+
error_msg = f"Failed to download diff for {github_account}/{github_repo} PR #{pr}! (HTTP status code: {status})"
731+
raise EasyBuildError(error_msg)
724732

725733
# download tarball for specific commit
726734
repo_commit = download_repo(repo=github_repo, commit=commit, account=github_account)

0 commit comments

Comments
 (0)