Skip to content

Commit 1803d49

Browse files
Tarun-Arorathealphadollarcanihavesomecoffee
authored
[IMPROVEMENT] Run Tests Only on those platforms whose Build is triggered (#674)
* Run tests only on that platform on which build is triggered * Split unit tests for windows and linux * Add unit test for case workflow run completed but pr already updated Co-authored-by: Shivam Kumar Jha <code@thealphadollar.me> Co-authored-by: Willem <github@canihavesome.coffee>
1 parent 2dc41fd commit 1803d49

File tree

2 files changed

+230
-74
lines changed

2 files changed

+230
-74
lines changed

mod_ci/controllers.py

Lines changed: 69 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -509,17 +509,18 @@ def schedule_test(gh_commit, commit, test_type, branch="master", pr_nr=0) -> Non
509509
log.critical(f'Could not post to GitHub! Response: {a.response}')
510510

511511

512-
def deschedule_test(gh_commit, commit, test_type, message="Tests have been cancelled", branch="master", pr_nr=0,
513-
state=Status.FAILURE) -> None:
512+
def deschedule_test(gh_commit, platform, message="Tests have been cancelled", state=Status.FAILURE) -> None:
514513
"""
515514
Post status to GitHub (default: as failure due to Github Actions incompletion).
516515
517516
:param gh_commit: The GitHub API call for the commit. Can be None
518517
:type gh_commit: Any
519518
:param commit: The commit hash.
520519
:type commit: str
521-
:param test_type: The type of test
522-
:type test_type: TestType
520+
:param platform: The platform name
521+
:type platform: TestPlatform
522+
:param message: The message to be posted to GitHub
523+
:type message: str
523524
:param branch: Branch name
524525
:type branch: str
525526
:param pr_nr: Pull Request number, if applicable.
@@ -530,29 +531,28 @@ def deschedule_test(gh_commit, commit, test_type, message="Tests have been cance
530531
from run import log
531532

532533
if gh_commit is not None:
533-
for platform in TestPlatform:
534-
try:
535-
gh_commit.post(
536-
state=state,
537-
description=message,
538-
context=f"CI - {platform.value}",
539-
)
540-
except ApiError as a:
541-
log.critical(f'Could not post to GitHub! Response: {a.response}')
534+
try:
535+
gh_commit.post(
536+
state=state,
537+
description=message,
538+
context=f"CI - {platform.value}",
539+
)
540+
except ApiError as a:
541+
log.critical(f'Could not post to GitHub! Response: {a.response}')
542542

543543

544-
def queue_test(db, gh_commit, commit, test_type, branch="master", pr_nr=0) -> None:
544+
def queue_test(gh_commit, commit, test_type, platform, branch="master", pr_nr=0) -> None:
545545
"""
546546
Store test details into Test model for each platform, and post the status to GitHub.
547547
548-
:param db: Database connection.
549-
:type db: sqlalchemy.orm.scoped_session
550548
:param gh_commit: The GitHub API call for the commit. Can be None
551549
:type gh_commit: Any
552550
:param commit: The commit hash.
553551
:type commit: str
554552
:param test_type: The type of test
555553
:type test_type: TestType
554+
:param platform: The platform name
555+
:type platform: TestPlatform
556556
:param branch: Branch name
557557
:type branch: str
558558
:param pr_nr: Pull Request number, if applicable.
@@ -569,38 +569,25 @@ def queue_test(db, gh_commit, commit, test_type, branch="master", pr_nr=0) -> No
569569
log.debug('pull request test type detected')
570570
branch = "pull_request"
571571

572-
linux_test = Test.query.filter(and_(Test.platform == TestPlatform.linux,
573-
Test.commit == commit,
574-
Test.fork_id == fork.id,
575-
Test.test_type == test_type,
576-
Test.branch == branch,
577-
Test.pr_nr == pr_nr
578-
)).first()
579-
windows_test = Test.query.filter(and_(Test.platform == TestPlatform.windows,
580-
Test.commit == commit,
581-
Test.fork_id == fork.id,
582-
Test.test_type == test_type,
583-
Test.branch == branch,
584-
Test.pr_nr == pr_nr
585-
)).first()
586-
add_customized_regression_tests(linux_test.id)
587-
add_customized_regression_tests(windows_test.id)
572+
platform_test = Test.query.filter(and_(Test.platform == platform,
573+
Test.commit == commit,
574+
Test.fork_id == fork.id,
575+
Test.test_type == test_type,
576+
Test.branch == branch,
577+
Test.pr_nr == pr_nr
578+
)).first()
579+
add_customized_regression_tests(platform_test.id)
588580

589581
if gh_commit is not None:
590-
status_entries = {
591-
linux_test.platform.value: linux_test.id,
592-
windows_test.platform.value: windows_test.id
593-
}
594-
for platform_name, test_id in status_entries.items():
595-
try:
596-
gh_commit.post(
597-
state=Status.PENDING,
598-
description="Tests queued",
599-
context=f"CI - {platform_name}",
600-
target_url=url_for('test.by_id', test_id=test_id, _external=True)
601-
)
602-
except ApiError as a:
603-
log.critical(f'Could not post to GitHub! Response: {a.response}')
582+
try:
583+
gh_commit.post(
584+
state=Status.PENDING,
585+
description="Tests queued",
586+
context=f"CI - {platform_test.platform.value}",
587+
target_url=url_for('test.by_id', test_id=platform_test.id, _external=True)
588+
)
589+
except ApiError as a:
590+
log.critical(f'Could not post to GitHub! Response: {a.response}')
604591

605592
log.debug("Created tests, waiting for cron...")
606593

@@ -844,24 +831,26 @@ def start_ci():
844831
if workflow['conclusion'] != "success":
845832
has_failed = True
846833
break
847-
if workflow['name'] == "Build CCExtractor on Linux":
834+
if workflow['name'] == Workflow_builds.LINUX:
848835
builds["linux"] = True
849-
elif workflow['name'] == "Build CCExtractor on Windows":
836+
elif workflow['name'] == Workflow_builds.WINDOWS:
850837
builds["windows"] = True
851838
elif workflow['status'] != "completed":
852839
is_complete = False
853840
break
854841

855842
if has_failed:
856843
# no runs to be scheduled since build failed
857-
deschedule_test(github_status, commit_hash, TestType.commit,
844+
deschedule_test(github_status, TestPlatform.linux,
845+
message="Cancelling tests as Github Action(s) failed")
846+
deschedule_test(github_status, TestPlatform.windows,
858847
message="Cancelling tests as Github Action(s) failed")
859848
elif is_complete:
860849
if payload['workflow_run']['event'] == "pull_request":
861850
# In case of pull request run tests only if it is still in an open state
862851
# and user is not blacklisted
863852
for pull_request in repository.pulls.get(state="open"):
864-
if pull_request['head']['sha'] == commit_hash and any(builds.values()):
853+
if pull_request['head']['sha'] == commit_hash:
865854
user_id = pull_request['user']['id']
866855
if BlockedUsers.query.filter(BlockedUsers.user_id == user_id).first() is not None:
867856
g.log.warning("User Blacklisted")
@@ -872,13 +861,37 @@ def start_ci():
872861
target_url=url_for('home.index', _external=True)
873862
)
874863
return 'ERROR'
875-
queue_test(g.db, github_status, commit_hash,
876-
TestType.pull_request, pr_nr=pull_request['number'])
877-
elif any(builds.values()):
878-
queue_test(g.db, github_status, commit_hash, TestType.commit)
864+
if builds['linux']:
865+
queue_test(github_status, commit_hash, TestType.pull_request,
866+
TestPlatform.linux, pr_nr=pull_request['number'])
867+
else:
868+
deschedule_test(github_status, TestPlatform.linux,
869+
message="Not ran - no code changes", state=Status.SUCCESS)
870+
if builds['windows']:
871+
queue_test(github_status, commit_hash, TestType.pull_request,
872+
TestPlatform.windows, pr_nr=pull_request['number'])
873+
else:
874+
deschedule_test(github_status, TestPlatform.windows,
875+
message="Not ran - no code changes", state=Status.SUCCESS)
876+
return json.dumps({'msg': 'EOL'})
877+
# Either PR head commit was updated or PR was closed, therefore cancelling tests
878+
deschedule_test(github_status, TestPlatform.linux,
879+
message="Tests cancelled", state=Status.FAILURE)
880+
deschedule_test(github_status, TestPlatform.windows,
881+
message="Tests cancelled", state=Status.FAILURE)
879882
else:
880-
deschedule_test(github_status, commit_hash, TestType.commit,
881-
message="Not ran - no code changes", state=Status.SUCCESS)
883+
if builds['linux']:
884+
queue_test(github_status, commit_hash,
885+
TestType.commit, TestPlatform.linux)
886+
else:
887+
deschedule_test(github_status, TestPlatform.linux,
888+
message="Not ran - no code changes", state=Status.SUCCESS)
889+
if builds['windows']:
890+
queue_test(github_status, commit_hash,
891+
TestType.commit, TestPlatform.windows)
892+
else:
893+
deschedule_test(github_status, TestPlatform.windows,
894+
message="Not ran - no code changes", state=Status.SUCCESS)
882895
elif payload['action'] == 'requested':
883896
schedule_test(github_status, commit_hash, TestType.commit)
884897
else:

0 commit comments

Comments
 (0)