-
Notifications
You must be signed in to change notification settings - Fork 137
103 lines (88 loc) · 4.04 KB
/
writeme.yml
File metadata and controls
103 lines (88 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
name: WRITEME - Scheduled Copilot README Review
on:
schedule:
# Runs at 17:00 UTC every Wednesday (3)
- cron: '0 17 * * 3'
# Optional manual trigger if you ever want to run it on-demand
workflow_dispatch: {}
permissions:
contents: write # Allow Copilot agent to create a branch/PR
pull-requests: write # Allow PR operations
jobs:
create-copilot-job:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Read prompt file
id: prompt
run: |
set -euo pipefail
FILE=".github/workflows/writeme/prompt.md"
if [ ! -f "$FILE" ]; then
echo "Prompt file not found: $FILE" >&2
exit 1
fi
# Convert entire file to a JSON string (properly escaped)
PROMPT_JSON=$(jq -Rs . < "$FILE")
echo "prompt=$PROMPT_JSON" >> "$GITHUB_OUTPUT"
- name: Submit Copilot agent job
id: submit
env:
TOKEN: ${{ secrets.COPILOT_PAT }}
RAW_PROMPT: ${{ steps.prompt.outputs.prompt }}
run: |
set -euo pipefail
# Derive base branch (fallback to main if not present in event payload)
BASE_BRANCH="${{ github.event.repository.default_branch }}"
if [ -z "$BASE_BRANCH" ]; then
BASE_BRANCH="main"
fi
# RAW_PROMPT is already a quoted JSON string; unwrap safely via jq later
BODY=$(jq -n --arg prompt "$RAW_PROMPT" --arg base "$BASE_BRANCH" ' {
problem_statement: ($prompt | fromjson),
event_type: "scheduled-workflow",
pull_request: {
title: "README Accuracy Review & Minimal Update",
base_ref: $base,
head_ref: null
}
}')
echo "Request body:" >&2
echo "$BODY" | jq . >&2 || echo "$BODY" >&2
RESP=$(curl -sS -w "\nHTTP_STATUS:%{http_code}" -X POST -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -H "Copilot-Integration-Id: copilot-github-actions" -H "User-Agent: scheduled-copilot-workflow" -H "Accept: application/json" https://api.githubcopilot.com/agents/swe/jobs/intellectronica/ruler -d "$BODY")
# Extract status code and response body
RESPONSE_BODY=$(echo "$RESP" | sed -n '1,/HTTP_STATUS:/p' | sed '$d')
STATUS_CODE=$(echo "$RESP" | grep "HTTP_STATUS:" | cut -d: -f2)
echo "HTTP Status Code: $STATUS_CODE" >&2
echo "Response Body: $RESPONSE_BODY" >&2
# Check if response is just "Unauthorized"
if [ "$RESPONSE_BODY" = "Unauthorized" ]; then
echo "Authentication failed: Token may be invalid or missing required scopes" >&2
exit 1
fi
JOB_ID=$(echo "$RESPONSE_BODY" | jq -r '.job_id // empty')
STATE=$(echo "$RESPONSE_BODY" | jq -r '.status // empty')
SESSION_URL=$(echo "$RESPONSE_BODY" | jq -r '.session_id // empty')
PR_URL=$(echo "$RESPONSE_BODY" | jq -r '.pull_request.html_url // empty')
if [ -z "$JOB_ID" ]; then
echo "Failed to create Copilot agent job" >&2
exit 1
fi
echo "job_id=$JOB_ID" >> "$GITHUB_OUTPUT"
echo "job_state=$STATE" >> "$GITHUB_OUTPUT"
echo "session_url=$SESSION_URL" >> "$GITHUB_OUTPUT"
echo "pr_url=$PR_URL" >> "$GITHUB_OUTPUT"
- name: Job summary
run: |
{
echo "### Copilot Agent Job Created";
echo "* Job ID: ${{ steps.submit.outputs.job_id }}";
echo "* State: ${{ steps.submit.outputs.job_state }}";
if [ -n "${{ steps.submit.outputs.session_url }}" ]; then
echo "* Session: ${{ steps.submit.outputs.session_url }}";
fi
if [ -n "${{ steps.submit.outputs.pr_url }}" ] && [ "${{ steps.submit.outputs.pr_url }}" != "null" ]; then
echo "* PR (if opened yet): ${{ steps.submit.outputs.pr_url }}";
fi
} >> "$GITHUB_STEP_SUMMARY"