diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..9aac95c --- /dev/null +++ b/.env.example @@ -0,0 +1 @@ +GITHUB_TOKEN_TEST= \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 2bd36fa..825497c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -30,6 +30,4 @@ jobs: with: commit: "chore: update versions" title: "chore: update versions" - publish: bunx changeset publish - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file + publish: bunx changeset publish \ No newline at end of file diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml new file mode 100644 index 0000000..455edb4 --- /dev/null +++ b/.github/workflows/testing.yml @@ -0,0 +1,20 @@ +name: "Testing" +on: + pull_request: + branches: [main] + +jobs: + test: + name: test + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v4 + - name: Install bun + uses: oven-sh/setup-bun@v1 + - name: Install dependencies + run: bun install + - name: Run tests + run: bun test + env: + GITHUB_TOKEN_TEST: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/README.md b/README.md index 8f58cbe..eda1ca2 100644 --- a/README.md +++ b/README.md @@ -83,3 +83,15 @@ bun run pkg # all packages bun run pkg-all ``` + +## Environement Variables + +Depending on your development activity, you may need to set some environment variables for the packages to work properly or to run tests. + +```bash +cp env.example .env +``` + +Fill in the values in the .env file. + +Ideally, `GITHUB_TOKEN_TEST` should have same permissions as testing CI: diff --git a/packages/collaborators/index.test.ts b/packages/collaborators/index.test.ts new file mode 100644 index 0000000..b3cbf04 --- /dev/null +++ b/packages/collaborators/index.test.ts @@ -0,0 +1,73 @@ +import { describe, test, expect } from 'bun:test'; +import { + getCollaborators, + getCollaboratorsResponseSchema, + getCollaboratorsResponseInvalidSchema, + type GetCollaboratorsResponse, + type GetCollaboratorsProps, +} from './index'; + +describe('collaborators', () => { + test('valid', async () => { + try { + const collaborator = (await getCollaborators({ + owner: 'privanote', + repo: 'privanote', + token: process.env.GITHUB_TOKEN_TEST as string, + })) as GetCollaboratorsResponse[]; + + if (Array.isArray(collaborator)) { + const result = collaborator + .map((c) => getCollaboratorsResponseSchema.safeParse(c).success) + .includes(false); + expect(result).toBe(false); + } else { + expect().fail( + 'This should not be reached: ' + JSON.stringify(collaborator), + ); + } + } catch (error) { + const tokenIssue = + error instanceof Error ? error.message.includes('token') : false; + + if (tokenIssue) { + expect().fail('Invalid token provided'); + } else { + expect().fail('This should not be reached: ' + error); + } + } + }); + + test('invalid repo or token', async () => { + try { + const collaborator = (await getCollaborators({ + owner: 'privanote', + repo: 'asdfasfdasdfsadfsasdfasdf', + token: process.env.GITHUB_TOKEN_TEST as string, + })) as GetCollaboratorsResponse[]; + + const result = + getCollaboratorsResponseInvalidSchema.safeParse(collaborator).success; + + expect(result).toBe(true); + } catch (error) { + const tokenIssue = + error instanceof Error ? error.message.includes('token') : false; + + if (tokenIssue) { + expect().fail('Invalid token provided'); + } else { + expect().fail('This should not be reached: ' + error); + } + } + }); + + test('error', async () => { + try { + await getCollaborators({} as GetCollaboratorsProps); + } catch (error) { + const result = error instanceof Error; + expect(result).toBe(true); + } + }); +}); diff --git a/packages/collaborators/index.ts b/packages/collaborators/index.ts index 70e5c53..c4f6f71 100644 --- a/packages/collaborators/index.ts +++ b/packages/collaborators/index.ts @@ -6,7 +6,7 @@ const getCollaboratorsPropsSchema = z.object({ token: z.string(), }); -const getCollaboratorsResponseSchema = z.object({ +export const getCollaboratorsResponseSchema = z.object({ login: z.string(), id: z.number(), node_id: z.string(), @@ -35,8 +35,18 @@ const getCollaboratorsResponseSchema = z.object({ role_name: z.string(), }); -type GetCollaboratorsResponse = z.infer; -type GetCollaboratorsProps = z.infer; +export const getCollaboratorsResponseInvalidSchema = z.object({ + message: z.string(), + documentation_url: z.string(), +}); + +export type GetCollaboratorsResponse = z.infer< + typeof getCollaboratorsResponseSchema +>; +export type GetCollaboratorsResponseInvalid = z.infer< + typeof getCollaboratorsResponseInvalidSchema +>; +export type GetCollaboratorsProps = z.infer; /** * Get the list of collaborators for a repository @@ -63,13 +73,15 @@ export const getCollaborators = async ({ ); const data = await response.json(); if (!response.ok) { - return data as { message: string; documentation_url: string }; + return data as GetCollaboratorsResponseInvalid; } else { return data as GetCollaboratorsResponse[]; } } catch (error: unknown) { if (error instanceof Error) { throw new Error(`Error: ${error.message}`); + } else { + throw new Error('An unknown error occurred'); } } };