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 9, 2025
1 parent da06336 commit f974964
Showing 1 changed file with 64 additions and 5 deletions.
69 changes: 64 additions & 5 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 @@ -169,6 +203,10 @@ def cmd_builds(data, commit, download_logs, status):
"--branch",
help="Branch to get results for",
)
@click.option(
"--git-folder",
help="Path of git repository folder",
)
@click.option(
"--commit",
help="Commit or tag to get results for",
Expand All @@ -195,11 +233,27 @@ 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,
git_folder,
giturl,
branch,
commit,
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()
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
if latest:
commit = get_latest_commit(origin, giturl, branch)
data = fetch_full_results(origin, giturl, branch, commit)
Expand All @@ -208,8 +262,13 @@ def results(ctx, origin, giturl, branch, commit, action, download_logs, latest,
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()
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
if latest:
commit = get_latest_commit(origin, giturl, branch)
data = fetch_full_results(origin, giturl, branch, commit)
Expand Down

0 comments on commit f974964

Please sign in to comment.