Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #525: Add eslint-plugin-promise and fix async/Promise issues #527

Merged
merged 1 commit into from
Jan 21, 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
49 changes: 47 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
extends: ['airbnb-base', 'prettier'],
plugins: ['prettier'],
extends: ['airbnb-base', 'prettier', 'plugin:promise/recommended'],
plugins: ['prettier', 'promise'],
env: {
jest: true,
browser: true,
Expand All @@ -23,6 +23,36 @@ module.exports = {
*/
'no-param-reassign': ['error', { props: false }],

/**Disallows unnecessary return await
* https://eslint.org/docs/rules/no-return-await
*/
'no-return-await': ['error'],

/**
* Disallow using an async function as a Promise executor
* https://eslint.org/docs/rules/no-async-promise-executor
*/
'no-async-promise-executor': ['error'],

/**
* Disallow await inside of loops
* https://eslint.org/docs/rules/no-await-in-loop
*/
'no-await-in-loop': ['error'],

/**
* Disallow assignments that can lead to race conditions due to
* usage of await or yield
* https://eslint.org/docs/rules/require-atomic-updates
*/
'require-atomic-updates': ['error'],

/**
* Disallow async functions which have no await expression
* https://eslint.org/docs/rules/require-await
*/
'require-await': ['error'],

/**
* Require or disallow named function expressions
* https://eslint.org/docs/rules/func-names
Expand All @@ -34,5 +64,20 @@ module.exports = {
* https://eslint.org/docs/rules/func-names
*/
'linebreak-style': 'off',

/**
* The following are eslint rules from the promise-plugin
* https://github.com/xjamundx/eslint-plugin-promise
c3ho marked this conversation as resolved.
Show resolved Hide resolved
*/

/**
* Prefer wait to then() for reading Promise values
*/
'promise/prefer-await-to-then': 'warn',

/**
* Prefer async/await to the callback pattern
*/
'promise/prefer-await-to-callbacks': 'warn',
},
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"eslint-config-prettier": "6.7.0",
"eslint-plugin-import": "2.18.2",
"eslint-plugin-prettier": "3.1.1",
"eslint-plugin-promise": "4.2.1",
"hint": "6.0.0",
"husky": "3.1.0",
"jest": "24.9.0",
Expand Down
2 changes: 1 addition & 1 deletion src/backend/feed/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ queue.addFeed = async function(feedInfo) {
};

try {
queue.add(feedInfo, options);
await queue.add(feedInfo, options);
} catch (err) {
logger.error({ err, feedInfo }, 'Unable to add job to queue');
}
Expand Down
14 changes: 2 additions & 12 deletions src/backend/utils/basic_analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,8 @@ module.exports = function analyzeText(text_) {

// asynchonize function
// to get a basic analysis information, getAsyAnalysis should be called
this.getAsyAnalysis = async function() {
return new Promise((res, rej) => {
if (isValidString) {
res(analysis);
} else {
res(textInfo);
}
if (isValidString === undefined) {
rej(textInfo);
}
});
this.getAsyAnalysis = function() {
return isValidString ? analysis : textInfo;
};

return this;
};
29 changes: 12 additions & 17 deletions src/backend/utils/email-sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ require('../lib/config');
const nodemailer = require('nodemailer');
const { logger } = require('./logger');

const log = logger.child({ module: 'email-sender' });

/*
HOW TO USE
Import this file - const sendEmail = require('./email-sender);
Expand Down Expand Up @@ -69,39 +67,36 @@ exports.verifyTransporter = function(transporter) {
transporter.verify(err => {
// If error then print to console
if (err) {
log.error({ err }, 'Transporter connection failed.');
logger.error({ err }, 'Transporter connection failed.');
return false;
}
// else print a ready message
log.info('Server is running properly');
logger.info('Server is running properly');
return true;
});
};

// Sends a message using the passed in parameters
exports.sendMessage = async function(receipiants, subjectMessage, message) {
return new Promise((resolve, reject) => {
const transporter = this.createTransporter(
try {
const transporter = await this.createTransporter(
process.env.NODEMAILER_SERVER,
2222,
false,
process.env.NODEMAILER_USERNAME,
process.env.NODEMAILER_PASSWORD
);
const allGood = this.verifyTransporter(transporter);
if (!allGood) {
reject(new Error()); // Send promise.reject if an error occurs
if (!this.verifyTransporter(transporter)) {
throw new Error('Email transport could not be verified');
}
// Creates email for to be sent
const mail = this.createMail(receipiants, subjectMessage, message);

// Send the email with the email content
transporter.sendMail(mail, (err, info) => {
if (err) {
reject(err); // Send promise.reject if an error occurs
} else {
resolve(info.accepted); // Send promise.resolve if an error occurs
}
});
});
const result = await transporter.sendMail(mail);
return result.accepted;
} catch (error) {
logger.error({ error }, 'Unable to send email');
throw error;
}
};
123 changes: 63 additions & 60 deletions src/backend/utils/github-url.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require('../lib/config');
const parseGithubUrl = require('parse-github-url');
const fetch = require('node-fetch');
const { logger } = require('./logger');

const githubAPI = 'http://api.github.com';

Expand Down Expand Up @@ -56,69 +57,71 @@ exports.getGithubUrlData = async incomingUrl => {
typeof process.env.GITHUB_TOKEN !== 'undefined'
? `?access_token=${process.env.GITHUB_TOKEN}`
: '';
let data;
try {
const fetchResult = await fetch(`${githubAPI}${subUrl}`);
data = await fetchResult.json();
} catch (error) {
logger.error({ error }, 'Unable to fetch GitHub API results');
throw error;
}
let fetchedData;
/**
* Format the fetched data in a specifi way depending if the url
* is for a repo, user or for a {pull request, issue}
*/
// User
if (ghUrl.repo === null) {
const { login: user, avatar_url: avatarURL, name, company, blog, email, bio } = data;

return fetch(`${githubAPI}${subUrl}`)
.then(res => res.json())
.then(data => {
let fetchedData;

/**
* Format the fetched data in a specifi way depending if the url
* is for a repo, user or for a {pull request, issue}
*/
// User
if (ghUrl.repo === null) {
const { login: user, avatar_url: avatarURL, name, company, blog, email, bio } = data;

fetchedData = {
user,
avatarURL,
name,
company,
blog,
email,
bio,
};
fetchedData = {
user,
avatarURL,
name,
company,
blog,
email,
bio,
};

// Repo
} else if (ghUrl.branch === 'master') {
const {
owner: { avatar_url: avatarURL },
description,
license,
open_issues: openIssues,
forks,
created_at: createdAt,
language,
} = data;
// Repo
} else if (ghUrl.branch === 'master') {
const {
owner: { avatar_url: avatarURL },
description,
license,
open_issues: openIssues,
forks,
created_at: createdAt,
language,
} = data;

fetchedData = {
avatarURL,
description,
license,
openIssues,
forks,
createdAt,
language,
};
fetchedData = {
avatarURL,
description,
license,
openIssues,
forks,
createdAt,
language,
};

// Issue or Pull Request
} else {
const {
user: { login, avatar_url: avatarURL },
body,
created_at: createdAt,
} = data;
// Issue or Pull Request
} else {
const {
user: { login, avatar_url: avatarURL },
body,
created_at: createdAt,
} = data;

fetchedData = {
login,
avatarURL,
body,
createdAt,
branch: ghUrl.branch,
repo: ghUrl.name,
};
}
return fetchedData;
});
fetchedData = {
login,
avatarURL,
body,
createdAt,
branch: ghUrl.branch,
repo: ghUrl.name,
};
}
return fetchedData;
};
Loading