2023-01-27 03:22:44 -08:00
import type {
2023-03-09 09:13:15 -08:00
IHookFunctions ,
IWebhookFunctions ,
2019-06-23 03:35:23 -07:00
IDataObject ,
INodeType ,
2020-10-01 05:01:39 -07:00
INodeTypeDescription ,
2019-10-11 04:02:44 -07:00
IWebhookResponseData ,
2023-02-27 19:39:43 -08:00
JsonObject ,
2019-06-23 03:35:23 -07:00
} from 'n8n-workflow' ;
2023-01-27 03:22:44 -08:00
import { NodeApiError , NodeOperationError } from 'n8n-workflow' ;
2019-06-23 03:35:23 -07:00
2022-08-17 08:50:24 -07:00
import { githubApiRequest } from './GenericFunctions' ;
2023-02-15 04:12:28 -08:00
import { getRepositories , getUsers } from './SearchFunctions' ;
2019-06-23 03:35:23 -07:00
export class GithubTrigger implements INodeType {
description : INodeTypeDescription = {
displayName : 'Github Trigger' ,
name : 'githubTrigger' ,
2024-06-26 00:35:59 -07:00
icon : { light : 'file:github.svg' , dark : 'file:github.dark.svg' } ,
2019-06-23 03:35:23 -07:00
group : [ 'trigger' ] ,
version : 1 ,
2022-08-17 08:50:24 -07:00
subtitle :
'={{$parameter["owner"] + "/" + $parameter["repository"] + ": " + $parameter["events"].join(", ")}}' ,
2021-07-03 05:40:16 -07:00
description : 'Starts the workflow when Github events occur' ,
2019-06-23 03:35:23 -07:00
defaults : {
name : 'Github Trigger' ,
} ,
inputs : [ ] ,
outputs : [ 'main' ] ,
credentials : [
{
name : 'githubApi' ,
required : true ,
2020-06-09 13:50:09 -07:00
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
authentication : [ 'accessToken' ] ,
2020-06-09 13:50:09 -07:00
} ,
} ,
} ,
{
name : 'githubOAuth2Api' ,
required : true ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
authentication : [ 'oAuth2' ] ,
2020-06-09 13:50:09 -07:00
} ,
} ,
} ,
2019-06-23 03:35:23 -07:00
] ,
webhooks : [
{
name : 'default' ,
httpMethod : 'POST' ,
2019-08-28 08:16:09 -07:00
responseMode : 'onReceived' ,
2019-06-23 03:35:23 -07:00
path : 'webhook' ,
} ,
] ,
properties : [
2023-03-08 08:04:45 -08:00
{
displayName :
'Only members with owner privileges for an organization or admin privileges for a repository can set up the webhooks this node requires.' ,
name : 'notice' ,
type : 'notice' ,
default : '' ,
} ,
2020-06-09 13:50:09 -07:00
{
displayName : 'Authentication' ,
name : 'authentication' ,
type : 'options' ,
options : [
{
name : 'Access Token' ,
value : 'accessToken' ,
} ,
{
name : 'OAuth2' ,
value : 'oAuth2' ,
} ,
] ,
default : 'accessToken' ,
} ,
2019-06-23 03:35:23 -07:00
{
displayName : 'Repository Owner' ,
name : 'owner' ,
2023-02-15 04:12:28 -08:00
type : 'resourceLocator' ,
default : { mode : 'list' , value : '' } ,
2019-06-23 03:35:23 -07:00
required : true ,
2023-02-15 04:12:28 -08:00
modes : [
{
displayName : 'Repository Owner' ,
name : 'list' ,
type : 'list' ,
placeholder : 'Select an owner...' ,
typeOptions : {
searchListMethod : 'getUsers' ,
searchable : true ,
searchFilterRequired : true ,
} ,
} ,
{
displayName : 'Link' ,
name : 'url' ,
type : 'string' ,
placeholder : 'e.g. https://github.com/n8n-io' ,
extractValue : {
type : 'regex' ,
regex : 'https:\\/\\/github.com\\/([-_0-9a-zA-Z]+)' ,
} ,
validation : [
{
type : 'regex' ,
properties : {
regex : 'https:\\/\\/github.com\\/([-_0-9a-zA-Z]+)(?:.*)' ,
errorMessage : 'Not a valid Github URL' ,
} ,
} ,
] ,
} ,
{
displayName : 'By Name' ,
name : 'name' ,
type : 'string' ,
placeholder : 'e.g. n8n-io' ,
validation : [
{
type : 'regex' ,
properties : {
regex : '[-_a-zA-Z0-9]+' ,
errorMessage : 'Not a valid Github Owner Name' ,
} ,
} ,
] ,
url : '=https://github.com/{{$value}}' ,
} ,
] ,
2019-06-23 03:35:23 -07:00
} ,
{
displayName : 'Repository Name' ,
name : 'repository' ,
2023-02-15 04:12:28 -08:00
type : 'resourceLocator' ,
default : { mode : 'list' , value : '' } ,
2019-06-23 03:35:23 -07:00
required : true ,
2023-02-15 04:12:28 -08:00
modes : [
{
displayName : 'Repository Name' ,
name : 'list' ,
type : 'list' ,
placeholder : 'Select an Repository...' ,
typeOptions : {
searchListMethod : 'getRepositories' ,
searchable : true ,
} ,
} ,
{
displayName : 'Link' ,
name : 'url' ,
type : 'string' ,
placeholder : 'e.g. https://github.com/n8n-io/n8n' ,
extractValue : {
type : 'regex' ,
regex : 'https:\\/\\/github.com\\/(?:[-_0-9a-zA-Z]+)\\/([-_.0-9a-zA-Z]+)' ,
} ,
validation : [
{
type : 'regex' ,
properties : {
regex : 'https:\\/\\/github.com\\/(?:[-_0-9a-zA-Z]+)\\/([-_.0-9a-zA-Z]+)(?:.*)' ,
errorMessage : 'Not a valid Github Repository URL' ,
} ,
} ,
] ,
} ,
{
displayName : 'By Name' ,
name : 'name' ,
type : 'string' ,
placeholder : 'e.g. n8n' ,
validation : [
{
type : 'regex' ,
properties : {
regex : '[-_.0-9a-zA-Z]+' ,
errorMessage : 'Not a valid Github Repository Name' ,
} ,
} ,
] ,
url : '=https://github.com/{{$parameter["owner"]}}/{{$value}}' ,
} ,
] ,
2019-06-23 03:35:23 -07:00
} ,
{
displayName : 'Events' ,
name : 'events' ,
type : 'multiOptions' ,
options : [
{
name : '*' ,
value : '*' ,
2022-05-06 14:01:25 -07:00
description : 'Any time any event is triggered (Wildcard Event)' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Check Run' ,
2019-06-23 03:35:23 -07:00
value : 'check_run' ,
2022-08-17 08:50:24 -07:00
description :
'Triggered when a check run is created, rerequested, completed, or has a requested_action' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Check Suite' ,
2019-06-23 03:35:23 -07:00
value : 'check_suite' ,
2022-05-06 14:01:25 -07:00
description : 'Triggered when a check suite is completed, requested, or rerequested' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Commit Comment' ,
2019-06-23 03:35:23 -07:00
value : 'commit_comment' ,
2022-05-06 14:01:25 -07:00
description : 'Triggered when a commit comment is created' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Create' ,
2019-06-23 03:35:23 -07:00
value : 'create' ,
2022-05-06 14:01:25 -07:00
description : 'Represents a created repository, branch, or tag' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Delete' ,
2019-06-23 03:35:23 -07:00
value : 'delete' ,
2022-05-06 14:01:25 -07:00
description : 'Represents a deleted branch or tag' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Deploy Key' ,
2019-06-23 03:35:23 -07:00
value : 'deploy_key' ,
2022-05-06 14:01:25 -07:00
description : 'Triggered when a deploy key is added or removed from a repository' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Deployment' ,
2019-06-23 03:35:23 -07:00
value : 'deployment' ,
2022-05-06 14:01:25 -07:00
description : 'Represents a deployment' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Deployment Status' ,
2019-06-23 03:35:23 -07:00
value : 'deployment_status' ,
2022-05-06 14:01:25 -07:00
description : 'Represents a deployment status' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Fork' ,
2019-06-23 03:35:23 -07:00
value : 'fork' ,
2022-05-06 14:01:25 -07:00
description : 'Triggered when a user forks a repository' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Github App Authorization' ,
2019-06-23 03:35:23 -07:00
value : 'github_app_authorization' ,
2022-05-06 14:01:25 -07:00
description : 'Triggered when someone revokes their authorization of a GitHub App' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Gollum' ,
2019-06-23 03:35:23 -07:00
value : 'gollum' ,
2022-05-06 14:01:25 -07:00
description : 'Triggered when a Wiki page is created or updated' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Installation' ,
2019-06-23 03:35:23 -07:00
value : 'installation' ,
2022-08-17 08:50:24 -07:00
description :
'Triggered when someone installs (created), uninstalls (deleted), or accepts new permissions (new_permissions_accepted) for a GitHub App. When a GitHub App owner requests new permissions, the person who installed the GitHub App must accept the new permissions request.' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Installation Repositories' ,
2019-06-23 03:35:23 -07:00
value : 'installation_repositories' ,
2022-05-06 14:01:25 -07:00
description : 'Triggered when a repository is added or removed from an installation' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Issue Comment' ,
2019-06-23 03:35:23 -07:00
value : 'issue_comment' ,
2022-05-06 14:01:25 -07:00
description : 'Triggered when an issue comment is created, edited, or deleted' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Issues' ,
2019-06-23 03:35:23 -07:00
value : 'issues' ,
2022-08-17 08:50:24 -07:00
description :
'Triggered when an issue is opened, edited, deleted, transferred, pinned, unpinned, closed, reopened, assigned, unassigned, labeled, unlabeled, locked, unlocked, milestoned, or demilestoned' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Label' ,
2019-06-23 03:35:23 -07:00
value : 'label' ,
2022-08-17 08:50:24 -07:00
description : "Triggered when a repository's label is created, edited, or deleted" ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Marketplace Purchase' ,
2019-06-23 03:35:23 -07:00
value : 'marketplace_purchase' ,
2022-08-17 08:50:24 -07:00
description :
'Triggered when someone purchases a GitHub Marketplace plan, cancels their plan, upgrades their plan (effective immediately), downgrades a plan that remains pending until the end of the billing cycle, or cancels a pending plan change' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Member' ,
2019-06-23 03:35:23 -07:00
value : 'member' ,
2022-08-17 08:50:24 -07:00
description :
'Triggered when a user accepts an invitation or is removed as a collaborator to a repository, or has their permissions changed' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Membership' ,
2019-06-23 03:35:23 -07:00
value : 'membership' ,
2022-08-17 08:50:24 -07:00
description :
'Triggered when a user is added or removed from a team. Organization hooks only.' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Meta' ,
2019-06-23 03:35:23 -07:00
value : 'meta' ,
2022-05-06 14:01:25 -07:00
description : 'Triggered when the webhook that this event is configured on is deleted' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Milestone' ,
2019-06-23 03:35:23 -07:00
value : 'milestone' ,
2022-08-17 08:50:24 -07:00
description :
'Triggered when a milestone is created, closed, opened, edited, or deleted' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Org Block' ,
2019-06-23 03:35:23 -07:00
value : 'org_block' ,
2022-08-17 08:50:24 -07:00
description :
'Triggered when an organization blocks or unblocks a user. Organization hooks only.' ,
2019-06-23 03:35:23 -07:00
} ,
2022-06-20 07:54:01 -07:00
{
name : 'Organization' ,
value : 'organization' ,
2022-08-17 08:50:24 -07:00
description :
'Triggered when an organization is deleted and renamed, and when a user is added, removed, or invited to an organization. Organization hooks only.' ,
2022-06-20 07:54:01 -07:00
} ,
2019-06-23 03:35:23 -07:00
{
2022-06-03 10:23:49 -07:00
name : 'Page Build' ,
2019-06-23 03:35:23 -07:00
value : 'page_build' ,
2022-08-17 08:50:24 -07:00
description :
'Triggered on push to a GitHub Pages enabled branch (gh-pages for project pages, master for user and organization pages)' ,
2019-06-23 03:35:23 -07:00
} ,
2022-06-20 07:54:01 -07:00
{
name : 'Project' ,
value : 'project' ,
2022-08-17 08:50:24 -07:00
description :
'Triggered when a project is created, updated, closed, reopened, or deleted' ,
2022-06-20 07:54:01 -07:00
} ,
2019-06-23 03:35:23 -07:00
{
2022-06-03 10:23:49 -07:00
name : 'Project Card' ,
2019-06-23 03:35:23 -07:00
value : 'project_card' ,
2022-08-17 08:50:24 -07:00
description :
'Triggered when a project card is created, edited, moved, converted to an issue, or deleted' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Project Column' ,
2019-06-23 03:35:23 -07:00
value : 'project_column' ,
2022-05-06 14:01:25 -07:00
description : 'Triggered when a project column is created, updated, moved, or deleted' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Public' ,
2019-06-23 03:35:23 -07:00
value : 'public' ,
2022-05-06 14:01:25 -07:00
description : 'Triggered when a private repository is open sourced' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-08-09 07:21:13 -07:00
name : 'Pull Request' ,
2019-06-23 03:35:23 -07:00
value : 'pull_request' ,
2022-08-17 08:50:24 -07:00
description :
'Triggered when a pull request is assigned, unassigned, labeled, unlabeled, opened, edited, closed, reopened, synchronize, ready_for_review, locked, unlocked, a pull request review is requested, or a review request is removed' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Pull Request Review' ,
2019-06-23 03:35:23 -07:00
value : 'pull_request_review' ,
2022-08-17 08:50:24 -07:00
description :
'Triggered when a pull request review is submitted into a non-pending state, the body is edited, or the review is dismissed' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Pull Request Review Comment' ,
2019-06-23 03:35:23 -07:00
value : 'pull_request_review_comment' ,
2022-08-17 08:50:24 -07:00
description :
"Triggered when a comment on a pull request's unified diff is created, edited, or deleted (in the Files Changed tab)" ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Push' ,
2019-06-23 03:35:23 -07:00
value : 'push' ,
2022-08-17 08:50:24 -07:00
description :
'Triggered on a push to a repository branch. Branch pushes and repository tag pushes also trigger webhook push events. This is the default event.' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Release' ,
2019-06-23 03:35:23 -07:00
value : 'release' ,
2022-08-17 08:50:24 -07:00
description :
'Triggered when a release is published, unpublished, created, edited, deleted, or prereleased' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Repository' ,
2019-06-23 03:35:23 -07:00
value : 'repository' ,
2022-08-17 08:50:24 -07:00
description :
'Triggered when a repository is created, archived, unarchived, renamed, edited, transferred, made public, or made private. Organization hooks are also triggered when a repository is deleted.' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Repository Import' ,
2019-06-23 03:35:23 -07:00
value : 'repository_import' ,
2022-08-17 08:50:24 -07:00
description :
'Triggered when a successful, cancelled, or failed repository import finishes for a GitHub organization or a personal repository' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Repository Vulnerability Alert' ,
2019-06-23 03:35:23 -07:00
value : 'repository_vulnerability_alert' ,
2022-05-06 14:01:25 -07:00
description : 'Triggered when a security alert is created, dismissed, or resolved' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Security Advisory' ,
2019-06-23 03:35:23 -07:00
value : 'security_advisory' ,
2022-08-17 08:50:24 -07:00
description :
'Triggered when a new security advisory is published, updated, or withdrawn' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Star' ,
2019-06-23 03:35:23 -07:00
value : 'star' ,
2022-05-06 14:01:25 -07:00
description : 'Triggered when a star is added or removed from a repository' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Status' ,
2019-06-23 03:35:23 -07:00
value : 'status' ,
2022-05-06 14:01:25 -07:00
description : 'Triggered when the status of a Git commit changes' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Team' ,
2019-06-23 03:35:23 -07:00
value : 'team' ,
2022-08-17 08:50:24 -07:00
description :
"Triggered when an organization's team is created, deleted, edited, added_to_repository, or removed_from_repository. Organization hooks only." ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Team Add' ,
2019-06-23 03:35:23 -07:00
value : 'team_add' ,
2022-05-06 14:01:25 -07:00
description : 'Triggered when a repository is added to a team' ,
2019-06-23 03:35:23 -07:00
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Watch' ,
2019-06-23 03:35:23 -07:00
value : 'watch' ,
2022-05-06 14:01:25 -07:00
description : 'Triggered when someone stars a repository' ,
2019-06-23 03:35:23 -07:00
} ,
] ,
required : true ,
default : [ ] ,
2022-05-06 14:01:25 -07:00
description : 'The events to listen to' ,
2019-06-23 03:35:23 -07:00
} ,
2024-01-09 02:34:05 -08:00
{
displayName : 'Options' ,
name : 'options' ,
type : 'collection' ,
placeholder : 'Add Option' ,
default : { } ,
options : [
{
displayName : 'Insecure SSL' ,
name : 'insecureSSL' ,
type : 'boolean' ,
default : false ,
description :
'Whether the SSL certificate of the n8n host be verified by GitHub when delivering payloads' ,
} ,
] ,
} ,
2019-06-23 03:35:23 -07:00
] ,
} ;
webhookMethods = {
default : {
async checkExists ( this : IHookFunctions ) : Promise < boolean > {
const webhookData = this . getWorkflowStaticData ( 'node' ) ;
if ( webhookData . webhookId === undefined ) {
// No webhook id is set so no webhook can exist
return false ;
}
// Webhook got created before so check if it still exists
2023-02-15 04:12:28 -08:00
const owner = this . getNodeParameter ( 'owner' , '' , { extractValue : true } ) as string ;
const repository = this . getNodeParameter ( 'repository' , '' , {
extractValue : true ,
} ) as string ;
2019-06-23 03:35:23 -07:00
const endpoint = ` /repos/ ${ owner } / ${ repository } /hooks/ ${ webhookData . webhookId } ` ;
try {
await githubApiRequest . call ( this , 'GET' , endpoint , { } ) ;
2021-04-16 09:33:36 -07:00
} catch ( error ) {
2022-10-07 10:10:28 -07:00
if ( error . cause . httpCode === '404' ) {
2019-06-23 03:35:23 -07:00
// Webhook does not exist
delete webhookData . webhookId ;
delete webhookData . webhookEvents ;
return false ;
}
2024-01-09 02:34:05 -08:00
// Some error occurred
2021-04-16 09:33:36 -07:00
throw error ;
2019-06-23 03:35:23 -07:00
}
// If it did not error then the webhook exists
return true ;
} ,
async create ( this : IHookFunctions ) : Promise < boolean > {
2019-12-23 05:29:28 -08:00
const webhookUrl = this . getNodeWebhookUrl ( 'default' ) as string ;
if ( webhookUrl . includes ( '//localhost' ) ) {
2022-08-17 08:50:24 -07:00
throw new NodeOperationError (
this . getNode ( ) ,
'The Webhook can not work on "localhost". Please, either setup n8n on a custom domain or start with "--tunnel"!' ,
) ;
2019-12-23 05:29:28 -08:00
}
2019-06-23 03:35:23 -07:00
2023-02-15 04:12:28 -08:00
const owner = this . getNodeParameter ( 'owner' , '' , { extractValue : true } ) as string ;
const repository = this . getNodeParameter ( 'repository' , '' , {
extractValue : true ,
} ) as string ;
2019-06-23 03:35:23 -07:00
const events = this . getNodeParameter ( 'events' , [ ] ) ;
const endpoint = ` /repos/ ${ owner } / ${ repository } /hooks ` ;
2024-01-09 02:34:05 -08:00
const options = this . getNodeParameter ( 'options' ) as { insecureSSL : boolean } ;
2019-06-23 03:35:23 -07:00
const body = {
name : 'web' ,
config : {
url : webhookUrl ,
content_type : 'json' ,
2024-01-09 02:34:05 -08:00
insecure_ssl : options.insecureSSL ? '1' : '0' ,
2019-06-23 03:35:23 -07:00
} ,
events ,
active : true ,
} ;
2019-12-14 12:44:56 -08:00
const webhookData = this . getWorkflowStaticData ( 'node' ) ;
2019-08-08 09:43:35 -07:00
let responseData ;
try {
responseData = await githubApiRequest . call ( this , 'POST' , endpoint , body ) ;
2021-04-16 09:33:36 -07:00
} catch ( error ) {
2022-10-07 10:10:28 -07:00
if ( error . cause . httpCode === '422' ) {
2019-12-14 12:44:56 -08:00
// Webhook exists already
// Get the data of the already registered webhook
responseData = await githubApiRequest . call ( this , 'GET' , endpoint , body ) ;
for ( const webhook of responseData as IDataObject [ ] ) {
2022-12-02 12:54:28 -08:00
if ( ( webhook . config ! as IDataObject ) . url ! === webhookUrl ) {
2019-12-14 12:44:56 -08:00
// Webhook got found
if ( JSON . stringify ( webhook . events ) === JSON . stringify ( events ) ) {
// Webhook with same events exists already so no need to
// create it again simply save the webhook-id
webhookData . webhookId = webhook . id as string ;
webhookData . webhookEvents = webhook . events as string [ ] ;
return true ;
}
}
}
2022-08-17 08:50:24 -07:00
throw new NodeOperationError (
this . getNode ( ) ,
'A webhook with the identical URL probably exists already. Please delete it manually on Github!' ,
2023-12-07 07:57:02 -08:00
{ level : 'warning' } ,
2022-08-17 08:50:24 -07:00
) ;
2019-08-08 09:43:35 -07:00
}
2023-03-08 08:04:45 -08:00
if ( error . cause . httpCode === '404' ) {
throw new NodeOperationError (
this . getNode ( ) ,
'Check that the repository exists and that you have permission to create the webhooks this node requires' ,
2023-12-07 07:57:02 -08:00
{ level : 'warning' } ,
2023-03-08 08:04:45 -08:00
) ;
}
2021-04-16 09:33:36 -07:00
throw error ;
2019-08-08 09:43:35 -07:00
}
2019-06-23 03:35:23 -07:00
if ( responseData . id === undefined || responseData . active !== true ) {
// Required data is missing so was not successful
2023-02-27 19:39:43 -08:00
throw new NodeApiError ( this . getNode ( ) , responseData as JsonObject , {
2022-08-17 08:50:24 -07:00
message : 'Github webhook creation response did not contain the expected data.' ,
} ) ;
2019-06-23 03:35:23 -07:00
}
webhookData . webhookId = responseData . id as string ;
webhookData . webhookEvents = responseData . events as string [ ] ;
return true ;
} ,
async delete ( this : IHookFunctions ) : Promise < boolean > {
const webhookData = this . getWorkflowStaticData ( 'node' ) ;
if ( webhookData . webhookId !== undefined ) {
2023-02-15 04:12:28 -08:00
const owner = this . getNodeParameter ( 'owner' , '' , { extractValue : true } ) as string ;
const repository = this . getNodeParameter ( 'repository' , '' , {
extractValue : true ,
} ) as string ;
2019-06-23 03:35:23 -07:00
const endpoint = ` /repos/ ${ owner } / ${ repository } /hooks/ ${ webhookData . webhookId } ` ;
const body = { } ;
try {
await githubApiRequest . call ( this , 'DELETE' , endpoint , body ) ;
2021-04-16 09:33:36 -07:00
} catch ( error ) {
2019-06-23 03:35:23 -07:00
return false ;
}
// Remove from the static workflow data so that it is clear
2023-03-03 09:49:19 -08:00
// that no webhooks are registered anymore
2019-06-23 03:35:23 -07:00
delete webhookData . webhookId ;
delete webhookData . webhookEvents ;
}
return true ;
} ,
} ,
} ;
2023-02-15 04:12:28 -08:00
methods = {
listSearch : {
getUsers ,
getRepositories ,
} ,
} ;
2019-10-11 04:02:44 -07:00
async webhook ( this : IWebhookFunctions ) : Promise < IWebhookResponseData > {
2019-06-23 03:35:23 -07:00
const bodyData = this . getBodyData ( ) ;
// Check if the webhook is only the ping from Github to confirm if it workshook_id
if ( bodyData . hook_id !== undefined && bodyData . action === undefined ) {
// Is only the ping and not an actual webhook call. So return 'OK'
// but do not start the workflow.
return {
2020-10-22 06:46:03 -07:00
webhookResponse : 'OK' ,
2019-06-23 03:35:23 -07:00
} ;
}
2022-09-02 07:13:17 -07:00
// Is a regular webhook call
2019-06-23 03:35:23 -07:00
// TODO: Add headers & requestPath
const returnData : IDataObject [ ] = [ ] ;
2022-08-17 08:50:24 -07:00
returnData . push ( {
body : bodyData ,
headers : this.getHeaderData ( ) ,
query : this.getQueryData ( ) ,
} ) ;
2019-06-23 03:35:23 -07:00
return {
2022-08-17 08:50:24 -07:00
workflowData : [ this . helpers . returnJsonArray ( returnData ) ] ,
2019-06-23 03:35:23 -07:00
} ;
}
}