Skip to content

Commit 7b6253f

Browse files
committed
Initial commit
0 parents  commit 7b6253f

File tree

206 files changed

+143047
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

206 files changed

+143047
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { createRequire } from 'module';
2+
import fetch from 'node-fetch';
3+
import yaml from 'js-yaml';
4+
5+
const require = createRequire(import.meta.url);
6+
const fs = require('fs');
7+
8+
const owner = process.env.REPO_OWNER;
9+
const repo = process.env.REPO_NAME;
10+
const pull_number = process.env.PR_NUMBER;
11+
12+
const requiredKeys = [
13+
'name',
14+
'description',
15+
'repo',
16+
'icon',
17+
'framework',
18+
'program_address',
19+
'categories'
20+
];
21+
22+
async function validateAndMerge() {
23+
const { Octokit } = await import('@octokit/rest');
24+
25+
const octokit = new Octokit({
26+
auth: process.env.GITHUB_TOKEN,
27+
request: {
28+
fetch: fetch,
29+
},
30+
});
31+
32+
try {
33+
const { data: pr } = await octokit.pulls.get({
34+
owner,
35+
repo,
36+
pull_number,
37+
});
38+
39+
const { data: files } = await octokit.pulls.listFiles({
40+
owner,
41+
repo,
42+
pull_number,
43+
});
44+
45+
const yamlFile = files.find(file => file.filename.endsWith('.yaml') || file.filename.endsWith('.yml'));
46+
47+
if (!yamlFile) {
48+
console.log('No YAML file found in the PR');
49+
return;
50+
}
51+
52+
const { data: fileContent } = await octokit.repos.getContent({
53+
owner,
54+
repo,
55+
path: yamlFile.filename,
56+
ref: pr.head.ref,
57+
});
58+
59+
const content = Buffer.from(fileContent.content, 'base64').toString('utf-8');
60+
61+
// Parse all YAML documents in the file
62+
const yamlDocuments = yaml.load(content);
63+
64+
for (const document of yamlDocuments) {
65+
const isValid = validateSubdocument(document);
66+
if (!isValid) {
67+
console.log(`Invalid subdocument: ${JSON.stringify(document)}`);
68+
return;
69+
}
70+
}
71+
72+
await octokit.pulls.merge({
73+
owner,
74+
repo,
75+
pull_number,
76+
merge_method: 'squash',
77+
});
78+
79+
console.log('PR automatically merged');
80+
} catch (error) {
81+
console.error('Error:', error.message);
82+
}
83+
}
84+
85+
function validateSubdocument(subdocument) {
86+
const isValid = requiredKeys.every(key => key in subdocument);
87+
88+
if (!isValid) {
89+
console.log(`Subdocument is missing required keys: ${JSON.stringify(subdocument)}`);
90+
return false;
91+
}
92+
93+
if (!Array.isArray(subdocument.categories) || subdocument.categories.length === 0) {
94+
console.log('Categories must be a non-empty array');
95+
return false;
96+
}
97+
98+
return true;
99+
}
100+
101+
validateAndMerge();

.github/workflows/auto-merge.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
name: Auto-merge PR
3+
4+
on:
5+
pull_request:
6+
types: [opened, synchronize, reopened]
7+
8+
jobs:
9+
auto-merge:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v3
13+
- uses: actions/setup-node@v3
14+
with:
15+
node-version: '18'
16+
- name: Install dependencies
17+
run: npm install
18+
- name: Validate and auto-merge PR
19+
env:
20+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
21+
PR_NUMBER: ${{ github.event.pull_request.number }}
22+
REPO_OWNER: ${{ github.repository_owner }}
23+
REPO_NAME: ${{ github.event.repository.name }}
24+
run: node --experimental-modules .github/scripts/validate-and-merge.mjs

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
node_modules

node_modules/@octokit/auth-token/LICENSE

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)