Skip to content

Artifacts

Artifacts #19

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);
}
}
});
// Debug logs
console.log('Parsed extension data:', extension);
// 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.');
}
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;
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
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
# Generate safe name and prepare directory
SAFE_NAME=$(echo "${{ fromJSON(steps.parse.outputs.result).name }}" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-zA-Z0-9-]/-/g')
mkdir -p "../../extensions/${SAFE_NAME}"
# Move files to final location
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: |
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"
git add extensions/
git add extensions.json
git commit -m "Update 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 ${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}`
});