Skip to content

Commit b69d4b3

Browse files
authored
Merge pull request #3863 from snyk/feat/pass-http-client-to-plugins
feat: pass snykHttpClient to plugin.inspect
2 parents 4fe1808 + 17b1273 commit b69d4b3

15 files changed

+222
-82
lines changed

package-lock.json

+32-49
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
"snyk-cpp-plugin": "2.20.1",
117117
"snyk-docker-plugin": "^5.4.3",
118118
"snyk-go-plugin": "1.19.2",
119-
"snyk-gradle-plugin": "3.22.2",
119+
"snyk-gradle-plugin": "3.23.0",
120120
"snyk-module": "3.1.0",
121121
"snyk-mvn-plugin": "2.31.0",
122122
"snyk-nodejs-lockfile-parser": "1.38.0",

src/cli/args.ts

+1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ export function args(rawArgv: string[]): Args {
223223
'prune-repeated-subdependencies',
224224
'dry-run',
225225
'sequential',
226+
'gradle-normalize-deps',
226227
];
227228
for (const dashedArg of argumentsToTransform) {
228229
if (argv[dashedArg]) {

src/lib/module-info/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export function ModuleInfo(plugin, policy) {
1010
root,
1111
targetFile,
1212
options,
13+
snykHttpClient,
1314
): Promise<pluginApi.SinglePackageResult | pluginApi.MultiProjectResult> {
1415
const pluginOptions = merge(
1516
{
@@ -19,7 +20,12 @@ export function ModuleInfo(plugin, policy) {
1920
);
2021

2122
debug('calling plugin inspect()', { root, targetFile, pluginOptions });
22-
const info = await plugin.inspect(root, targetFile, pluginOptions);
23+
const info = await plugin.inspect(
24+
root,
25+
targetFile,
26+
pluginOptions,
27+
snykHttpClient,
28+
);
2329
debug('plugin inspect() done');
2430

2531
// attach policy if not provided by plugin

src/lib/plugins/get-single-plugin-result.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import plugins = require('.');
22
import { ModuleInfo } from '../module-info';
33
import { legacyPlugin as pluginApi } from '@snyk/cli-interface';
44
import { TestOptions, Options, MonitorOptions } from '../types';
5+
import { snykHttpClient } from '../request/snyk-http-client';
56

67
export async function getSinglePluginResult(
78
root: string,
@@ -14,6 +15,7 @@ export async function getSinglePluginResult(
1415
root,
1516
targetFile || options.file,
1617
{ ...options },
18+
snykHttpClient,
1719
);
1820
return inspectRes;
1921
}

src/lib/request/snyk-http-client.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import * as needle from 'needle';
2+
import { OutgoingHttpHeaders } from 'http';
3+
import { NeedleHttpVerbs } from 'needle';
4+
import { makeRequest } from '../request/index';
5+
import { getAuthHeader } from '../api-token';
6+
import config from '../config';
7+
import { Payload } from '../request/types';
8+
9+
interface RequestInfo {
10+
method: NeedleHttpVerbs;
11+
path: string;
12+
body: any;
13+
headers?: OutgoingHttpHeaders;
14+
qs?: {};
15+
json?: boolean;
16+
timeout?: number;
17+
family?: number;
18+
}
19+
20+
export async function snykHttpClient(
21+
requestInfo: RequestInfo,
22+
): Promise<{
23+
res: needle.NeedleResponse;
24+
body: any;
25+
}> {
26+
let { path } = requestInfo;
27+
if (!path.startsWith('/')) path = `/${path}`;
28+
29+
const payload: Payload = {
30+
...requestInfo,
31+
url: `${config.API_REST_URL}${path}`,
32+
headers: {
33+
...requestInfo.headers,
34+
Authorization: getAuthHeader(),
35+
},
36+
};
37+
38+
return makeRequest(payload);
39+
}

src/lib/types.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ export type SupportedUserReachableFacingCliArgs =
225225
| 'sub-project'
226226
| 'trust-policies'
227227
| 'yarn-workspaces'
228-
| 'maven-aggregate-project';
228+
| 'maven-aggregate-project'
229+
| 'gradle-normalize-deps';
229230

230231
export enum SupportedCliCommands {
231232
version = 'version',

test/tap/cli-monitor.acceptance.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import { DepGraphBuilder } from '@snyk/dep-graph';
4141
import * as depGraphLib from '@snyk/dep-graph';
4242
import { getFixturePath } from '../jest/util/getFixturePath';
4343
import { getWorkspacePath } from '../jest/util/getWorkspacePath';
44+
import { snykHttpClient } from '../../src/lib/request/snyk-http-client';
4445

4546
/*
4647
TODO: enable these tests, once we switch from node-tap
@@ -927,6 +928,7 @@ if (!isWindows) {
927928
packageManager: 'pip',
928929
path: 'pip-app',
929930
},
931+
snykHttpClient,
930932
],
931933
'calls python plugin',
932934
);
@@ -990,6 +992,7 @@ if (!isWindows) {
990992
packageManager: 'pip',
991993
path: 'pip-app',
992994
},
995+
snykHttpClient,
993996
],
994997
'calls python plugin',
995998
);
@@ -1042,6 +1045,7 @@ if (!isWindows) {
10421045
file: 'build.gradle',
10431046
path: 'gradle-app',
10441047
},
1048+
snykHttpClient,
10451049
],
10461050
'calls gradle plugin',
10471051
);
@@ -1111,6 +1115,7 @@ if (!isWindows) {
11111115
packageManager: 'gradle',
11121116
path: 'gradle-app',
11131117
},
1118+
snykHttpClient,
11141119
],
11151120
'calls gradle plugin',
11161121
);
@@ -1170,6 +1175,7 @@ if (!isWindows) {
11701175
packageManager: 'gradle',
11711176
path: 'gradle-app',
11721177
},
1178+
snykHttpClient,
11731179
],
11741180
'calls gradle plugin',
11751181
);
@@ -1224,6 +1230,7 @@ if (!isWindows) {
12241230
packageManager: 'gradle',
12251231
path: 'gradle-app',
12261232
},
1233+
snykHttpClient,
12271234
],
12281235
'calls plugin for the 1st path',
12291236
);
@@ -1239,6 +1246,7 @@ if (!isWindows) {
12391246
packageManager: 'pip',
12401247
path: 'pip-app',
12411248
},
1249+
snykHttpClient,
12421250
],
12431251
'calls plugin for the 2nd path',
12441252
);
@@ -1316,6 +1324,7 @@ if (!isWindows) {
13161324
packageManager: 'gomodules',
13171325
path: 'golang-gomodules',
13181326
},
1327+
snykHttpClient,
13191328
],
13201329
'calls golang plugin',
13211330
);
@@ -1364,6 +1373,7 @@ if (!isWindows) {
13641373
packageManager: 'golangdep',
13651374
path: 'golang-app',
13661375
},
1376+
snykHttpClient,
13671377
],
13681378
'calls golang plugin',
13691379
);
@@ -1412,6 +1422,7 @@ if (!isWindows) {
14121422
packageManager: 'govendor',
14131423
path: 'golang-app',
14141424
},
1425+
snykHttpClient,
14151426
],
14161427
'calls golang plugin',
14171428
);
@@ -1458,6 +1469,7 @@ if (!isWindows) {
14581469
packageManager: 'cocoapods',
14591470
path: './',
14601471
},
1472+
snykHttpClient,
14611473
],
14621474
'calls CocoaPods plugin',
14631475
);
@@ -1506,6 +1518,7 @@ if (!isWindows) {
15061518
packageManager: 'cocoapods',
15071519
path: './',
15081520
},
1521+
snykHttpClient,
15091522
],
15101523
'calls CocoaPods plugin',
15111524
);
@@ -1560,6 +1573,7 @@ if (!isWindows) {
15601573
packageManager: 'cocoapods',
15611574
path: './',
15621575
},
1576+
snykHttpClient,
15631577
],
15641578
'calls CocoaPods plugin',
15651579
);

test/tap/cli-test.acceptance.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const after = tap.runOnly ? only : test;
6565
// Should be after `process.env` setup.
6666
import * as plugins from '../../src/lib/plugins/index';
6767
import * as ecoSystemPlugins from '../../src/lib/ecosystems/plugins';
68+
import { snykHttpClient } from '../../src/lib/request/snyk-http-client';
6869

6970
/*
7071
TODO: enable these tests, once we switch from node-tap
@@ -134,6 +135,7 @@ test('Languages', async (t) => {
134135
languageTest.tests[testName](
135136
{ server, plugins, ecoSystemPlugins, versionNumber, cli },
136137
{ chdirWorkspaces },
138+
snykHttpClient,
137139
),
138140
);
139141
server.restore();

0 commit comments

Comments
 (0)