Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dandlezzz committed Oct 14, 2024
0 parents commit 7b6253f
Show file tree
Hide file tree
Showing 206 changed files with 143,047 additions and 0 deletions.
101 changes: 101 additions & 0 deletions .github/scripts/validate-and-merge.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { createRequire } from 'module';
import fetch from 'node-fetch';
import yaml from 'js-yaml';

const require = createRequire(import.meta.url);
const fs = require('fs');

const owner = process.env.REPO_OWNER;
const repo = process.env.REPO_NAME;
const pull_number = process.env.PR_NUMBER;

const requiredKeys = [
'name',
'description',
'repo',
'icon',
'framework',
'program_address',
'categories'
];

async function validateAndMerge() {
const { Octokit } = await import('@octokit/rest');

const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
request: {
fetch: fetch,
},
});

try {
const { data: pr } = await octokit.pulls.get({
owner,
repo,
pull_number,
});

const { data: files } = await octokit.pulls.listFiles({
owner,
repo,
pull_number,
});

const yamlFile = files.find(file => file.filename.endsWith('.yaml') || file.filename.endsWith('.yml'));

if (!yamlFile) {
console.log('No YAML file found in the PR');
return;
}

const { data: fileContent } = await octokit.repos.getContent({
owner,
repo,
path: yamlFile.filename,
ref: pr.head.ref,
});

const content = Buffer.from(fileContent.content, 'base64').toString('utf-8');

// Parse all YAML documents in the file
const yamlDocuments = yaml.load(content);

for (const document of yamlDocuments) {
const isValid = validateSubdocument(document);
if (!isValid) {
console.log(`Invalid subdocument: ${JSON.stringify(document)}`);
return;
}
}

await octokit.pulls.merge({
owner,
repo,
pull_number,
merge_method: 'squash',
});

console.log('PR automatically merged');
} catch (error) {
console.error('Error:', error.message);
}
}

function validateSubdocument(subdocument) {
const isValid = requiredKeys.every(key => key in subdocument);

if (!isValid) {
console.log(`Subdocument is missing required keys: ${JSON.stringify(subdocument)}`);
return false;
}

if (!Array.isArray(subdocument.categories) || subdocument.categories.length === 0) {
console.log('Categories must be a non-empty array');
return false;
}

return true;
}

validateAndMerge();
24 changes: 24 additions & 0 deletions .github/workflows/auto-merge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

name: Auto-merge PR

on:
pull_request:
types: [opened, synchronize, reopened]

jobs:
auto-merge:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install dependencies
run: npm install
- name: Validate and auto-merge PR
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
REPO_OWNER: ${{ github.repository_owner }}
REPO_NAME: ${{ github.event.repository.name }}
run: node --experimental-modules .github/scripts/validate-and-merge.mjs
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

node_modules
21 changes: 21 additions & 0 deletions node_modules/@octokit/auth-token/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7b6253f

Please sign in to comment.