From 3e0887e06941144a95e795181c23b821cebaeed0 Mon Sep 17 00:00:00 2001 From: michelle0927 Date: Wed, 3 Aug 2022 16:06:29 -0400 Subject: [PATCH 1/4] new-card-in-column historical events --- components/github/github.app.mjs | 37 ++++++++++++++++++- .../github/sources/common/common-webhook.mjs | 10 ++++- .../new-card-in-column/new-card-in-column.mjs | 36 ++++++++++++------ 3 files changed, 70 insertions(+), 13 deletions(-) diff --git a/components/github/github.app.mjs b/components/github/github.app.mjs index a1bf03dd629db..7fa590da76e4f 100644 --- a/components/github/github.app.mjs +++ b/components/github/github.app.mjs @@ -83,6 +83,17 @@ export default { })); }, }, + branch: { + label: "Branch", + description: "Branch to monitor for new commits", + type: "string", + async options({ repoFullname }) { + const branches = await this.getBranches({ + repoFullname, + }); + return branches.map((branch) => branch.name); + }, + }, pullNumber: { type: "integer", label: "PR Number", @@ -136,7 +147,7 @@ export default { async removeWebhook({ repoFullname, webhookId, }) { - return this._client().request(`DELETE /webhooks/${repoFullname}/hooks/${webhookId}`, {}); + return this._client().request(`DELETE /repos/${repoFullname}/hooks/${webhookId}`, {}); }, async getOrganizations() { const response = await this._client().request("GET /user/orgs", {}); @@ -273,5 +284,29 @@ export default { return response.data; }, + async getCommits({ + repoFullname, data, + }) { + const { data: commits } = await this._client().request(`GET /repos/${repoFullname}/commits`, { + ...data, + }); + return commits; + }, + async getBranches({ + repoFullname, data, + }) { + const { data: branches } = await this._client().request(`GET /repos/${repoFullname}/branches`, { + ...data, + }); + return branches; + }, + async getProjectCards({ + columnId, ...data + }) { + const { data: cards } = await this._client().request(`GET /projects/columns/${columnId}/cards`, { + ...data, + }); + return cards; + }, }, }; diff --git a/components/github/sources/common/common-webhook.mjs b/components/github/sources/common/common-webhook.mjs index 1dcb66beb8990..261582dc7b1bf 100644 --- a/components/github/sources/common/common-webhook.mjs +++ b/components/github/sources/common/common-webhook.mjs @@ -27,6 +27,15 @@ export default { }, }, hooks: { + async deploy() { + const events = await this.loadHistoricalEvents(); + if (!events) { + return; + } + for (const event of events) { + this.$emit(event, this.generateMeta(event)); + } + }, async activate() { const response = await this.github.createWebhook({ repoFullname: this.repoFullname, @@ -39,7 +48,6 @@ export default { events: this.getWebhookEvents(), }, }); - this._setWebhookId(response.id); }, async deactivate() { diff --git a/components/github/sources/new-card-in-column/new-card-in-column.mjs b/components/github/sources/new-card-in-column/new-card-in-column.mjs index 5034a7a62db61..58beeece601df 100644 --- a/components/github/sources/new-card-in-column/new-card-in-column.mjs +++ b/components/github/sources/new-card-in-column/new-card-in-column.mjs @@ -5,7 +5,7 @@ export default { key: "github-new-card-in-column", name: "New Card in Column (Instant)", description: "Emit new event when a project card is created or moved to a specific column", - version: "0.0.1", + version: "0.0.2", type: "source", props: { ...common.props, @@ -59,24 +59,38 @@ export default { ts: Date.parse(card.updated_at), }; }, + async loadHistoricalEvents() { + const cards = await this.github.getProjectCards({ + columnId: this.getThisColumnValue(), + per_page: 25, + }); + for (const card of cards) { + await this.processCard(card); + } + }, + async processCard(card) { + const meta = this.generateMeta(card); + const issue = await this.github.getIssueFromProjectCard({ + repoFullName: this.repoFullname, + cardId: card.id, + }); + this.$emit({ + card, + issue, + }, meta); + }, }, async run(event) { const card = event.body.project_card; + if (!card) { + return; + } if (!this.isCardInThisColumn(card)) { console.log(`Card not in ${this.getThisColumnLabel()}. Skipping...`); return; } - const meta = this.generateMeta(card); - const issue = await this.github.getIssueFromProjectCard({ - repoFullName: this.repoFullname, - cardId: card.id, - }); - - this.$emit({ - card, - issue, - }, meta); + await this.processCard(card); }, }; From c0f840aa08b28d806e007f5892d399da7f657e7c Mon Sep 17 00:00:00 2001 From: michelle0927 Date: Wed, 3 Aug 2022 17:13:33 -0400 Subject: [PATCH 2/4] webhook-event updates --- .../github/sources/common/common-webhook.mjs | 6 ++++++ .../github/sources/webhook-event/webhook-event.mjs | 14 ++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/components/github/sources/common/common-webhook.mjs b/components/github/sources/common/common-webhook.mjs index 261582dc7b1bf..d37d30c913706 100644 --- a/components/github/sources/common/common-webhook.mjs +++ b/components/github/sources/common/common-webhook.mjs @@ -25,6 +25,12 @@ export default { getWebhookEvents() { throw new Error("getWebhookEvents is not implemented"); }, + generateMeta() { + throw new Error("generateMeta is not implemented"); + }, + loadHistoricalEvents() { + return false; + }, }, hooks: { async deploy() { diff --git a/components/github/sources/webhook-event/webhook-event.mjs b/components/github/sources/webhook-event/webhook-event.mjs index 32a3babc69f4e..8d8fde6a4c072 100644 --- a/components/github/sources/webhook-event/webhook-event.mjs +++ b/components/github/sources/webhook-event/webhook-event.mjs @@ -3,16 +3,16 @@ import constants from "../common/constants.mjs"; export default { ...common, - key: "github-weebhook-vents", + key: "github-weebhook-events", name: "New Webhook Event (Instant)", - description: "Emit new event for each selected event types", + description: "Emit new event for each selected event type", type: "source", - version: "0.0.1", + version: "0.0.2", props: { ...common.props, events: { label: "Webhook Events", - description: "The event will be emited", + description: "The event types to be emited", type: "string[]", options: constants.REPOSITORY_WEBHOOK_EVENTS, }, @@ -30,6 +30,12 @@ export default { body, } = event; + // skip initial response from Github + if (body?.zen) { + console.log(body.zen); + return; + } + this.$emit(body, { id: headers["x-github-delivery"], summary: `New event ${headers["x-github-hook-installation-target-id"]} of type ${headers["x-github-hook-installation-target-type"]}}`, From 1b8a14ac076633510b79507f20b6ec0bc02515b0 Mon Sep 17 00:00:00 2001 From: michelle0927 Date: Thu, 4 Aug 2022 12:50:48 -0400 Subject: [PATCH 3/4] new-commit historical events --- components/github/github.app.mjs | 9 ++- .../github/sources/common/common-webhook.mjs | 10 +-- .../new-card-in-column/new-card-in-column.mjs | 4 +- .../new-commit-instant/new-commit-instant.js | 54 ------------- .../github/sources/new-commit/new-commit.mjs | 81 +++++++++++++++++++ 5 files changed, 91 insertions(+), 67 deletions(-) delete mode 100644 components/github/sources/new-commit-instant/new-commit-instant.js create mode 100644 components/github/sources/new-commit/new-commit.mjs diff --git a/components/github/github.app.mjs b/components/github/github.app.mjs index 7fa590da76e4f..d8f4dd21d8a8b 100644 --- a/components/github/github.app.mjs +++ b/components/github/github.app.mjs @@ -91,7 +91,10 @@ export default { const branches = await this.getBranches({ repoFullname, }); - return branches.map((branch) => branch.name); + return branches.map((branch) => ({ + label: branch.name, + value: branch.commit.sha, + })); }, }, pullNumber: { @@ -285,7 +288,7 @@ export default { return response.data; }, async getCommits({ - repoFullname, data, + repoFullname, ...data }) { const { data: commits } = await this._client().request(`GET /repos/${repoFullname}/commits`, { ...data, @@ -293,7 +296,7 @@ export default { return commits; }, async getBranches({ - repoFullname, data, + repoFullname, ...data }) { const { data: branches } = await this._client().request(`GET /repos/${repoFullname}/branches`, { ...data, diff --git a/components/github/sources/common/common-webhook.mjs b/components/github/sources/common/common-webhook.mjs index d37d30c913706..417018354021c 100644 --- a/components/github/sources/common/common-webhook.mjs +++ b/components/github/sources/common/common-webhook.mjs @@ -29,18 +29,12 @@ export default { throw new Error("generateMeta is not implemented"); }, loadHistoricalEvents() { - return false; + return true; }, }, hooks: { async deploy() { - const events = await this.loadHistoricalEvents(); - if (!events) { - return; - } - for (const event of events) { - this.$emit(event, this.generateMeta(event)); - } + await this.loadHistoricalEvents(); }, async activate() { const response = await this.github.createWebhook({ diff --git a/components/github/sources/new-card-in-column/new-card-in-column.mjs b/components/github/sources/new-card-in-column/new-card-in-column.mjs index 58beeece601df..15eac1aa9b06a 100644 --- a/components/github/sources/new-card-in-column/new-card-in-column.mjs +++ b/components/github/sources/new-card-in-column/new-card-in-column.mjs @@ -71,7 +71,7 @@ export default { async processCard(card) { const meta = this.generateMeta(card); const issue = await this.github.getIssueFromProjectCard({ - repoFullName: this.repoFullname, + repoFullname: this.repoFullname, cardId: card.id, }); this.$emit({ @@ -91,6 +91,6 @@ export default { return; } - await this.processCard(card); + this.processCard(card); }, }; diff --git a/components/github/sources/new-commit-instant/new-commit-instant.js b/components/github/sources/new-commit-instant/new-commit-instant.js deleted file mode 100644 index 2c02df800c31a..0000000000000 --- a/components/github/sources/new-commit-instant/new-commit-instant.js +++ /dev/null @@ -1,54 +0,0 @@ -/* eslint @typescript-eslint/no-var-requires: "off" */ -const common = require("../common-webhook.js"); - -module.exports = { - ...common, - key: "github-new-commit-instant", - name: "New Commit (Instant)", - description: "Emit new events on new commits to a repo or branch", - version: "0.0.1", - type: "source", - dedupe: "unique", - props: { - ...common.props, - branch: { - propDefinition: [ - common.props.github, - "branch", - (c) => ({ - repoFullName: c.repoFullName, - }), - ], - description: "Branch to monitor for new commits", - optional: true, - }, - }, - methods: { - getEventNames() { - return [ - "push", - ]; - }, - isEventForThisBranch(branch) { - return !this.branch || branch === this.branch; - }, - generateMeta(data) { - return { - id: data.id, - summary: data.message, - ts: Date.parse(data.timestamp), - }; - }, - emitEvent(body) { - const branch = body.ref.split("refs/heads/").pop(); - if (!this.isEventForThisBranch(branch)) { - return; - } - const { commits } = body; - for (const commit of commits) { - const meta = this.generateMeta(commit); - this.$emit(commit, meta); - } - }, - }, -}; diff --git a/components/github/sources/new-commit/new-commit.mjs b/components/github/sources/new-commit/new-commit.mjs new file mode 100644 index 0000000000000..3edc85a49caec --- /dev/null +++ b/components/github/sources/new-commit/new-commit.mjs @@ -0,0 +1,81 @@ +import common from "../common/common-webhook.mjs"; + +export default { + ...common, + key: "github-new-commit", + name: "New Commit (Instant)", + description: "Emit new events on new commits to a repo or branch", + version: "0.0.2", + type: "source", + dedupe: "unique", + props: { + ...common.props, + branch: { + propDefinition: [ + common.props.github, + "branch", + (c) => ({ + repoFullname: c.repoFullname, + }), + ], + description: "Branch to monitor for new commits. Defaults to master", + optional: true, + withLabel: true, + }, + }, + methods: { + ...common.methods, + getWebhookEvents() { + return [ + "push", + ]; + }, + isEventForThisBranch(branch) { + return !this.branch || branch === this.branch.label; + }, + generateMeta(data) { + return { + id: data.id, + summary: data.message, + ts: Date.parse(data.timestamp), + }; + }, + async loadHistoricalEvents() { + const commitInfo = await this.github.getCommits({ + repoFullname: this.repoFullname, + sha: this.branch + ? this.branch.value + : undefined, + per_page: 25, + }); + const commits = commitInfo.map((info) => ({ + id: info.commit.url.split("/").pop(), + timestamp: info.commit.committer.date, + ...info.commit, + })); + this.processCommits(commits); + }, + processCommits(commits) { + for (const commit of commits) { + const meta = this.generateMeta(commit); + this.$emit(commit, meta); + } + }, + }, + async run(event) { + const { body } = event; + + // skip initial response from Github + if (body?.zen) { + console.log(body.zen); + return; + } + + const branch = body.ref.split("refs/heads/").pop(); + if (!this.isEventForThisBranch(branch)) { + return; + } + + this.processCommits(body.commits); + }, +}; From 393a3ede3ec2df3b4c11c19aee137154276bae03 Mon Sep 17 00:00:00 2001 From: michelle0927 Date: Tue, 9 Aug 2022 10:57:34 -0400 Subject: [PATCH 4/4] updates per review --- components/github/readme.md | 6 ++++++ .../sources/new-card-in-column/new-card-in-column.mjs | 3 ++- components/github/sources/new-commit/new-commit.mjs | 2 +- components/github/sources/webhook-event/webhook-event.mjs | 2 +- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/components/github/readme.md b/components/github/readme.md index 08e698c0ef0cc..5fb7278c48663 100644 --- a/components/github/readme.md +++ b/components/github/readme.md @@ -249,3 +249,9 @@ If an issue _doesn't_ yet exist and you need to create one, please [use the issu You can read about our platform security and privacy [here](https://pipedream.com/docs/privacy-and-security/). If you'd like to report a suspected vulnerability or security issue, or have any questions about the security of the product, please contact our security team at **security@pipedream.com**. + +## Troubleshooting + +Note: Event Source [New Card in Column](https://github.com/PipedreamHQ/pipedream/blob/master/components/github/sources/new-card-in-column/new-card-in-column.mjs) only supports legacy (classic) projects. + +Please [reach out](https://pipedream.com/support/) to the Pipedream team with any technical issues or questions about the Github integration. We're happy to help! diff --git a/components/github/sources/new-card-in-column/new-card-in-column.mjs b/components/github/sources/new-card-in-column/new-card-in-column.mjs index 15eac1aa9b06a..9b6fda2d96eb3 100644 --- a/components/github/sources/new-card-in-column/new-card-in-column.mjs +++ b/components/github/sources/new-card-in-column/new-card-in-column.mjs @@ -5,7 +5,7 @@ export default { key: "github-new-card-in-column", name: "New Card in Column (Instant)", description: "Emit new event when a project card is created or moved to a specific column", - version: "0.0.2", + version: "0.1.0", type: "source", props: { ...common.props, @@ -83,6 +83,7 @@ export default { async run(event) { const card = event.body.project_card; if (!card) { + console.log("No card in event. Skipping event."); return; } diff --git a/components/github/sources/new-commit/new-commit.mjs b/components/github/sources/new-commit/new-commit.mjs index 3edc85a49caec..b4f438ade0671 100644 --- a/components/github/sources/new-commit/new-commit.mjs +++ b/components/github/sources/new-commit/new-commit.mjs @@ -5,7 +5,7 @@ export default { key: "github-new-commit", name: "New Commit (Instant)", description: "Emit new events on new commits to a repo or branch", - version: "0.0.2", + version: "0.1.0", type: "source", dedupe: "unique", props: { diff --git a/components/github/sources/webhook-event/webhook-event.mjs b/components/github/sources/webhook-event/webhook-event.mjs index 8d8fde6a4c072..652a00fb9b20b 100644 --- a/components/github/sources/webhook-event/webhook-event.mjs +++ b/components/github/sources/webhook-event/webhook-event.mjs @@ -3,7 +3,7 @@ import constants from "../common/constants.mjs"; export default { ...common, - key: "github-weebhook-events", + key: "github-webhook-events", name: "New Webhook Event (Instant)", description: "Emit new event for each selected event type", type: "source",