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

Fix/logging #47

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ config.sh
*.key.pub
serverless-output.yml
package-lock.json
ngrok
3 changes: 3 additions & 0 deletions example-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
export GITHUB_CLIENT_ID=# <GitHub OAuth App Client ID>
export GITHUB_CLIENT_SECRET=# <GitHub OAuth App Client Secret>
export COGNITO_REDIRECT_URI=# https://<Your Cognito Domain>/oauth2/idpresponse
export GITHUB_API_URL="https://api.github.com"
export GITHUB_LOGIN_URL="https://github.com"
export NODE_LOG_LEVEL="debug"

# Variables required if used with GitHub Enterprise
# GITHUB_API_URL=# https://<GitHub Enterprise Host>/api/v3
Expand Down
17 changes: 13 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"build": "webpack",
"test": "jest --runInBand --coverage",
"test-dev": "jest --runInBand --watch",
"start": "webpack --watch --display errors-only",
"start": "webpack --watch",
"lint": "eslint 'src/**' --ext .js",
"preinstall": "./scripts/create-key.sh",
"prebuild-dist": "npm run lint && npm run test",
Expand Down
2 changes: 1 addition & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
GITHUB_API_URL: process.env.GITHUB_API_URL,
GITHUB_LOGIN_URL: process.env.GITHUB_LOGIN_URL,
PORT: parseInt(process.env.PORT, 10) || undefined,

LOG_LEVEL: process.env.NODE_LOG_LEVEL,
// Splunk logging variables
SPLUNK_URL: process.env.SPLUNK_URL,
SPLUNK_TOKEN: process.env.SPLUNK_TOKEN,
Expand Down
5 changes: 3 additions & 2 deletions src/connectors/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ const {
SPLUNK_TOKEN,
SPLUNK_SOURCE,
SPLUNK_SOURCETYPE,
SPLUNK_INDEX
SPLUNK_INDEX,
LOG_LEVEL
} = require('../config');

const logger = winston.createLogger({
level: 'info'
level: LOG_LEVEL || 'info'
});

// Activate Splunk logging if Splunk's env variables are set
Expand Down
21 changes: 19 additions & 2 deletions src/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,22 @@ const getApiEndpoints = (
oauthAuthorize: `${loginBaseUrl}/login/oauth/authorize`
});

const debug_error = (err) => {
if (err.response && err.response.data) {
logger.error('Github response: %s', err.response.data, {})
const custom_error = new Error(err.response.data || 'Unknown error');
custom_error.status = err.response.status || 500;
custom_error.description = err.response.data ? err.response.data.statusText : null;
throw custom_error;
}
throw new Error(err);
}

axios.interceptors.response.use(r => r, debug_error);

const check = response => {
logger.debug('Checking response: %j', response, {});

if (response.data) {
if (response.data.error) {
throw new Error(
Expand Down Expand Up @@ -57,8 +71,11 @@ module.exports = (apiBaseUrl, loginBaseUrl) => {
)}&state=${state}&response_type=${response_type}`,
getUserDetails: accessToken =>
gitHubGet(urls.userDetails, accessToken).then(check),
getUserEmails: accessToken =>
gitHubGet(urls.userEmails, accessToken).then(check),
getUserEmails: (accessToken) => {
logger.debug('Using access token: %s', accessToken, {})
logger.debug('Fetching: %s', urls.userEmails, {})
return gitHubGet(urls.userEmails, accessToken).then(check)
},
getToken: (code, state) => {
const data = {
// OAuth required fields
Expand Down