Skip to content

Commit acb4412

Browse files
committed
Check serverless.yml to correct no-default false positives
1 parent 3353faa commit acb4412

File tree

6 files changed

+49
-11
lines changed

6 files changed

+49
-11
lines changed

src/components/resourceTable.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { getStackResources } from "../services/stackResources";
77
import { padString } from "../utils/padString";
88
import { lambdaStatisticsModal, lambdaInvokeModal } from "../modals";
99
import { getLambdaFunctions } from "../services";
10+
import { abbreviateFunction } from "../utils/abbreviateFunction";
1011

1112
const contrib = require("blessed-contrib");
1213
const open = require("open");
@@ -256,9 +257,9 @@ class ResourceTable {
256257
this.table.data = lambdaFunctionResources.map((lam) => {
257258
const funcName = lam.PhysicalResourceId;
258259
const func = this.lambdaFunctions[funcName];
259-
const shortenedFuncName = lam.PhysicalResourceId.replace(
260-
`${this.program.stackName}-`,
261-
""
260+
const shortenedFuncName = abbreviateFunction(
261+
lam.PhysicalResourceId,
262+
this.program.stackName
262263
);
263264
this.fullFunctionNames[shortenedFuncName] = funcName;
264265
let timeout = "?";

src/guardian/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
getStackResources,
1313
getLambdaFunctions,
1414
} from "../services";
15+
import Serverless from "../services/serverless";
1516

1617
const infoLog = chalk.greenBright;
1718
const titleLog = chalk.greenBright.underline.bold;
@@ -55,6 +56,8 @@ class GuardianCI {
5556
if (this.config) {
5657
this.ignoreConfig = this.config.ignore;
5758
}
59+
60+
this.SLS = new Serverless(program.location);
5861
}
5962

6063
async getAllLambdaFunctions() {
@@ -139,7 +142,12 @@ class GuardianCI {
139142
// eslint-disable-next-line no-restricted-syntax
140143
for (const Check of this.checksToRun) {
141144
console.group();
142-
const check = new Check(this.AWS, this.stackName, this.stackFunctions);
145+
const check = new Check(
146+
this.AWS,
147+
this.stackName,
148+
this.stackFunctions,
149+
this.SLS
150+
);
143151
if (!this.ignoreCheck(check)) {
144152
const filteredStack = this.ignoreArns(check, this.stackFunctions);
145153
check.stackFunctions = filteredStack;

src/guardian/rules/best_practices/no-default-memory/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
1+
import { abbreviateFunction } from "../../../../utils/abbreviateFunction";
2+
13
class NoDefaultMemory {
2-
constructor(AWS, stackName, stackFunctions) {
4+
constructor(AWS, stackName, stackFunctions, SLS) {
35
this.name = "no-default-memory";
46
this.AWS = AWS;
57
this.stackName = stackName;
68
this.stackFunctions = stackFunctions;
79
this.result = false;
810
this.defaultMemory = 1024;
911
this.failingResources = [];
12+
this.SLS = SLS;
1013
this.failureMessage =
1114
"The following functions have their memory set as default.";
1215
this.rulePage =
1316
"See (https://theodo-uk.github.io/sls-dev-tools/docs/no-default-memory) for impact and how to to resolve.";
1417
}
1518

1619
hasDefaultMemory(lambdaFunction) {
17-
return lambdaFunction.MemorySize === this.defaultMemory;
20+
const shortenedName = abbreviateFunction(
21+
lambdaFunction.FunctionName,
22+
this.stackName
23+
);
24+
return (
25+
lambdaFunction.MemorySize === this.defaultMemory &&
26+
!this.SLS.getFunctionConfig(shortenedName).memorySize
27+
);
1828
}
1929

2030
async run() {

src/guardian/rules/best_practices/no-default-timeout/index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { abbreviateFunction } from "../../../../utils/abbreviateFunction";
2+
13
class NoDefaultTimeout {
2-
constructor(AWS, stackName, stackFunctions) {
4+
constructor(AWS, stackName, stackFunctions, SLS) {
35
this.name = "no-default-timeout";
46
this.AWS = AWS;
57
this.stackName = stackName;
@@ -8,17 +10,23 @@ class NoDefaultTimeout {
810
this.defaultTimeoutAWS = 3;
911
this.defaultTimeoutServerlessFramework = 6;
1012
this.failingResources = [];
13+
this.SLS = SLS;
1114
this.failureMessage =
1215
"The following functions have their timeout set as default.";
1316
this.rulePage =
1417
"See (https://theodo-uk.github.io/sls-dev-tools/docs/no-default-timeout) for impact and how to to resolve.";
1518
}
1619

1720
hasDefaultTimeout(lambdaFunction) {
18-
return [
19-
this.defaultTimeoutAWS,
20-
this.defaultTimeoutServerlessFramework,
21-
].includes(lambdaFunction.Timeout);
21+
const shortenedName = abbreviateFunction(
22+
lambdaFunction.FunctionName,
23+
this.stackName
24+
);
25+
return (
26+
[this.defaultTimeoutAWS, this.defaultTimeoutServerlessFramework].includes(
27+
lambdaFunction.Timeout
28+
) && !this.SLS.getFunctionConfig(shortenedName).timeout
29+
);
2230
}
2331

2432
async run() {

src/services/serverless.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ class Serverless {
2020
}
2121
}
2222

23+
getFunctionConfig(functionName) {
24+
return this.config.functions[functionName];
25+
}
26+
2327
getStage() {
2428
if (typeof this.config !== "object") {
2529
return "dev";

src/utils/abbreviateFunction.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function abbreviateFunction(fullFuncName, stackName) {
2+
return fullFuncName.replace(`${stackName}-`, "");
3+
}
4+
5+
module.exports = {
6+
abbreviateFunction,
7+
};

0 commit comments

Comments
 (0)