From 3cba8fc3fa5f1e4dcc48e3601cfe4b5b400eced6 Mon Sep 17 00:00:00 2001 From: Luan Cazarine Date: Thu, 18 Jul 2024 15:17:29 -0300 Subject: [PATCH] lambdatest init --- components/lambdatest/lambdatest.app.mjs | 96 +++++++++++++++- components/lambdatest/package.json | 2 +- .../marked-new-issue/marked-new-issue.mjs | 103 ++++++++++++++++++ .../new-build-test-instant.mjs | 69 ++++++++++++ 4 files changed, 267 insertions(+), 3 deletions(-) create mode 100644 components/lambdatest/sources/marked-new-issue/marked-new-issue.mjs create mode 100644 components/lambdatest/sources/new-build-test-instant/new-build-test-instant.mjs diff --git a/components/lambdatest/lambdatest.app.mjs b/components/lambdatest/lambdatest.app.mjs index b1ab5cab7c0d2..cdd36d190c5f1 100644 --- a/components/lambdatest/lambdatest.app.mjs +++ b/components/lambdatest/lambdatest.app.mjs @@ -1,11 +1,103 @@ +import { axios } from "@pipedream/platform"; + export default { type: "app", app: "lambdatest", - propDefinitions: {}, + propDefinitions: { + priorityLevel: { + type: "string", + label: "Priority Level", + description: "The priority level of the issue", + options: [ + "Low", + "Medium", + "High", + ], + }, + status: { + type: "string", + label: "Status", + description: "The status of the issue", + options: [ + "New", + "In Progress", + "Resolved", + "Closed", + ], + }, + assignee: { + type: "string", + label: "Assignee", + description: "The assignee of the issue", + async options() { + const users = await this.getUsers(); + return users.map((user) => ({ + label: user.name, + value: user.id, + })); + }, + }, + filters: { + type: "string", + label: "Filters", + description: "Optional filters for the issue", + optional: true, + }, + labels: { + type: "string", + label: "Labels", + description: "Optional labels for the issue", + optional: true, + }, + }, methods: { // this.$auth contains connected account data authKeys() { console.log(Object.keys(this.$auth)); }, + _baseUrl() { + return "https://api.lambdatest.com"; + }, + async _makeRequest(opts = {}) { + const { + $ = this, method = "GET", path = "/", headers, ...otherOpts + } = opts; + return axios($, { + ...otherOpts, + method, + url: this._baseUrl() + path, + headers: { + ...headers, + Authorization: `Bearer ${this.$auth.api_key}`, + }, + }); + }, + async getUsers(opts = {}) { + return this._makeRequest({ + path: "/users", + ...opts, + }); + }, + async emitBuildOrTestEvent(opts = {}) { + return this._makeRequest({ + path: "/builds-or-tests", + ...opts, + }); + }, + async emitNewIssueEvent({ + priorityLevel, status, assignee, filters, labels, + }) { + return this._makeRequest({ + path: "/issues", + method: "POST", + data: { + priority_level: priorityLevel, + status, + assignee, + filters, + labels, + }, + }); + }, }, -}; \ No newline at end of file +}; diff --git a/components/lambdatest/package.json b/components/lambdatest/package.json index 261c5532fa72a..c5e4e81b7fbca 100644 --- a/components/lambdatest/package.json +++ b/components/lambdatest/package.json @@ -12,4 +12,4 @@ "publishConfig": { "access": "public" } -} \ No newline at end of file +} diff --git a/components/lambdatest/sources/marked-new-issue/marked-new-issue.mjs b/components/lambdatest/sources/marked-new-issue/marked-new-issue.mjs new file mode 100644 index 0000000000000..ab2dac8c7a839 --- /dev/null +++ b/components/lambdatest/sources/marked-new-issue/marked-new-issue.mjs @@ -0,0 +1,103 @@ +import { axios } from "@pipedream/platform"; +import lambdatest from "../../lambdatest.app.mjs"; + +export default { + key: "lambdatest-marked-new-issue", + name: "New Issue Marked in LambdaTest", + description: "Emit a new event when an issue is marked as new in LambdaTest. [See the documentation](https://www.lambdatest.com/support/api-doc/)", + version: "0.0.{{ts}}", + type: "source", + dedupe: "unique", + props: { + lambdatest, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: 60, + }, + }, + priorityLevel: { + propDefinition: [ + lambdatest, + "priorityLevel", + ], + }, + status: { + propDefinition: [ + lambdatest, + "status", + ], + }, + assignee: { + propDefinition: [ + lambdatest, + "assignee", + ], + }, + filters: { + propDefinition: [ + lambdatest, + "filters", + ], + optional: true, + }, + labels: { + propDefinition: [ + lambdatest, + "labels", + ], + optional: true, + }, + }, + hooks: { + async deploy() { + await this.run(); + }, + async activate() { + // Perform any setup necessary when the source is activated + }, + async deactivate() { + // Perform any teardown necessary when the source is deactivated + }, + }, + methods: { + async _getIssues() { + const issues = await this.lambdatest.emitNewIssueEvent({ + priorityLevel: this.priorityLevel, + status: this.status, + assignee: this.assignee, + filters: this.filters, + labels: this.labels, + }); + return issues; + }, + _getLastTimestamp() { + return this.db.get("lastTimestamp") || 0; + }, + _setLastTimestamp(timestamp) { + this.db.set("lastTimestamp", timestamp); + }, + }, + async run() { + const issues = await this._getIssues(); + const lastTimestamp = this._getLastTimestamp(); + let maxTimestamp = lastTimestamp; + + for (const issue of issues) { + const issueTimestamp = new Date(issue.created_at).getTime(); + if (issueTimestamp > lastTimestamp) { + this.$emit(issue, { + id: issue.id, + summary: `New Issue: ${issue.title}`, + ts: issueTimestamp, + }); + if (issueTimestamp > maxTimestamp) { + maxTimestamp = issueTimestamp; + } + } + } + + this._setLastTimestamp(maxTimestamp); + }, +}; diff --git a/components/lambdatest/sources/new-build-test-instant/new-build-test-instant.mjs b/components/lambdatest/sources/new-build-test-instant/new-build-test-instant.mjs new file mode 100644 index 0000000000000..a0174e9222d28 --- /dev/null +++ b/components/lambdatest/sources/new-build-test-instant/new-build-test-instant.mjs @@ -0,0 +1,69 @@ +import lambdatest from "../../lambdatest.app.mjs"; +import { axios } from "@pipedream/platform"; + +export default { + key: "lambdatest-new-build-test-instant", + name: "New Build or Test Executed", + description: "Emit new event when a build or test is executed in LambdaTest. [See the documentation](https://www.lambdatest.com/support/api-doc/)", + version: "0.0.{{ts}}", + type: "source", + dedupe: "unique", + props: { + lambdatest: { + type: "app", + app: "lambdatest", + }, + http: { + type: "$.interface.http", + customResponse: false, + }, + db: "$.service.db", + }, + hooks: { + async deploy() { + const events = await this.lambdatest.emitBuildOrTestEvent({ + paginate: true, + max: 50, + }); + for (const event of events) { + this.$emit(event, { + id: event.id, + summary: `New event: ${event.name}`, + ts: Date.parse(event.ts), + }); + } + }, + async activate() { + const webhookId = await this.lambdatest.emitBuildOrTestEvent({ + method: "POST", + }); + this._setWebhookId(webhookId); + }, + async deactivate() { + const webhookId = this._getWebhookId(); + if (webhookId) { + await this.lambdatest.emitBuildOrTestEvent({ + method: "DELETE", + data: { + id: webhookId, + }, + }); + } + }, + }, + methods: { + _getWebhookId() { + return this.db.get("webhookId"); + }, + _setWebhookId(id) { + this.db.set("webhookId", id); + }, + }, + async run(event) { + this.$emit(event.body, { + id: event.body.id, + summary: `New build/test executed: ${event.body.name}`, + ts: Date.parse(event.body.timestamp), + }); + }, +};