Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions .github/workflows/user-stats.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: GitHub User Statistics

on:
# Allow manual trigger
workflow_dispatch:
inputs:
username:
description: 'GitHub username to analyze'
required: false
default: 'rajbos'
days:
description: 'Number of days to look back'
required: false
default: '30'

# Run on a schedule (weekly on Monday at 9 AM UTC)
schedule:
- cron: '0 9 * * 1'

# Run on push to main branch (for testing)
push:
paths:
- 'fetch_user_stats.py'
- '.github/workflows/user-stats.yml'

jobs:
fetch-statistics:
runs-on: ubuntu-latest

permissions:
contents: read

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
cache: 'pip'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt

- name: Fetch user statistics
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
python fetch_user_stats.py \
--username ${{ github.event.inputs.username || 'rajbos' }} \
--days ${{ github.event.inputs.days || '30' }} \
--output-file user_stats_report.txt

- name: Display statistics summary
if: success() || failure()
run: |
echo "Statistics report generated successfully"
echo "See artifact for detailed report"

- name: Upload statistics as artifact
if: success() || failure()
uses: actions/upload-artifact@v4
with:
name: user-statistics-report
path: user_stats_report.txt
retention-days: 30
46 changes: 46 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# Virtual environments
venv/
ENV/
env/
.venv

# IDE
.vscode/
.idea/
*.swp
*.swo
*~

# OS
.DS_Store
Thumbs.db

# Reports and logs
*.log
user_stats_report.txt

# Environment variables
.env
236 changes: 236 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
# GitHub User Statistics

A Python-based tool to fetch and analyze GitHub user activity statistics, including pull requests, issues, and discussions. The tool can be run locally or automated via GitHub Actions.

## Features

- 📊 Fetch comprehensive user activity statistics from GitHub
- 🔄 Configurable lookback period (days)
- 📋 Track pull requests, issues, and discussions
- 🤖 GitHub Actions workflow for automation
- 📝 Detailed activity reports
- 💾 Export results as artifacts
- 📄 Automatic GitHub Actions step summary integration

## Prerequisites

- Python 3.11 or higher
- GitHub Personal Access Token (PAT) with appropriate scopes:
- `repo` (for private repositories)
- `read:user` (for user data)
- `read:discussion` (for discussions)

## Installation

1. Clone the repository:
```bash
git clone https://github.com/devops-actions/github-user-stats.git
cd github-user-stats
```

2. Install dependencies:
```bash
pip install -r requirements.txt
```

## Usage

### Local Execution

Run the script with default settings (user: `rajbos`, lookback: 30 days):

```bash
export GITHUB_TOKEN=your_github_token_here
python fetch_user_stats.py
```

### Custom Parameters

Specify a different user and lookback period:

```bash
python fetch_user_stats.py --username octocat --days 90
```

Or provide the token directly:

```bash
python fetch_user_stats.py --username rajbos --days 7 --token your_token_here
```

### Command-Line Options

- `--username`: GitHub username to fetch statistics for (default: `rajbos`)
- `--days`: Number of days to look back (default: `30`)
- `--token`: GitHub personal access token (alternatively, use `GITHUB_TOKEN` environment variable)
- `--output-file`: Optional path to write the output to a file instead of stdout

### Save Output to File

Save the statistics report to a file:

```bash
python fetch_user_stats.py --username rajbos --days 30 --output-file report.txt
```


## GitHub Actions Workflow

The repository includes a GitHub Actions workflow that automatically fetches user statistics.

### Triggers

The workflow runs:
- **Manually**: Via workflow dispatch in the Actions tab
- **Scheduled**: Weekly on Mondays at 9 AM UTC
- **On Push**: When the script or workflow files are modified

### Manual Workflow Run

1. Go to the **Actions** tab in your GitHub repository
2. Select the "GitHub User Statistics" workflow
3. Click "Run workflow"
4. Optionally specify:
- Username to analyze (default: `rajbos`)
- Number of days to look back (default: `30`)

### Workflow Outputs

The workflow:
1. Displays statistics in the GitHub Actions logs
2. Generates a detailed report file
3. Uploads the report as an artifact (retained for 30 days)
4. Writes statistics to the GitHub Actions step summary (visible in the workflow run summary page)

### GitHub Actions Step Summary

When running in GitHub Actions, the script automatically detects the `GITHUB_STEP_SUMMARY` environment variable and writes the statistics to both the console logs and the step summary. This provides an easy-to-read summary directly on the workflow run page without needing to download artifacts.

### Download Artifacts

After a workflow run completes:
1. Go to the workflow run summary page
2. View the statistics in the step summary section at the bottom of the page
3. Or scroll to the "Artifacts" section and download `user-statistics-report` for offline viewing

## Example Output

```
================================================================================
GitHub User Statistics for @rajbos
Name: Rob Bos
Period: Last 30 days (since 2025-11-20)
================================================================================

📋 Pull Requests: 15

Recent Pull Requests:
✅ devops-actions/load-used-actions
Update dependency versions
https://github.com/devops-actions/load-used-actions/pull/123 (2025-12-15)
🟢 devops-actions/github-user-stats
Add new statistics feature
https://github.com/devops-actions/github-user-stats/pull/5 (2025-12-10)

🐛 Issues: 8

Recent Issues:
🟢 devops-actions/marketplace-checks
Enhancement: Add support for new action types
https://github.com/devops-actions/marketplace-checks/issues/45 (2025-12-18)

💬 Discussions: 3

Recent Discussions:
💬 Best practices for GitHub Actions security
https://github.com/community/discussions/67890 (2025-12-12)

================================================================================
Summary:
Total Activity Items: 26
================================================================================
```

## Security Considerations

- Never commit your GitHub token to the repository
- Use GitHub Secrets for storing tokens in workflows
- The default `GITHUB_TOKEN` provided by GitHub Actions has appropriate permissions for public repositories
- For private repositories, you may need to create a PAT with additional scopes

## Configuration

### GitHub Actions Secrets

The workflow uses `secrets.GITHUB_TOKEN`, which is automatically provided by GitHub Actions. No additional configuration is needed for public repositories.

For enhanced permissions or private repositories, create a PAT and add it as a secret named `GITHUB_TOKEN` in your repository settings.

## Customization

### Modify the Lookback Period

Edit the default value in the workflow file (`.github/workflows/user-stats.yml`):

```yaml
inputs:
days:
description: 'Number of days to look back'
required: false
default: '30' # Change this value
```

### Change the Default User

Update the default username in the workflow file:

```yaml
inputs:
username:
description: 'GitHub username to analyze'
required: false
default: 'rajbos' # Change this value
```

## Troubleshooting

### Authentication Errors

If you encounter authentication errors:
- Verify your token has the required scopes
- Check that the token hasn't expired
- Ensure the `GITHUB_TOKEN` environment variable is set correctly

### Rate Limiting

GitHub API has rate limits:
- Authenticated requests: 5,000 requests per hour
- The script uses GraphQL API which is more efficient
- If you hit rate limits, wait before retrying or reduce the lookback period

### No Data Returned

If no activity is found:
- Verify the username is correct
- Check if there's activity in the specified time period
- Ensure the token has access to view the user's activity

### Data Limitations

The script fetches the most recent 100 items of each type (pull requests, issues, discussions):
- For very active users, some older items within the time period may not be included
- This is a limitation of the GitHub GraphQL API query structure
- The 100-item limit per type typically covers most use cases
- If you need complete historical data, consider reducing the lookback period

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## Author

Maintained by [DevOps Actions](https://github.com/devops-actions)
Loading