-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from nuclearcat/add-checkout
feat(checkout): Add custom checkout command
- Loading branch information
Showing
5 changed files
with
158 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters