Skip to content

Commit

Permalink
Merge pull request #779 from SSWConsulting/tech-debt-api-refactor
Browse files Browse the repository at this point in the history
Refactor APIs that queries url string and Removed unnecessary loggings
  • Loading branch information
zacharykeeping committed Nov 22, 2023
2 parents b02c881 + 62b7cc6 commit 1dffc64
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 97 deletions.
8 changes: 4 additions & 4 deletions api/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ app.post('/config/:api/ignore', async (req, res) => {
app.put('/:api/addalertemailaddresses', async (req, res) =>
res.json(await addAlertEmailAddresses(req.params.api, req.body)));

app.get('/getalertemailaddresses/:api/:url', async (req, res) =>
res.json(await getAlertEmailAddressesFromTokenAndUrl(req.params.api, req.params.url)));
app.post('/getalertemailaddresses/:api', async (req, res) =>
res.json(await getAlertEmailAddressesFromTokenAndUrl(req.params.api, req.body.url)));

app.delete('/deletealertemailaddress', async (req, res) =>
res.json(await removeAlertEmailAddress(req.body.api, req.body.rowkey)));
Expand Down Expand Up @@ -184,8 +184,8 @@ app.post('/latest/:api', async (req, res) => {
});
});

app.get('/scanSummaryFromUrl/:api/:url', async (req, res) => {
res.json(await getAllScanSummaryFromUrl(req.params.url, req.params.api));
app.post('/scanSummaryFromUrl/:api', async (req, res) => {
res.json(await getAllScanSummaryFromUrl(req.body.url, req.params.api));
});

app.post('/comparescanlatestandsecond/:api', async (req, res) => {
Expand Down
36 changes: 20 additions & 16 deletions docker/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,17 @@ exports.addHTMLHintRulesForScan = (api, url, runId, selectedRules) => {
};

exports.getAlertEmailAddresses = (api, url) => {
return fetch(`${endpoint}/api/getalertemailaddresses/${api}/${url}`).then(
(res) => {
if (res.ok) {
return res.json();
} else {
throw Error("Failed to find shared email addresses");
}
return fetch(`${endpoint}/api/getalertemailaddresses/${api}`, {
method: "POST",
body: JSON.stringify({url}),
headers: { "Content-Type": "application/json" },
}).then((res) => {
if (res) {
return res.ok ? res.json() : [];
} else {
throw Error("Failed to find alert email addresses");
}
);
});
};

exports.getAlertEmailConfig = (api) => {
Expand All @@ -114,15 +116,17 @@ exports.getAlertEmailConfig = (api) => {
};

exports.getAllScanSummaryFromUrl = (api, url) => {
return fetch(`${endpoint}/api/scanSummaryFromUrl/${api}/${url}`).then(
(res) => {
if (res.ok) {
return res.json();
} else {
throw Error("Failed to retrieve scan summary");
}
return fetch(`${endpoint}/api/scanSummaryFromUrl/${api}`, {
method: "POST",
body: JSON.stringify({url}),
headers: { "Content-Type": "application/json" },
}).then((res) => {
if (res) {
return res.ok ? res.json() : [];
} else {
throw Error("Failed to retrieve scan summary");
}
);
});
};

exports.getCustomHtmlRuleOptions = (api, url) => {
Expand Down
20 changes: 9 additions & 11 deletions docker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const {
processBrokenLinks,
getFinalEval,
sendAlertEmail,
convertSpecialCharUrl,
runLighthouseReport,
runArtilleryLoadTest
} = require("./utils");
Expand Down Expand Up @@ -354,18 +353,17 @@ const processAndUpload = async (

// Send alert email to shared participants
if (args.token) {
let urlPathWithSpecChars = convertSpecialCharUrl(args.url)

let emailConfig = await getAlertEmailConfig(args.token)

// Get latest scan summary
let res = await getAllScanSummaryFromUrl(args.token, urlPathWithSpecChars)
let scanSummary = await res[0]

const emailConfig = await getAlertEmailConfig(args.token)

if (emailConfig) {
const alertEmails = await getAlertEmailAddresses(args.token, urlPathWithSpecChars)

const alertEmails = await getAlertEmailAddresses(args.token, args.url)
if (alertEmails && alertEmails.length > 0) {

// Get latest scan summary
let res = await getAllScanSummaryFromUrl(args.token, args.url)
let scanSummary = await res[0]

alertEmails.forEach(item => sendAlertEmail(item.emailAddress, emailConfig, scanSummary))
}
} else {
Expand Down
57 changes: 0 additions & 57 deletions docker/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,50 +526,6 @@ const outputBadDataCsv = (records) => {
},
],
});
console.log(`"Source","Destination","Anchor","StatusCode","Status"`);
console.log(csvStringifier.stringifyRecords(records));
};

/**
* Print to console the list of HTML hint issues found
* @param {Array} htmlIssues - HtmlHint issues array
*/
const printHtmlIssuesToConsole = (htmlIssues) => {
R.pipe(
// restructure the output of the HTMLHint
R.map(
R.applySpec({
url: R.prop("url"),
errors: R.pipe(
R.identity,
R.pipe(
R.prop("errors"),
R.converge(
R.zipWith((x, y) => ({
error: x,
locations: y,
})),
[R.keys, R.values]
)
)
),
})
),
R.tap(() => consoleBox("List of HTML Issues", "red")),
R.pipe(
R.forEach((x) => {
console.log(`${x.url}`);
R.pipe(
R.prop("errors"),
R.forEach((error) => {
console.log(`${error.error}`);
R.pipe(R.prop("locations"), R.forEach(console.log))(error);
console.log("");
})
)(x);
})
)
)(htmlIssues);
};

/**
Expand Down Expand Up @@ -752,10 +708,6 @@ exports.printResultsToConsole = (
consoleBox(getLinkToBuild(runId), "green");
} else {
badLinks.length && outputBadDataCsv(badLinks);

// if (htmlIssues) {
// printHtmlIssuesToConsole(htmlIssues);
// }
}
};

Expand Down Expand Up @@ -862,13 +814,4 @@ exports.getFinalEval = (
}
consoleBox(`Errors Detected`, "red");
return "FAIL";
};

exports.convertSpecialCharUrl = (url) => {
// Replace special characters in URL string
const specialChars = {
':': '%3A',
'/': '%2F'
};
return url.replace(/[:/]/g, m => specialChars[m]);
};
9 changes: 6 additions & 3 deletions ui/src/components/misccomponents/SendAlertModal.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
import LoadingFlat from "./LoadingFlat.svelte";
import { CONSTS, validateEmail, convertSpecialCharUrl } from "../../utils/utils";
import { CONSTS, validateEmail } from "../../utils/utils";
export let show;
export let url;
Expand All @@ -22,8 +22,11 @@
};
const reloadSharedEmailList = async () => {
let fullUrl = convertSpecialCharUrl(url)
const res = await fetch(`${CONSTS.API}/api/getalertemailaddresses/${userApiKey}/${fullUrl}`);
const res = await fetch(`${CONSTS.API}/api/getalertemailaddresses/${userApiKey}`, {
method: "POST",
body: JSON.stringify({url}),
headers: { "Content-Type": "application/json" },
})
sharedEmailAddresses = await res.json();
}
Expand Down
14 changes: 10 additions & 4 deletions ui/src/components/summaryitemcomponents/CardSummary.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@
const emailAlertModal = async () => {
if ($isLoggedIn) {
let fullUrl = convertSpecialCharUrl(value.url)
const res = await fetch(`${CONSTS.API}/api/getalertemailaddresses/${userApiKey}/${fullUrl}`);
const res = await fetch(`${CONSTS.API}/api/getalertemailaddresses/${userApiKey}`, {
method: "POST",
body: JSON.stringify({url: value.url}),
headers: { "Content-Type": "application/json" },
})
sharedEmailAddresses = await res.json();
showShareAlert = true;
} else {
Expand All @@ -59,8 +62,11 @@
const getPreviousScans = async () => {
isLoading = true;
let fullUrl = convertSpecialCharUrl(value.url)
const res = await fetch(`${CONSTS.API}/api/scanSummaryFromUrl/${userApiKey}/${fullUrl}`);
const res = await fetch(`${CONSTS.API}/api/scanSummaryFromUrl/${userApiKey}` , {
method: "POST",
body: JSON.stringify({url: value.url}),
headers: { "Content-Type": "application/json" },
})
if (res) {
previousScans = await res.json();
isLoading = false;
Expand Down
9 changes: 7 additions & 2 deletions ui/src/stores.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,18 @@ export const getLatestBuildDetails = async (api, url) => {
};

export const getAllScanSummaryFromUrl = async (api, url) => {
const res = await fetch(`${CONSTS.API}/api/scanSummaryFromUrl/${api}/${url}`);
const res = await fetch(`${CONSTS.API}/api/scanSummaryFromUrl/${api}`, {
method: "POST",
body: JSON.stringify({url}),
headers: { "Content-Type": "application/json" },
});

const result = await res.json();

if (res.ok) {
return result;
} else {
throw new Error('Failed to load');
throw new Error('Failed to retrieve all scan summaries');
}
};

Expand Down

0 comments on commit 1dffc64

Please sign in to comment.