Skip to content

Commit

Permalink
Add basic automation
Browse files Browse the repository at this point in the history
  • Loading branch information
fuksman committed Dec 11, 2023
1 parent ec76bf5 commit 9407818
Show file tree
Hide file tree
Showing 6 changed files with 283 additions and 0 deletions.
68 changes: 68 additions & 0 deletions .github/workflows/manage-experience.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
on:
issue_comment:
types: [created]

name: Manage Experience
jobs:
manage-experience:
if: |
github.event.issue.author_association == 'MEMBER' &&
github.event.issue.state == 'open' &&
contains(github.event.comment.body, '/gallery')
runs-on: ubuntu-latest
permissions:
issues: write
contents: write
pull-requests: write
steps:
- uses: actions/checkout@v4
- uses: stefanbuck/github-issue-parser@v3
id: issue-parser
with:
template-path: .github/ISSUE_TEMPLATE/new_experience.yml
- run: |
echo "experience_name=$(echo ${{ steps.issue-parser.outputs.issueparser_title }} | sed -e 's/[^[:alnum:]]/_/g' | tr -s '_' | tr A-Z a-z)" >> "$GITHUB_ENV"
echo "command=$(echo ${{ github.event.comment.body }} | cut -d' ' -f2)" >> "$GITHUB_ENV"
- run: echo "pr=$(gh pr view ${{ env.experience_name }} --json=number -q='.number')" >> "$GITHUB_ENV"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- id: reject
if: env.command == 'reject'
run: |
gh pr close ${{ env.pr }} --delete-branch
gh issue close ${{ github.event.issue.number }} -c "Rejected :disappointed: Feel free to submit another Experience!"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- if: env.command == 'approve'
uses: actions/checkout@v4
with:
ref: ${{ env.experience_name }}
token: ${{ secrets.ANY_CLA_TOKEN }}
- id: upload-to-cdn
if: env.command == 'approve'
run: |
rm -f experiences/${{ env.experience_name }}/test-manifest.json
aws s3 cp --recursive experiences/${{ env.experience_name }} s3://${{ env.experience_name }}/
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: 'auto'
AWS_ENDPOINT_URL: ${{ secrets.AWS_ENDPOINT_URL }}
- id: commit-changes
if: env.command == 'approve'
run: |
git config --global user.email "[email protected]"
git config --global user.name "Any Association"
git add .
if ! git diff-index --quiet HEAD; then
git commit -m "Prepare ${{ steps.issue-parser.outputs.issueparser_title }} for publishing"
git push origin ${{ env.experience_name }}
fi
- id: merge-pr
if: env.command == 'approve'
run: |
gh issue comment ${{ github.event.issue.number }} -b "@any contributor @${{ github.event.issue.user.login }} gallery"
gh issue close ${{ github.event.issue.number }} -c "Approved! :tada: Thank you for your contribution!"
gh pr merge ${{ env.pr }} --squash --delete-branch
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
56 changes: 56 additions & 0 deletions .github/workflows/parse-new-experience.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
on:
issues:
types: [labeled]

name: Parse New Experience
jobs:
new_experience:
if: github.event.label.name == 'experience'
runs-on: ubuntu-latest
permissions:
issues: write
contents: write
pull-requests: write
steps:
- uses: actions/checkout@v4
- uses: stefanbuck/github-issue-parser@v3
id: issue-parser
with:
template-path: .github/ISSUE_TEMPLATE/new_experience.yml
- run: echo '${{ steps.issue-parser.outputs.jsonString }}' > issue.json
- run: |
echo "experience_name=$(echo ${{ steps.issue-parser.outputs.issueparser_title }} | sed -e 's/[^[:alnum:]]/_/g' | tr -s '_' | tr A-Z a-z)" >> "$GITHUB_ENV"
echo "id=$(uuidgen)" >> "$GITHUB_ENV"
- run: |
jq '. + {"author": "${{ github.event.issue.user.login }}", "name": "${{ env.experience_name }}", "id": "${{ env.id }}"}' issue.json > tmp && mv tmp issue.json
- id: manifest-builder
run: |
node tools/prepare-experience.js
echo "test-manifest=anytype://main/import/?type=experience&source=https%3A%2F%2Fgithub.com%2Fanyproto%2Fgallery%2Fraw%2F${{ env.experience_name }}%2Fexperiences%2F${{ env.experience_name }}%2Ftest-manifest.json" >> "$GITHUB_ENV"
- run: rm issue.json
- name: Create Pull Request
id: cpr
uses: peter-evans/create-pull-request@v5
with:
commit-message: "Add preview for ${{ steps.issue-parser.outputs.issueparser_title }}"
committer: "Any Association <[email protected]>"
branch: ${{ env.experience_name }}
delete-branch: true
title: "Add experience ${{ steps.issue-parser.outputs.issueparser_title }}"
body: "Based on issue #${{ github.event.issue.number }}"
- name: Comment on Issue
run: gh issue comment "$NUMBER" --body "$BODY"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
NUMBER: ${{ github.event.issue.number }}
BODY: >
Thanks for your contribution! :tada:
I have created a pull request #${{ steps.cpr.outputs.pull-request-number }} based on data you provided.
If everything worked correctly, you can test the Experience:
[${{ env.test-manifest }}](${{ env.test-manifest }})
The Experience will be added to the Gallery after someone from the Any team reviews it.
25 changes: 25 additions & 0 deletions .github/workflows/prepare-index.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
on:
pull_request:
types: [closed]
workflow_dispatch:

name: "Prepare Gallery Index"
jobs:
prepare-index:
if: github.event.pull_request.merged == true || github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.ANY_CLA_TOKEN }}
- run: |
git config --global user.email "[email protected]"
git config --global user.name "Any Association"
node tools/prepare-index.js
git add .
if ! git diff-index --quiet HEAD; then
git commit -m "Update Gallery Index"
git push origin main
fi
3 changes: 3 additions & 0 deletions tools/featured.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
"knowledge_base"
]
90 changes: 90 additions & 0 deletions tools/prepare-experience.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const fs = require('fs');
const issue = require('../issue.json');
const [issueExperience, issueTitle, issueScreenshot, issueDescription, issueCategories, issueLanguage, issueAuthor, name, id] = Object.values(issue);

const author = "https://github.com/" + issueAuthor;
const license = "MIT";
const categories = issueCategories.split(", ");

var experience = {
$schema: "https://tools.gallery.any.coop/experience.schema.json",
id: id,
name: name,
author: author,
license: license,
title: issueTitle,
description: issueDescription,
categories: categories,
language: issueLanguage,
screenshots: [],
downloadLink: "",
fileSize: 0
}

if (!fs.existsSync('experiences')) {
fs.mkdirSync('experiences');
}

const dir = `experiences/${name}`;
if (fs.existsSync(dir)) {
console.log(`Experience with name ${name} already exists`);
} else {
fs.mkdirSync(dir);
}

var regex = /https:\/\/github.com\/.*\/.*\/.*\/.*\.zip/g;
const zipLink = issueExperience.match(regex)[0];

if (!zipLink) {
throw new Error(`Experience with name ${name} has no link to zip archive`);
}

(async () => {
const response = await fetch(zipLink);
const buffer = await response.arrayBuffer();
experience.fileSize = buffer.byteLength;
fs.writeFileSync(`${dir}/${name}.zip`, Buffer.from(buffer));
})();

const testDownloadLink = "https://github.com/anyproto/gallery/raw/" + name + "/experiences/" + name + "/" + name + ".zip"
const prodDownloadLink = "https://storage.gallery.any.coop/" + name + "/" + name + ".zip"

var regex = /src\s*=\s*"(.+?)"/g;
const extension = "png";
const screenshotLinks = issueScreenshot.match(regex);
const screenshotsDir = `${dir}/screenshots`;

if (!screenshotLinks) {
throw new Error(`Experience with name ${name} has no screenshots`);
}

var testScreenshotLinks = [];
var prodScreenshotLinks = [];
for (var i = 1; i <= screenshotLinks.length; i++) {
testScreenshotLinks.push("https://github.com/anyproto/gallery/raw/" + name + "/experiences/" + name + "/screenshots/screenshot-" + i + "." + extension);
prodScreenshotLinks.push("https://storage.gallery.any.coop/" + name + "/screenshots/screenshot-" + i + "." + extension);
}


if (fs.existsSync(screenshotsDir)) {
console.log(`Screenshots for experience with name ${name} already exists`);
} else {
fs.mkdirSync(screenshotsDir);
}

var count = 1;
screenshotLinks.forEach(async (link) => {
const response = await fetch(link.replace(/src="/, '').replace(/"/, ''));
const buffer = await response.arrayBuffer();
fs.writeFileSync(`${screenshotsDir}/screenshot-${count}.${extension}`, Buffer.from(buffer));
count++;
});


experience.downloadLink = testDownloadLink
experience.screenshots = testScreenshotLinks
fs.writeFileSync(`${dir}/test-manifest.json`, JSON.stringify(experience, null, 2));

experience.downloadLink = prodDownloadLink
experience.screenshots = prodScreenshotLinks
fs.writeFileSync(`${dir}/manifest.json`, JSON.stringify(experience, null, 2));
41 changes: 41 additions & 0 deletions tools/prepare-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 1. create an object where each key is a category name
// 2. fill keys based on the experience.schema.json
// 3. add "Featured" and "Made by Any"
// 4. go through each expierence's manifest and add it's name to the corresponding categories
// 5. save the object to index.json rewriting the old one

const fs = require('fs');
const schema = require('./experience.schema.json');
const featured = require('./featured.json');

var categories = schema.$defs.category.enum;
categories.push("Featured");
categories.push("Made by Any");

var index = {
"categories": {},
"experiences": {}
};

for (var i = 0; i < categories.length; i++) {
index.categories[categories[i]] = [];
}

index.categories["Featured"] = featured;

const experiences = fs.readdirSync('experiences').filter(file => fs.statSync(`experiences/${file}`).isDirectory());
for (var i = 0; i < experiences.length; i++) {
const experienceName = experiences[i];
const manifest = require(`../experiences/${experienceName}/manifest.json`);
const experienceCategories = manifest.categories;
for (var j = 0; j < experienceCategories.length; j++) {
const category = experienceCategories[j];
index.categories[category].push(experienceName);
}
if (manifest.author == "https://github.com/any-association") {
index.categories["Made by Any"].push(experienceName);
}
index.experiences[experienceName] = manifest;
}

fs.writeFileSync('tools/index.json', JSON.stringify(index, null, 2));

0 comments on commit 9407818

Please sign in to comment.