Skip to content

Commit

Permalink
new component
Browse files Browse the repository at this point in the history
  • Loading branch information
michelle0927 committed Nov 7, 2024
1 parent 43ca975 commit 1a3cf02
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 84 deletions.
46 changes: 46 additions & 0 deletions components/heroku/common/constants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const ENTITIES = [
{
value: "api:addon-attachment",
label: "addon-attachment - An add-on has been attached or removed from the app",
},
{
value: "api:addon",
label: "addon - An add-on for the app has been newly provisioned or deleted, or its details have been modified",
},
{
value: "api:app",
label: "app - The app itself has been provisioned or deleted, or its details have been modified",
},
{
value: "api:build",
label: "build - A new build for the app has been initiated or the build’s status has changed since the last notification",
},
{
value: "api:collaborator",
label: "collaborator - A collaborator has been added or removed from the app, or an existing collaborator’s details have been modified",
},
{
value: "api:domain",
label: "domain - Custom domain details have been added or removed from the app",
},
{
value: "api:dyno",
label: "dyno - A new dyno has begun running for the app",
},
{
value: "api:formation",
label: "formation - The dyno formation for a particular process type has been modified",
},
{
value: "api:release",
label: "release - A new release for the app has been initiated or the release’s status has changed since the last notification",
},
{
value: "api:sni-endpoint",
label: "sni-endpoint - An SNI endpoint has been specified or removed for the app, or the existing SNI endpoint’s details have been modified",
},
];

export default {
ENTITIES,
};
72 changes: 26 additions & 46 deletions components/heroku/heroku.app.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { axios } from "@pipedream/platform";
import constants from "./common/constants.mjs";

export default {
type: "app",
Expand All @@ -9,83 +10,62 @@ export default {
label: "App ID",
description: "The ID of the app",
async options() {
const { data } = await this.listApps();
return data.map((app) => ({
const apps = await this.listApps();
return apps?.map((app) => ({
label: app.name,
value: app.id,
}));
})) || [];
},
},
entity: {
entities: {
type: "string[]",
label: "Entity",
description: "The entity to subscribe to",
options: [
"api:addon-attachment",
"api:addon",
"api:app",
"api:build",
"api:collaborator",
"api:domain",
"api:dyno",
"api:formation",
"api:release",
"api:sni-endpoint",
],
},
eventTypes: {
type: "string[]",
label: "Event Types",
description: "The types of events to subscribe to",
options: [
"create",
"destroy",
"update",
],
label: "Entities",
description: "The entity or entities to subscribe to",
options: constants.ENTITIES,
},
},
methods: {
_baseUrl() {
return "https://api.heroku.com";
},
async _makeRequest(opts = {}) {
_makeRequest(opts = {}) {
const {
$ = this,
method = "GET",
path,
headers,
...otherOpts
} = opts;
return axios($, {
...otherOpts,
method,
url: this._baseUrl() + path,
url: `${this._baseUrl()}${path}`,
headers: {
...headers,
"Authorization": `Bearer ${this.$auth.oauth_access_token}`,
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
Accept: "application/vnd.heroku+json; version=3",
},
});
},
async listApps() {
listApps(opts = {}) {
return this._makeRequest({
path: "/apps",
...opts,
});
},
async createWebhookSubscription(appId, entity) {
createWebhookSubscription({
appId, ...opts
}) {
return this._makeRequest({
method: "POST",
path: `/apps/${appId}/webhooks`,
data: {
include: entity,
level: "notify",
secret: "my_secret",
url: "https://example.com/hooks",
authorization: this.$auth.oauth_access_token,
},
...opts,
});
},
authKeys() {
console.log(Object.keys(this.$auth));
deleteWebhookSubscription({
appId, hookId, ...opts
}) {
return this._makeRequest({
method: "DELETE",
path: `/apps/${appId}/webhooks/${hookId}`,
...opts,
});
},
},
};
18 changes: 18 additions & 0 deletions components/heroku/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@pipedream/heroku",
"version": "0.0.1",
"description": "Pipedream Heroku Components",
"main": "heroku.app.mjs",
"keywords": [
"pipedream",
"heroku"
],
"homepage": "https://pipedream.com/apps/heroku",
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.0.3"
}
}
Original file line number Diff line number Diff line change
@@ -1,70 +1,74 @@
import heroku from "../../heroku.app.mjs";
import sampleEmit from "./test-event.mjs";

export default {
key: "heroku-new-webhook-event-instant",
name: "New Webhook Event Instant",
description: "Emit new event on each webhook event. [See the documentation](https://devcenter.heroku.com/categories/platform-api)",
version: "0.0.{{ts}}",
name: "New Webhook Event (Instant)",
description: "Emit new event on each webhook event. [See the documentation](https://devcenter.heroku.com/articles/app-webhooks-schema#webhook-create)",
version: "0.0.1",
type: "source",
dedupe: "unique",
props: {
heroku,
http: {
type: "$.interface.http",
customResponse: false,
},
http: "$.interface.http",
db: "$.service.db",
appId: {
propDefinition: [
heroku,
"appId",
],
},
entity: {
propDefinition: [
heroku,
"entity",
],
},
eventTypes: {
entities: {
propDefinition: [
heroku,
"eventTypes",
"entities",
],
},
},
hooks: {
async activate() {
const webhookId = await this.heroku.createWebhookSubscription(
this.appId,
this.entity,
this.eventTypes,
);
this.db.set("webhookId", webhookId);
const { id } = await this.heroku.createWebhookSubscription({
appId: this.appId,
data: {
include: this.entities,
level: "notify",
url: this.http.endpoint,
},
});
this._setHookId(id);
},
async deactivate() {
const webhookId = this.db.get("webhookId");
if (webhookId) {
await this.heroku.deleteWebhookSubscription(webhookId);
const hookId = this._getHookId();
if (hookId) {
await this.heroku.deleteWebhookSubscription({
appId: this.appId,
hookId,
});
}
},
},
methods: {
_getHookId() {
return this.db.get("hookId");
},
_setHookId(hookId) {
this.db.set("hookId", hookId);
},
generateMeta(event) {
return {
id: event.id,
summary: `New ${event.webhook_metadata.event.include} - ${event.action} Event`,
ts: Date.now(),
};
},
},
async run(event) {
const {
body, headers,
} = event;

// Validate the incoming webhook payload
if (headers["heroku-webhook-hmac-sha256"] !== this.heroku.$auth.webhook_secret) {
console.log("Invalid signature");
const { body } = event;
if (!body) {
return;
}

// Emit the event
this.$emit(body, {
id: body.id,
summary: `New event: ${body.event_type}`,
ts: Date.parse(body.created_at),
});
const meta = this.generateMeta(body);
this.$emit(body, meta);
},
sampleEmit,
};
69 changes: 69 additions & 0 deletions components/heroku/sources/new-webhook-event-instant/test-event.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
export default {
"id": "a5b27512-1f14-4f45-bc26-fe6bf03686fe",
"data": {
"id": "9a4eaeed-24cc-4b23-a8ca-7867b251eaf3",
"acm": false,
"name": "",
"team": null,
"owner": {
"id": "7b25912d-b147-48d6-b03d-f0fc72be381b",
"email": ""
},
"space": null,
"stack": {
"id": "74cfe988-7527-4ca9-9667-77bb9f3029cf",
"name": "heroku-24"
},
"region": {
"id": "59accabd-516d-4f0e-83e6-6e3757701145",
"name": "us"
},
"git_url": "",
"web_url": "",
"repo_size": null,
"slug_size": null,
"created_at": "2024-11-07T16:38:38Z",
"updated_at": "2024-11-07T17:14:52Z",
"archived_at": null,
"build_stack": {
"id": "74cfe988-7527-4ca9-9667-77bb9f3029cf",
"name": "heroku-24"
},
"maintenance": false,
"released_at": "2024-11-07T16:38:39Z",
"organization": null,
"internal_routing": null,
"buildpack_provided_description": null
},
"actor": {
"id": "7b25912d-b147-48d6-b03d-f0fc72be381b",
"email": ""
},
"action": "update",
"version": "application/vnd.heroku+json; version=3",
"resource": "app",
"sequence": null,
"created_at": "2024-11-07T17:14:52.475272Z",
"updated_at": "2024-11-07T17:14:52.475276Z",
"published_at": "2024-11-07T17:14:52Z",
"previous_data": {
"name": "",
"git_url": "",
"updated_at": "2024-11-07T17:13:42Z"
},
"webhook_metadata": {
"attempt": {
"id": "16e90948-01d8-4ba0-97de-5868a4aed0bf"
},
"delivery": {
"id": "bbf72057-15f2-40b0-87c7-99ccf95e1666"
},
"event": {
"id": "a5b27512-1f14-4f45-bc26-fe6bf03686fe",
"include": "api:app"
},
"webhook": {
"id": "c385bb39-42ab-49c6-9255-108ff4cabdbd"
}
}
}

0 comments on commit 1a3cf02

Please sign in to comment.