mirror of
https://github.com/github/awesome-copilot.git
synced 2026-02-20 02:15:12 +00:00
feat: add hooks functionality with automated workflows
- Introduced hooks to enable automated workflows triggered by specific events during GitHub Copilot sessions. - Added documentation for hooks in AGENTS.md and README.md. - Created a new directory structure for hooks, including README.md and hooks.json files. - Implemented two example hooks: Session Auto-Commit and Session Logger. - Developed scripts for logging session events and auto-committing changes. - Enhanced validation and parsing for hook metadata. - Updated build and validation scripts to accommodate new hooks functionality.
This commit is contained in:
90
hooks/session-auto-commit/README.md
Normal file
90
hooks/session-auto-commit/README.md
Normal file
@@ -0,0 +1,90 @@
|
||||
---
|
||||
name: 'Session Auto-Commit'
|
||||
description: 'Automatically commits and pushes changes when a Copilot coding agent session ends'
|
||||
tags: ['automation', 'git', 'productivity']
|
||||
---
|
||||
|
||||
# Session Auto-Commit Hook
|
||||
|
||||
Automatically commits and pushes changes when a GitHub Copilot coding agent session ends, ensuring your work is always saved and backed up.
|
||||
|
||||
## Overview
|
||||
|
||||
This hook runs at the end of each Copilot coding agent session and automatically:
|
||||
- Detects if there are uncommitted changes
|
||||
- Stages all changes
|
||||
- Creates a timestamped commit
|
||||
- Pushes to the remote repository
|
||||
|
||||
## Features
|
||||
|
||||
- **Automatic Backup**: Never lose work from a Copilot session
|
||||
- **Timestamped Commits**: Each auto-commit includes the session end time
|
||||
- **Safe Execution**: Only commits when there are actual changes
|
||||
- **Error Handling**: Gracefully handles push failures
|
||||
|
||||
## Installation
|
||||
|
||||
1. Copy this hook folder to your repository's `.github/hooks/` directory:
|
||||
```bash
|
||||
cp -r hooks/session-auto-commit .github/hooks/
|
||||
```
|
||||
|
||||
2. Ensure the script is executable:
|
||||
```bash
|
||||
chmod +x .github/hooks/session-auto-commit/auto-commit.sh
|
||||
```
|
||||
|
||||
3. Commit the hook configuration to your repository's default branch
|
||||
|
||||
## Configuration
|
||||
|
||||
The hook is configured in `hooks.json` to run on the `sessionEnd` event:
|
||||
|
||||
```json
|
||||
{
|
||||
"version": 1,
|
||||
"hooks": {
|
||||
"sessionEnd": [
|
||||
{
|
||||
"type": "command",
|
||||
"bash": ".github/hooks/session-auto-commit/auto-commit.sh",
|
||||
"timeoutSec": 30
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## How It Works
|
||||
|
||||
1. When a Copilot coding agent session ends, the hook executes
|
||||
2. Checks if inside a Git repository
|
||||
3. Detects uncommitted changes using `git status`
|
||||
4. Stages all changes with `git add -A`
|
||||
5. Creates a commit with format: `auto-commit: YYYY-MM-DD HH:MM:SS`
|
||||
6. Attempts to push to remote
|
||||
7. Reports success or failure
|
||||
|
||||
## Customization
|
||||
|
||||
You can customize the hook by modifying `auto-commit.sh`:
|
||||
|
||||
- **Commit Message Format**: Change the timestamp format or message prefix
|
||||
- **Selective Staging**: Use specific git add patterns instead of `-A`
|
||||
- **Branch Selection**: Push to specific branches only
|
||||
- **Notifications**: Add desktop notifications or Slack messages
|
||||
|
||||
## Disabling
|
||||
|
||||
To temporarily disable auto-commits:
|
||||
|
||||
1. Remove or comment out the `sessionEnd` hook in `hooks.json`
|
||||
2. Or set an environment variable: `export SKIP_AUTO_COMMIT=true`
|
||||
|
||||
## Notes
|
||||
|
||||
- The hook uses `--no-verify` to avoid triggering pre-commit hooks
|
||||
- Failed pushes won't block session termination
|
||||
- Requires appropriate git credentials configured
|
||||
- Works with both Copilot coding agent and GitHub Copilot CLI
|
||||
45
hooks/session-auto-commit/auto-commit.sh
Executable file
45
hooks/session-auto-commit/auto-commit.sh
Executable file
@@ -0,0 +1,45 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Session Auto-Commit Hook
|
||||
# Automatically commits and pushes changes when a Copilot session ends
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# Check if SKIP_AUTO_COMMIT is set
|
||||
if [[ "${SKIP_AUTO_COMMIT:-}" == "true" ]]; then
|
||||
echo "⏭️ Auto-commit skipped (SKIP_AUTO_COMMIT=true)"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Check if we're in a git repository
|
||||
if ! git rev-parse --is-inside-work-tree &>/dev/null; then
|
||||
echo "⚠️ Not in a git repository"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Check for uncommitted changes
|
||||
if [[ -z "$(git status --porcelain)" ]]; then
|
||||
echo "✨ No changes to commit"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "📦 Auto-committing changes from Copilot session..."
|
||||
|
||||
# Stage all changes
|
||||
git add -A
|
||||
|
||||
# Create timestamped commit
|
||||
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
|
||||
git commit -m "auto-commit: $TIMESTAMP" --no-verify 2>/dev/null || {
|
||||
echo "⚠️ Commit failed"
|
||||
exit 0
|
||||
}
|
||||
|
||||
# Attempt to push
|
||||
if git push 2>/dev/null; then
|
||||
echo "✅ Changes committed and pushed successfully"
|
||||
else
|
||||
echo "⚠️ Push failed - changes committed locally"
|
||||
fi
|
||||
|
||||
exit 0
|
||||
12
hooks/session-auto-commit/hooks.json
Normal file
12
hooks/session-auto-commit/hooks.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"version": 1,
|
||||
"hooks": {
|
||||
"sessionEnd": [
|
||||
{
|
||||
"type": "command",
|
||||
"bash": ".github/hooks/session-auto-commit/auto-commit.sh",
|
||||
"timeoutSec": 30
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
64
hooks/session-logger/README.md
Normal file
64
hooks/session-logger/README.md
Normal file
@@ -0,0 +1,64 @@
|
||||
---
|
||||
name: 'Session Logger'
|
||||
description: 'Logs all Copilot coding agent session activity for audit and analysis'
|
||||
tags: ['logging', 'audit', 'analytics']
|
||||
---
|
||||
|
||||
# Session Logger Hook
|
||||
|
||||
Comprehensive logging for GitHub Copilot coding agent sessions, tracking session starts, ends, and user prompts for audit trails and usage analytics.
|
||||
|
||||
## Overview
|
||||
|
||||
This hook provides detailed logging of Copilot coding agent activity:
|
||||
- Session start/end times
|
||||
- User prompts and questions
|
||||
- Session duration
|
||||
- Working directory context
|
||||
|
||||
## Features
|
||||
|
||||
- **Complete Audit Trail**: Track all Copilot interactions
|
||||
- **Structured Logging**: JSON format for easy parsing
|
||||
- **Searchable History**: Review past sessions and prompts
|
||||
- **Analytics Ready**: Export data for usage analysis
|
||||
- **Privacy Aware**: Configurable to exclude sensitive data
|
||||
|
||||
## Installation
|
||||
|
||||
1. Copy this hook folder to your repository's `.github/hooks/` directory:
|
||||
```bash
|
||||
cp -r hooks/session-logger .github/hooks/
|
||||
```
|
||||
|
||||
2. Create the logs directory:
|
||||
```bash
|
||||
mkdir -p logs/copilot
|
||||
```
|
||||
|
||||
3. Ensure scripts are executable:
|
||||
```bash
|
||||
chmod +x .github/hooks/session-logger/*.sh
|
||||
```
|
||||
|
||||
4. Commit the hook configuration to your repository's default branch
|
||||
|
||||
## Log Format
|
||||
|
||||
Logs are written to `logs/copilot/session.log` in JSON format:
|
||||
|
||||
```json
|
||||
{
|
||||
"timestamp": "2024-01-15T10:30:00Z",
|
||||
"event": "sessionStart",
|
||||
"sessionId": "abc123",
|
||||
"cwd": "/workspace/project"
|
||||
}
|
||||
```
|
||||
|
||||
## Privacy & Security
|
||||
|
||||
- Add `logs/` to `.gitignore` to avoid committing session data
|
||||
- Use `LOG_LEVEL=ERROR` to only log errors
|
||||
- Set `SKIP_LOGGING=true` environment variable to disable
|
||||
- Logs are stored locally only
|
||||
32
hooks/session-logger/hooks.json
Normal file
32
hooks/session-logger/hooks.json
Normal file
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"version": 1,
|
||||
"hooks": {
|
||||
"sessionStart": [
|
||||
{
|
||||
"type": "command",
|
||||
"bash": ".github/hooks/session-logger/log-session-start.sh",
|
||||
"cwd": ".",
|
||||
"timeoutSec": 5
|
||||
}
|
||||
],
|
||||
"sessionEnd": [
|
||||
{
|
||||
"type": "command",
|
||||
"bash": ".github/hooks/session-logger/log-session-end.sh",
|
||||
"cwd": ".",
|
||||
"timeoutSec": 5
|
||||
}
|
||||
],
|
||||
"userPromptSubmitted": [
|
||||
{
|
||||
"type": "command",
|
||||
"bash": ".github/hooks/session-logger/log-prompt.sh",
|
||||
"cwd": ".",
|
||||
"env": {
|
||||
"LOG_LEVEL": "INFO"
|
||||
},
|
||||
"timeoutSec": 5
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
24
hooks/session-logger/log-prompt.sh
Executable file
24
hooks/session-logger/log-prompt.sh
Executable file
@@ -0,0 +1,24 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Log user prompt submission
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# Skip if logging disabled
|
||||
if [[ "${SKIP_LOGGING:-}" == "true" ]]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Read input from Copilot (contains prompt info)
|
||||
INPUT=$(cat)
|
||||
|
||||
# Create logs directory if it doesn't exist
|
||||
mkdir -p logs/copilot
|
||||
|
||||
# Extract timestamp
|
||||
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
||||
|
||||
# Log prompt (you can parse INPUT for more details)
|
||||
echo "{\"timestamp\":\"$TIMESTAMP\",\"event\":\"userPromptSubmitted\",\"level\":\"${LOG_LEVEL:-INFO}\"}" >> logs/copilot/prompts.log
|
||||
|
||||
exit 0
|
||||
25
hooks/session-logger/log-session-end.sh
Executable file
25
hooks/session-logger/log-session-end.sh
Executable file
@@ -0,0 +1,25 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Log session end event
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# Skip if logging disabled
|
||||
if [[ "${SKIP_LOGGING:-}" == "true" ]]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Read input from Copilot
|
||||
INPUT=$(cat)
|
||||
|
||||
# Create logs directory if it doesn't exist
|
||||
mkdir -p logs/copilot
|
||||
|
||||
# Extract timestamp
|
||||
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
||||
|
||||
# Log session end
|
||||
echo "{\"timestamp\":\"$TIMESTAMP\",\"event\":\"sessionEnd\"}" >> logs/copilot/session.log
|
||||
|
||||
echo "📝 Session end logged"
|
||||
exit 0
|
||||
26
hooks/session-logger/log-session-start.sh
Executable file
26
hooks/session-logger/log-session-start.sh
Executable file
@@ -0,0 +1,26 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Log session start event
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# Skip if logging disabled
|
||||
if [[ "${SKIP_LOGGING:-}" == "true" ]]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Read input from Copilot
|
||||
INPUT=$(cat)
|
||||
|
||||
# Create logs directory if it doesn't exist
|
||||
mkdir -p logs/copilot
|
||||
|
||||
# Extract timestamp and session info
|
||||
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
||||
CWD=$(pwd)
|
||||
|
||||
# Log session start
|
||||
echo "{\"timestamp\":\"$TIMESTAMP\",\"event\":\"sessionStart\",\"cwd\":\"$CWD\"}" >> logs/copilot/session.log
|
||||
|
||||
echo "📝 Session logged"
|
||||
exit 0
|
||||
Reference in New Issue
Block a user