-
Notifications
You must be signed in to change notification settings - Fork 3
156 lines (131 loc) · 5.5 KB
/
post-merge.yml
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
name: Post-Merge Processing
on:
push:
branches:
- main
jobs:
process_merge:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: read
steps:
- name: Checkout Repository
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up Git Safe Directory
run: |
git config --global --add safe.directory $GITHUB_WORKSPACE
- name: Set up Git user
run: |
git config user.name "GitHub Action"
git config user.email "[email protected]"
- name: Set up Python 3.x
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
run: |
pip install boto3
- name: Check if Latest Commit is a Merge Commit
id: check_merge
run: |
PARENT_COUNT=$(git show -s --format=%P HEAD | wc -w)
echo "Parent count: $PARENT_COUNT"
if [ "$PARENT_COUNT" -gt 1 ]; then
echo "is_merge=true" >> $GITHUB_OUTPUT
else
echo "is_merge=false" >> $GITHUB_OUTPUT
fi
- name: Show HEAD Commit Details
run: |
git show --no-patch --format=fuller HEAD
- name: Get Associated Pull Request
if: steps.check_merge.outputs.is_merge == 'true'
id: get_pr
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const commit_sha = '${{ github.sha }}';
const prs = await github.rest.repos.listPullRequestsAssociatedWithCommit({
owner: context.repo.owner,
repo: context.repo.repo,
commit_sha: commit_sha,
});
if (prs.data.length > 0) {
const pr = prs.data[0];
core.setOutput('pr_number', pr.number.toString());
core.setOutput('pr_body', pr.body || '');
} else {
core.setFailed('No pull request found for this commit.');
core.setOutput('pr_number', '');
core.setOutput('pr_body', '');
}
- name: Parse Pull Request Description
if: steps.get_pr.outputs.pr_number != ''
uses: actions/github-script@v6
id: parse_pr
with:
script: |
const prBody = process.env.PR_BODY;
const filenameMatch = prBody.match(/^Filename:\s*(.+)$/m);
const nameMatch = prBody.match(/^Name:\s*(.+)$/m);
const categoryMatch = prBody.match(/^Category:\s*(.+)$/m);
if (!filenameMatch) throw new Error('Filename not found or invalid in PR description.');
if (!nameMatch) throw new Error('Name not found or invalid in PR description.');
if (!categoryMatch) throw new Error('Category not found or invalid in PR description.');
core.setOutput('filename', filenameMatch[1].trim());
core.setOutput('title', nameMatch[1].trim());
core.setOutput('category', categoryMatch[1].trim());
env:
PR_BODY: ${{ steps.get_pr.outputs.pr_body }}
- name: Ensure Scripts are Executable
if: steps.get_pr.outputs.pr_number != ''
run: |
chmod +x ./append_floppy ./calc_crc32 ./upload_image
- name: Calculate CRC32 of the floppy image
if: steps.get_pr.outputs.pr_number != ''
id: calculate_crc32
run: |
# Run the CRC32 calculation and capture the output
crc32_value=$(./calc_crc32 "${{ steps.parse_pr.outputs.filename }}")
# Save the CRC32 value to an environment variable for further use
echo "crc32_value=$crc32_value" >> $GITHUB_ENV
- name: Update the database
if: steps.get_pr.outputs.pr_number != ''
run: |
./append_floppy "${{ steps.parse_pr.outputs.title }}" "${{ env.crc32_value }}" "${{ steps.parse_pr.outputs.category }}"
env:
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
- name: Upload the image if it does not exist
if: (steps.get_pr.outputs.pr_number != '') && (env.crc32_value == steps.parse_pr.outputs.filename)
run: |
./upload_image "${{ steps.parse_pr.outputs.filename }}" "MISC"
env:
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
- name: Delete the Floppy Image and remove it from the repository
if: steps.get_pr.outputs.pr_number != ''
run: |
rm "${{ steps.parse_pr.outputs.filename }}"
git rm "${{ steps.parse_pr.outputs.filename }}"
git add images_crc32.csv
git add images.log
git commit -m "Add ${{steps.parse_pr.outputs.title}}"
git push
- name: Notify Maintainers
if: failure() && steps.get_pr.outputs.pr_number != ''
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const issueNumber = parseInt('${{ steps.get_pr.outputs.pr_number }}');
github.issues.createComment({
issue_number: issueNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'An error occurred while processing your Floppy image submission. Please check the repository logs for more details.'
});