Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

add new eslint rules and fix errors #4823

Merged
merged 7 commits into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
node: [8, 10]
node: [10]
os: [ubuntu-16.04, ubuntu-latest]

steps:
Expand All @@ -131,7 +131,6 @@ jobs:
- name: yarn install and test
run: |
cd src/rest-server
yarn config set ignore-engines true
yarn install --frozen-lockfiles
yarn test

Expand Down
12 changes: 1 addition & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ matrix:
- mvn clean test jacoco:report

- language: node_js
node_js: lts/carbon
node_js: lts/dubnium
env: NODE_ENV=test
before_install:
- cd src/rest-server
Expand All @@ -97,16 +97,6 @@ matrix:
- npm test
- npm run coveralls

- language: node_js
node_js: lts/dubnium
env: NODE_ENV=test
before_install:
- cd src/rest-server
install:
- yarn install --ignore-engines
script:
- npm test

- language: node_js
node_js: lts/carbon
before_install:
Expand Down
38 changes: 24 additions & 14 deletions src/rest-server/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
module.exports = {
"parserOptions": {
"ecmaVersion": 2017,
"ecmaFeatures": {
"experimentalObjectRestSpread": true
}
plugins: ['eslint-plugin-prettier'],
env: {
es6: true,
node: true,
mocha: true,
browser: false,
},
"env": {
"es6": true,
"node": true,
extends: ['standard', 'plugin:prettier/recommended', 'prettier'],
parserOptions: {
ecmaFeatures: {
ecmaVersion: 8,
experimentalObjectRestSpread: true,
},
sourceType: 'module',
},
"extends": ["eslint:recommended", "google"],
"rules": {
"max-len": [0, 80],
"require-jsdoc": 0,
"valid-jsdoc": 0,
"linebreak-style": 0,
rules: {
'prettier/prettier': ['error'],
'max-len': [
'error',
{
code: 120,
ignoreComments: true,
ignoreStrings: false,
ignoreTemplateLiterals: true,
},
],
},
};
2 changes: 1 addition & 1 deletion src/rest-server/build/rest-server.common.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

FROM node:carbon
FROM node:dubnium

WORKDIR /usr/src/app

Expand Down
22 changes: 16 additions & 6 deletions src/rest-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
"url": "https://github.com/Microsoft/pai/issues"
},
"engines": {
"node": "^8.9.0"
"node": "^10"
},
"dependencies": {
"openpaidbsdk": "file:./openpaidbsdk",
"@kubernetes/client-node": "^0.11.0",
"ajv": "^6.10.0",
"ajv-merge-patch": "~4.1.0",
Expand All @@ -37,8 +38,6 @@
"cors": "~2.8.4",
"coveralls": "~3.0.0",
"dotenv": "~4.0.0",
"eslint": "~4.18.2",
"eslint-config-google": "~0.9.1",
"express": "~4.16.2",
"express-rate-limit": "^5.1.1",
"fs-extra": "~7.0.1",
Expand All @@ -54,7 +53,6 @@
"nock": "~9.1.6",
"node-cache": "~4.2.0",
"nyc": "^14.1.1",
"openpaidbsdk": "file:./openpaidbsdk",
"pg": "^7.17.1",
"querystring": "~0.2.0",
"sequelize": "^5.21.3",
Expand All @@ -70,11 +68,23 @@
},
"scripts": {
"coveralls": "nyc report --reporter=text-lcov | coveralls ..",
"lint": "eslint .",
"lint": "eslint --ext .js ./src",
"mocha": "mocha --file ./test/setup --ui bdd --recursive --timeout 1000 --exit",
"preinstall": "npx ncp ../database-controller/sdk openpaidbsdk || echo skip copying openpaidbsdk",
"postinstall": "rm -rf openpaidbsdk",
"postinstall": "rimraf openpaidbsdk",
"start": "node index.js",
"test": "npm run lint && nyc npm run mocha"
},
"devDependencies": {
"eslint": "^7.7.0",
"eslint-config-prettier": "~6.11.0",
"eslint-config-standard": "~14.1.1",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "~3.1.4",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"prettier": "~2.0.5",
"rimraf": "^3.0.2"
}
}
7 changes: 7 additions & 0 deletions src/rest-server/prettier.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
semi: true,
// Trailing commas help with git merging and conflict resolution
trailingComma: 'all',
// Use single quote in all files. https://github.com/prettier/prettier/issues/1080#issuecomment-390363232
singleQuote: true,
};
29 changes: 17 additions & 12 deletions src/rest-server/src/config/authn.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ if (authnConfig.authnMethod === 'OIDC') {
const initOIDCEndpointAndGroupUrl = async () => {
try {
const response = await axios.get(authnConfig.OIDCConfig.wellKnownURL);
authnConfig.OIDCConfig.authorization_endpoint = response.data.authorization_endpoint;
authnConfig.OIDCConfig.authorization_endpoint =
response.data.authorization_endpoint;
authnConfig.OIDCConfig.token_endpoint = response.data.token_endpoint;
authnConfig.OIDCConfig.msgraph_host = response.data.msgraph_host;
} catch (error) {
Expand All @@ -46,8 +47,10 @@ if (authnConfig.authnMethod === 'OIDC') {
if (process.env.OIDC_CONFIG_PATH) {
odicConfigPath = process.env.OIDC_CONFIG_PATH;
}
authnConfig.OIDCConfig = yaml.safeLoad(fs.readFileSync(odicConfigPath, 'utf8'));
(async function() {
authnConfig.OIDCConfig = yaml.safeLoad(
fs.readFileSync(odicConfigPath, 'utf8'),
);
(async function () {
await initOIDCEndpointAndGroupUrl();
})();
}
Expand All @@ -57,22 +60,24 @@ try {
if (process.env.GROUP_CONFIG_PATH) {
groupConfigPath = process.env.GROUP_CONFIG_PATH;
}
authnConfig.groupConfig = yaml.safeLoad(fs.readFileSync(groupConfigPath, 'utf8'));
authnConfig.groupConfig = yaml.safeLoad(
fs.readFileSync(groupConfigPath, 'utf8'),
);
} catch (error) {
logger.error('Failed to load group config from configmap file.');
throw error;
}

// define the schema for authn
const authnSchema = Joi.object().keys({
authnMethod: Joi.string().empty('')
.valid('OIDC', 'basic'),
OIDCConfig: Joi.object().pattern(/\w+/, Joi.required()),
groupConfig: Joi.object().pattern(/\w+/, Joi.required()),
}).required();
const authnSchema = Joi.object()
.keys({
authnMethod: Joi.string().empty('').valid('OIDC', 'basic'),
OIDCConfig: Joi.object().pattern(/\w+/, Joi.required()),
groupConfig: Joi.object().pattern(/\w+/, Joi.required()),
})
.required();


const {error, value} = Joi.validate(authnConfig, authnSchema);
const { error, value } = Joi.validate(authnConfig, authnSchema);
if (error) {
throw new Error(`config error\n${error}`);
}
Expand Down
13 changes: 6 additions & 7 deletions src/rest-server/src/config/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


// module dependencies
const fs = require('fs');
const cors = require('cors');
Expand Down Expand Up @@ -44,14 +43,14 @@ app.set('json spaces', config.env === 'development' ? 4 : 0);

app.use(cors());
app.use(compress());
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(bodyParser.text({type: 'text/*'}));
app.use(bodyParser.text({ type: 'text/*' }));
app.use(cookieParser());
app.use(limiter.api);

// setup the logger for requests
app.use(morgan('dev', {'stream': logger.stream}));
app.use(morgan('dev', { stream: logger.stream }));

// mount all v1 APIs to /api/v1
app.use('/api/v1', routers.v1);
Expand All @@ -70,9 +69,9 @@ app.use((req, res, next) => {

if (authnConfig.authnMethod === 'OIDC') {
// error handler for /api/v1/authn/oidc/return
app.use('/api/v1/authn/oidc/return', function(err, req, res, next) {
app.use('/api/v1/authn/oidc/return', function (err, req, res, next) {
logger.warn(err);
let qsData = {
const qsData = {
errorMessage: err.message,
};
let redirectURI = err.targetURI ? err.targetURI : process.env.WEBPORTAL_URL;
Expand All @@ -87,7 +86,7 @@ app.use((err, req, res, next) => {
res.status(err.status || 500).json({
code: err.code,
message: err.message,
stack: config.env === 'development' ? err.stack.split('\n') : void 0,
stack: config.env === 'development' ? err.stack.split('\n') : undefined,
});
});

Expand Down
72 changes: 39 additions & 33 deletions src/rest-server/src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


// module dependencies
const fse = require('fs-extra');
const Joi = require('joi');
const dotenv = require('dotenv');
const mustache = require('mustache');


/* eslint-disable-next-line node/no-deprecated-api */
require.extensions['.mustache'] = (module, filename) => {
module.exports = fse.readFileSync(filename, 'utf8');
};
Expand All @@ -31,20 +30,29 @@ dotenv.config();

mustache.escape = (string) => {
// https://stackoverflow.com/questions/15783701/which-characters-need-to-be-escaped-when-using-bash/27817504#27817504
return String(string)
/* eslint-disable no-control-regex */
.replace(/[\x20-\x24\x26-\x2A\x2C\x3B\x3C\x3E\x3F\x5B-\x5E\x60\x7B-\x7E]/g, '\\$&')
.replace(/[\x00-\x1F\x7F]/g, (c) => ({
/* eslint-enable no-control-regex */
0x07: '\\a',
0x08: '\\b',
0x09: '\\t',
0x0A: '\\n',
0x0B: '\\v',
0x0C: '\\f',
0x0D: '\\r',
0x1B: '\\E',
}[c.charCodeAt(0)] || ''));
return (
String(string)
/* eslint-disable no-control-regex */
.replace(
/[\x20-\x24\x26-\x2A\x2C\x3B\x3C\x3E\x3F\x5B-\x5E\x60\x7B-\x7E]/g,
'\\$&',
)
.replace(
/[\x00-\x1F\x7F]/g,
(c) =>
({
/* eslint-enable no-control-regex */
0x07: '\\a',
0x08: '\\b',
0x09: '\\t',
0x0a: '\\n',
0x0b: '\\v',
0x0c: '\\f',
0x0d: '\\r',
0x1b: '\\E',
}[c.charCodeAt(0)] || ''),
)
);
};

// get config from environment variables
Expand All @@ -56,24 +64,22 @@ let config = {
};

// define config schema
const configSchema = Joi.object().keys({
env: Joi.string()
.allow(['test', 'development', 'production'])
.default('development'),
logLevel: Joi.string()
.allow(['error', 'warn', 'info', 'verbose', 'debug', 'silly'])
.default('debug'),
serverPort: Joi.number()
.integer()
.min(8000)
.max(65535)
.default(9186),
jwtSecret: Joi.string()
.required()
.description('JWT Secret required to sign'),
}).required();
const configSchema = Joi.object()
.keys({
env: Joi.string()
.allow(['test', 'development', 'production'])
.default('development'),
logLevel: Joi.string()
.allow(['error', 'warn', 'info', 'verbose', 'debug', 'silly'])
.default('debug'),
serverPort: Joi.number().integer().min(8000).max(65535).default(9186),
jwtSecret: Joi.string()
.required()
.description('JWT Secret required to sign'),
})
.required();

const {error, value} = Joi.validate(config, configSchema);
const { error, value } = Joi.validate(config, configSchema);
if (error) {
throw new Error(`config error\n${error}`);
}
Expand Down
Loading