Skip to content

Instantly share code, notes, and snippets.

@danieljin
Last active June 13, 2023 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danieljin/026a3b7abc711456bc959213cbafc586 to your computer and use it in GitHub Desktop.
Save danieljin/026a3b7abc711456bc959213cbafc586 to your computer and use it in GitHub Desktop.
don't include inprogress builds
// New Commit
function newCommit(steps){
if (steps.trigger.event.body.push.changes[0].closed){
return []
}
const title = !steps.trigger.event.body.push.changes[0].created ? "New commit(s) pushed" : "New branch created";
const commitCount = parseInt(steps.trigger.event.body.push.changes[0].commits.length);
const commitText = commitCount >= 5 ? `${commitCount}+ commits` : `${commitCount} commit${commitCount > 1 ? 's' : ''}`;
const branchUrl = `${steps.trigger.event.body.repository.links.html.href}/branch/${steps.trigger.event.body.push.changes[0].new.name}`;
const branchName = steps.trigger.event.body.push.changes[0].new.name;
const commitLines = steps.trigger.event.body.push.changes[0].commits.map(commit => {
const avatarUrl = commit.author.user.links.avatar.href;
const truncatedHash = commit.hash.substring(0, 6);
const commitUrl = commit.links.html.href;
const commitMessage = commit.message;
return {
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"width": "auto",
"items": [
{
"type": "Image",
"url": avatarUrl,
"size": "Small",
"style": "Person"
}
]
},
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "TextBlock",
"text": `[**${truncatedHash}**](${commitUrl}) ${commitMessage}`,
"spacing": "Small"
}
]
}
]
}
});
return [
{
"type": "Container",
"items": [
{
"type": "TextBlock",
"text": title,
"weight": "Bolder",
"size": "Medium"
},
{
"type": "TextBlock",
"text": `**${steps.trigger.event.body.actor.display_name}**${(steps.trigger.event.body.push.changes[0].forced ? " force" : "")} pushed ${commitText} to [**${branchName}**](${branchUrl}):`,
"spacing": "Small"
}
]
},
{
"type": "Container",
"spacing": "Medium",
"items": [
...commitLines
]
}
]
}
// Commit Comment
function commitCommentCreated(steps) {
const comment = steps.trigger.event.body.comment;
const commit = steps.trigger.event.body.commit;
const repo = steps.trigger.event.body.repository;
const author = comment.user;
const commentText = comment.content.raw;
const commentUrl = comment.links.html.href;
const commitHash = commit.hash.substring(0, 6);
const authorAvatarUrl = author.links.avatar.href;
const authorDisplayName = author.display_name;
return [
{
"type": "Container",
"items": [
{
"type": "TextBlock",
"text": "New Commit Comment Added",
"weight": "Bolder",
"size": "Medium"
},
{
"type": "TextBlock",
"text": `**${authorDisplayName}** commented on [**${commitHash}**](${commentUrl}):`,
"spacing": "Small"
}
]
},
{
"type": "ColumnSet",
"spacing": "Medium",
"columns": [
{
"type": "Column",
"width": "auto",
"items": [
{
"type": "Image",
"url": authorAvatarUrl,
"size": "Small",
"style": "Person"
}
]
},
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "TextBlock",
"text": `**Comment**: ${commentText}`,
"spacing": "Small"
}
]
}
]
}
];
}
// Build Status Updated
function buildStatusUpdated(steps) {
if (steps.trigger.event.body.commit_status.state === "INPROGRESS"){
return []
}
const buildStatus = steps.trigger.event.body.commit_status;
const commit = buildStatus.commit;
const commitName = buildStatus.name;
const commitHash = commit.hash.substring(0, 6);
const commitUrl = commit.links.html.href;
const commitMessage = commit.message;
const buildState = buildStatus.state;
const buildUrl = buildStatus.url;
const buildID = buildUrl.split('=')[1];
const author = commit.author.user;
const authorAvatar = author.links.avatar.href;
const textColor = buildState === "SUCCESSFUL" ? "Good" : buildState === "FAILED" ? "Attention" : "Default";
return [
{
"type": "Container",
"items": [
{
"type": "TextBlock",
"text": "Build Status Updated",
"weight": "Bolder",
"size": "Medium"
},
{
"type": "TextBlock",
"text": `Build status for [**${buildID}**](${buildUrl}) on **${commitName}** has been updated to **${buildState}**`,
"color": textColor,
"wrap": true
}
]
},
{
"type": "Container",
"spacing": "Medium",
"items": [
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"width": "auto",
"items": [
{
"type": "Image",
"url": authorAvatar,
"size": "Small",
"style": "Person"
}
]
},
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "TextBlock",
"text": `[**${commitHash}**](${commitUrl}) ${commitMessage}`,
"spacing": "Small"
}
]
}
]
}
]
}
];
}
// PULL REQUESTS
// Helper Function
function createColumnSet(avatarUrl, sourceBranchName, destinationBranchName, baseUrl) {
return {
"type": "ColumnSet",
"spacing": "Medium",
"columns": [
{
"type": "Column",
"width": "auto",
"items": [
{
"type": "Image",
"url": avatarUrl,
"size": "Small",
"style": "Person"
}
]
},
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "TextBlock",
"text": `[**${sourceBranchName}**](${baseUrl}/branch/${sourceBranchName}) → [**${destinationBranchName}**](${baseUrl}/branch/${destinationBranchName})`,
"spacing": "Small"
}
]
}
]
};
}
// PR Created
function prCreated(steps) {
const pr = steps.trigger.event.body.pullrequest;
const prUrl = pr.links.html.href;
const prTitle = pr.title;
const prId = pr.id;
const prAuthorAvatarUrl = pr.author.links.avatar.href;
const prAuthorDisplayName = pr.author.display_name;
return [
{
"type": "Container",
"items": [
{
"type": "TextBlock",
"text": "New Pull Request Created",
"weight": "Bolder",
"size": "Medium"
},
{
"type": "TextBlock",
"text": `**${prAuthorDisplayName}** created [**Pull Request #${prId}: ${prTitle}**](${prUrl})`,
"spacing": "Small"
}
]
},
createColumnSet(prAuthorAvatarUrl, pr.source.branch.name, pr.destination.branch.name, steps.trigger.event.body.repository.links.html.href)
]
}
function prApproved(steps) {
const pr = steps.trigger.event.body.pullrequest;
const prUrl = pr.links.html.href;
const prTitle = pr.title;
const prId = pr.id;
const prAuthorAvatarUrl = pr.author.links.avatar.href;
const approverDisplayName = steps.trigger.event.body.approval.user.display_name;
return [
{
"type": "Container",
"items": [
{
"type": "TextBlock",
"text": "Pull Request Approved",
"weight": "Bolder",
"size": "Medium"
},
{
"type": "TextBlock",
"text": `**${approverDisplayName}** approved [**Pull Request #${prId}: ${prTitle}**](${prUrl})`,
"spacing": "Small"
}
]
},
createColumnSet(prAuthorAvatarUrl, pr.source.branch.name, pr.destination.branch.name, steps.trigger.event.body.repository.links.html.href)
]
}
function prChangesRequested(steps) {
const pr = steps.trigger.event.body.pullrequest;
const prUrl = pr.links.html.href;
const prTitle = pr.title;
const prId = pr.id;
const prAuthorAvatarUrl = pr.author.links.avatar.href;
const reviewerDisplayName = steps.trigger.event.body.user.display_name;
return [
{
"type": "Container",
"items": [
{
"type": "TextBlock",
"text": "Changes Requested on Pull Request",
"weight": "Bolder",
"size": "Medium"
},
{
"type": "TextBlock",
"text": `**${reviewerDisplayName}** requested changes on [**Pull Request #${prId}: ${prTitle}**](${prUrl})`,
"spacing": "Small"
}
]
},
createColumnSet(prAuthorAvatarUrl, pr.source.branch.name, pr.destination.branch.name, steps.trigger.event.body.repository.links.html.href)
]
}
function prMerged(steps) {
const pr = steps.trigger.event.body.pullrequest;
const prUrl = pr.links.html.href;
const prTitle = pr.title;
const prId = pr.id;
const prAuthorAvatarUrl = pr.author.links.avatar.href;
const mergedByDisplayName = steps.trigger.event.body.actor.display_name;
return [
{
"type": "Container",
"items": [
{
"type": "TextBlock",
"text": "Pull Request Merged",
"weight": "Bolder",
"size": "Medium"
},
{
"type": "TextBlock",
"text": `**${mergedByDisplayName}** merged [**Pull Request #${prId}: ${prTitle}**](${prUrl})`,
"spacing": "Small"
}
]
},
createColumnSet(prAuthorAvatarUrl, pr.source.branch.name, pr.destination.branch.name, steps.trigger.event.body.repository.links.html.href)
]
}
function prDeclined(steps) {
const pr = steps.trigger.event.body.pullrequest;
const prUrl = pr.links.html.href;
const prTitle = pr.title;
const prId = pr.id;
const prAuthorAvatarUrl = pr.author.links.avatar.href;
const declinedByDisplayName = steps.trigger.event.body.actor.display_name;
return [
{
"type": "Container",
"items": [
{
"type": "TextBlock",
"text": "Pull Request Declined",
"weight": "Bolder",
"size": "Medium"
},
{
"type": "TextBlock",
"text": `**${declinedByDisplayName}** declined [**Pull Request #${prId}: ${prTitle}**](${prUrl})`,
"spacing": "Small"
}
]
},
createColumnSet(prAuthorAvatarUrl, pr.source.branch.name, pr.destination.branch.name, steps.trigger.event.body.repository.links.html.href)
]
}
const eventHandlers = {
"repo:push": newCommit,
"repo:commit_comment_created": commitCommentCreated,
"repo:commit_status_created": buildStatusUpdated,
"repo:commit_status_updated": buildStatusUpdated,
"pullrequest:created": prCreated,
"pullrequest:approved": prApproved,
"pullrequest:unapproved": prChangesRequested,
"pullrequest:fulfilled": prMerged,
"pullrequest:rejected": prDeclined,
};
export default defineComponent({
async run({ steps, $ }) {
const eventKey = steps.trigger.event.headers["x-event-key"];
const eventHandler = eventHandlers[eventKey];
if (eventHandler) {
const body = eventHandler(steps);
return JSON.stringify(body);
} else {
throw new Error(`Unhandled event: ${eventKey}`);
}
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment