Created
April 5, 2023 09:49
-
-
Save daniel-sc/a06e9b497a05f8854aebbb5b3104a65d to your computer and use it in GitHub Desktop.
Deno script that can be deployed to https://deno.com/deploy and registered as a bitbucket webhook to notifiy new PRs in a MS Teams channel.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Application, Router } from 'https://deno.land/x/oak/mod.ts'; | |
// Load environment variables | |
const PORT = 8080; | |
const TEAMS_WEBHOOK_URL = 'https://XYZ.webhook.office.com/webhookb2/XYZ'; | |
const TARGET_USERNAME = 'xyz'; | |
if (!TEAMS_WEBHOOK_URL || !TARGET_USERNAME) { | |
throw new Error('Missing required environment variables.'); | |
} | |
// Create a function to send a message to a Microsoft Teams channel | |
async function sendTeamsMessage(text: string) { | |
const response = await fetch(TEAMS_WEBHOOK_URL, { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify({ | |
'@type': 'MessageCard', | |
'@context': 'http://schema.org/extensions', | |
text: text, | |
}), | |
}); | |
if (!response.ok) { | |
throw new Error('Failed to send message to MS Teams.'); | |
} | |
} | |
const router = new Router(); | |
router.post('/webhook', async (context) => { | |
const requestBody = await context.request.body({ type: 'json' }).value; | |
console.log('got request', requestBody); | |
const eventType = requestBody.eventKey; | |
const pullRequest = requestBody.pullRequest; | |
const authorUsername = pullRequest?.author?.user?.name; | |
if (eventType === 'pr:opened' && authorUsername === TARGET_USERNAME) { | |
const title = pullRequest.title; | |
const url = pullRequest.links?.self?.[0]?.href; | |
await sendTeamsMessage( | |
`Please Review: [${title}](${url}) LG Daniel` | |
); | |
context.response.status = 200; | |
context.response.body = { success: true }; | |
} else { | |
context.response.status = 200; | |
context.response.body = { success: false }; | |
} | |
}); | |
const app = new Application(); | |
app.use(router.routes()); | |
app.use(router.allowedMethods()); | |
console.log(`Listening on port ${PORT}`); | |
await app.listen({ port: PORT }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment