Skip to content

Commit

Permalink
mach: Make ./mach try a little friendlier (servo#31290)
Browse files Browse the repository at this point in the history
1. Move `./mach try` to `testing_commands.py which is a bit more
   consistent.
2. Make `./mach try` print out the remote name always and properly
   form the URL for ssh repositories.
3. Print out the try configuration matrix to make it more obvious
   what is being triggered remotely.
4. Better error handling. Print and error and exit if the remote isn't
   on GitHub and also clean up properly if something fails after making
   the temporary commit.
  • Loading branch information
mrobinson authored and Taym95 committed Feb 11, 2024
1 parent 59f16d6 commit c2f0e26
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 37 deletions.
35 changes: 0 additions & 35 deletions python/servo/devenv_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
)

from servo.command_base import CommandBase, cd, call
from servo.try_parser import Config


@CommandProvider
Expand Down Expand Up @@ -272,37 +271,3 @@ def ndk_gdb(self, release, target):
"--verbose",
], env=env)
return p.wait()

@Command('try',
description='Runs try jobs by force pushing to try branch',
category='devenv')
@CommandArgument(
'--remote', '-r', default="origin",
help='git remote to use for try pushes')
@CommandArgument(
'try_string', default=None, nargs='...',
help="Try string")
def try_jobs(self, remote="origin", try_string=None):
if not try_string:
try_string = "full"
else:
try_string = " ".join(try_string)
conf = Config(try_string)
result = call(["git", "commit", "--allow-empty", "-m", try_string, "-m", f"{conf.to_json()}"])
if result != 0:
return result

git_remote = subprocess.check_output(["git", "config", "--get", f"remote.{remote}.url"]).decode()
if "servo/servo" in git_remote:
print("WARNING: You are triggering try build in upstream repo!")

result = call(["git", "push", remote, "--force", "HEAD:try"])
if result != 0:
return result

git_remote = git_remote.replace(".git", "/actions")
print(f"You can find triggered workflow here: {git_remote}")

# Remove the last commit which only contains the try configuration.
result += call(["git", "reset", "--soft", "HEAD~1"])
return result
50 changes: 50 additions & 0 deletions python/servo/testing_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import os.path as path
import shutil
import subprocess
import textwrap

import wpt
import wpt.manifestupdate
Expand All @@ -27,6 +28,7 @@
Command,
)

import servo.try_parser
import tidy

from servo.command_base import BuildType, CommandBase, call, check_call
Expand Down Expand Up @@ -747,3 +749,51 @@ def smoketest(self, build_type: BuildType, params):
# of panics won't cause timeouts on CI.
return self.context.commands.dispatch('run', self.context, build_type=build_type,
params=params + ['-f', 'tests/html/close-on-load.html'])

@Command('try', description='Runs try jobs by force pushing to try branch', category='testing')
@CommandArgument('--remote', '-r', default="origin", help='A git remote to run the try job on')
@CommandArgument('try_strings', default=["full"], nargs='...',
help="A list of try strings specifying what kind of job to run.")
def try_command(self, remote: str, try_strings: list[str]):
remote_url = subprocess.check_output(["git", "config", "--get", f"remote.{remote}.url"]).decode().strip()
if "github.com" not in remote_url:
print(f"The remote provided ({remote_url}) isn't a GitHub remote.")
return 1

try_string = " ".join(try_strings)
config = servo.try_parser.Config(try_string)
print(f"Trying on {remote} ({remote_url}) with following configuration:")
print()
print(textwrap.indent(config.to_json(indent=2), prefix=" "))
print()

# The commit message is composed of both the last commit message and the try string.
commit_message = subprocess.check_output(["git", "show", "-s", "--format=%s"]).decode().strip()
commit_message = f"{commit_message} ({try_string})"

result = call(["git", "commit", "--quiet", "--allow-empty", "-m", commit_message, "-m", f"{config.to_json()}"])
if result != 0:
return result

# From here on out, we need to always clean up the commit we added to the branch.
try:
result = call(["git", "push", "--quiet", remote, "--force", "HEAD:try"])
if result != 0:
return result

# TODO: This is a pretty naive approach to turning a GitHub remote URL (either SSH or HTTPS)
# into a URL to the Actions page. It might be better to create this action with the `gh`
# tool and get the real URL.
actions_url = remote_url.replace(".git", "/actions")
if not actions_url.startswith("https"):
actions_url = actions_url.replace(':', '/')
actions_url = actions_url.replace("git@", "")
actions_url = f"https://{actions_url}"
print(f"Actions available at: {actions_url}")

finally:
# Remove the last commit which only contains the try configuration.
result = call(["git", "reset", "--quiet", "--soft", "HEAD~1"])
if result != 0:
print("Could not clean up try commit. Sorry! Please try to reset to the previous commit.")
return result
4 changes: 2 additions & 2 deletions python/servo/try_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ def parse(self, input: str):
else:
self.matrix.append(preset)

def to_json(self) -> str:
return json.dumps(self, cls=Encoder)
def to_json(self, **kwargs) -> str:
return json.dumps(self, cls=Encoder, **kwargs)


def main():
Expand Down

0 comments on commit c2f0e26

Please sign in to comment.