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 151167e
Showing 1 changed file with 74 additions and 5 deletions.
79 changes: 74 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,45 @@ def fetch_tree_fast(origin):
return fetch_from_api("tree-fast", params)


def get_folder_repository(git_folder):
kci_msg("git folder: " + str(git_folder))
if git_folder:
current_folder = git_folder
else:
current_folder = os.getcwd()

previous_folder = os.getcwd()
os.chdir(current_folder)
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
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()

os.chdir(previous_folder)
return git_url, branch_name, last_commit_hash
else:
os.chdir(previous_folder)
kci_err("Not a GIT folder")
raise click.Abort()


def get_latest_commit(origin, giturl, branch):
trees = fetch_tree_fast(origin)
for t in trees:
Expand Down Expand Up @@ -169,6 +211,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 +241,28 @@ 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()
kci_msg("Getting git repository informations for the current folder")
git_url, branch_name, last_commit_hash = get_folder_repository(git_folder)
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 +271,14 @@ 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()
kci_msg("Getting git repository informations for the current folder")
git_url, branch_name, last_commit_hash = get_folder_repository(git_folder)
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 151167e

Please sign in to comment.