Skip to content

Commit

Permalink
chore: Update ESLint package (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
kunalnagar authored Feb 13, 2024
1 parent 2a2fc03 commit 6f2ffeb
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 1,207 deletions.
11 changes: 8 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
"parserOptions": {
"ecmaVersion": 9,
"sourceType": "module",
"project": "./tsconfig.json"
"project": "./tsconfig.json",
},
"env": {
"node": true,
"es6": true
}
"es6": true,
},
"rules": {
"import/prefer-default-export": "off",
"import/no-cycle": "off",
"radix": "off",
},
}
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"source.fixAll.eslint": "explicit"
},
"editor.rulers": [80],
"files.insertFinalNewline": true
"files.insertFinalNewline": true,
"typescript.tsdk": "node_modules/typescript/lib"
}
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"build": "tsc",
"format": "prettier --write src/**/*.ts",
"format-check": "prettier --check src/**/*.ts",
"lint": "eslint --fix src/**/*.ts",
"lint": "eslint src/**/*.ts",
"lint:fix": "eslint --fix src/**/*.ts",
"package": "ncc build --source-map --license licenses.txt",
"test": "jest",
"all": "yarn build && yarn format && yarn lint && yarn package && yarn test",
Expand Down Expand Up @@ -56,7 +57,7 @@
"nodemailer": "6.9.9"
},
"devDependencies": {
"@kunalnagarco/eslint-config": "^0.2.7",
"@kunalnagarco/eslint-config": "2.1.0",
"@semantic-release/changelog": "6.0.3",
"@semantic-release/commit-analyzer": "11.1.0",
"@semantic-release/git": "10.0.1",
Expand All @@ -66,11 +67,18 @@
"@types/node": "20.11.16",
"@types/node-fetch": "2.6.11",
"@types/nodemailer": "6.4.14",
"@typescript-eslint/eslint-plugin": "^7.0.1",
"@typescript-eslint/parser": "^7.0.1",
"@vercel/ncc": "0.38.1",
"conventional-changelog-conventionalcommits": "7.0.2",
"eslint": "^8.56.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-airbnb-typescript": "^17.1.0",
"eslint-config-prettier": "^9.1.0",
"eslint-config-react-app": "^7.0.1",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jest": "^27.6.3",
"eslint-plugin-jsx-a11y": "^6.8.0",
"eslint-plugin-react": "^7.33.2",
"husky": "9.0.10",
"jest": "29.7.0",
"lint-staged": "15.2.1",
Expand Down
32 changes: 14 additions & 18 deletions src/destinations/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@ import SMTPTransport from 'nodemailer/lib/smtp-transport'
import { ACTION_SHORT_SUMMARY, ACTION_URL } from '../constants'
import { Alert, getFullRepositoryNameFromAlert } from '../entities'

const createTableRow = (alert: Alert): string => `
<tr>
<td>${alert.packageName}</td>
<td>${alert.vulnerability?.vulnerableVersionRange}</td>
<td>${alert.vulnerability?.firstPatchedVersion}</td>
<td>${alert.advisory?.severity}</td>
<td>${alert.advisory?.summary}</td>
<td><a href="${alert.advisory?.url}">View</a></td>
</tr>
`

const createTable = (alerts: Alert[]): string => {
let rowData = ''
for (const alert of alerts) {
alerts.forEach((alert) => {
rowData += createTableRow(alert)
}
})
return `
<table border="1" cellpadding="10" width="100%">
<thead>
Expand All @@ -26,28 +37,13 @@ const createTable = (alerts: Alert[]): string => {
`
}

const createTableRow = (alert: Alert): string => {
return `
<tr>
<td>${alert.packageName}</td>
<td>${alert.vulnerability?.vulnerableVersionRange}</td>
<td>${alert.vulnerability?.firstPatchedVersion}</td>
<td>${alert.advisory?.severity}</td>
<td>${alert.advisory?.summary}</td>
<td><a href="${alert.advisory?.url}">View</a></td>
</tr>
`
}

const createEmailBody = (alerts: Alert[]): string => {
return `
const createEmailBody = (alerts: Alert[]): string => `
<p>Hello,</p>
<p>You are receiving this message as you have set up email notifications for vulnerabilities in <b>${getFullRepositoryNameFromAlert(
alerts[0],
)}</b> via <a href="${ACTION_URL}">${ACTION_SHORT_SUMMARY}</a>.</p>
${createTable(alerts)}
`
}

export const sendAlertsToEmailSmtp = async (
config: SMTPTransport.Options,
Expand Down
4 changes: 2 additions & 2 deletions src/destinations/microsoft-teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const sendAlertsToMicrosoftTeams = async (
),
)

for (const alert of alerts) {
alerts.forEach((alert) => {
const container = createContainer(true, true)
container.addItem(createTableRow('Package Name', alert.packageName))
container.addItem(
Expand All @@ -72,7 +72,7 @@ export const sendAlertsToMicrosoftTeams = async (
container.addItem(createTableRow('Summary', alert.advisory?.summary || ''))
container.addItem(createTableButtonRow(alert.advisory?.url || ''))
adaptiveCard.addItem(container)
}
})

const body = {
type: 'message',
Expand Down
78 changes: 35 additions & 43 deletions src/destinations/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,77 +4,69 @@ import { KnownBlock } from '@slack/types'
import { ACTION_ICON, ACTION_SHORT_SUMMARY } from '../constants'
import { Alert } from '../entities'

export const MAX_COUNT_SLACK = 30

const createMaxAlertsMarkdownNotice = (): string =>
`*Note:* Only ${MAX_COUNT_SLACK} have been sent due to message length restrictions.`

const createSummaryBlock = (
alertCount: number,
repositoryName: string,
repositoryOwner: string,
): KnownBlock => {
return {
type: 'section',
text: {
type: 'mrkdwn',
text: `
): KnownBlock => ({
type: 'section',
text: {
type: 'mrkdwn',
text: `
You have ${alertCount} vulnerabilities in *${repositoryOwner}/${repositoryName}*.
${alertCount > MAX_COUNT_SLACK ? createMaxAlertsMarkdownNotice() : ''}
`,
},
}
}
},
})

const createDividerBlock = (): KnownBlock => {
return {
type: 'divider',
}
}
const createDividerBlock = (): KnownBlock => ({
type: 'divider',
})

const createAlertBlock = (alert: Alert): KnownBlock => {
return {
type: 'section',
text: {
type: 'mrkdwn',
text: `
const createAlertBlock = (alert: Alert): KnownBlock => ({
type: 'section',
text: {
type: 'mrkdwn',
text: `
*Package name:* ${alert.packageName}
*Vulnerability Version Range:* ${alert.vulnerability?.vulnerableVersionRange}
*Patched Version:* ${alert.vulnerability?.firstPatchedVersion}
*Severity:* ${alert.advisory?.severity}
*Summary:* ${alert.advisory?.summary}
`,
},
accessory: {
type: 'button',
text: {
type: 'plain_text',
text: 'View Advisory',
emoji: true,
},
accessory: {
type: 'button',
text: {
type: 'plain_text',
text: 'View Advisory',
emoji: true,
},
style: 'danger',
url: alert.advisory?.url,
},
}
}

const createMaxAlertsMarkdownNotice = (): string => {
return `*Note:* Only ${MAX_COUNT_SLACK} have been sent due to message length restrictions.`
}
style: 'danger',
url: alert.advisory?.url,
},
})

export const validateSlackWebhookUrl = (url: string): boolean => {
const regexPattern = new RegExp(
/^https:\/\/hooks\.slack\.com\/services\/T[a-zA-Z0-9_]{8,10}\/B[a-zA-Z0-9_]{10}\/[a-zA-Z0-9_]{24}/,
)
const regexPattern =
/^https:\/\/hooks\.slack\.com\/services\/T[a-zA-Z0-9_]{8,10}\/B[a-zA-Z0-9_]{10}\/[a-zA-Z0-9_]{24}/
return regexPattern.test(url)
}

export const MAX_COUNT_SLACK = 30

export const sendAlertsToSlack = async (
webhookUrl: string,
alerts: Alert[],
): Promise<void> => {
const webhook = new IncomingWebhook(webhookUrl)
const alertBlocks: KnownBlock[] = []
for (const alert of alerts) {
alerts.forEach((alert) => {
alertBlocks.push(createAlertBlock(alert))
}
})
await webhook.send({
blocks: [
createSummaryBlock(
Expand Down
4 changes: 2 additions & 2 deletions src/destinations/zenduty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ export const sendAlertsToZenduty = async (
---
`
for (const alert of alerts) {
alerts.forEach((alert) => {
summary += `
Package name: ${alert.packageName}
Vulnerability Version Range: ${alert.vulnerability?.vulnerableVersionRange}
Patched Version: ${alert.vulnerability?.firstPatchedVersion}
Severity: ${alert.advisory?.severity}
Summary: ${alert.advisory?.summary}
`
}
})

summary += `
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ async function run(): Promise<void> {
const emailTransportSmtpPassword = getInput('email_transport_smtp_password')
const count = parseInt(getInput('count'))
const severity = getInput('severity')
const owner = context.repo.owner
const repo = context.repo.repo
const { owner } = context.repo
const { repo } = context.repo
const alerts = await fetchAlerts(token, repo, owner, severity, count)
if (alerts.length > 0) {
if (microsoftTeamsWebhookUrl) {
Expand Down
Loading

0 comments on commit 6f2ffeb

Please sign in to comment.