diff --git a/components/circleci/actions/get-job-artifacts/get-job-artifacts.mjs b/components/circleci/actions/get-job-artifacts/get-job-artifacts.mjs new file mode 100644 index 0000000000000..434cba3175dd3 --- /dev/null +++ b/components/circleci/actions/get-job-artifacts/get-job-artifacts.mjs @@ -0,0 +1,54 @@ +import circleci from "../../circleci.app.mjs"; + +export default { + key: "circleci-get-job-artifacts", + name: "Get Job Artifacts", + description: "Retrieves a job's artifacts. [See the documentation](https://circleci.com/docs/api/v2/index.html#tag/Job/operation/getJobArtifacts).", + version: "0.0.1", + type: "action", + props: { + circleci, + projectSlug: { + propDefinition: [ + circleci, + "projectSlug", + ], + }, + pipelineId: { + propDefinition: [ + circleci, + "pipelineId", + (c) => ({ + projectSlug: c.projectSlug, + }), + ], + }, + workflowId: { + propDefinition: [ + circleci, + "workflowId", + (c) => ({ + pipelineId: c.pipelineId, + }), + ], + }, + jobNumber: { + propDefinition: [ + circleci, + "jobNumber", + (c) => ({ + workflowId: c.workflowId, + }), + ], + }, + }, + async run({ $ }) { + const response = await this.circleci.getJobArtifacts({ + $, + projectSlug: this.projectSlug, + jobNumber: this.jobNumber, + }); + $.export("$summary", `Successfully retrieved artifacts for job number: ${this.jobNumber}`); + return response; + }, +}; diff --git a/components/circleci/actions/rerun-workflow/rerun-workflow.mjs b/components/circleci/actions/rerun-workflow/rerun-workflow.mjs new file mode 100644 index 0000000000000..a997d763cd744 --- /dev/null +++ b/components/circleci/actions/rerun-workflow/rerun-workflow.mjs @@ -0,0 +1,79 @@ +import circleci from "../../circleci.app.mjs"; + +export default { + key: "circleci-rerun-workflow", + name: "Rerun Workflow", + description: "Reruns the specified workflow. [See the documentation](https://circleci.com/docs/api/v2/index.html#tag/Workflow/operation/rerunWorkflow)", + version: "0.0.1", + type: "action", + props: { + circleci, + projectSlug: { + propDefinition: [ + circleci, + "projectSlug", + ], + }, + pipelineId: { + propDefinition: [ + circleci, + "pipelineId", + (c) => ({ + projectSlug: c.projectSlug, + }), + ], + }, + workflowId: { + propDefinition: [ + circleci, + "workflowId", + (c) => ({ + pipelineId: c.pipelineId, + }), + ], + }, + enableSsh: { + type: "boolean", + label: "Enable SSH", + description: "Whether to enable SSH access for the triggering user on the newly-rerun job. Requires the jobs parameter to be used and so is mutually exclusive with the from_failed parameter.", + optional: true, + }, + fromFailed: { + type: "boolean", + label: "From Failed", + description: "Whether to rerun the workflow from the failed job. Mutually exclusive with the jobs parameter.", + optional: true, + }, + jobIds: { + propDefinition: [ + circleci, + "jobIds", + (c) => ({ + workflowId: c.workflowId, + }), + ], + }, + sparseTree: { + type: "boolean", + label: "Sparse Tree", + description: "", + optional: true, + }, + }, + async run({ $ }) { + const response = await this.circleci.rerunWorkflow({ + $, + workflowId: this.workflowId, + data: { + enable_ssh: this.enableSsh, + from_failed: this.fromFailed, + jobs: typeof this.jobIds === "string" + ? JSON.parse(this.jobIds) + : this.jobIds, + sparse_tree: this.sparseTree, + }, + }); + $.export("$summary", `Successfully started a rerun of workflow with ID: ${this.workflowId}`); + return response; + }, +}; diff --git a/components/circleci/actions/trigger-pipeline/trigger-pipeline.mjs b/components/circleci/actions/trigger-pipeline/trigger-pipeline.mjs new file mode 100644 index 0000000000000..2d531baeaacfd --- /dev/null +++ b/components/circleci/actions/trigger-pipeline/trigger-pipeline.mjs @@ -0,0 +1,87 @@ +import circleci from "../../circleci.app.mjs"; + +export default { + key: "circleci-trigger-pipeline", + name: "Trigger a Pipeline", + description: "Trigger a pipeline given a pipeline definition ID. Supports all integrations except GitLab. [See the documentation](https://circleci.com/docs/api/v2/index.html#tag/Pipeline/operation/triggerPipelineRun)", + version: "0.0.1", + type: "action", + props: { + circleci, + alert: { + type: "alert", + alertType: "info", + content: "Supports all integrations except GitLab.", + }, + projectSlug: { + propDefinition: [ + circleci, + "projectSlug", + ], + }, + definitionId: { + type: "string", + label: "Definition ID", + description: "The unique ID for the pipeline definition. This can be found in the page Project Settings > Pipelines.", + }, + configBranch: { + type: "string", + label: "Config Branch", + description: "The branch that should be used to fetch the config file. Note that branch and tag are mutually exclusive. To trigger a pipeline for a PR by number use pull//head for the PR ref or pull//merge for the merge ref (GitHub only)", + optional: true, + }, + configTag: { + type: "string", + label: "Config Tag", + description: "The tag that should be used to fetch the config file. The commit that this tag points to is used for the pipeline. Note that branch and tag are mutually exclusive.", + optional: true, + }, + checkoutBranch: { + type: "string", + label: "Checkout Branch", + description: "The branch that should be used to check out code on a checkout step. Note that branch and tag are mutually exclusive. To trigger a pipeline for a PR by number use pull//head for the PR ref or pull//merge for the merge ref (GitHub only)", + optional: true, + }, + checkoutTag: { + type: "string", + label: "Checkout Tag", + description: "The tag that should be used to check out code on a checkout step. The commit that this tag points to is used for the pipeline. Note that branch and tag are mutually exclusive.", + optional: true, + }, + parameters: { + type: "object", + label: "Parameters", + description: "An object containing pipeline parameters and their values. Pipeline parameters have the following size limits: 100 max entries, 128 maximum key length, 512 maximum value length.", + optional: true, + }, + }, + async run({ $ }) { + const response = await this.circleci.triggerPipeline({ + $, + projectSlug: this.projectSlug, + data: { + definition_id: this.definitionId, + config: this.configBranch || this.configTag + ? { + branch: this.configBranch, + tag: this.configTag, + } + : undefined, + checkout: this.checkoutBranch || this.checkoutTag + ? { + branch: this.checkoutBranch, + tag: this.checkoutTag, + } + : undefined, + parameters: typeof this.parameters === "string" + ? JSON.parse(this.parameters) + : this.parameters, + }, + }); + + if (response?.id) { + $.export("$summary", `Successfully triggered pipeline with ID: ${response.id}`); + } + return response; + }, +}; diff --git a/components/circleci/circleci.app.mjs b/components/circleci/circleci.app.mjs index 83f7f387fef73..b44cefa1746ad 100644 --- a/components/circleci/circleci.app.mjs +++ b/components/circleci/circleci.app.mjs @@ -1,11 +1,211 @@ +import { axios } from "@pipedream/platform"; + export default { type: "app", app: "circleci", - propDefinitions: {}, + propDefinitions: { + pipelineId: { + type: "string", + label: "Pipeline ID", + description: "The identifier of a pipeline", + async options({ + projectSlug, prevContext, + }) { + const { + items, nextPageToken, + } = await this.listPipelines({ + projectSlug, + params: prevContext?.next + ? { + "page-token": prevContext.next, + } + : {}, + }); + return { + options: items?.map(({ id }) => id) || [], + context: { + next: nextPageToken, + }, + }; + }, + }, + workflowId: { + type: "string", + label: "Workflow ID", + description: "The identifier of a workflow", + async options({ + pipelineId, prevContext, + }) { + const { + items, nextPageToken, + } = await this.listPipelineWorkflows({ + pipelineId, + params: prevContext?.next + ? { + "page-token": prevContext.next, + } + : {}, + }); + return { + options: items?.map(({ + id: value, name: label, + }) => ({ + value, + label, + })) || [], + context: { + next: nextPageToken, + }, + }; + }, + }, + jobNumber: { + type: "string", + label: "Job Number", + description: "The job number of a job", + async options({ + workflowId, prevContext, + }) { + const { + items, nextPageToken, + } = await this.listWorkflowJobs({ + workflowId, + params: prevContext?.next + ? { + "page-token": prevContext.next, + } + : {}, + }); + return { + options: items?.map(({ + job_number: value, name: label, + }) => ({ + value, + label, + })) || [], + context: { + next: nextPageToken, + }, + }; + }, + }, + jobIds: { + type: "string[]", + label: "Jobs", + description: "A list of job IDs to rerun", + optional: true, + async options({ + workflowId, prevContext, + }) { + const { + items, nextPageToken, + } = await this.listWorkflowJobs({ + workflowId, + params: prevContext?.next + ? { + "page-token": prevContext.next, + } + : {}, + }); + return { + options: items?.map(({ + id: value, name: label, + }) => ({ + value, + label, + })) || [], + context: { + next: nextPageToken, + }, + }; + }, + }, + projectSlug: { + type: "string", + label: "Project Slug", + description: "Project slug in the form `vcs-slug/org-name/repo-name` (found in Project Settings)", + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _baseUrl() { + return "https://circleci.com/api/v2"; + }, + async _makeRequest(opts = {}) { + const { + $ = this, + path, + ...otherOpts + } = opts; + return axios($, { + url: `${this._baseUrl()}${path}`, + headers: { + "Circle-Token": this.$auth.token, + }, + ...otherOpts, + }); + }, + createWebhook(opts = {}) { + return this._makeRequest({ + method: "POST", + path: "/webhook", + ...opts, + }); + }, + deleteWebhook(webhookId) { + return this._makeRequest({ + method: "DELETE", + path: `/webhook/${webhookId}`, + }); + }, + listPipelines({ + projectSlug, ...opts + }) { + return this._makeRequest({ + path: `/project/${projectSlug}/pipeline/mine`, + ...opts, + }); + }, + listPipelineWorkflows({ + pipelineId, ...opts + }) { + return this._makeRequest({ + path: `/pipeline/${pipelineId}/workflow`, + ...opts, + }); + }, + listWorkflowJobs({ + workflowId, ...opts + }) { + return this._makeRequest({ + path: `/workflow/${workflowId}/job`, + ...opts, + }); + }, + triggerPipeline({ + projectSlug, ...opts + }) { + return this._makeRequest({ + method: "POST", + path: `/project/${projectSlug}/pipeline/run`, + ...opts, + }); + }, + getJobArtifacts({ + projectSlug, jobNumber, ...opts + }) { + return this._makeRequest({ + path: `/project/${projectSlug}/${jobNumber}/artifacts`, + ...opts, + }); + }, + rerunWorkflow({ + workflowId, ...opts + }) { + return this._makeRequest({ + method: "POST", + path: `/workflow/${workflowId}/rerun`, + ...opts, + }); }, }, }; diff --git a/components/circleci/package.json b/components/circleci/package.json new file mode 100644 index 0000000000000..7066275ff6b20 --- /dev/null +++ b/components/circleci/package.json @@ -0,0 +1,19 @@ +{ + "name": "@pipedream/circleci", + "version": "0.0.1", + "description": "Pipedream CircleCI Components", + "main": "circleci.app.mjs", + "keywords": [ + "pipedream", + "circleci" + ], + "homepage": "https://pipedream.com/apps/circleci", + "author": "Pipedream Msupport@pipedream.com> (https://pipedream.com/)", + "publishConfig": { + "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^3.0.3", + "uuid": "^11.0.3" + } +} diff --git a/components/circleci/sources/common/base.mjs b/components/circleci/sources/common/base.mjs new file mode 100644 index 0000000000000..031b792f7be4e --- /dev/null +++ b/components/circleci/sources/common/base.mjs @@ -0,0 +1,119 @@ +import circleci from "../../circleci.app.mjs"; +import { v4 as uuidv4 } from "uuid"; +import crypto from "crypto"; + +export default { + props: { + circleci, + db: "$.service.db", + http: { + type: "$.interface.http", + customResponse: true, + }, + name: { + type: "string", + label: "Webhook Name", + description: "Name of the webhook", + }, + projectId: { + type: "string", + label: "Project ID", + description: "The identifier of a project. Can be found in Project Settings -> Overview", + }, + }, + hooks: { + async activate() { + const secret = uuidv4(); + const { id } = await this.circleci.createWebhook({ + data: { + "name": this.name, + "events": this.getEvents(), + "url": this.http.endpoint, + "verify-tls": true, + "signing-secret": secret, + "scope": { + "id": this.projectId, + "type": "project", + }, + }, + }); + this._setHookId(id); + this._setSigningSecret(secret); + }, + async deactivate() { + const webhookId = this._getHookId(); + if (webhookId) { + await this.circleci.deleteWebhook(webhookId); + } + }, + }, + methods: { + _getHookId() { + return this.db.get("hookId"); + }, + _setHookId(hookId) { + this.db.set("hookId", hookId); + }, + _getSigningSecret() { + return this.db.get("signingSecret"); + }, + _setSigningSecret(secret) { + this.db.set("signingSecret", secret); + }, + verifySignature({ + headers, body, + }) { + if (!headers["circleci-signature"]) { + return false; + } + const secret = this._getSigningSecret(); + const signatureFromHeader = headers["circleci-signature"] + .split(",") + .reduce((acc, pair) => { + const [ + key, + value, + ] = pair.split("="); + acc[key] = value; + return acc; + }, {}).v1; + + const validSignature = crypto + .createHmac("sha256", secret) + .update(JSON.stringify(body), "utf8") + .digest("hex"); + + return crypto.timingSafeEqual( + Buffer.from(validSignature, "hex"), + Buffer.from(signatureFromHeader, "hex"), + ); + }, + generateMeta(event) { + return { + id: event.id, + summary: this.getSummary(event), + ts: Date.parse(event.happened_at), + }; + }, + getEvents() { + throw new Error("getEvents is not implemented"); + }, + getSummary() { + throw new Error("getSummary is not implemented"); + }, + }, + async run(event) { + this.http.respond({ + status: 200, + }); + + if (!this.verifySignature(event)) { + console.log("Invalid webhook signature"); + return; + } + + const { body } = event; + const meta = this.generateMeta(body); + this.$emit(body, meta); + }, +}; diff --git a/components/circleci/sources/new-job-completed-instant/new-job-completed-instant.mjs b/components/circleci/sources/new-job-completed-instant/new-job-completed-instant.mjs new file mode 100644 index 0000000000000..cd907c4b068c4 --- /dev/null +++ b/components/circleci/sources/new-job-completed-instant/new-job-completed-instant.mjs @@ -0,0 +1,24 @@ +import common from "../common/base.mjs"; +import sampleEmit from "./test-event.mjs"; + +export default { + ...common, + key: "circleci-new-job-completed-instant", + name: "New Job Completed (Instant)", + description: "Emit new event when a job is completed in CircleCI.", + version: "0.0.1", + type: "source", + dedupe: "unique", + methods: { + ...common.methods, + getEvents() { + return [ + "job-completed", + ]; + }, + getSummary(event) { + return `Job Completed: ${event.job.name}`; + }, + }, + sampleEmit, +}; diff --git a/components/circleci/sources/new-job-completed-instant/test-event.mjs b/components/circleci/sources/new-job-completed-instant/test-event.mjs new file mode 100644 index 0000000000000..46d16fff09a71 --- /dev/null +++ b/components/circleci/sources/new-job-completed-instant/test-event.mjs @@ -0,0 +1,82 @@ +export default { + "happened_at": "2024-12-18T18:51:31.379158Z", + "pipeline": { + "id": "5c8229b0-194a-4942-bddd-2b6a40c5ab35", + "number": 5, + "created_at": "2024-12-18T17:23:29.629Z", + "trigger": { + "type": "api" + }, + "trigger_parameters": { + "github_app": { + "commit_author_name": "", + "owner": "", + "full_ref": "refs/heads/circleci-project-setup", + "forced": "false", + "user_username": "", + "branch": "circleci-project-setup", + "commit_title": "CircleCI Commit", + "repo_url": "", + "ref": "circleci-project-setup", + "repo_name": "", + "commit_author_email": "", + "commit_sha": "9e2e4bed59d2e6c51d00e0e0b49f1b79ff146ab1" + }, + "git": { + "commit_author_name": "", + "repo_owner": "", + "branch": "circleci-project-setup", + "commit_message": "CircleCI Commit", + "repo_url": "", + "ref": "refs/heads/circleci-project-setup", + "author_avatar_url": "", + "checkout_url": "", + "author_login": "", + "repo_name": "", + "commit_author_email": "", + "checkout_sha": "9e2e4bed59d2e6c51d00e0e0b49f1b79ff146ab1", + "default_branch": "master" + }, + "circleci": { + "event_time": "2024-12-18 17:23:29.361740256 +0000 UTC", + "provider_actor_id": "864c50f3-d0b6-4929-a524-036d3cd4e23f", + "actor_id": "864c50f3-d0b6-4929-a524-036d3cd4e23f", + "event_type": "create pipeline run api", + "trigger_type": "github_app" + }, + "webhook": { + "body": "{}" + } + } + }, + "webhook": { + "id": "6973c364-58bc-43f4-9074-1f3ea720fb6c", + "name": "" + }, + "type": "job-completed", + "organization": { + "id": "48c30eda-389d-48a8-ac64-7091f80c69df", + "name": "" + }, + "workflow": { + "id": "c5aa9edf-4de9-43fd-b50e-2bfd75e32cb1", + "name": "say-hello-workflow", + "created_at": "2024-12-18T18:51:06.137Z", + "stopped_at": "2024-12-18T18:51:31.300Z", + "url": "https://app.circleci.com/pipelines/circleci/9z8PamRuKaKWrW91o7Kqvn/AHWysFUuUfBgs6VeUqm6tg/5/workflows/c5aa9edf-4de9-43fd-b50e-2bfd75e32cb1" + }, + "project": { + "id": "4b309fd6-d103-401a-bee5-1de19651d969", + "name": "", + "slug": "circleci/9z8PamRuKaKWrW91o7Kqvn/AHWysFUuUfBgs6VeUqm6tg" + }, + "id": "1c457f7f-5b15-341b-9b86-6370d1357f61", + "job": { + "status": "success", + "id": "00f06c85-7476-4a75-ba64-7daa608dc61e", + "name": "say-hello", + "number": 12, + "started_at": "2024-12-18T18:51:09.357Z", + "stopped_at": "2024-12-18T18:51:31.300Z" + } +} \ No newline at end of file diff --git a/components/circleci/sources/new-workflow-completed-instant/new-workflow-completed-instant.mjs b/components/circleci/sources/new-workflow-completed-instant/new-workflow-completed-instant.mjs new file mode 100644 index 0000000000000..f4296e6964d06 --- /dev/null +++ b/components/circleci/sources/new-workflow-completed-instant/new-workflow-completed-instant.mjs @@ -0,0 +1,24 @@ +import common from "../common/base.mjs"; +import sampleEmit from "./test-event.mjs"; + +export default { + ...common, + key: "circleci-new-workflow-completed-instant", + name: "New Workflow Completed (Instant)", + description: "Emit new event when a workflow is completed in CircleCI.", + version: "0.0.1", + type: "source", + dedupe: "unique", + methods: { + ...common.methods, + getEvents() { + return [ + "workflow-completed", + ]; + }, + getSummary(event) { + return `Workflow Completed: ${event.workflow.name}`; + }, + }, + sampleEmit, +}; diff --git a/components/circleci/sources/new-workflow-completed-instant/test-event.mjs b/components/circleci/sources/new-workflow-completed-instant/test-event.mjs new file mode 100644 index 0000000000000..6f57ea3d3fbd0 --- /dev/null +++ b/components/circleci/sources/new-workflow-completed-instant/test-event.mjs @@ -0,0 +1,75 @@ +export default { + "type": "workflow-completed", + "id": "ab1491fb-c4ae-3496-a958-c248b4732020", + "happened_at": "2024-12-18T18:44:38.000100Z", + "webhook": { + "id": "7d0a502e-0880-414a-8951-97ba4c361aea", + "name": "" + }, + "workflow": { + "id": "7acc6aa9-aac0-40ac-8cf1-48fca8d8455d", + "name": "say-hello-workflow", + "created_at": "2024-12-18T18:44:20.682Z", + "stopped_at": "2024-12-18T18:44:37.867Z", + "url": "", + "status": "success" + }, + "pipeline": { + "id": "5c8229b0-194a-4942-bddd-2b6a40c5ab35", + "number": 5, + "created_at": "2024-12-18T17:23:29.629Z", + "trigger": { + "type": "api" + }, + "trigger_parameters": { + "github_app": { + "commit_author_name": "", + "owner": "", + "full_ref": "refs/heads/circleci-project-setup", + "forced": "false", + "user_username": "", + "branch": "circleci-project-setup", + "commit_title": "CircleCI Commit", + "repo_url": "", + "ref": "circleci-project-setup", + "repo_name": "", + "commit_author_email": "", + "commit_sha": "9e2e4bed59d2e6c51d00e0e0b49f1b79ff146ab1" + }, + "git": { + "commit_author_name": "", + "repo_owner": "", + "branch": "circleci-project-setup", + "commit_message": "CircleCI Commit", + "repo_url": "", + "ref": "refs/heads/circleci-project-setup", + "author_avatar_url": "", + "checkout_url": "", + "author_login": "", + "repo_name": "", + "commit_author_email": "", + "checkout_sha": "9e2e4bed59d2e6c51d00e0e0b49f1b79ff146ab1", + "default_branch": "master" + }, + "circleci": { + "event_time": "2024-12-18 17:23:29.361740256 +0000 UTC", + "provider_actor_id": "864c50f3-d0b6-4929-a524-036d3cd4e23f", + "actor_id": "864c50f3-d0b6-4929-a524-036d3cd4e23f", + "event_type": "create pipeline run api", + "trigger_type": "github_app" + }, + "webhook": { + "body": "{}" + } + } + }, + "project": { + "id": "4b309fd6-d103-401a-bee5-1de19651d969", + "name": "", + "slug": "circleci/9z8PamRuKaKWrW91o7Kqvn/AHWysFUuUfBgs6VeUqm6tg" + }, + "organization": { + "id": "48c30eda-389d-48a8-ac64-7091f80c69df", + "name": "" + } +} \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 39ba56f95f156..abd1daede4df5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1754,6 +1754,15 @@ importers: specifier: ^1.5.1 version: 1.6.6 + components/circleci: + dependencies: + '@pipedream/platform': + specifier: ^3.0.3 + version: 3.0.3 + uuid: + specifier: ^11.0.3 + version: 11.0.3 + components/cisco_meraki: dependencies: '@pipedream/platform':