Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix posting of comment in PR with --upload-test-report #3272

Merged
merged 1 commit into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions easybuild/tools/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,9 @@ def fetch_files_from_pr(pr, path=None, github_user=None, github_repo=None):

def create_gist(txt, fn, descr=None, github_user=None, github_token=None):
"""Create a gist with the provided text."""

dry_run = build_option('dry_run') or build_option('extended_dry_run')

if descr is None:
descr = "(none)"

Expand All @@ -508,8 +511,12 @@ def create_gist(txt, fn, descr=None, github_user=None, github_token=None):
}
}
}
g = RestClient(GITHUB_API_URL, username=github_user, token=github_token)
status, data = g.gists.post(body=body)

if dry_run:
status, data = HTTP_STATUS_CREATED, {'html_url': 'https://gist.github.com/DRY_RUN'}
else:
g = RestClient(GITHUB_API_URL, username=github_user, token=github_token)
status, data = g.gists.post(body=body)

if status != HTTP_STATUS_CREATED:
raise EasyBuildError("Failed to create gist; status %s, data: %s", status, data)
Expand Down
14 changes: 7 additions & 7 deletions easybuild/tools/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from easybuild.tools.build_log import EasyBuildError
from easybuild.tools.config import build_option
from easybuild.tools.filetools import find_easyconfigs, mkdir, read_file, write_file
from easybuild.tools.github import create_gist, post_comment_in_issue
from easybuild.tools.github import GITHUB_EASYCONFIGS_REPO, create_gist, post_comment_in_issue
from easybuild.tools.jenkins import aggregate_xml_in_dirs
from easybuild.tools.parallelbuild import build_easyconfigs_in_parallel
from easybuild.tools.robot import resolve_dependencies
Expand Down Expand Up @@ -143,7 +143,7 @@ def create_test_report(msg, ecs_with_res, init_session_state, pr_nr=None, gist_l

github_user = build_option('github_user')
pr_target_account = build_option('pr_target_account')
pr_target_repo = build_option('pr_target_repo')
pr_target_repo = build_option('pr_target_repo') or GITHUB_EASYCONFIGS_REPO

end_time = gmtime()

Expand Down Expand Up @@ -252,10 +252,13 @@ def post_easyconfigs_pr_test_report(pr_nr, test_report, msg, init_session_state,
"""Post test report in a gist, and submit comment in easyconfigs PR."""

github_user = build_option('github_user')
pr_target_account = build_option('pr_target_account')
pr_target_repo = build_option('pr_target_repo') or GITHUB_EASYCONFIGS_REPO

# create gist with test report
descr = "EasyBuild test report for easyconfigs PR #%s" % pr_nr
fn = 'easybuild_test_report_easyconfigs_pr%s_%s.md' % (pr_nr, strftime("%Y%M%d-UTC-%H-%M-%S", gmtime()))
descr = "EasyBuild test report for %s/%s PR #%s" % (pr_target_account, pr_target_repo, pr_nr)
timestamp = strftime("%Y%M%d-UTC-%H-%M-%S", gmtime())
fn = 'easybuild_test_report_%s_%s_pr%s_%s.md' % (pr_nr, pr_target_account, pr_target_repo, timestamp)
gist_url = upload_test_report_as_gist(test_report, descr=descr, fn=fn)

# post comment to report test result
Expand Down Expand Up @@ -283,9 +286,6 @@ def post_easyconfigs_pr_test_report(pr_nr, test_report, msg, init_session_state,
]
comment = '\n'.join(comment_lines)

pr_target_account = build_option('pr_target_account')
pr_target_repo = build_option('pr_target_repo')

post_comment_in_issue(pr_nr, comment, account=pr_target_account, repo=pr_target_repo, github_user=github_user)

msg = "Test report uploaded to %s and mentioned in a comment in easyconfigs PR#%s" % (gist_url, pr_nr)
Expand Down
34 changes: 34 additions & 0 deletions test/framework/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from easybuild.tools.configobj import ConfigObj
from easybuild.tools.filetools import read_file, write_file
from easybuild.tools.github import VALID_CLOSE_PR_REASONS
from easybuild.tools.testing import post_easyconfigs_pr_test_report, session_state
from easybuild.tools.py2vs3 import HTTPError, URLError, ascii_letters
import easybuild.tools.github as gh

Expand Down Expand Up @@ -778,6 +779,39 @@ def test_push_branch_to_github(self):
regex = re.compile(pattern)
self.assertTrue(regex.match(stdout.strip()), "Pattern '%s' doesn't match: %s" % (regex.pattern, stdout))

def test_post_easyconfigs_pr_test_report(self):
"""Test for post_easyconfigs_pr_test_report function."""
if self.skip_github_tests:
print("Skipping test_post_easyconfigs_pr_test_report, no GitHub token available?")
return

init_config(build_options={
'dry_run': True,
'github_user': GITHUB_TEST_ACCOUNT,
})

test_report = os.path.join(self.test_prefix, 'test_report.txt')
write_file(test_report, "This is a test report!")

init_session_state = session_state()

self.mock_stderr(True)
self.mock_stdout(True)
post_easyconfigs_pr_test_report('1234', test_report, "OK!", init_session_state, True)
stderr, stdout = self.get_stderr(), self.get_stdout()
self.mock_stderr(False)
self.mock_stdout(False)

self.assertEqual(stderr, '')

patterns = [
r"^\[DRY RUN\] Adding comment to easybuild-easyconfigs issue #1234: 'Test report by @easybuild_test",
r"^See https://gist.github.com/DRY_RUN for a full test report.'",
]
for pattern in patterns:
regex = re.compile(pattern, re.M)
self.assertTrue(regex.search(stdout), "Pattern '%s' should be found in: %s" % (regex.pattern, stdout))


def suite():
""" returns all the testcases in this module """
Expand Down