Skip to content

Commit

Permalink
results: Use current folder repository informations for getting results
Browse files Browse the repository at this point in the history
Signed-off-by: Arisu Tachibana <[email protected]>
  • Loading branch information
aliceinwire committed Jan 8, 2025
1 parent da06336 commit 246920b
Showing 1 changed file with 83 additions and 11 deletions.
94 changes: 83 additions & 11 deletions kcidev/subcommands/results.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import configparser
import gzip
import json
import os
import subprocess
import urllib

import click
Expand Down Expand Up @@ -44,6 +47,37 @@ def fetch_tree_fast(origin):
return fetch_from_api("tree-fast", params)


def get_folder_repository():
current_folder = os.getcwd()
dot_git_folder = os.path.join(current_folder, ".git")

# Check if we are in a git repository
if os.path.exists(dot_git_folder):
# Get remote origin url
kci_msg("git folder")
git_config_path = os.path.join(dot_git_folder, "config")
git_config = configparser.ConfigParser()
git_config.read(git_config_path)
git_url = git_config.get('remote "origin"', "url")
# Get current branch name
process = subprocess.Popen(
["git", "branch", "--show-current"], stdout=subprocess.PIPE, text=True
)
branch_name, branch_error = process.communicate()
branch_name = branch_name.strip()

# Get last commit hash
last_commit_hash_path = os.path.join(
dot_git_folder, "refs", "heads", branch_name
)
last_commit_hash = open(last_commit_hash_path, "r").read()

return git_url, branch_name, last_commit_hash
else:
kci_err("Not a GIT folder")
pass


def get_latest_commit(origin, giturl, branch):
trees = fetch_tree_fast(origin)
for t in trees:
Expand Down Expand Up @@ -173,6 +207,11 @@ def cmd_builds(data, commit, download_logs, status):
"--commit",
help="Commit or tag to get results for",
)
@click.option(
"--current-folder",
is_flag=True,
help="Get results for current folder git repository",
)
@click.option(
"--action",
type=click.Choice(["summary", "trees", "builds"], case_sensitive=True),
Expand All @@ -195,23 +234,56 @@ def cmd_builds(data, commit, download_logs, status):
default="all",
)
@click.pass_context
def results(ctx, origin, giturl, branch, commit, action, download_logs, latest, status):
def results(
ctx,
origin,
giturl,
branch,
commit,
current_folder,
action,
download_logs,
latest,
status,
):
if action == None or action == "summary":
if not giturl or not branch or not ((commit != None) ^ latest):
kci_err("--giturl AND --branch AND (--commit XOR --latest) are required")
raise click.Abort()
if latest:
commit = get_latest_commit(origin, giturl, branch)
if not current_folder:
if not giturl or not branch or not ((commit != None) ^ latest):
kci_err(
"--giturl AND --branch AND (--commit XOR --latest) are required"
)
raise click.Abort()
if latest:
commit = get_latest_commit(origin, giturl, branch)
if current_folder:
git_url, branch_name, last_commit_hash = get_folder_repository()
kci_msg(git_url)
kci_msg(branch_name)
kci_msg(last_commit_hash)
giturl = git_url
branch = branch_name
commit = last_commit_hash
data = fetch_full_results(origin, giturl, branch, commit)
cmd_summary(data)
elif action == "trees":
cmd_list_trees(origin)
elif action == "builds":
if not giturl or not branch or not ((commit != None) ^ latest):
kci_err("--giturl AND --branch AND (--commit XOR --latest) are required")
raise click.Abort()
if latest:
commit = get_latest_commit(origin, giturl, branch)
if not current_folder:
if not giturl or not branch or not ((commit != None) ^ latest):
kci_err(
"--giturl AND --branch AND (--commit XOR --latest) are required"
)
raise click.Abort()
if latest:
commit = get_latest_commit(origin, giturl, branch)
if current_folder:
git_url, branch_name, last_commit_hash = get_folder_repository()
kci_msg(git_url)
kci_msg(branch_name)
kci_msg(last_commit_hash)
giturl = git_url
branch = branch_name
commit = last_commit_hash
data = fetch_full_results(origin, giturl, branch, commit)
cmd_builds(data, commit, download_logs, status)
else:
Expand Down

0 comments on commit 246920b

Please sign in to comment.