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

feat: Add destination - Microsoft Teams (channels) #49

Merged
merged 27 commits into from
Jan 15, 2022
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
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
uses: ./
with:
token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
# microsoft_teams_webhook: ${{ secrets.MICROSOFT_TEAMS_WEBHOOK }}
# slack_webhook: ${{ secrets.SLACK_WEBHOOK }}
# pager_duty_integration_key: ${{ secrets.PAGER_DUTY_INTEGRATION_KEY }}
# zenduty_api_key: ${{ secrets.ZENDUTY_API_KEY }}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

A [GitHub action](https://github.com/features/actions) that sends Dependabot Vulnerability Alerts to multiple sources:

- Microsoft Teams
- Slack
- PagerDuty
- Zenduty
Expand Down
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ author: '@kunalnagar'
inputs:
token:
description: 'GitHub Personal Access Token'
microsoft_teams_webhook:
description: 'Microsoft Teams Channel Webhook URL. More info: https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook'
slack_webhook:
description: 'Slack Webhook URL. More info: https://api.slack.com/messaging/webhooks'
pager_duty_integration_key:
Expand Down
11,339 changes: 11,337 additions & 2 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions dist/licenses.txt

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

145 changes: 75 additions & 70 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@actions/github": "5.0.0",
"@pagerduty/pdjs": "2.2.4",
"@slack/webhook": "6.0.0",
"adaptivecards": "^2.10.0",
"node-fetch": "2.6.6"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/destinations/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './slack'
export * from './pager-duty'
export * from './zenduty'
export * from './microsoft-teams'
96 changes: 96 additions & 0 deletions src/destinations/microsoft-teams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* eslint-disable i18n-text/no-en */
import {
Row,
createAdaptiveCard,
createColumn,
createContainer,
createLinkButton,
createRow,
createTextBlock,
request,
} from '../utils'
import { ACTION_SHORT_SUMMARY } from '../constants'
import { Alert } from '../entities'

const createTableRow = (key: string, value: string): Row => {
const row = createRow()
const keyColumn = createColumn()
keyColumn.addItem(createTextBlock(key, true))
row.addColumn(keyColumn)
const valueColumn = createColumn()
valueColumn.addItem(createTextBlock(value))
row.addColumn(valueColumn)
return row
}

const createTableButtonRow = (url: string): Row => {
const row = createRow()
const keyColumn = createColumn()
keyColumn.addItem(createTextBlock('Advisory URL', true))
row.addColumn(keyColumn)
const urlColumn = createColumn()
urlColumn.addItem(createLinkButton('View Advisory', url))
row.addColumn(urlColumn)
return row
}

export const sendAlertsToMicrosoftTeams = async (
webhookUrl: string,
alerts: Alert[],
): Promise<void> => {
const alertCount = alerts.length
const repositoryOwner = alerts[0].repository.owner
const repositoryName = alerts[0].repository.name

const adaptiveCard = createAdaptiveCard()

adaptiveCard.addItem(createTextBlock(ACTION_SHORT_SUMMARY))

adaptiveCard.addItem(
createTextBlock(
`You have ${alertCount} vulnerabilities in ${repositoryOwner}/${repositoryName}`,
),
)

for (const alert of alerts) {
const container = createContainer(true, true)
container.addItem(createTableRow('Package Name', alert.packageName))
container.addItem(
createTableRow(
'Vulnerability Version Range',
alert.vulnerability?.vulnerableVersionRange || '',
),
)
container.addItem(
createTableRow(
'Patched Version',
alert.vulnerability?.firstPatchedVersion || '',
),
)
container.addItem(
createTableRow('Severity', alert.advisory?.severity || ''),
)
container.addItem(createTableRow('Summary', alert.advisory?.summary || ''))
container.addItem(createTableButtonRow(alert.advisory?.url || ''))
adaptiveCard.addItem(container)
}

const body = {
type: 'message',
attachments: [
{
contentType: 'application/vnd.microsoft.card.adaptive',
contentUrl: null,
content: adaptiveCard.toJSON(),
},
],
}

await request(webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
})
}
5 changes: 5 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getInput, setFailed } from '@actions/core'
import {
sendAlertsToMicrosoftTeams,
sendAlertsToPagerDuty,
sendAlertsToSlack,
sendAlertsToZenduty,
Expand All @@ -11,6 +12,7 @@ import { fetchAlerts } from './fetch-alerts'
async function run(): Promise<void> {
try {
const token = getInput('token')
const microsoftTeamsWebhookUrl = getInput('microsoft_teams_webhook')
const slackWebhookUrl = getInput('slack_webhook')
const pagerDutyIntegrationKey = getInput('pager_duty_integration_key')
const zenDutyApiKey = getInput('zenduty_api_key')
Expand All @@ -21,6 +23,9 @@ async function run(): Promise<void> {
const repo = context.repo.repo
const alerts = await fetchAlerts(token, repo, owner, count)
if (alerts.length > 0) {
if (microsoftTeamsWebhookUrl) {
await sendAlertsToMicrosoftTeams(microsoftTeamsWebhookUrl, alerts)
}
if (slackWebhookUrl) {
if (!validateSlackWebhookUrl(slackWebhookUrl)) {
setFailed(new Error('Invalid Slack Webhook URL'))
Expand Down
Loading