2022-08-17 08:50:24 -07:00
import { IHookFunctions , IWebhookFunctions } from 'n8n-core' ;
2019-06-23 03:35:23 -07:00
import {
IDataObject ,
INodeType ,
2020-10-01 05:01:39 -07:00
INodeTypeDescription ,
2019-10-11 04:02:44 -07:00
IWebhookResponseData ,
2021-04-16 09:33:36 -07:00
NodeApiError ,
NodeOperationError ,
2019-06-23 03:35:23 -07:00
} from 'n8n-workflow' ;
2022-08-17 08:50:24 -07:00
import { githubApiRequest } from './GenericFunctions' ;
2019-06-23 03:35:23 -07:00
export class GithubTrigger implements INodeType {
description : INodeTypeDescription = {
displayName : 'Github Trigger' ,
name : 'githubTrigger' ,
2021-01-19 23:33:17 -08:00
icon : 'file:github.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 : [
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' ,
type : 'string' ,
default : '' ,
required : true ,
placeholder : 'n8n-io' ,
2022-05-06 14:01:25 -07:00
description : 'Owner of the repsitory' ,
2019-06-23 03:35:23 -07:00
} ,
{
displayName : 'Repository Name' ,
name : 'repository' ,
type : 'string' ,
default : '' ,
required : true ,
placeholder : 'n8n' ,
2022-05-06 14:01:25 -07:00
description : 'The name of the repsitory' ,
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 : 'Content Reference' ,
2019-06-23 03:35:23 -07:00
value : 'content_reference' ,
2022-08-17 08:50:24 -07:00
description :
'Triggered when the body or comment of an issue or pull request includes a URL that matches a configured content reference domain. Only GitHub Apps can receive this event.' ,
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
} ,
] ,
} ;
// @ts-ignore (because of request)
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
const owner = this . getNodeParameter ( 'owner' ) as string ;
const repository = this . getNodeParameter ( 'repository' ) as string ;
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 ) {
2021-04-30 19:12:11 -07:00
if ( error . httpCode === '404' ) {
2019-06-23 03:35:23 -07:00
// Webhook does not exist
delete webhookData . webhookId ;
delete webhookData . webhookEvents ;
return false ;
}
// Some error occured
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
const owner = this . getNodeParameter ( 'owner' ) as string ;
const repository = this . getNodeParameter ( 'repository' ) as string ;
const events = this . getNodeParameter ( 'events' , [ ] ) ;
const endpoint = ` /repos/ ${ owner } / ${ repository } /hooks ` ;
const body = {
name : 'web' ,
config : {
url : webhookUrl ,
content_type : 'json' ,
// secret: '...later...',
insecure_ssl : '1' , // '0' -> not allow inscure ssl | '1' -> allow insercure SSL
} ,
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 ) {
2021-04-30 19:12:11 -07:00
if ( error . 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 [ ] ) {
if ( ( webhook ! . config ! as IDataObject ) . url ! === webhookUrl ) {
// 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!' ,
) ;
2019-08-08 09:43:35 -07: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
2022-08-17 08:50:24 -07:00
throw new NodeApiError ( this . getNode ( ) , responseData , {
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 ) {
const owner = this . getNodeParameter ( 'owner' ) as string ;
const repository = this . getNodeParameter ( 'repository' ) as string ;
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
// that no webhooks are registred anymore
delete webhookData . webhookId ;
delete webhookData . webhookEvents ;
}
return true ;
} ,
} ,
} ;
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
} ;
}
// Is a regular webhoook call
// 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
} ;
}
}