Install workflow from .github/workflows/optimize.yaml

This commit is contained in:
afik.cohen 2023-11-13 21:08:21 -08:00
parent 6878ec3358
commit f46bc244b4

View file

@ -3,6 +3,7 @@ import bodyParser from 'body-parser';
// import { createWorkflow } from './setupAction'; // Adjust the path if necessary
import {App} from "octokit";
import fs from 'fs';
import path from 'path';
const expressApp = express();
expressApp.use(bodyParser.json());
@ -15,66 +16,54 @@ const CLIENT_SECRET: string = process.env.CLIENT_SECRET || ''; // Replace with y
const app = new App({
appId: APP_ID,
privateKey: PRIVATE_KEY,
appId: APP_ID,
privateKey: PRIVATE_KEY,
});
expressApp.post('/webhook', async (req: Request, res: Response) => {
console.log('Received webhook:', req.body);
console.log('Received webhook:', req.body);
try {
if (req.body.action === 'added' && req.body.installation) {
// Extract necessary info from the webhook payload
const repoOwner = req.body.installation.account.login;
const repoName = req.body.repositories_added[0].name;
const installationId = req.body.installation.id;
try {
if (req.body.action === 'added' && req.body.installation) {
// Extract necessary info from the webhook payload
const repoOwner = req.body.installation.account.login;
const repoName = req.body.repositories_added[0].name;
const installationId = req.body.installation.id;
// Call the setupAction function
const installationOctokit = await app.getInstallationOctokit(installationId);
// Call the setupAction function
const installationOctokit = await app.getInstallationOctokit(installationId);
// await createWorkflow(repoOwner, repoName, octokit);
const content = Buffer.from(`
name: Code Optimization
// await createWorkflow(repoOwner, repoName, octokit);
const git = require('simple-git')();
const rootDir = await git.revparse(['--show-toplevel']);
const workflowFilePath = path.join(rootDir, '.github/workflows/optimize.yaml');
const workflowContent = fs.readFileSync(workflowFilePath, 'utf8');
const content = Buffer.from(workflowContent).toString('base64');
on:
push:
branches:
- main
await installationOctokit.rest.repos.createOrUpdateFileContents({
owner: repoOwner,
repo: repoName,
path: '.github/workflows/optimize.yml',
message: 'Setup Code Optimization action',
content: Buffer.from(content).toString('base64'),
});
jobs:
optimize:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: CodeFlash/optimize-action@main
with:
token:
`).toString('base64');
await installationOctokit.rest.repos.createOrUpdateFileContents({
owner: repoOwner,
repo: repoName,
path: '.github/workflows/optimize.yml',
message: 'Setup Code Optimization action',
content: Buffer.from(content).toString('base64'),
});
console.log('Workflow setup successfully');
console.log(installationOctokit);
console.log('Workflow setup successfully');
console.log(installationOctokit);
}
} catch (error: unknown) {
if (error instanceof Error) {
console.error('Error setting up workflow:', error.message);
res.status(500).send(`Internal Server Error: ${error.message}`);
} else {
console.error('Error setting up workflow:', error);
res.status(500).send('Internal Server Error');
}
return;
}
} catch (error: unknown) {
if (error instanceof Error) {
console.error('Error setting up workflow:', error.message);
res.status(500).send(`Internal Server Error: ${error.message}`);
} else {
console.error('Error setting up workflow:', error);
res.status(500).send('Internal Server Error');
}
return;
}
res.status(200).send('Webhook received and processed');
res.status(200).send('Webhook received and processed');
});
const PORT = 3000;