Skip to content

Commit

Permalink
Merge pull request #7 from PasseiDireto/feat/workflow-teams
Browse files Browse the repository at this point in the history
feat: add support to workflow teams webhook
  • Loading branch information
jhonsouza authored Nov 28, 2024
2 parents f5df30c + 72050f8 commit 628e628
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 35 deletions.
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ inputs:
required: true
ms-teams-webhook-uri:
description: "Microsoft Teams Webhook URI"
required: true
required: false
workflow-teams-webhook-uri:
description: "Microsoft Workflow Teams Webhook URI"
required: false
notification-summary:
description: "Message to be sent to Microsoft Teams channel"
required: true
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "pd-teams-notification-action",
"description": "GitHub Action to send MS Teams Notification",
"version": "1.1.0",
"version": "1.2.0",
"private": true,
"engines": {
"node": ">=20.0.0",
Expand Down
21 changes: 18 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ async function run(): Promise<void> {
try {
const githubToken = core.getInput('github-token', { required: true });
const msTeamsWebhookUri: string = core.getInput('ms-teams-webhook-uri', {
required: true,
required: false,
});
const workflowWebhookUri: string = core.getInput('workflow-teams-webhook-uri', { required: false });

const notificationSummary = core.getInput('notification-summary') || 'GitHub Action Notification';
const notificationColor = core.getInput('notification-color') || '0b93ff';
Expand Down Expand Up @@ -41,12 +42,25 @@ async function run(): Promise<void> {
repoName,
sha,
repoUrl,
timestamp
timestamp,
workflowWebhookUri
);

console.log(messageCard);

axios
if(!msTeamsWebhookUri){
axios
.post(workflowWebhookUri, messageCard)
.then(function (response) {
console.log(response);
core.debug(response.data);
})
.catch(function (error) {
core.debug(error);
});
}
else{
axios
.post(msTeamsWebhookUri, messageCard)
.then(function (response) {
console.log(response);
Expand All @@ -55,6 +69,7 @@ async function run(): Promise<void> {
.catch(function (error) {
core.debug(error);
});
}
} catch (error) {
console.log(error);
core.setFailed(error.message);
Expand Down
124 changes: 95 additions & 29 deletions src/message-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,107 @@ export function createMessageCard(
repoName: string,
sha: string,
repoUrl: string,
timestamp: string
timestamp: string,
workflowWebhook: string
): any {
let messageCard
let avatar_url = 'https://avatars.githubusercontent.com/u/6343056?s=200&v=4';
if (author) {
if (author.avatar_url) {
avatar_url = author.avatar_url;
}
}
const messageCard = {
'@type': 'MessageCard',
'@context': 'https://schema.org/extensions',
summary: notificationSummary,
themeColor: notificationColor,
title: notificationSummary,
sections: [
{
activityTitle: `**CI #${runNum} (commit ${sha.substr(0, 7)})** on [${repoName}](${repoUrl})`,
activityImage: avatar_url,
activitySubtitle: `by ${commit.data.commit.author.name} [(@${authorLogin})](${author.html_url}) on ${timestamp}`,
},
],
potentialAction: [
{
'@context': 'http://schema.org',
target: [`${repoUrl}/actions/runs/${runId}`],
'@type': 'ViewAction',
name: 'View Workflow Run',
},
{
'@context': 'http://schema.org',
target: [commit.data.html_url],
'@type': 'ViewAction',
name: 'View Commit Changes',
},
],
};
if(!workflowWebhook){
messageCard = {
'@type': 'MessageCard',
'@context': 'https://schema.org/extensions',
summary: notificationSummary,
themeColor: notificationColor,
title: notificationSummary,
sections: [
{
activityTitle: `**CI #${runNum} (commit ${sha.substr(0, 7)})** on [${repoName}](${repoUrl})`,
activityImage: avatar_url,
activitySubtitle: `by ${commit.data.commit.author.name} [(@${authorLogin})](${author.html_url}) on ${timestamp}`,
},
],
potentialAction: [
{
'@context': 'http://schema.org',
target: [`${repoUrl}/actions/runs/${runId}`],
'@type': 'ViewAction',
name: 'View Workflow Run',
},
{
'@context': 'http://schema.org',
target: [commit.data.html_url],
'@type': 'ViewAction',
name: 'View Commit Changes',
},
],
};
}
else{
messageCard = {
"contentType": "application/vnd.microsoft.card.adaptive",
"content": {
"type": "AdaptiveCard",
"version": "1.2",
"body": [
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"width": "auto",
"items": [
{
"type": "Image",
"url": avatar_url,
"size": "small"
}
]
},
{
"type": "Column",
"width": "*",
"items": [
{
"type": "TextBlock",
"text": notificationSummary,
"wrap": true,
"color": notificationColor
},
{
"type": "TextBlock",
"text": `CI #${runNum} (commit ${sha.substr(0, 7)}) on [${repoName}](${repoUrl})`,
"wrap": true
},
{
"type": "TextBlock",
"text": `by ${commit.data.commit.author.name} [(@${authorLogin})](${author.html_url}) on ${timestamp}`,
"wrap": true
}
]
}
]
}
],
"actions": [
{
"type": "ACtion.OpenUrl",
"title": "View Workflow Run",
"url": `${repoUrl}/actions/runs/${runId}`
},
{
"type": "Action.OpenUrl",
"title": "View Commit Changes",
"url": commit.data.html_url
}
]
}
}
}

return messageCard;
}

0 comments on commit 628e628

Please sign in to comment.