-
Notifications
You must be signed in to change notification settings - Fork 534
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: send target data on monitor to registry
- Loading branch information
Showing
7 changed files
with
142 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as gitTargetBuilder from './target-builders/git'; | ||
import { GitTarget } from './types'; | ||
|
||
const TARGET_BUILDERS = [ | ||
gitTargetBuilder, | ||
]; | ||
|
||
export async function getInfo(packageInfo): Promise<GitTarget|null> { | ||
for (const builder of TARGET_BUILDERS) { | ||
const target = await builder.getInfo(packageInfo); | ||
|
||
if (target) { | ||
return target; | ||
} | ||
} | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import fs = require('fs'); | ||
import GitUrlParse = require('git-url-parse'); | ||
|
||
import subProcess = require('../../sub-process'); | ||
import { GitTarget } from '../types'; | ||
|
||
export async function getInfo(packageInfo): Promise<GitTarget|null> { | ||
let origin: string|null|undefined; | ||
|
||
if (packageInfo.docker) { | ||
return null; | ||
} | ||
|
||
try { | ||
origin = (await subProcess.execute('git', ['remote', 'get-url', 'origin'])).trim(); | ||
|
||
if (!origin) { | ||
return null; | ||
} | ||
|
||
const parsedOrigin = GitUrlParse(origin); | ||
const branch = (await subProcess.execute('git', ['rev-parse', '--abbrev-ref', 'HEAD'])).trim(); | ||
|
||
return { | ||
remoteUrl: parsedOrigin.toString('http'), | ||
branch, | ||
}; | ||
} catch (err) { | ||
// Swallowing exception since we don't want to break the monitor if there is a problem | ||
// executing git commands. | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface GitTarget { | ||
remoteUrl: string; | ||
branch: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use strict'; | ||
|
||
const test = require('tape').test; | ||
const zlib = require('zlib'); | ||
const requestLib = require('needle'); | ||
|
||
import * as _ from 'lodash'; | ||
import * as sinon from 'sinon'; | ||
|
||
import * as cli from '../src/cli/commands'; | ||
import subProcess = require('../src/lib/sub-process'); | ||
|
||
test('Make sure that target is sent correctly', async (t) => { | ||
const subProcessStub = sinon.stub(subProcess, 'execute'); | ||
|
||
subProcessStub.withArgs('git', ['remote', 'get-url', 'origin']) | ||
.resolves('http://github.com/snyk/project.git'); | ||
|
||
subProcessStub.withArgs('git', ['rev-parse', '--abbrev-ref', 'HEAD']) | ||
.resolves('master'); | ||
|
||
const { data, spy } = await getMonitorRequestDataAndSpy(); | ||
|
||
t.true(spy.calledOnce, 'needle.request was called once'); | ||
t.true(!_.isEmpty(data.target), 'target passed to request'); | ||
t.true(!_.isEmpty(data.targetFileRelativePath), 'targetFileRelativePath passed to request'); | ||
t.equals(data.target.branch, 'master', 'correct branch passed to request'); | ||
t.equals(data.target.remoteUrl, 'http://github.com/snyk/project.git', 'correct name passed to request'); | ||
t.equals(data.targetFileRelativePath, 'package.json', 'correct relative target file path passed to request'); | ||
|
||
subProcessStub.restore(); | ||
spy.restore(); | ||
t.end(); | ||
}); | ||
|
||
test('Make sure it\'s not failing monitor for non git projects', async (t) => { | ||
const subProcessStub = sinon.stub(subProcess, 'execute').resolves(''); | ||
const { data, spy } = await getMonitorRequestDataAndSpy(); | ||
|
||
t.true(spy.calledOnce, 'needle.request was called once'); | ||
t.true(_.isEmpty(data.target), 'empty target passed to request'); | ||
t.equals(data.targetFileRelativePath, 'package.json', 'targetFileRelativePath passed to request'); | ||
|
||
subProcessStub.restore(); | ||
spy.restore(); | ||
t.end(); | ||
}); | ||
|
||
test('Make sure it\'s not failing if there is no remote configured', async (t) => { | ||
const subProcessStub = sinon.stub(subProcess, 'execute').rejects(); | ||
const { data, spy } = await getMonitorRequestDataAndSpy(); | ||
|
||
t.true(spy.calledOnce, 'needle.request was called once'); | ||
t.true(_.isEmpty(data.target), 'empty target passed to request'); | ||
t.equals(data.targetFileRelativePath, 'package.json', 'targetFileRelativePath passed to request'); | ||
|
||
subProcessStub.restore(); | ||
spy.restore(); | ||
t.end(); | ||
}); | ||
|
||
async function getMonitorRequestDataAndSpy() { | ||
const requestSpy = sinon.spy(requestLib, 'request'); | ||
|
||
await cli.monitor(); | ||
|
||
const data = JSON.parse(zlib.gunzipSync(requestSpy.getCall(0).args[2]).toString()); | ||
|
||
return { | ||
data, | ||
spy: requestSpy, | ||
}; | ||
} |