-
-
Notifications
You must be signed in to change notification settings - Fork 82
171 lines (144 loc) · 5.86 KB
/
changerawr-sync.yml
File metadata and controls
171 lines (144 loc) · 5.86 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
name: Sync PR to Changerawr
permissions:
contents: read
issues: write
pull-requests: read
on:
pull_request:
types:
- closed
branches:
- master
jobs:
post-to-changerawr:
# Only run if the PR was merged (not just closed)
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 10
- name: Extract and prepare release notes
id: prepare_notes
env:
GH_TOKEN: ${{ github.token }}
run: |
set -eo pipefail
# Function to extract release notes from PR body
extract_release_notes() {
local body="$1"
# Remove "Summary by CodeRabbit" section and auto-generated comment line
local cleaned_body="$(printf '%s\n' "$body" \
| grep -v '<!-- end of auto-generated comment: release notes by coderabbit.ai -->' \
| awk '
BEGIN { skip=0 }
/^## Summary by CodeRabbit/ { skip=1; next }
/^## / && skip==1 { skip=0 }
skip==0 { print }
')"
# Try to extract content under "## Release Notes" heading
local notes="$(printf '%s\n' "$cleaned_body" \
| awk 'f && /^## /{exit} /^## Release Notes/{f=1; next} f')"
# If no specific section found, use the entire cleaned body
if [ -z "$notes" ]; then
notes="$cleaned_body"
fi
printf '%s\n' "$notes"
}
echo "Fetching PR #${{ github.event.pull_request.number }} details..."
# Fetch the PR body using GitHub CLI
PR_BODY=$(gh pr view "${{ github.event.pull_request.number }}" --json body --jq '.body' 2>/dev/null || echo "")
NOTES=""
if [ -n "$PR_BODY" ]; then
echo "PR body found, extracting release notes..."
NOTES="$(extract_release_notes "$PR_BODY")"
fi
# Fallback to PR title and recent commits if no body found
if [ -z "$NOTES" ] || [ "$NOTES" = "" ]; then
echo "No PR body found, using PR title and commits..."
NOTES="## ${{ github.event.pull_request.title }}"
NOTES="$NOTES"$'\n\n'"$(git log -n 5 --pretty=format:'- %s')"
fi
# Save to file and environment
echo "$NOTES" > release_notes.txt
# For multiline output, use delimiter
{
echo 'RELEASE_NOTES<<EOF'
echo "$NOTES"
echo 'EOF'
} >> "$GITHUB_OUTPUT"
echo "Release notes prepared:"
cat release_notes.txt
- name: Post to Changerawr API
uses: actions/github-script@v7
env:
CHANGERAWR_API_KEY: ${{ secrets.CHANGERAWR_API_KEY }}
CHANGERAWR_PROJECT_ID: ${{ secrets.CHANGERAWR_PROJECT_ID }}
RELEASE_NOTES: ${{ steps.prepare_notes.outputs.RELEASE_NOTES }}
with:
script: |
const prNumber = context.payload.pull_request.number;
const prTitle = context.payload.pull_request.title;
const prUrl = context.payload.pull_request.html_url;
const releaseNotes = process.env.RELEASE_NOTES || '';
// Check if required secrets are set
if (!process.env.CHANGERAWR_API_KEY || !process.env.CHANGERAWR_PROJECT_ID) {
console.log('⚠️ Changerawr API credentials not configured, skipping release notes submission');
return;
}
// Prepare the payload for Changerawr API
const payload = {
title: prTitle,
content: releaseNotes,
metadata: {
pr_number: prNumber,
pr_title: prTitle,
pr_url: prUrl,
merged_at: context.payload.pull_request.merged_at,
merged_by: context.payload.pull_request.merged_by?.login || 'unknown',
commit_sha: context.payload.pull_request.merge_commit_sha
}
};
console.log('Sending release notes to Changerawr...');
console.log('Payload:', JSON.stringify(payload, null, 2));
try {
const response = await fetch(
`https://clog.resgrid.com/api/projects/${process.env.CHANGERAWR_PROJECT_ID}/changelog`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.CHANGERAWR_API_KEY}`
},
body: JSON.stringify(payload)
}
);
const responseText = await response.text();
if (!response.ok) {
console.warn(`⚠️ Changerawr API request failed: ${response.status} - ${responseText}`);
// Don't fail the workflow, just log the error
return;
}
let result;
try {
result = JSON.parse(responseText);
} catch (e) {
result = responseText;
}
console.log('✅ Successfully posted to Changerawr:', result);
// Optionally, comment on the PR with confirmation
try {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: '✅ Change notes have been posted to Changerawr.'
});
} catch (commentError) {
console.log('Could not post comment to PR:', commentError.message);
}
} catch (error) {
console.error('⚠️ Error posting to Changerawr:', error);
// Don't fail the workflow
}