Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GITHUB_TOKEN_TEST=
4 changes: 1 addition & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,4 @@ jobs:
with:
commit: "chore: update versions"
title: "chore: update versions"
publish: bunx changeset publish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
publish: bunx changeset publish
20 changes: 20 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
@@ -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 }}
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,15 @@ bun run pkg <pkg-name> <script-name>
# all packages
bun run pkg-all <script-name>
```

## 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:
73 changes: 73 additions & 0 deletions packages/collaborators/index.test.ts
Original file line number Diff line number Diff line change
@@ -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);
}
});
});
20 changes: 16 additions & 4 deletions packages/collaborators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -35,8 +35,18 @@ const getCollaboratorsResponseSchema = z.object({
role_name: z.string(),
});

type GetCollaboratorsResponse = z.infer<typeof getCollaboratorsResponseSchema>;
type GetCollaboratorsProps = z.infer<typeof getCollaboratorsPropsSchema>;
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<typeof getCollaboratorsPropsSchema>;

/**
* Get the list of collaborators for a repository
Expand All @@ -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');
}
}
};