From fdf1b2148c1a514fb74ce8c0ca7510c5097952f5 Mon Sep 17 00:00:00 2001 From: Akhil Date: Mon, 31 Jul 2023 14:20:41 +0530 Subject: [PATCH] feat: added git database tasks for github integration (#204) * feat: added git database tasks for github integration * feat: added examples for git database tasks --- .changeset/tender-sloths-lay.md | 5 + examples/nextjs-example/src/jobs/github.ts | 129 ++++++ integrations/github/src/tasks.ts | 509 +++++++++++++++++++++ 3 files changed, 643 insertions(+) create mode 100644 .changeset/tender-sloths-lay.md diff --git a/.changeset/tender-sloths-lay.md b/.changeset/tender-sloths-lay.md new file mode 100644 index 0000000000..85cd4d3789 --- /dev/null +++ b/.changeset/tender-sloths-lay.md @@ -0,0 +1,5 @@ +--- +"@trigger.dev/github": patch +--- + +added git database tasks diff --git a/examples/nextjs-example/src/jobs/github.ts b/examples/nextjs-example/src/jobs/github.ts index 88be9a41a9..11be64c2d4 100644 --- a/examples/nextjs-example/src/jobs/github.ts +++ b/examples/nextjs-example/src/jobs/github.ts @@ -244,3 +244,132 @@ client.defineJob({ return { payload, ctx }; }, }); + +client.defineJob({ + id: "github-integration-on-pull-request-merge-commit", + name: "GitHub Integration - on Pull Request Merge Commit", + version: "0.1.0", + integrations: { github }, + trigger: githubApiKey.triggers.repo({ + event: events.onPullRequest, + owner: "triggerdotdev", + repo: "empty", + }), + run: async (payload, io, ctx) => { + await io.logger.info("This is a simple log info message"); + + if (payload.pull_request.merged && payload.pull_request.merge_commit_sha) { + const commit = await io.github.getCommit("get merge commit", { + owner: payload.repository.owner.login, + repo: payload.repository.name, + commitSHA: payload.pull_request.merge_commit_sha, + }); + await io.logger.info("Merge Commit Details", commit); + } + + return { payload, ctx }; + }, +}); + +client.defineJob({ + id: "github-integration-get-tree", + name: "GitHub Integration - Get Tree", + version: "0.1.0", + integrations: { github }, + trigger: githubApiKey.triggers.repo({ + event: events.onPullRequest, + owner: "triggerdotdev", + repo: "empty", + }), + run: async (payload, io, ctx) => { + await io.logger.info("This is a simple log info message"); + + if (payload.pull_request.merged && payload.pull_request.merge_commit_sha) { + const tree = await io.github.getTree("get merge commit", { + owner: payload.repository.owner.login, + repo: payload.repository.name, + treeSHA: payload.pull_request.merge_commit_sha, + }); + await io.logger.info("Tree ", tree); + } + + return { payload, ctx }; + }, +}); + +client.defineJob({ + id: "github-integration-get-reference", + name: "GitHub Integration - Get Reference", + integrations: { github }, + version: "0.1.0", + trigger: githubApiKey.triggers.repo({ + event: events.onNewBranch, + owner: "triggerdotdev", + repo: "empty", + }), + run: async (payload, io, ctx) => { + await io.logger.info("This is a simple log info message"); + + const ref = await io.github.getReference("Get reference", { + owner: payload.repository.owner.login, + repo: payload.repository.name, + ref: payload.ref, + }); + + await io.logger.info("Reference ", ref); + + return { payload, ctx }; + }, +}); + +client.defineJob({ + id: "github-integration-list-matching-references", + name: "GitHub Integration - List Matching References", + integrations: { github }, + version: "0.1.0", + trigger: githubApiKey.triggers.repo({ + event: events.onNewBranch, + owner: "triggerdotdev", + repo: "empty", + }), + run: async (payload, io, ctx) => { + await io.logger.info("This is a simple log info message"); + + const ref = await io.github.listMatchingReferences( + "List Matching References", + { + owner: payload.repository.owner.login, + repo: payload.repository.name, + ref: payload.ref, + } + ); + + await io.logger.info("Reference ", ref); + + return { payload, ctx }; + }, +}); + +client.defineJob({ + id: "github-integration-get-tag", + name: "GitHub Integration - Get Tag", + version: "0.1.0", + integrations: { github }, + trigger: githubApiKey.triggers.repo({ + event: events.onNewBranchOrTag, + owner: "triggerdotdev", + repo: "empty", + }), + run: async (payload, io, ctx) => { + await io.logger.info("This is a simple log info message"); + if (payload.ref_type === "tag") { + const tag = io.github.getTag("Get Tag", { + owner: payload.repository.owner.login, + repo: payload.repository.name, + tagSHA: payload.ref, + }); + await io.logger.info("Tag ", tag); + } + return { payload, ctx }; + }, +}); diff --git a/integrations/github/src/tasks.ts b/integrations/github/src/tasks.ts index 37febd7cce..a22f150a57 100644 --- a/integrations/github/src/tasks.ts +++ b/integrations/github/src/tasks.ts @@ -604,6 +604,502 @@ const listOrgWebhooks: GithubAuthenticatedTask< }, }; +type Endcoding = "utf-8" | "base-64"; + +type CreateBlobTask = GithubAuthenticatedTask< + { + owner: string; + repo: string; + content: string; + encoding?: Endcoding; + }, + OctokitClient["rest"]["git"]["createBlob"] +>; +const createBlob: CreateBlobTask = { + onError, + run: async (params, client) => { + return client.rest.git + .createBlob({ + owner: params.owner, + repo: params.repo, + content: params.content, + encoding: params.encoding, + }) + .then((response) => response.data); + }, + init: (params) => { + return { + name: "Create Blob", + params, + properties: [ + ...repoProperties(params), + { + label: "Content", + text: params.content, + }, + ], + }; + }, +}; + +type GetBlobTask = GithubAuthenticatedTask< + { + owner: string; + repo: string; + fileSHA: string; + }, + OctokitClient["rest"]["git"]["getBlob"] +>; + +const getBlob: GetBlobTask = { + onError, + run: async (params, client) => { + return client.rest.git + .getBlob({ + owner: params.owner, + repo: params.repo, + file_sha: params.fileSHA, + }) + .then((response) => response.data); + }, + init: (params) => { + return { + name: "Get Blob", + params, + properties: [...repoProperties(params)], + }; + }, +}; + +type AuthorContent = { + name: string; + email: string; + date?: string; +}; + +type CommitterContent = { + name?: string; + email?: string; + date?: string; +}; + +type CreateCommitTask = GithubAuthenticatedTask< + { + owner: string; + repo: string; + message: string; + tree: string; + parents?: string[]; + author?: AuthorContent; + committer?: CommitterContent; + signature?: string; + }, + OctokitClient["rest"]["git"]["createCommit"] +>; + +const createCommit: CreateCommitTask = { + onError, + run: async (params, client) => { + return client.rest.git + .createCommit({ + ...params, + }) + .then((response) => response.data); + }, + init: (params) => { + return { + name: "Create Commit", + params, + properties: [ + ...repoProperties(params), + { + label: "Message", + text: params.message, + }, + { + label: "Tree", + text: params.tree, + }, + ], + }; + }, +}; + +type GetCommitTask = GithubAuthenticatedTask< + { + owner: string; + repo: string; + commitSHA: string; + }, + OctokitClient["rest"]["git"]["getCommit"] +>; + +const getCommit: GetCommitTask = { + onError, + run: async (params, client) => { + return client.rest.git + .getCommit({ + owner: params.owner, + repo: params.repo, + commit_sha: params.commitSHA, + }) + .then((response) => response.data); + }, + init: (params) => { + return { + name: "Get Commit", + params, + properties: [ + ...repoProperties(params), + { + label: "Commit SHA", + text: params.commitSHA, + }, + ], + }; + }, +}; + +type ListMatchingReferencesTask = GithubAuthenticatedTask< + { + owner: string; + repo: string; + ref: string; + }, + OctokitClient["rest"]["git"]["listMatchingRefs"] +>; +const listMatchingReferences: ListMatchingReferencesTask = { + onError, + run: async (params, client) => { + return client.rest.git + .listMatchingRefs({ + ...params, + }) + .then((response) => response.data); + }, + init: (params) => { + return { + name: "List Matching References", + params, + properties: [ + ...repoProperties(params), + { + label: "Ref", + text: params.ref, + }, + ], + }; + }, +}; + +type GetReferenceTask = GithubAuthenticatedTask< + { + owner: string; + repo: string; + ref: string; + }, + OctokitClient["rest"]["git"]["getRef"] +>; + +const getReference: GetReferenceTask = { + onError, + run: async (params, client) => { + return client.rest.git + .getRef({ + ...params, + }) + .then((response) => response.data); + }, + init: (params) => { + return { + name: "Get Reference", + params, + properties: [ + ...repoProperties(params), + { + label: "Ref", + text: params.ref, + }, + ], + }; + }, +}; + +type CreateReferenceTask = GithubAuthenticatedTask< + { + owner: string; + repo: string; + ref: string; + sha: string; + }, + OctokitClient["rest"]["git"]["createRef"] +>; + +const createReference: CreateReferenceTask = { + onError, + run: async (params, client) => { + return client.rest.git + .createRef({ + ...params, + }) + .then((response) => response.data); + }, + init: (params) => { + return { + name: "Create Reference", + params, + properties: [ + ...repoProperties(params), + { + label: "Ref", + text: params.ref, + }, + { + label: "SHA", + text: params.ref, + }, + ], + }; + }, +}; + +type UpdateReferenceTask = GithubAuthenticatedTask< + { + owner: string; + repo: string; + ref: string; + sha: string; + force?: boolean; + }, + OctokitClient["rest"]["git"]["updateRef"] +>; + +const updateReference: UpdateReferenceTask = { + onError, + run: async (params, client) => { + return client.rest.git + .updateRef({ + ...params, + }) + .then((response) => response.data); + }, + init: (params) => { + return { + name: "Update Reference", + params, + properties: [ + ...repoProperties(params), + { + label: "Ref", + text: params.ref, + }, + { + label: "SHA", + text: params.ref, + }, + ], + }; + }, +}; + +type DeleteReferenceTask = GithubAuthenticatedTask< + { + owner: string; + repo: string; + ref: string; + }, + OctokitClient["rest"]["git"]["deleteRef"] +>; + +const deleteReference: DeleteReferenceTask = { + onError, + run: async (params, client) => { + return client.rest.git + .deleteRef({ + ...params, + }) + .then((response) => response.data); + }, + init: (params) => { + return { + name: "Delete Reference", + params, + properties: [ + ...repoProperties(params), + { + label: "Ref", + text: params.ref, + }, + ], + }; + }, +}; + +type TagType = "commit" | "tree" | "blob"; +type TaggerContent = { + name: string; + email: string; + date?: string; +}; +type CreateTagTask = GithubAuthenticatedTask< + { + owner: string; + repo: string; + tag: string; + message: string; + object: string; + type: TagType; + tagger?: TaggerContent; + }, + OctokitClient["rest"]["git"]["createTag"] +>; + +const createTag: CreateTagTask = { + onError, + run: async (params, client) => { + return client.rest.git + .createTag({ + ...params, + }) + .then((response) => response.data); + }, + init: (params) => { + return { + name: "Create Tag", + params, + properties: [ + ...repoProperties(params), + { + label: "Tag", + text: params.tag, + }, + { + label: "Message", + text: params.message, + }, + { + label: "Object", + text: params.object, + }, + { + label: "Tag Type", + text: params.type, + }, + ], + }; + }, +}; + +type GetTagTask = GithubAuthenticatedTask< + { + owner: string; + repo: string; + tagSHA: string; + }, + OctokitClient["rest"]["git"]["getTag"] +>; + +const getTag: GetTagTask = { + onError, + run: async (params, client) => { + return client.rest.git + .getTag({ + owner: params.owner, + repo: params.repo, + tag_sha: params.tagSHA, + }) + .then((response) => response.data); + }, + init: (params) => { + return { + name: "Get Tag", + params, + properties: [ + ...repoProperties(params), + { + label: "Tag SHA", + text: params.tagSHA, + }, + ], + }; + }, +}; + +type TreeType = { + path?: string | undefined; + mode?: "100644" | "100755" | "040000" | "160000" | "120000" | undefined; + type?: "commit" | "tree" | "blob" | undefined; + sha?: string | null | undefined; + content?: string | undefined; +}; + +type CreateTreeTask = GithubAuthenticatedTask< + { + owner: string; + repo: string; + tree: TreeType[]; + baseTree?: string; + }, + OctokitClient["rest"]["git"]["createTree"] +>; + +const createTree: CreateTreeTask = { + onError, + run: async (params, client) => { + return client.rest.git + .createTree({ + owner: params.owner, + repo: params.repo, + tree: params.tree, + }) + .then((response) => response.data); + }, + init: (params) => { + return { + name: "Create Tree", + params, + properties: [...repoProperties(params)], + }; + }, +}; + +type GetTreeTask = GithubAuthenticatedTask< + { + owner: string; + repo: string; + treeSHA: string; + recursive?: string; + }, + OctokitClient["rest"]["git"]["getTree"] +>; + +const getTree: GetTreeTask = { + onError, + run: async (params, client) => { + return client.rest.git + .getTree({ + owner: params.owner, + repo: params.repo, + tree_sha: params.treeSHA, + recursive: params.recursive, + }) + .then((response) => response.data); + }, + init: (params) => { + return { + name: "Get Tree", + params, + properties: [ + ...repoProperties(params), + { + label: "Tree SHA", + text: params.treeSHA, + }, + ], + }; + }, +}; + export const tasks = { createIssue, addIssueAssignees, @@ -619,4 +1115,17 @@ export const tasks = { updateOrgWebhook, createOrgWebhook, listOrgWebhooks, + createBlob, + getBlob, + createCommit, + getCommit, + listMatchingReferences, + getReference, + createReference, + updateReference, + deleteReference, + createTag, + getTag, + createTree, + getTree, };