Skip to content

Commit

Permalink
add warnings and dependency outdated check
Browse files Browse the repository at this point in the history
  • Loading branch information
whitelisab committed Aug 7, 2024
1 parent 635b9fa commit e055145
Show file tree
Hide file tree
Showing 5 changed files with 293 additions and 103 deletions.
42 changes: 39 additions & 3 deletions .github/workflows/app-review-utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
const { exec } = require('child_process');
const util = require('util');

const execPromise = util.promisify(exec);

const FAILURE_LABEL = 'Further development recommended';
const SUCCESS_LABEL = 'Ready for review';

Expand All @@ -17,9 +22,19 @@ const getNewAppDirectories = (files) => {
return [...new Set(newAppDirs)];
};

const installAppDependencies = async (newAppDir) => {
try {
await execPromise(`(cd ${newAppDir} && npm ci)`);
} catch (error) {
console.error(`Failed to install app dependencies for ${newAppDir}: ${error}`);
}
};

const validateNewApps = async (validators, { github, context, core }, newAppDirs, files) => {
const failures = {};
const warnings = {};
for (const newAppDir of newAppDirs) {
await installAppDependencies(newAppDir);
for (const [check, validator] of Object.entries(validators)) {
if (typeof validator.validate === 'function') {
const validation = await validator.validate({ github, context, core }, newAppDir, files);
Expand All @@ -28,19 +43,25 @@ const validateNewApps = async (validators, { github, context, core }, newAppDirs
if (!validation.result) {
failures[check] = validation.message;
}
if (validation.warning) {
warnings[check] = validation.warning;
}
}
}
}
return failures;
return {
failures,
warnings,
};
};

const handleValidationFailures = async (github, context, prNumber, failures) => {
const comment_body = '😡\n' + Object.values(failures).join('\n');
const commentBody = '😡\n' + Object.values(failures).join('\n');

await github.rest.issues.createComment({
...context.repo,
issue_number: prNumber,
body: comment_body,
body: commentBody,
});

try {
Expand Down Expand Up @@ -73,6 +94,20 @@ const handleValidationSuccess = async (github, context, prNumber) => {
});
};

const handleValidationWarnings = async (github, context, prNumber, warnings) => {
const commentBody =
'Please acknowledge the following warnings: ' +
Object.values(warnings)
.map((warning) => `- [ ] ${warning}`)
.join('\n');

await github.rest.issues.createComment({
...context.repo,
issue_number: prNumber,
body: commentBody,
});
};

const hasPackageJson = async (files, newAppDir) => !!files.find((file) => file.status === 'added' && file.filename.startsWith(`${newAppDir}/package.json`));

module.exports = {
Expand All @@ -81,5 +116,6 @@ module.exports = {
validateNewApps,
handleValidationFailures,
handleValidationSuccess,
handleValidationWarnings,
hasPackageJson,
};
15 changes: 13 additions & 2 deletions .github/workflows/new-app-review/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
const { getPullRequestFiles, getNewAppDirectories, validateNewApps, handleValidationFailures, handleValidationSuccess } = require('../app-review-utils.js');
const {
getPullRequestFiles,
getNewAppDirectories,
validateNewApps,
handleValidationFailures,
handleValidationSuccess,
handleValidationWarnings,
} = require('../app-review-utils.js');
const fs = require('fs');
const path = require('path');

Expand Down Expand Up @@ -31,7 +38,11 @@ async function review({ github, context, core }) {

console.log('New app submissions found:', newAppDirs);

const failures = await validateNewApps(validators, { github, context, core }, newAppDirs, files);
const { failures, warnings } = await validateNewApps(validators, { github, context, core }, newAppDirs, files);

if (Object.keys(warnings).length > 0) {
await handleValidationWarnings(github, context, prNumber, warnings);
}

if (Object.keys(failures).length > 0) {
await handleValidationFailures(github, context, prNumber, failures);
Expand Down
41 changes: 41 additions & 0 deletions .github/workflows/new-app-review/validators/dependencies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { exec } = require('child_process');
const util = require('util');

const execPromise = util.promisify(exec);

module.exports = {
validate: async ({ github, context, core }, newAppDir, files) => {
let warning = '';
let result = true;
let message = 'Dependency check finished';
try {
const { stdout, stderr } = await execPromise(`pwd`); //cd ${newAppDir} && npm outdated --json`);
console.log({ stdout, stderr });

if (stderr) {
result = false;
message = 'Unable to check for outdated dependencies';
console.error(`stderr: ${stderr}`);
}

console.log(stdout);
const outdatedDependencies = JSON.parse(stdout);

if (Object.keys(outdatedDependencies).length > 0) {
warning += 'The following dependencies are outdated:\n';
warning += outdatedDependencies;
console.log(warning);
}
} catch (error) {
message = 'Failed to check for outdated dependencies';
console.error(`exec error: ${error}`);
result = false;
}

return {
result,
message,
warning,
};
},
};
Loading

0 comments on commit e055145

Please sign in to comment.