File tree 2 files changed +152
-2
lines changed
sources/new-webhook-event-instant
2 files changed +152
-2
lines changed Original file line number Diff line number Diff line change
1
+ import { axios } from "@pipedream/platform" ;
2
+
1
3
export default {
2
4
type : "app" ,
3
5
app : "heroku" ,
4
- propDefinitions : { } ,
6
+ propDefinitions : {
7
+ appId : {
8
+ type : "string" ,
9
+ label : "App ID" ,
10
+ description : "The ID of the app" ,
11
+ async options ( ) {
12
+ const { data } = await this . listApps ( ) ;
13
+ return data . map ( ( app ) => ( {
14
+ label : app . name ,
15
+ value : app . id ,
16
+ } ) ) ;
17
+ } ,
18
+ } ,
19
+ entity : {
20
+ type : "string[]" ,
21
+ label : "Entity" ,
22
+ description : "The entity to subscribe to" ,
23
+ options : [
24
+ "api:addon-attachment" ,
25
+ "api:addon" ,
26
+ "api:app" ,
27
+ "api:build" ,
28
+ "api:collaborator" ,
29
+ "api:domain" ,
30
+ "api:dyno" ,
31
+ "api:formation" ,
32
+ "api:release" ,
33
+ "api:sni-endpoint" ,
34
+ ] ,
35
+ } ,
36
+ eventTypes : {
37
+ type : "string[]" ,
38
+ label : "Event Types" ,
39
+ description : "The types of events to subscribe to" ,
40
+ options : [
41
+ "create" ,
42
+ "destroy" ,
43
+ "update" ,
44
+ ] ,
45
+ } ,
46
+ } ,
5
47
methods : {
6
- // this.$auth contains connected account data
48
+ _baseUrl ( ) {
49
+ return "https://api.heroku.com" ;
50
+ } ,
51
+ async _makeRequest ( opts = { } ) {
52
+ const {
53
+ $ = this ,
54
+ method = "GET" ,
55
+ path,
56
+ headers,
57
+ ...otherOpts
58
+ } = opts ;
59
+ return axios ( $ , {
60
+ ...otherOpts ,
61
+ method,
62
+ url : this . _baseUrl ( ) + path ,
63
+ headers : {
64
+ ...headers ,
65
+ "Authorization" : `Bearer ${ this . $auth . oauth_access_token } ` ,
66
+ } ,
67
+ } ) ;
68
+ } ,
69
+ async listApps ( ) {
70
+ return this . _makeRequest ( {
71
+ path : "/apps" ,
72
+ } ) ;
73
+ } ,
74
+ async createWebhookSubscription ( appId , entity ) {
75
+ return this . _makeRequest ( {
76
+ method : "POST" ,
77
+ path : `/apps/${ appId } /webhooks` ,
78
+ data : {
79
+ include : entity ,
80
+ level : "notify" ,
81
+ secret : "my_secret" ,
82
+ url : "https://example.com/hooks" ,
83
+ authorization : this . $auth . oauth_access_token ,
84
+ } ,
85
+ } ) ;
86
+ } ,
7
87
authKeys ( ) {
8
88
console . log ( Object . keys ( this . $auth ) ) ;
9
89
} ,
Original file line number Diff line number Diff line change
1
+ import heroku from "../../heroku.app.mjs" ;
2
+
3
+ export default {
4
+ key : "heroku-new-webhook-event-instant" ,
5
+ name : "New Webhook Event Instant" ,
6
+ description : "Emit new event on each webhook event. [See the documentation](https://devcenter.heroku.com/categories/platform-api)" ,
7
+ version : "0.0.{{ts}}" ,
8
+ type : "source" ,
9
+ dedupe : "unique" ,
10
+ props : {
11
+ heroku,
12
+ http : {
13
+ type : "$.interface.http" ,
14
+ customResponse : false ,
15
+ } ,
16
+ db : "$.service.db" ,
17
+ appId : {
18
+ propDefinition : [
19
+ heroku ,
20
+ "appId" ,
21
+ ] ,
22
+ } ,
23
+ entity : {
24
+ propDefinition : [
25
+ heroku ,
26
+ "entity" ,
27
+ ] ,
28
+ } ,
29
+ eventTypes : {
30
+ propDefinition : [
31
+ heroku ,
32
+ "eventTypes" ,
33
+ ] ,
34
+ } ,
35
+ } ,
36
+ hooks : {
37
+ async activate ( ) {
38
+ const webhookId = await this . heroku . createWebhookSubscription (
39
+ this . appId ,
40
+ this . entity ,
41
+ this . eventTypes ,
42
+ ) ;
43
+ this . db . set ( "webhookId" , webhookId ) ;
44
+ } ,
45
+ async deactivate ( ) {
46
+ const webhookId = this . db . get ( "webhookId" ) ;
47
+ if ( webhookId ) {
48
+ await this . heroku . deleteWebhookSubscription ( webhookId ) ;
49
+ }
50
+ } ,
51
+ } ,
52
+ async run ( event ) {
53
+ const {
54
+ body, headers,
55
+ } = event ;
56
+
57
+ // Validate the incoming webhook payload
58
+ if ( headers [ "heroku-webhook-hmac-sha256" ] !== this . heroku . $auth . webhook_secret ) {
59
+ console . log ( "Invalid signature" ) ;
60
+ return ;
61
+ }
62
+
63
+ // Emit the event
64
+ this . $emit ( body , {
65
+ id : body . id ,
66
+ summary : `New event: ${ body . event_type } ` ,
67
+ ts : Date . parse ( body . created_at ) ,
68
+ } ) ;
69
+ } ,
70
+ } ;
You can’t perform that action at this time.
0 commit comments