Skip to content

Commit aded444

Browse files
authored
Merge pull request #3622 from Flamefire/improveGists
mention easyblocks PR in gist when uploading test report for it + fix clean_gists.py script
2 parents 568dca1 + 4012ecb commit aded444

File tree

2 files changed

+89
-38
lines changed

2 files changed

+89
-38
lines changed

easybuild/scripts/clean_gists.py

Lines changed: 69 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@
3232
from easybuild.base.generaloption import simple_option
3333
from easybuild.base.rest import RestClient
3434
from easybuild.tools.build_log import EasyBuildError
35-
from easybuild.tools.github import GITHUB_API_URL, HTTP_STATUS_OK, GITHUB_EASYCONFIGS_REPO
35+
from easybuild.tools.github import GITHUB_API_URL, HTTP_STATUS_OK, GITHUB_EASYCONFIGS_REPO, GITHUB_EASYBLOCKS_REPO
3636
from easybuild.tools.github import GITHUB_EB_MAIN, fetch_github_token
3737
from easybuild.tools.options import EasyBuildOptions
38+
from easybuild.tools.py2vs3 import HTTPError, URLError
3839

3940
HTTP_DELETE_OK = 204
4041

@@ -49,6 +50,7 @@ def main():
4950
'closed-pr': ('Delete all gists from closed pull-requests', None, 'store_true', True, 'p'),
5051
'all': ('Delete all gists from Easybuild ', None, 'store_true', False, 'a'),
5152
'orphans': ('Delete all gists without a pull-request', None, 'store_true', False, 'o'),
53+
'dry-run': ("Only show which gists will be deleted but don't actually delete them", None, 'store_true', False),
5254
}
5355

5456
go = simple_option(options)
@@ -58,6 +60,7 @@ def main():
5860
raise EasyBuildError("Please tell me what to do?")
5961

6062
if go.options.github_user is None:
63+
EasyBuildOptions.DEFAULT_LOGLEVEL = None # Don't overwrite log level
6164
eb_go = EasyBuildOptions(envvar_prefix='EASYBUILD', go_args=[])
6265
username = eb_go.options.github_user
6366
log.debug("Fetch github username from easybuild, found: %s", username)
@@ -88,51 +91,88 @@ def main():
8891
break
8992

9093
log.info("Found %s gists", len(all_gists))
91-
regex = re.compile(r"(EasyBuild test report|EasyBuild log for failed build).*?(?:PR #(?P<PR>[0-9]+))?\)?$")
94+
re_eb_gist = re.compile(r"(EasyBuild test report|EasyBuild log for failed build)(.*?)$")
95+
re_pr_nr = re.compile(r"(EB )?PR #([0-9]+)")
9296

9397
pr_cache = {}
9498
num_deleted = 0
9599

96100
for gist in all_gists:
97101
if not gist["description"]:
98102
continue
99-
re_pr_num = regex.search(gist["description"])
100-
delete_gist = False
101-
102-
if re_pr_num:
103-
log.debug("Found a Easybuild gist (id=%s)", gist["id"])
104-
pr_num = re_pr_num.group("PR")
105-
if go.options.all:
106-
delete_gist = True
107-
elif pr_num and go.options.closed_pr:
108-
log.debug("Found Easybuild test report for PR #%s", pr_num)
109-
110-
if pr_num not in pr_cache:
111-
status, pr = gh.repos[GITHUB_EB_MAIN][GITHUB_EASYCONFIGS_REPO].pulls[pr_num].get()
103+
104+
gist_match = re_eb_gist.search(gist["description"])
105+
106+
if not gist_match:
107+
log.debug("Found a non-Easybuild gist (id=%s)", gist["id"])
108+
continue
109+
110+
log.debug("Found an Easybuild gist (id=%s)", gist["id"])
111+
112+
pr_data = gist_match.group(2)
113+
114+
pr_nrs_matches = re_pr_nr.findall(pr_data)
115+
116+
if go.options.all:
117+
delete_gist = True
118+
elif not pr_nrs_matches:
119+
log.debug("Found Easybuild test report without PR (id=%s).", gist["id"])
120+
delete_gist = go.options.orphans
121+
elif go.options.closed_pr:
122+
# All PRs must be closed
123+
delete_gist = True
124+
for pr_nr_match in pr_nrs_matches:
125+
eb_str, pr_num = pr_nr_match
126+
if eb_str or GITHUB_EASYBLOCKS_REPO in pr_data:
127+
repo = GITHUB_EASYBLOCKS_REPO
128+
else:
129+
repo = GITHUB_EASYCONFIGS_REPO
130+
131+
cache_key = "%s-%s" % (repo, pr_num)
132+
133+
if cache_key not in pr_cache:
134+
try:
135+
status, pr = gh.repos[GITHUB_EB_MAIN][repo].pulls[pr_num].get()
136+
except HTTPError as e:
137+
status, pr = e.code, e.msg
112138
if status != HTTP_STATUS_OK:
113139
raise EasyBuildError("Failed to get pull-request #%s: error code %s, message = %s",
114140
pr_num, status, pr)
115-
pr_cache[pr_num] = pr["state"]
116-
117-
if pr_cache[pr_num] == "closed":
118-
log.debug("Found report from closed PR #%s (id=%s)", pr_num, gist["id"])
119-
delete_gist = True
120-
121-
elif not pr_num and go.options.orphans:
122-
log.debug("Found Easybuild test report without PR (id=%s)", gist["id"])
123-
delete_gist = True
141+
pr_cache[cache_key] = pr["state"]
142+
143+
if pr_cache[cache_key] == "closed":
144+
log.debug("Found report from closed %s PR #%s (id=%s)", repo, pr_num, gist["id"])
145+
elif delete_gist:
146+
if len(pr_nrs_matches) > 1:
147+
log.debug("Found at least 1 PR, that is not closed yet: %s/%s (id=%s)",
148+
repo, pr_num, gist["id"])
149+
delete_gist = False
150+
else:
151+
delete_gist = True
124152

125153
if delete_gist:
126-
status, del_gist = gh.gists[gist["id"]].delete()
154+
if go.options.dry_run:
155+
log.info("DRY-RUN: Delete gist with id=%s", gist["id"])
156+
num_deleted += 1
157+
continue
158+
try:
159+
status, del_gist = gh.gists[gist["id"]].delete()
160+
except HTTPError as e:
161+
status, del_gist = e.code, e.msg
162+
except URLError as e:
163+
status, del_gist = None, e.reason
127164

128165
if status != HTTP_DELETE_OK:
129-
raise EasyBuildError("Unable to remove gist (id=%s): error code %s, message = %s",
130-
gist["id"], status, del_gist)
166+
log.warning("Unable to remove gist (id=%s): error code %s, message = %s",
167+
gist["id"], status, del_gist)
131168
else:
132-
log.info("Delete gist with id=%s", gist["id"])
169+
log.info("Deleted gist with id=%s", gist["id"])
133170
num_deleted += 1
134171

135-
log.info("Deleted %s gists", num_deleted)
172+
if go.options.dry_run:
173+
log.info("DRY-RUN: Would delete %s gists", num_deleted)
174+
else:
175+
log.info("Deleted %s gists", num_deleted)
136176

137177

138178
if __name__ == '__main__':

easybuild/tools/testing.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,22 +138,30 @@ def session_state():
138138
}
139139

140140

141-
def create_test_report(msg, ecs_with_res, init_session_state, pr_nr=None, gist_log=False):
141+
def create_test_report(msg, ecs_with_res, init_session_state, pr_nr=None, gist_log=False, easyblock_pr_nrs=None):
142142
"""Create test report for easyconfigs PR, in Markdown format."""
143143

144144
github_user = build_option('github_user')
145145
pr_target_account = build_option('pr_target_account')
146-
pr_target_repo = build_option('pr_target_repo') or GITHUB_EASYCONFIGS_REPO
146+
pr_target_repo = build_option('pr_target_repo')
147147

148148
end_time = gmtime()
149149

150150
# create a gist with a full test report
151151
test_report = []
152152
if pr_nr is not None:
153+
repo = pr_target_repo or GITHUB_EASYCONFIGS_REPO
153154
test_report.extend([
154-
"Test report for https://github.com/%s/%s/pull/%s" % (pr_target_account, pr_target_repo, pr_nr),
155+
"Test report for https://github.com/%s/%s/pull/%s" % (pr_target_account, repo, pr_nr),
155156
"",
156157
])
158+
if easyblock_pr_nrs:
159+
repo = pr_target_repo or GITHUB_EASYBLOCKS_REPO
160+
test_report.extend([
161+
"Test report for https://github.com/%s/%s/pull/%s" % (pr_target_account, repo, nr)
162+
for nr in easyblock_pr_nrs
163+
])
164+
test_report.append("")
157165
test_report.extend([
158166
"#### Test result",
159167
"%s" % msg,
@@ -184,6 +192,8 @@ def create_test_report(msg, ecs_with_res, init_session_state, pr_nr=None, gist_l
184192
descr = "(partial) EasyBuild log for failed build of %s" % ec['spec']
185193
if pr_nr is not None:
186194
descr += " (PR #%s)" % pr_nr
195+
if easyblock_pr_nrs:
196+
descr += "".join(" (easyblock PR #%s)" % nr for nr in easyblock_pr_nrs)
187197
fn = '%s_partial.log' % os.path.basename(ec['spec'])[:-3]
188198
gist_url = create_gist(partial_log_txt, fn, descr=descr, github_user=github_user)
189199
test_log = "(partial log available at %s)" % gist_url
@@ -318,20 +328,21 @@ def overall_test_report(ecs_with_res, orig_cnt, success, msg, init_session_state
318328
"""
319329
dump_path = build_option('dump_test_report')
320330
pr_nr = build_option('from_pr')
321-
eb_pr_nrs = build_option('include_easyblocks_from_pr')
331+
easyblock_pr_nrs = build_option('include_easyblocks_from_pr')
322332
upload = build_option('upload_test_report')
323333

324334
if upload:
325335
msg = msg + " (%d easyconfigs in total)" % orig_cnt
326-
test_report = create_test_report(msg, ecs_with_res, init_session_state, pr_nr=pr_nr, gist_log=True)
336+
test_report = create_test_report(msg, ecs_with_res, init_session_state, pr_nr=pr_nr, gist_log=True,
337+
easyblock_pr_nrs=easyblock_pr_nrs)
327338
if pr_nr:
328339
# upload test report to gist and issue a comment in the PR to notify
329340
txt = post_pr_test_report(pr_nr, GITHUB_EASYCONFIGS_REPO, test_report, msg, init_session_state, success)
330-
elif eb_pr_nrs:
341+
elif easyblock_pr_nrs:
331342
# upload test report to gist and issue a comment in the easyblocks PR to notify
332-
for eb_pr_nr in map(int, eb_pr_nrs):
333-
txt = post_pr_test_report(eb_pr_nr, GITHUB_EASYBLOCKS_REPO, test_report, msg, init_session_state,
334-
success)
343+
for easyblock_pr_nr in map(int, easyblock_pr_nrs):
344+
txt = post_pr_test_report(easyblock_pr_nr, GITHUB_EASYBLOCKS_REPO, test_report, msg,
345+
init_session_state, success)
335346
else:
336347
# only upload test report as a gist
337348
gist_url = upload_test_report_as_gist(test_report['full'])

0 commit comments

Comments
 (0)