Skip to content

Commit

Permalink
Handle much more events
Browse files Browse the repository at this point in the history
  • Loading branch information
DEVTomatoCake committed Nov 3, 2023
1 parent 043f8e5 commit e15e4ae
Show file tree
Hide file tree
Showing 23 changed files with 145 additions and 109 deletions.
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,18 @@ const hookFunc = async (req, res) => {
message = JSON.stringify(require(path.join(__dirname, "templates", githubEventSani + ".js")).find(msg => msg.action == action) || require(path.join(__dirname, "templates", githubEventSani + ".js"))[0])
}

// Handling {{ count(array) }}
message = message.replace(/{{ ?count\(([^}]+)\) ?}}/gi, (match, group) => {
const parts = group.split(".")
let obj = data
for (const part of parts) obj = obj[part]
return obj.length
})

const recursiveFunc = (obj, currentPath = "") => {
for (const property in obj) {
if (typeof obj[property] == "object") recursiveFunc(obj[property], currentPath + property + ".")
// Possible syntax: {sender.login} or {{ sender.login }} or something in between
else message = message.replace(new RegExp("{{? ?" + currentPath + property + " ?}}?", "gi"), obj[property])
else message = message.replace(new RegExp("{{ ?" + currentPath + property + " ?}}", "gi"), obj[property])
}
}
recursiveFunc(data)
Expand Down
36 changes: 18 additions & 18 deletions templates/code_scanning_alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `code_scanning_alert` (`appeared_in_branch`)",
url: "{{ repository.html_url }}",
color: color("black")
title: "[{{ repository.name }}] Code scanning alert **#{{ alert.number }}** appeared in {{ ref }}",
url: "{{ alert.html_url }}",
color: color("red")
}]
},{
action: "closed_by_user",
Expand All @@ -19,9 +19,9 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `code_scanning_alert` (`closed_by_user`)",
url: "{{ repository.html_url }}",
color: color("black")
title: "[{{ repository.name }}] Code scanning alert **#{{ alert.number }}** closed as {{ alert.dismissed_reason }}",
url: "{{ alert.html_url }}",
color: color("red")
}]
},{
action: "created",
Expand All @@ -30,9 +30,9 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `code_scanning_alert` (`created`)",
url: "{{ repository.html_url }}",
color: color("green")
title: "[{{ repository.name }}] Code scanning alert **#{{ alert.number }}** created",
url: "{{ alert.html_url }}",
color: color("red")
}]
},{
action: "fixed",
Expand All @@ -41,9 +41,9 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `code_scanning_alert` (`fixed`)",
url: "{{ repository.html_url }}",
color: color("black")
title: "[{{ repository.name }}] Code scanning alert **#{{ alert.number }}** fixed",
url: "{{ alert.html_url }}",
color: color("green")
}]
},{
action: "reopened",
Expand All @@ -52,9 +52,9 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `code_scanning_alert` (`reopened`)",
url: "{{ repository.html_url }}",
color: color("black")
title: "[{{ repository.name }}] Code scanning alert **#{{ alert.number }}** reopened",
url: "{{ alert.html_url }}",
color: color("red")
}]
},{
action: "reopened_by_user",
Expand All @@ -63,9 +63,9 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `code_scanning_alert` (`reopened_by_user`)",
url: "{{ repository.html_url }}",
color: color("black")
title: "[{{ repository.name }}] Code scanning alert **#{{ alert.number }}** reopened",
url: "{{ alert.html_url }}",
color: color("red")
}]
}
]
5 changes: 3 additions & 2 deletions templates/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `create`",
title: "[{{ repository.name }}] Git {{ ref_type }} created",
url: "{{ repository.html_url }}",
color: color("black")
description: "```{{ ref }}```",
color: color("darkGreen")
}]
}
]
4 changes: 2 additions & 2 deletions templates/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] Git {{ ref_type }} deleted",
title: "[{{ repository.name }}] Git {{ ref_type }} deleted",
url: "{{ repository.html_url }}",
description: "```{{ ref }}```",
color: color("black")
color: color("darkRed")
}]
}
]
4 changes: 2 additions & 2 deletions templates/deploy_key.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.full_name }}] Deploy key **{{ key.title }}** created",
title: "[{{ repository.name }}] Deploy key **{{ key.title }}** created",
url: "{{ repository.html_url }}",
color: color("green")
}]
Expand All @@ -19,7 +19,7 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.full_name }}] Deploy key **{{ key.title }}** deleted",
title: "[{{ repository.name }}] Deploy key **{{ key.title }}** deleted",
url: "{{ repository.html_url }}",
color: color("red")
}]
Expand Down
3 changes: 1 addition & 2 deletions templates/deployment.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ module.exports = [
title: "[{{ repository.name }}:{{ repository.default_branch }}] Deployment created",
url: "{{ deployment.url }}",
description: "{{ deployment.description }}\n\n{{ deployment.environment }}",
color: color("green"),
timestamp: "{{ deployment.created_at }}"
color: color("green")
}]
}
]
2 changes: 1 addition & 1 deletion templates/deployment_protection_rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] Deployment protection rule requested",
title: "[{{ repository.name }}] Deployment protection rule requested by {{ event }}",
url: "{{ repository.html_url }}",
color: color("black")
}]
Expand Down
5 changes: 3 additions & 2 deletions templates/fork.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ module.exports = [
embeds: [{
author: {
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
icon_url: "{{ sender.avatar_url }}",
url: "{{ sender.html_url }}"
},
title: "[{{ forkee.full_name }}] Fork created",
url: "{{ forkee.html_url }}",
color: color("black")
color: color("magenta")
}]
}
]
15 changes: 9 additions & 6 deletions templates/issue_comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] Issue comment created",
url: "{{ repository.html_url }}",
title: "[{{ repository.name }}] Commented on issue **#{{ issue.number }}**",
url: "{{ comment.html_url }}",
description: "{{ comment.body }}",
color: color("green")
}]
},{
Expand All @@ -19,8 +20,9 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] Issue comment deleted",
url: "{{ repository.html_url }}",
title: "[{{ repository.name }}] Comment on issue **#{{ issue.number }}** deleted",
url: "{{ comment.html_url }}",
description: "{{ comment.body }}",
color: color("red")
}]
},{
Expand All @@ -30,8 +32,9 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] Issue comment edited",
url: "{{ repository.html_url }}",
title: "[{{ repository.name }}] Comment on issue **#{{ issue.number }}** edited",
url: "{{ comment.html_url }}",
description: "{{ comment.body }}",
color: color("cyan")
}]
}
Expand Down
9 changes: 6 additions & 3 deletions templates/label.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `label` (`created`)",
title: "[{{ repository.name }}] Label **{{ label.name }}** created",
url: "{{ repository.html_url }}",
description: "{{ label.description }}\n\nColor: #{{ label.color }}",
color: color("green")
}]
},{
Expand All @@ -19,8 +20,9 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `label` (`deleted`)",
title: "[{{ repository.name }}] Label **{{ label.name }}** deleted",
url: "{{ repository.html_url }}",
description: "{{ label.description }}\n\nColor: #{{ label.color }}",
color: color("red")
}]
},{
Expand All @@ -30,8 +32,9 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `label` (`edited`)",
title: "[{{ repository.name }}] Label **{{ label.name }}** edited",
url: "{{ repository.html_url }}",
description: "{{ label.description }}\n\nColor: #{{ label.color }}",
color: color("cyan")
}]
}
Expand Down
1 change: 1 addition & 0 deletions templates/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = [
},
title: "[{{ repository.name }}] This webhook has been deleted",
url: "{{ repository.html_url }}",
description: "The following events were enabled:\n```{{ hook.events }}```",
color: color("darkRed")
}]
}
Expand Down
21 changes: 12 additions & 9 deletions templates/personal_access_token_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `personal_access_token_request` (`approved`)",
url: "{{ repository.html_url }}",
title: "[{{ organization.name }}] Personal access token request was approved",
description: "Requested by {{ personal_access_token_request.owner.login }}",
thumbnail: {
url: "{{ personal_access_token_request.owner.avatar_url }}"
},
color: color("green")
}]
},{
Expand All @@ -19,8 +22,8 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `personal_access_token_request` (`cancelled`)",
url: "{{ repository.html_url }}",
title: "[{{ organization.name }}] Personal access token request was cancelled",
url: "{{ organization.html_url }}",
color: color("red")
}]
},{
Expand All @@ -30,8 +33,8 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `personal_access_token_request` (`created`)",
url: "{{ repository.html_url }}",
title: "[{{ organization.name }}] Personal access token requested",
url: "{{ organization.html_url }}",
color: color("green")
}]
},{
Expand All @@ -41,9 +44,9 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `personal_access_token_request` (`denied`)",
url: "{{ repository.html_url }}",
color: color("black")
title: "[{{ organization.name }}] Personal access token request was denied",
url: "{{ organization.html_url }}",
color: color("red")
}]
}
]
4 changes: 2 additions & 2 deletions templates/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `public`",
title: "[{{ repository.full_name }}] Now open source!",
url: "{{ repository.html_url }}",
color: color("black")
color: color("darkGreen")
}]
}
]
4 changes: 2 additions & 2 deletions templates/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module.exports = [
name: "{{ sender.login }}",
icon_url: "{{ sender.avatar_url }}"
},
title: "[{{ repository.name }}:{{ repository.default_branch }}] `push`",
url: "{{ repository.html_url }}",
title: "[{{ repository.name }}:{{ base_ref }}] {{ count(commits) }} new commit(s)",
url: "{{ compare }}",
color: color("black")
}]
}
Expand Down
Loading

0 comments on commit e15e4ae

Please sign in to comment.