script to pull github activity report

 import os

import requests

import csv

from datetime import datetime


# Configuration

REPO = 'username/reponame'  # Repository name

START_DATE = '2023-09-03T00:00:00Z'  # Start of the date range

END_DATE = '2023-11-13T23:59:59Z'  # End of the date range

GITHUB_TOKEN = os.getenv('GITHUB_TOKEN')  # Retrieve GitHub token from environment variable


headers = {

    "Authorization": f"token {GITHUB_TOKEN}",

    "Accept": "application/vnd.github.v3+json"

}


output_file = 'github_report.csv'


def fetch_commits():

    """Fetch commits within the specified date range."""

    url = f'https://api.github.com/repos/{REPO}/commits'

    params = {

        'since': START_DATE,

        'until': END_DATE,

    }

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

    commits = response.json()

    

    commit_data = []

    for commit in commits:

        date = commit['commit']['author']['date'][:10]  # Extract only YYYY-MM-DD

        message = commit['commit']['message'][:100]  # Limit message to 100 characters

        commit_time = commit['commit']['author']['date']

        commit_data.append((date, message, commit_time, commit_time, 0))  # Commits have same start and end time


    return commit_data


def fetch_issues():

    """Fetch issues (including bugs) created within the specified date range."""

    url = f'https://api.github.com/repos/{REPO}/issues'

    params = {

        'since': START_DATE,

        'state': 'all'

    }

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

    issues = response.json()


    issue_data = []

    for issue in issues:

        created_at = issue['created_at'][:10]  # Extract only YYYY-MM-DD

        if START_DATE <= issue['created_at'] <= END_DATE:

            title = issue['title'][:100]  # Limit title to 100 characters

            created_time = issue['created_at']

            closed_time = issue.get('closed_at', created_time)  # Use created time if not closed

            # Calculate duration if issue was closed

            duration = calculate_duration(created_time, closed_time) if closed_time else 0

            issue_data.append((created_at, title, created_time, closed_time, duration))


    return issue_data


def calculate_duration(start_time, end_time):

    """Calculate duration in hours between start and end time."""

    fmt = "%Y-%m-%dT%H:%M:%SZ"

    start_dt = datetime.strptime(start_time, fmt)

    end_dt = datetime.strptime(end_time, fmt)

    return round((end_dt - start_dt).total_seconds() / 3600, 2)


def write_csv(data):

    """Write data to a CSV file."""

    with open(output_file, mode='w', newline='') as file:

        writer = csv.writer(file)

        writer.writerow(["date", "description of item-incident-commit", "initial login time", "final login time", "total hours between initial and final time"])

        writer.writerows(data)


if __name__ == "__main__":

    commit_data = fetch_commits()

    issue_data = fetch_issues()

    all_data = commit_data + issue_data

    write_csv(all_data)

    print(f"Report saved to {output_file}")


Popular posts from this blog

Recent Experiments testing ChatGPT's limits and some useful prompts

Testing ChatGPT on coding - a script to make CSV to ICS

Ollama3 works - running a local LLM