Skip to content

Commit

Permalink
Drop nodegit, use lambda-git instead (#6)
Browse files Browse the repository at this point in the history
`nodegit` caused all sorts of issues, and it takes too long to compile whether locally or on CI.  Even worse, due to [a bug in `serverless-webpack`](serverless-heaven/serverless-webpack#309) we have to compile it twice, which takes over 15 minutes on Travis.

This PR drops `nodegit` and replaces it with a regular `git` binary [compiled specifically for AWS Lambda](https://github.com/pimterry/lambda-git).

This may or may not work!
  • Loading branch information
forabi authored Feb 28, 2018
1 parent 64c8f8e commit 0c053f2
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 185 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ build
# Created by Serverless
.build
.webpack
.serverless

# We use yarn instead, but serverless-webpack uses npm
# to install the dependencies before deploying the functions.
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@
"lint-staged": "lint-staged"
},
"dependencies": {
"@hollowverse/common": "hollowverse/common",
"@octokit/rest": "^14.0.9",
"aws-sdk": "^2.197.0",
"bluebird": "^3.5.1",
"common-tags": "^1.7.2",
"date-fns": "^1.29.0",
"got": "^8.1.0",
"lambda-git": "^0.1.1",
"lighthouse": "^2.9.1",
"lighthouse-security": "^0.6.0",
"lodash": "^4.17.5",
"nodegit": "^0.20.3",
"prettier": "^1.10.2",
"shelljs": "^0.8.1",
"tmp": "^0.0.33",
Expand All @@ -42,16 +43,15 @@
"@babel/core": "^7.0.0-beta.40",
"@babel/polyfill": "^7.0.0-beta.40",
"@babel/preset-env": "^7.0.0-beta.40",
"@hollowverse/common": "hollowverse/common",
"@hollowverse/validate-filenames": "^1.3.5",
"@types/aws-lambda": "^0.0.32",
"@types/bluebird": "^3.5.20",
"@types/common-tags": "^1.4.0",
"@types/got": "^7.1.7",
"@types/lodash": "^4.14.104",
"@types/node": "6",
"@types/nodegit": "^0.18.5",
"@types/prettier": "^1.10.0",
"@types/shelljs": "^0.7.8",
"@types/tmp": "^0.0.33",
"@types/webpack": "^3.8.8",
"babel-eslint": "^8.2.1",
Expand Down
120 changes: 44 additions & 76 deletions src/runReporters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@
// This is how `@types/*` packages can be used in TypeScript.
//
// Not to be confused with https://www.npmjs.com/package/aws-lambda
import { Handler } from 'aws-lambda'; // tslint:disable-line:no-implicit-dependencies
import { collectReports } from './helpers/collectReports';
import { format as formatDate } from 'date-fns';
import bluebird from 'bluebird';
import NodeGit from 'nodegit';
import { join } from 'path';
import Octokit from '@octokit/rest';
import { renderReport } from './helpers/renderReport';
import bluebird from 'bluebird';
import executeCommand from '@hollowverse/common/helpers/executeCommand';
import executeCommands from '@hollowverse/common/helpers/executeCommands';
import initGit from 'lambda-git';
import prettier from 'prettier';
import shelljs from 'shelljs';
import tmp from 'tmp';
import { WebPageTestReporter } from './reporters/WebPageTestReporter';
import { Handler } from 'aws-lambda'; // tslint:disable-line:no-implicit-dependencies
import { SecurityHeadersReporter } from './reporters/SecurityHeadersReporter';
import { WebPageTestReporter } from './reporters/WebPageTestReporter';
import { collectReports } from './helpers/collectReports';
import { config } from './config';
import { format as formatDate } from 'date-fns';
import { join } from 'path';
import { keyBy, mapValues } from 'lodash';
import { writeFile } from './helpers/writeFile';
import { renderReport } from './helpers/renderReport';
import { stripIndents } from 'common-tags';
import prettier from 'prettier';
import { writeFile } from './helpers/writeFile';

// tslint:disable no-console
// tslint:disable-next-line:max-func-body-length
Expand Down Expand Up @@ -61,81 +64,46 @@ export const runReporters: Handler = async (_event, _context) => {

markdownReport = prettier.format(markdownReport, { parser: 'markdown' });

const repoTempDir = tmp.dirSync().name;

const credentials = (_url: string, userName: string) => {
return NodeGit.Cred.sshKeyNew(
userName,
config.sshKeyPublicKeyPath,
config.sshPrivateKeyPath,
'',
);
};

const repoPath = tmp.dirSync().name;
const branchName = `report-${dateStr}`;
const repo = await NodeGit.Clone.clone(
'[email protected]:hollowverse/perf-reports.git',
repoTempDir,
{
fetchOpts: {
callbacks: {
credentials,
},
},
},
);

const repoPath = repo.workdir();

console.log('Repo cloned to', repoPath);

const filesToAdd = {
'mostRecent.md': markdownReport,
'mostRecent.json': JSON.stringify(rawReports, undefined, 2),
};

await bluebird.map(
Object.entries(filesToAdd),
async ([fileName, contents]) => {
await writeFile(join(repoPath, fileName), contents);
await executeCommands([
async () => {
if (process.env.AWS) {
await initGit();
await executeCommands([
'git config --global user.name hollowbot',
'git config --global user.email [email protected]',
]);
}
},
);

console.info('Report files written');

const hollowbotSignature = NodeGit.Signature.now(
'hollowbot',
'[email protected]',
);

await repo.createBranch(
branchName,
await repo.getReferenceCommit('HEAD'),
false,
hollowbotSignature,
'',
);

await repo.checkoutBranch(branchName);

await repo.createCommitOnHead(
Object.keys(filesToAdd),
hollowbotSignature,
hollowbotSignature,
`Update report file with results from ${dateStr}`,
);

const remote = await repo.getRemote('origin');
const ref = await repo.getReference(branchName);

await NodeGit.Branch.setUpstream(ref, branchName);
() => {
shelljs.env.GIT_SSH_COMMAND = `ssh -i ${config.sshPrivateKeyPath}`;
},
`git clone [email protected]:hollowverse/perf-reports.git ${repoPath}`,
() => {
shelljs.cd(repoPath);
},
`git checkout -b ${branchName}`,
async () => {
await bluebird.map(
Object.entries(filesToAdd),
async ([fileName, contents]) => {
await writeFile(join(repoPath, fileName), contents);
},
);
},
`git add ${Object.keys(filesToAdd).join(' ')}`,
`git commit 'Update report file with results from ${dateStr}'`,
`git push origin -u ${branchName} --force`,
]);

if (process.env.PUSH === 'true') {
await remote.push([`+${ref.toString()}`], {
callbacks: {
credentials,
},
});
await executeCommand(`git push origin -u ${branchName} --force`);

const octokit = new Octokit();

Expand Down
3 changes: 1 addition & 2 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"allowJs": false,
"allowJs": true,
"checkJs": false,
"maxNodeModuleJsDepth": 0,
"skipLibCheck": true
}
}
1 change: 1 addition & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": ["./node_modules/@hollowverse/common/tslint.json"],
"rules": {
"no-submodule-imports": false,
"function-name": [
true,
{
Expand Down
Loading

0 comments on commit 0c053f2

Please sign in to comment.