|
| 1 | +name:Sync PR to Changerawr |
| 2 | + |
| 3 | +on: |
| 4 | +pull_request: |
| 5 | +types: |
| 6 | + -closed |
| 7 | +branches: |
| 8 | + -master |
| 9 | + |
| 10 | +jobs: |
| 11 | +post-to-changerawr: |
| 12 | +# Only run if the PR was merged (not just closed) |
| 13 | +if:github.event.pull_request.merged == true |
| 14 | +runs-on:ubuntu-latest |
| 15 | + |
| 16 | +steps: |
| 17 | + -name:Post to Changerawr API |
| 18 | +uses:actions/github-script@v7 |
| 19 | +env: |
| 20 | +CHANGERAWR_API_KEY:${{ secrets.CHANGERAWR_API_KEY }} |
| 21 | +CHANGERAWR_PROJECT_ID:${{ secrets.CHANGERAWR_PROJECT_ID }} |
| 22 | +with: |
| 23 | +script:| |
| 24 | + const prBody = context.payload.pull_request.body || ''; |
| 25 | + const prNumber = context.payload.pull_request.number; |
| 26 | + const prTitle = context.payload.pull_request.title; |
| 27 | + const prUrl = context.payload.pull_request.html_url; |
| 28 | +
|
| 29 | + // Prepare the payload for Changerawr API |
| 30 | + const payload = { |
| 31 | + notes: prBody, |
| 32 | + metadata: { |
| 33 | + pr_number: prNumber, |
| 34 | + pr_title: prTitle, |
| 35 | + pr_url: prUrl, |
| 36 | + merged_at: context.payload.pull_request.merged_at, |
| 37 | + merged_by: context.payload.pull_request.merged_by.login |
| 38 | + } |
| 39 | + }; |
| 40 | +
|
| 41 | + try { |
| 42 | + const response = await fetch( |
| 43 | + `https://clog.resgrid.com/api/projects/${process.env.CHANGERAWR_PROJECT_ID}/changelog`, |
| 44 | + { |
| 45 | + method: 'POST', |
| 46 | + headers: { |
| 47 | + 'Content-Type': 'application/json', |
| 48 | + 'Authorization': `Bearer ${process.env.CHANGERAWR_API_KEY}` |
| 49 | + }, |
| 50 | + body: JSON.stringify(payload) |
| 51 | + } |
| 52 | + ); |
| 53 | +
|
| 54 | + if (!response.ok) { |
| 55 | + const errorText = await response.text(); |
| 56 | + throw new Error(`Changerawr API request failed: ${response.status} - ${errorText}`); |
| 57 | + } |
| 58 | +
|
| 59 | + const result = await response.json(); |
| 60 | + console.log('Successfully posted to Changerawr:', result); |
| 61 | +
|
| 62 | + // Optionally, comment on the PR with confirmation |
| 63 | + await github.rest.issues.createComment({ |
| 64 | + owner: context.repo.owner, |
| 65 | + repo: context.repo.repo, |
| 66 | + issue_number: prNumber, |
| 67 | + body: '✅ Change notes have been posted to Changerawr.' |
| 68 | + }); |
| 69 | +
|
| 70 | + } catch (error) { |
| 71 | + console.error('Error posting to Changerawr:', error); |
| 72 | + core.setFailed(`Failed to post to Changerawr: ${error.message}`); |
| 73 | + } |