cohenaj194 / GitFailGuard

Apache License 2.0
1 stars 2 forks source link

save git log cleanup script #3

Open cohenaj194 opened 1 month ago

cohenaj194 commented 1 month ago
import os
import requests

def fetch_logs(repo_owner, repo_name, run_id, job_id):
    github_token = os.getenv('GITHUB_TOKEN')
    headers = {
        "Authorization": f"token {github_token}",
        "Accept": "application/vnd.github.v3+json"
    }

    url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/actions/jobs/{job_id}/logs"

    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        return response.text
    else:
        return f"Error: Unable to fetch logs, status code: {response.status_code}"

def extract_info_from_url(url):
    # Split the URL by '/'
    parts = url.split("/")

    # Extract repository owner, repository name, run ID, and job ID from the URL
    repo_owner = parts[3]
    repo_name = parts[4]
    run_id = parts[7]
    job_id = parts[9]

    return repo_owner, repo_name, run_id, job_id

def cleanup_logs(logs):
    # split by newline
    logs = logs.split("\n")
    # find the index of all lines containing ##[endgroup]
    endgroup_indices = [i for i, line in enumerate(logs) if "##[endgroup]" in line]
    # find the index of the line containing ##[error]
    error_index = [i for i, line in enumerate(logs) if "##[error]" in line]

    if len(error_index) == 0:
        return "No error found in logs"

    # get all logs between the last ##[endgroup] and the first ##[error] including he line with ##[error]
    cleaned_logs = logs[endgroup_indices[-1]:error_index[0]+1]
    return "\n".join(cleaned_logs)

def main():
    # Example usage
    url = "https://github.com/ff14-advanced-market-search/mysql-population/actions/runs/9216448689/job/25356749022"
    # url = "https://github.com/ff14-advanced-market-search/temp-fe/actions/runs/9066730415/job/24910289744"

    repo_owner, repo_name, run_id, job_id = extract_info_from_url(url)

    # # Example usage
    # repo_owner = "ff14-advanced-market-search"
    # repo_name = "mysql-population"
    # run_id = "9199994409"
    # job_id = "25305762140"

    logs = fetch_logs(repo_owner, repo_name, run_id, job_id)
    clean_logs = cleanup_logs(logs)

    if logs is not None:
        print(clean_logs)

if __name__ == "__main__":
    main()
cohenaj194 commented 1 month ago

result

2024-05-24T00:03:46.4406371Z ##[endgroup]
2024-05-24T00:03:46.7798325Z WARNING: The directory '/github/home/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you should use sudo's -H flag.
2024-05-24T00:03:46.7844760Z error: externally-managed-environment
2024-05-24T00:03:46.7845289Z 
2024-05-24T00:03:46.7846147Z × This environment is externally managed
2024-05-24T00:03:46.7847800Z ╰─> To install Python packages system-wide, try apt install
2024-05-24T00:03:46.7848966Z     python3-xyz, where xyz is the package you are trying to
2024-05-24T00:03:46.7849759Z     install.
2024-05-24T00:03:46.7850216Z     
2024-05-24T00:03:46.7851067Z     If you wish to install a non-Debian-packaged Python package,
2024-05-24T00:03:46.7852158Z     create a virtual environment using python3 -m venv path/to/venv.
2024-05-24T00:03:46.7853152Z     Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
2024-05-24T00:03:46.7854228Z     sure you have python3-full installed.
2024-05-24T00:03:46.7854825Z     
2024-05-24T00:03:46.7855531Z     If you wish to install a non-Debian packaged Python application,
2024-05-24T00:03:46.7856674Z     it may be easiest to use pipx install xyz, which will manage a
2024-05-24T00:03:46.7857667Z     virtual environment for you. Make sure you have pipx installed.
2024-05-24T00:03:46.7858545Z     
2024-05-24T00:03:46.7859207Z     See /usr/share/doc/python3.12/README.venv for more information.
2024-05-24T00:03:46.7859828Z 
2024-05-24T00:03:46.7861521Z note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
2024-05-24T00:03:46.7863595Z hint: See PEP 668 for the detailed specification.
2024-05-24T00:03:46.8331050Z ##[error]Process completed with exit code 1.