Skip to content

Artifacts

Artifacts #10

name: Create 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.startsWith('**Tags:**')) extension.tags = line.replace('**Tags:**', '').trim();
});
// Debug logs
console.log('Issue body:', body);
// Find ZIP attachment with multiple possible patterns
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);
});
console.log('Found attachments in body:', attachments);
// Check comments for attachments too
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.issue.number
});
comments.data.forEach(comment => {
attachmentPatterns.forEach(pattern => {
const matches = comment.body.match(pattern) || [];
attachments.push(...matches);
});
console.log('Found attachments in comment:', comment.body);
});
console.log('All attachments found:', attachments);
// Extract file ID from GitHub attachment URL
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. Please attach a ZIP file containing your extension.crx and icon.png files.');
}
// Convert GitHub attachment URL to raw download URL if needed
let downloadUrl = zipFile;
if (zipFile.includes('github.com')) {
const fileId = zipFile.match(/\/files\/(\d+)\//)[1];
downloadUrl = `https://github.com/user-attachments/files/${fileId}/artifact.zip`;
}
extension.zip_url = downloadUrl;
console.log('Final download URL:', downloadUrl);
return extension;
- name: Download and Process ZIP
run: |
# Create temp directory
mkdir -p temp
cd temp
# Download and extract ZIP
wget -O package.zip "${{ fromJSON(steps.parse.outputs.result).zip_url }}"
unzip package.zip
# Handle different possible file structures
if [ -d "artifact" ]; then
cd artifact
fi
# Check for files with different possible names
CRX_FILE=$(find . -name "*.crx" -o -name "build.crx" | head -n 1)
ICON_FILE=$(find . -name "icon*.png" -o -name "*.png" | head -n 1)
if [ -z "$CRX_FILE" ] || [ -z "$ICON_FILE" ]; then
echo "Missing required files (.crx file and icon)"
exit 1
fi
# Move files to final location with correct names
SAFE_NAME=$(echo "${{ fromJSON(steps.parse.outputs.result).name }}" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-zA-Z0-9-]/-/g')
mkdir -p "../../extensions/${SAFE_NAME}"
cp "$CRX_FILE" "../../extensions/${SAFE_NAME}/${SAFE_NAME}.crx"
cp "$ICON_FILE" "../../extensions/${SAFE_NAME}/icon.png"
# Clean up
cd ../..
rm -rf temp
- name: Update Extensions JSON
run: |
# Generate safe name for URLs
SAFE_NAME=$(echo "${{ fromJSON(steps.parse.outputs.result).name }}" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-zA-Z0-9-]/-/g')
# Generate URLs based on our repository and new directory structure
DOWNLOAD_URL="https://raw.githubusercontent.com/${{ github.repository }}/raw/refs/heads/main/extensions/${SAFE_NAME}/${SAFE_NAME}.crx"
ICON_URL="https://raw.githubusercontent.com/${{ github.repository }}/blob/main/extensions/${SAFE_NAME}/icon.png"
GITHUB_URL="https://github.com/${{ github.repository }}/blob/main/extensions/${SAFE_NAME}/"
# Initialize extensions.json if it doesn't exist
if [ ! -f extensions.json ]; then
echo '{"extensions": []}' > extensions.json
fi
# Process tags: split by comma, remove whitespace
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" \
--arg tags "${{ fromJSON(steps.parse.outputs.result).tags }}" \
'.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 | split(",") | map(gsub("^\\s+|\\s+$"; ""))),
"created_at": (now | strftime("%Y-%m-%dT%H:%M:%SZ")),
"updated_at": (now | strftime("%Y-%m-%dT%H:%M:%SZ"))
}]' 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"
git add extensions/
git add extensions.json
git commit -m "Add new extension: ${{ fromJSON(steps.parse.outputs.result).name }}"
git push
- 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 added!\n\nDownload URL: ${process.env.DOWNLOAD_URL}\nIcon URL: ${process.env.ICON_URL}\nGitHub URL: ${process.env.GITHUB_URL}`
});