Skip to content

Commit

Permalink
Merge pull request #26 from michaellatman/add-pr-check
Browse files Browse the repository at this point in the history
  • Loading branch information
michaellatman authored Dec 5, 2024
2 parents 4ccb3e3 + f763558 commit 917e601
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"version:patch": "npm version patch",
"version:minor": "npm version minor",
"version:major": "npm version major",
"publish:npm": "npm run build && npm publish --access public"
"publish:npm": "npm run build && npm publish --access public",
"pr-check": "node src/scripts/pr-check.js"
},
"bin": {
"mcp-get": "dist/index.js"
Expand Down
32 changes: 32 additions & 0 deletions src/.github/workflows/pr-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: PR Check

on:
pull_request:
branches:
- main

jobs:
pr-check:
runs-on: ubuntu-latest

steps:
- name: Check out repository
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '18'

- name: Install dependencies
run: npm ci

- name: Run PR check script
run: npm run pr-check

- name: Provide feedback
run: |
COMMENT="PR check completed. Please review the results."
gh pr comment ${{ github.event.pull_request.number }} --body "$COMMENT"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
89 changes: 89 additions & 0 deletions src/scripts/pr-check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const { Octokit } = require('@octokit/rest');

const REQUIRED_FIELDS = ['name', 'description', 'vendor', 'sourceUrl', 'homepage', 'license', 'runtime'];

async function validatePackages() {
const packageListPath = path.join(__dirname, '../../packages/package-list.json');
const packageList = JSON.parse(fs.readFileSync(packageListPath, 'utf-8'));

const newPackages = getNewPackages(packageList);

for (const pkg of newPackages) {
validateRequiredFields(pkg);
await checkNpmPublication(pkg.name);
}

await provideFeedback(newPackages);
}

function getNewPackages(packageList) {
const baseBranch = 'main';
const diffOutput = execSync(`git diff origin/${baseBranch} -- packages/package-list.json`).toString();
const newPackages = [];

const diffLines = diffOutput.split('\n');
let currentPackage = null;

for (const line of diffLines) {
if (line.startsWith('+') && !line.startsWith('+++')) {
const trimmedLine = line.substring(1).trim();
if (trimmedLine.startsWith('{')) {
currentPackage = {};
} else if (trimmedLine.startsWith('}')) {
if (currentPackage) {
newPackages.push(currentPackage);
currentPackage = null;
}
} else if (currentPackage) {
const [key, value] = trimmedLine.split(':').map(s => s.trim().replace(/,$/, '').replace(/"/g, ''));
currentPackage[key] = value;
}
}
}

return newPackages;
}

function validateRequiredFields(pkg) {
for (const field of REQUIRED_FIELDS) {
if (!pkg[field]) {
throw new Error(`Package ${pkg.name} is missing required field: ${field}`);
}
}
}

async function checkNpmPublication(packageName) {
try {
execSync(`npm view ${packageName}`);
} catch (error) {
throw new Error(`Package ${packageName} is not published on npm`);
}
}

async function provideFeedback(newPackages) {
const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN });
const { data: pullRequest } = await octokit.pulls.get({
owner: process.env.GITHUB_REPOSITORY_OWNER,
repo: process.env.GITHUB_REPOSITORY,
pull_number: process.env.GITHUB_PULL_REQUEST_NUMBER
});

const feedback = newPackages.length > 0
? `The following new packages were validated successfully:\n${newPackages.map(pkg => `- ${pkg.name}`).join('\n')}`
: 'No new packages were added in this PR.';

await octokit.issues.createComment({
owner: process.env.GITHUB_REPOSITORY_OWNER,
repo: process.env.GITHUB_REPOSITORY,
issue_number: pullRequest.number,
body: feedback
});
}

validatePackages().catch(error => {
console.error(error);
process.exit(1);
});

0 comments on commit 917e601

Please sign in to comment.