diff --git a/packages/github/src/index.ts b/packages/github/src/index.ts index 64229fb..dcc981a 100644 --- a/packages/github/src/index.ts +++ b/packages/github/src/index.ts @@ -3,8 +3,10 @@ import * as Context from 'effect/Context' import { Octokit } from '@octokit/rest' import { pipe } from 'effect/Function' import * as Layer from 'effect/Layer' +import * as S from '@effect/schema/Schema' -export const GitHub = Context.Tag('GitHub') +export type GitHub = Octokit +export const GitHub = Context.Tag('GitHub') /** * Returns the contents of a file from a GitHub repository @@ -95,3 +97,56 @@ export const createOrUpdateFileContents = (args: { export const makeGitHubLayer = (token?: string) => Layer.succeed(GitHub, GitHub.of(new Octokit({ auth: token }))) + +export const GitCommit = S.struct({ + sha: S.string, + node_id: S.string, + url: S.string, + author: S.struct({ + date: S.string, + email: S.string, + name: S.string, + }), + committer: S.struct({ + date: S.string, + email: S.string, + name: S.string, + }), + message: S.string, + tree: S.struct({ + sha: S.string, + url: S.string, + }), + parents: S.array( + S.struct({ + sha: S.string, + url: S.string, + html_url: S.string, + }) + ), + verification: S.struct({ + verified: S.boolean, + reason: S.string, + signature: S.nullable(S.string), + payload: S.nullable(S.string), + }), + html_url: S.string, +}) +export type GitCommit = S.Schema.To + +// https://docs.github.com/en/rest/git/commits?apiVersion=2022-11-28#get-a-commit-object +export const getCommit = (args: { + owner: string + repo: string + commit_sha: string +}): Effect.Effect => + Effect.flatMap(GitHub, (api) => + Effect.tryPromise(() => + api + .request('GET /repos/{owner}/{repo}/git/commits/{commit_sha}', { + headers: { 'X-GitHub-Api-Version': '2022-11-28' }, + ...args, + }) + .then((x) => x.data) + ) + )