-
Notifications
You must be signed in to change notification settings - Fork 1
303 lines (257 loc) · 11.5 KB
/
create-extension.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
name: Create or Update Extension
on:
issues:
types: [opened, edited]
jobs:
process-extension:
if: contains(github.event.issue.labels.*.name, 'new-extension')
runs-on: ubuntu-latest
permissions:
contents: write
issues: write
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Parse Issue and Download Files
id: parse
uses: actions/github-script@v6
with:
script: |
const body = context.payload.issue.body;
const lines = body.split('\n');
const extension = {};
// Parse basic info
lines.forEach(line => {
if (line.startsWith('**Name:**')) extension.name = line.replace('**Name:**', '').trim();
if (line.startsWith('**Description:**')) extension.description = line.replace('**Description:**', '').trim();
if (line.startsWith('**Version:**')) extension.version = line.replace('**Version:**', '').trim();
if (line.startsWith('**Author:**')) extension.author = line.replace('**Author:**', '').trim();
if (line.toLowerCase().includes('tags') && line.includes('comma-separated')) {
const tagsMatch = line.match(/:(.*)/);
if (tagsMatch) {
const tagsStr = tagsMatch[1].trim();
extension.tags = tagsStr.split(',').map(tag => tag.trim()).filter(tag => tag);
console.log('Found tags:', extension.tags);
}
}
});
// Find ZIP attachment
const attachmentPatterns = [
/https:\/\/user-images\.githubusercontent\.com\/[^\s)]+\.zip/g,
/https:\/\/github\.com\/[^\s)]+\/files\/[^\s)]+\.zip/g
];
let attachments = [];
attachmentPatterns.forEach(pattern => {
const matches = body.match(pattern) || [];
attachments.push(...matches);
});
const zipFile = attachments[0];
if (!zipFile) {
console.log('No ZIP file found in attachments');
throw new Error('Missing required ZIP file containing .crx and icon.');
}
// Keep the original URL for GitHub-uploaded files
extension.zip_url = zipFile;
console.log('ZIP URL:', extension.zip_url);
return extension;
- name: Create Directory Structure
run: |
mkdir -p extensions
touch extensions/.gitkeep
- name: Download and Process ZIP
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Create temp directory
mkdir -p temp
cd temp
# Get extension name and create safe name
EXTENSION_NAME="${{ fromJSON(steps.parse.outputs.result).name }}"
if [ -z "$EXTENSION_NAME" ]; then
echo "Extension name not found in issue"
exit 1
fi
# Generate safe name for directory
SAFE_NAME=$(echo "$EXTENSION_NAME" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-zA-Z0-9-]/-/g')
echo "Extension name: $EXTENSION_NAME"
echo "Safe name for directory: $SAFE_NAME"
# Create extension directory early
mkdir -p "../extensions/${SAFE_NAME}"
echo "Created directory: ../extensions/${SAFE_NAME}"
# Download and extract ZIP
ZIP_URL="${{ fromJSON(steps.parse.outputs.result).zip_url }}"
echo "Original ZIP URL: $ZIP_URL"
if [[ "$ZIP_URL" == *"user-images.githubusercontent.com"* ]]; then
curl -L -o package.zip "$ZIP_URL"
else
FILE_ID=$(echo "$ZIP_URL" | grep -o '/files/[0-9]\+/' | grep -o '[0-9]\+')
echo "File ID: $FILE_ID"
DOWNLOAD_URL=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/comments" | \
jq -r --arg file_id "$FILE_ID" '.[] |
select(.body | contains($file_id)) |
capture("(?<url>https://github\\.com/[^)]+\\.zip)") |
.url')
echo "Download URL from API: $DOWNLOAD_URL"
if [ -z "$DOWNLOAD_URL" ]; then
DOWNLOAD_URL=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.issue.number }}" | \
jq -r --arg file_id "$FILE_ID" '.body |
capture("(?<url>https://github\\.com/[^)]+\\.zip)") |
.url')
fi
if [ -z "$DOWNLOAD_URL" ]; then
DOWNLOAD_URL="$ZIP_URL"
fi
echo "Final download URL: $DOWNLOAD_URL"
curl -L -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/octet-stream" \
-o package.zip "$DOWNLOAD_URL"
fi
if [ ! -f package.zip ]; then
echo "Failed to download ZIP file"
exit 1
fi
echo "Download successful. File size:"
ls -lh package.zip
echo "Extracting ZIP file..."
unzip -v package.zip
unzip package.zip
echo "Current directory contents:"
ls -la
echo "Parent directory contents:"
ls -la ..
echo "Extensions directory contents:"
ls -la ../extensions/
# Find extracted directory (could be any name)
EXTRACTED_DIR=$(find . -type d -mindepth 1 -maxdepth 1 | head -n 1)
if [ -n "$EXTRACTED_DIR" ]; then
cd "$EXTRACTED_DIR"
echo "Entered extracted directory: $EXTRACTED_DIR"
echo "Contents:"
ls -la
fi
# Check for files with more flexible patterns
CRX_FILE=$(find . -type f -name "*.crx" | head -n 1)
if [ -z "$CRX_FILE" ]; then
echo "Looking for alternative CRX file..."
CRX_FILE=$(find . -type f -name "build.crx" -o -name "extension.crx" | head -n 1)
fi
ICON_FILE=$(find . -type f -name "icon*.png" | head -n 1)
if [ -z "$ICON_FILE" ]; then
echo "Looking for alternative icon file..."
ICON_FILE=$(find . -type f -name "*.png" | head -n 1)
fi
if [ -z "$CRX_FILE" ] || [ -z "$ICON_FILE" ]; then
echo "Missing required files"
echo "CRX file found: $CRX_FILE"
echo "Icon file found: $ICON_FILE"
echo "Directory contents:"
find . -type f
exit 1
fi
echo "Found files:"
echo "CRX: $CRX_FILE"
echo "Icon: $ICON_FILE"
# Copy files to final location
echo "Copying files to: ../extensions/${SAFE_NAME}/"
cp "$CRX_FILE" "../extensions/${SAFE_NAME}/${SAFE_NAME}.crx"
cp "$ICON_FILE" "../extensions/${SAFE_NAME}/icon.png"
# Verify files were copied
echo "Final extension directory contents:"
ls -la "../extensions/${SAFE_NAME}/"
# Clean up
cd ..
rm -rf temp
- name: Update Extensions JSON
run: |
SAFE_NAME=$(echo "${{ fromJSON(steps.parse.outputs.result).name }}" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-zA-Z0-9-]/-/g')
# Generate URLs using GitHub API format
REPO="${{ github.repository }}"
BRANCH="main"
PATH_PREFIX="extensions/${SAFE_NAME}"
DOWNLOAD_URL="https://api.github.com/repos/${REPO}/contents/${PATH_PREFIX}/${SAFE_NAME}.crx?ref=${BRANCH}"
ICON_URL="https://api.github.com/repos/${REPO}/contents/${PATH_PREFIX}/icon.png?ref=${BRANCH}"
GITHUB_URL="https://github.com/${REPO}/tree/${BRANCH}/${PATH_PREFIX}"
# Initialize extensions.json if it doesn't exist
if [ ! -f extensions.json ]; then
echo '{"extensions": []}' > extensions.json
fi
# Convert tags array to JSON string
TAGS_JSON=$(echo '${{ toJSON(fromJSON(steps.parse.outputs.result).tags) }}')
# Update existing entry or add new one
jq --arg name "${{ fromJSON(steps.parse.outputs.result).name }}" \
--arg desc "${{ fromJSON(steps.parse.outputs.result).description }}" \
--arg ver "${{ fromJSON(steps.parse.outputs.result).version }}" \
--arg author "${{ fromJSON(steps.parse.outputs.result).author }}" \
--arg download "$DOWNLOAD_URL" \
--arg icon "$ICON_URL" \
--arg github "$GITHUB_URL" \
--argjson tags "$TAGS_JSON" \
--arg now "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \
'
def update_extension:
if .name == $name then
. + {
"description": $desc,
"version": $ver,
"author": $author,
"download_url": $download,
"icon_url": $icon,
"github_url": $github,
"tags": $tags,
"updated_at": $now
}
else
.
end;
if (.extensions | map(.name == $name) | any)
then
.extensions |= map(update_extension)
else
.extensions += [{
"id": ((.extensions | length) + 1 | tostring),
"name": $name,
"description": $desc,
"version": $ver,
"author": $author,
"download_url": $download,
"icon_url": $icon,
"github_url": $github,
"tags": $tags,
"created_at": $now,
"updated_at": $now
}]
end
' extensions.json > temp.json && mv temp.json extensions.json
- name: Commit and Push Changes
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
# Create directories if they don't exist
mkdir -p extensions
# Stage all changes
git add -f extensions/
git add -f extensions.json
# Only commit if there are changes
if git diff --staged --quiet; then
echo "No changes to commit"
else
git commit -m "Update extension: ${{ fromJSON(steps.parse.outputs.result).name }}"
git push
fi
- name: Comment on Issue
uses: actions/github-script@v6
with:
script: |
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.issue.number,
body: `Extension has been successfully ${context.payload.action === 'opened' ? 'added' : 'updated'}!\n\nDownload URL: ${process.env.DOWNLOAD_URL}\nIcon URL: ${process.env.ICON_URL}\nGitHub URL: ${process.env.GITHUB_URL}`
});