Skip to content

Commit

Permalink
Merge pull request #24 from nuclearcat/add-checkout
Browse files Browse the repository at this point in the history
feat(checkout): Add custom checkout command
  • Loading branch information
aliceinwire authored Oct 7, 2024
2 parents a2a7eaf + 5e1ff87 commit 0b3a8f5
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 1 deletion.
21 changes: 21 additions & 0 deletions docs/checkout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
### checkout

This command allow to test arbitary commit on the KernelCI Pipeline instance. This might be useful in several cases:
- You want to test a specific commit, if it fails or pass test, or introduce any other degradation comparing to the current, or another commit.
- You want to create snapshot of the test results on specific tags (releases, etc).
- Use this command for regression bisection

This command can execute all tests configured for particular tree/branch, or you can provide jobfilter to execute specific tests and builds.

Example:
```sh
kci-dev checkout --giturl https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git --branch master --commit f06021a18fcf8d8a1e79c5e0a8ec4eb2b038e153 --jobfilter "kbuild-gcc-12-x86"
```

Where:
- `giturl` is the URL of the git repository to test.
- `branch` is the branch of the git repository to test.
- `commit` is the commit hash to test.
- `jobfilter` is the job filter to use for the test (optional parameter)

You can also set instead of --commit option --tipoftree which will retrieve the latest commit of the tree.
4 changes: 4 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ kci-dev --settings /path/to/.kci-dev.toml

## Commands

### checkout

- [checkout](checkout.md)

### testretry

- [testretry](testretry.md)
Expand Down
3 changes: 2 additions & 1 deletion kci-dev/kci-dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import click
from libs.common import *
from subcommands import commit, patch, results, testretry
from subcommands import checkout, commit, patch, results, testretry


@click.group(
Expand All @@ -29,6 +29,7 @@ def cli(ctx, settings, instance):


def run():
cli.add_command(checkout.checkout)
cli.add_command(commit.commit)
cli.add_command(patch.patch)
cli.add_command(results.results)
Expand Down
120 changes: 120 additions & 0 deletions kci-dev/subcommands/checkout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json
import re
import subprocess

import click
import requests
from git import Repo
from libs.common import *


def api_connection(host):
click.secho("api connect: " + host, fg="green")
return host


def display_api_error(response):
click.secho(f"API response error code: {response.status_code}", fg="red")
try:
click.secho(response.json(), fg="red")
except json.decoder.JSONDecodeError:
click.secho(f"No JSON response. Plain text: {response.text}", fg="yellow")
return


def send_checkout_full(baseurl, token, **kwargs):
url = baseurl + "api/checkout"
headers = {
"Content-Type": "application/json; charset=utf-8",
"Authorization": f"{token}",
}
data = {
"url": kwargs["giturl"],
"branch": kwargs["branch"],
"commit": kwargs["commit"],
"jobfilter": kwargs["jobfilter"],
}
jdata = json.dumps(data)
try:
response = requests.post(url, headers=headers, data=jdata)
except requests.exceptions.RequestException as e:
click.secho(f"API connection error: {e}", fg="red")
return

if response.status_code != 200:
display_api_error(response)
return None
return response.json()


def retrieve_tot_commit(repourl, branch):
"""
Retrieve the latest commit on a branch
Unfortunately, gitpython does not support fetching the latest commit
on a branch without having to clone the repo.
"""
process = subprocess.Popen(
["git", "ls-remote", repourl, f"refs/heads/{branch}"], stdout=subprocess.PIPE
)
stdout, stderr = process.communicate()
sha = re.split(r"\t+", stdout.decode("ascii"))[0]
return sha


@click.command(help="Create custom tree checkout on KernelCI and trigger a tests")
@click.option(
"--giturl",
help="Git URL to checkout",
required=True,
)
@click.option(
"--branch",
help="Branch to checkout",
required=True,
)
@click.option(
"--commit",
help="Commit to checkout",
)
@click.option(
"--tipoftree",
help="Checkout on latest commit on tree/branch",
is_flag=True,
)
# jobfilter is a list, might be one or more jobs
@click.option(
"--jobfilter",
help="Job filter to trigger",
multiple=True,
)
@click.pass_context
def checkout(ctx, giturl, branch, commit, jobfilter, tipoftree):
cfg = ctx.obj.get("CFG")
instance = ctx.obj.get("INSTANCE")
url = api_connection(cfg[instance]["host"])
token = cfg[instance]["token"]
if not jobfilter:
jobfilter = None
click.secho("No job filter defined. All jobs will be triggered!", fg="yellow")
if not commit and not tipoftree:
click.secho("No commit or tree/branch latest commit defined", fg="red")
return
if tipoftree:
click.secho(
f"Retrieving latest commit on tree: {giturl} branch: {branch}", fg="green"
)
commit = retrieve_tot_commit(giturl, branch)
click.secho(f"Commit to checkout: {commit}", fg="green")
resp = send_checkout_full(
url, token, giturl=giturl, branch=branch, commit=commit, jobfilter=jobfilter
)
if resp and "message" in resp:
click.secho(resp["message"], fg="green")


if __name__ == "__main__":
main_kcidev()
11 changes: 11 additions & 0 deletions tests/test_kcidev.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ def test_kcidev_testretry_help():
assert result.returncode == 0


def test_kcidev_checkout_help():
command = ["poetry", "run", "kci-dev", "checkout", "--help"]
result = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True)
print("returncode: " + str(result.returncode))
print("#### stdout ####")
print(result.stdout)
print("#### stderr ####")
print(result.stderr)
assert result.returncode == 0


def test_kcidev_results_tests():
command = [
"poetry",
Expand Down

0 comments on commit 0b3a8f5

Please sign in to comment.