From 80bb16d01d30af055f9ad5ade32be3ece03ddc3d Mon Sep 17 00:00:00 2001 From: Jason Xie Date: Wed, 28 Feb 2024 06:17:53 -0500 Subject: [PATCH 1/6] ci: setup test ci --- .env.example | 1 + .github/workflows/{release.yml => ci.yml} | 20 +++++-- README.md | 12 ++++ packages/collaborators/index.test.ts | 67 +++++++++++++++++++++++ packages/collaborators/index.ts | 20 +++++-- 5 files changed, 112 insertions(+), 8 deletions(-) create mode 100644 .env.example rename .github/workflows/{release.yml => ci.yml} (67%) create mode 100644 packages/collaborators/index.test.ts diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..e5e3391 --- /dev/null +++ b/.env.example @@ -0,0 +1 @@ +GITHUB_TOKEN_REPO= \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/ci.yml similarity index 67% rename from .github/workflows/release.yml rename to .github/workflows/ci.yml index 2bd36fa..2b01249 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/ci.yml @@ -1,4 +1,4 @@ -name: "Release" +name: "CI" on: push: branches: [main] @@ -6,6 +6,20 @@ on: concurrency: ${{ github.workflow }}-${{ github.ref }} 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: ${{ secrets.GITHUB_TOKEN }} release: name: release runs-on: ubuntu-latest @@ -30,6 +44,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/README.md b/README.md index 8f58cbe..8be3bfa 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` should have same permissions as CI: diff --git a/packages/collaborators/index.test.ts b/packages/collaborators/index.test.ts new file mode 100644 index 0000000..8348247 --- /dev/null +++ b/packages/collaborators/index.test.ts @@ -0,0 +1,67 @@ +import { describe, test, expect } from 'bun:test'; +import { + getCollaborators, + getCollaboratorsResponseSchema, + getCollaboratorsResponseInvalidSchema, + type GetCollaboratorsResponse, + type GetCollaboratorsProps, +} from './index'; + +describe('collaborators', () => { + test('valid repo', async () => { + try { + const collaborator = (await getCollaborators({ + owner: 'privanote', + repo: 'privanote', + token: process.env.GITHUB_TOKEN_REPO as string, + })) as GetCollaboratorsResponse[]; + + const result = collaborator + .map((c) => getCollaboratorsResponseSchema.safeParse(c).success) + .includes(false); + expect(result).toBe(false); + } 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'); + } + } + }); + + test('invalid repo', async () => { + try { + const collaborator = (await getCollaborators({ + owner: 'privanote', + repo: 'asdfasfdasdfsadfsasdfasdf', + token: process.env.GITHUB_TOKEN_REPO 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'); + } + } + }); + + 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'); } } }; From b9a1ac51cc130fbfd5761dcb5960076332afa395 Mon Sep 17 00:00:00 2001 From: Jason Xie Date: Wed, 28 Feb 2024 06:32:19 -0500 Subject: [PATCH 2/6] chore: set test ci on pr --- .env.example | 2 +- .github/workflows/{ci.yml => release.yml} | 16 +--------------- .github/workflows/testing.yml | 22 ++++++++++++++++++++++ README.md | 2 +- packages/collaborators/index.test.ts | 4 ++-- 5 files changed, 27 insertions(+), 19 deletions(-) rename .github/workflows/{ci.yml => release.yml} (69%) create mode 100644 .github/workflows/testing.yml diff --git a/.env.example b/.env.example index e5e3391..9aac95c 100644 --- a/.env.example +++ b/.env.example @@ -1 +1 @@ -GITHUB_TOKEN_REPO= \ No newline at end of file +GITHUB_TOKEN_TEST= \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/release.yml similarity index 69% rename from .github/workflows/ci.yml rename to .github/workflows/release.yml index 2b01249..825497c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/release.yml @@ -1,4 +1,4 @@ -name: "CI" +name: "Release" on: push: branches: [main] @@ -6,20 +6,6 @@ on: concurrency: ${{ github.workflow }}-${{ github.ref }} 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: ${{ secrets.GITHUB_TOKEN }} release: name: release runs-on: ubuntu-latest diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml new file mode 100644 index 0000000..e8f8223 --- /dev/null +++ b/.github/workflows/testing.yml @@ -0,0 +1,22 @@ +name: "Testing" +on: + pull_request: + branches: [main] + +permissions: read-all + +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 8be3bfa..eda1ca2 100644 --- a/README.md +++ b/README.md @@ -94,4 +94,4 @@ cp env.example .env Fill in the values in the .env file. -Ideally, `GITHUB_TOKEN` should have same permissions as CI: +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 index 8348247..2d50bba 100644 --- a/packages/collaborators/index.test.ts +++ b/packages/collaborators/index.test.ts @@ -13,7 +13,7 @@ describe('collaborators', () => { const collaborator = (await getCollaborators({ owner: 'privanote', repo: 'privanote', - token: process.env.GITHUB_TOKEN_REPO as string, + token: process.env.GITHUB_TOKEN_TEST as string, })) as GetCollaboratorsResponse[]; const result = collaborator @@ -37,7 +37,7 @@ describe('collaborators', () => { const collaborator = (await getCollaborators({ owner: 'privanote', repo: 'asdfasfdasdfsadfsasdfasdf', - token: process.env.GITHUB_TOKEN_REPO as string, + token: process.env.GITHUB_TOKEN_TEST as string, })) as GetCollaboratorsResponse[]; const result = From 29d5ad5facdeb8392e837948ba810e5741d53d0f Mon Sep 17 00:00:00 2001 From: Jason Xie Date: Wed, 28 Feb 2024 06:40:10 -0500 Subject: [PATCH 3/6] fix: collaborators test --- packages/collaborators/index.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/collaborators/index.test.ts b/packages/collaborators/index.test.ts index 2d50bba..e00a6fd 100644 --- a/packages/collaborators/index.test.ts +++ b/packages/collaborators/index.test.ts @@ -27,7 +27,7 @@ describe('collaborators', () => { if (tokenIssue) { expect().fail('Invalid token provided'); } else { - expect().fail('This should not be reached'); + expect().fail('This should not be reached: ' + error); } } }); @@ -51,7 +51,7 @@ describe('collaborators', () => { if (tokenIssue) { expect().fail('Invalid token provided'); } else { - expect().fail('This should not be reached'); + expect().fail('This should not be reached: ' + error); } } }); From b73cced6e2cbaa01ce91af09b503bd39a88b0723 Mon Sep 17 00:00:00 2001 From: Jason Xie Date: Wed, 28 Feb 2024 06:51:24 -0500 Subject: [PATCH 4/6] fix: check for wrong return --- packages/collaborators/index.test.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/collaborators/index.test.ts b/packages/collaborators/index.test.ts index e00a6fd..b3cbf04 100644 --- a/packages/collaborators/index.test.ts +++ b/packages/collaborators/index.test.ts @@ -8,7 +8,7 @@ import { } from './index'; describe('collaborators', () => { - test('valid repo', async () => { + test('valid', async () => { try { const collaborator = (await getCollaborators({ owner: 'privanote', @@ -16,10 +16,16 @@ describe('collaborators', () => { token: process.env.GITHUB_TOKEN_TEST as string, })) as GetCollaboratorsResponse[]; - const result = collaborator - .map((c) => getCollaboratorsResponseSchema.safeParse(c).success) - .includes(false); - expect(result).toBe(false); + 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; @@ -32,7 +38,7 @@ describe('collaborators', () => { } }); - test('invalid repo', async () => { + test('invalid repo or token', async () => { try { const collaborator = (await getCollaborators({ owner: 'privanote', From baa58448787e46f6f3eb70a63ac543deae93969f Mon Sep 17 00:00:00 2001 From: Jason Xie Date: Wed, 28 Feb 2024 06:54:37 -0500 Subject: [PATCH 5/6] fix: testing workflow --- .github/workflows/testing.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index e8f8223..50282c7 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -3,10 +3,9 @@ on: pull_request: branches: [main] -permissions: read-all - jobs: test: + permissions: read-all name: test runs-on: ubuntu-latest steps: From 0643e2e1aad029cfcb31f2abce2efbcd40dc22f6 Mon Sep 17 00:00:00 2001 From: Jason Xie Date: Wed, 28 Feb 2024 06:58:11 -0500 Subject: [PATCH 6/6] fix: testing workflow --- .github/workflows/testing.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index 50282c7..455edb4 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -5,7 +5,6 @@ on: jobs: test: - permissions: read-all name: test runs-on: ubuntu-latest steps: