diff --git a/.env b/.env index bef9c44..fc20761 100644 --- a/.env +++ b/.env @@ -4,3 +4,5 @@ BITBUCKET_CLOUD_APP_PASSWORD= BITBUCKET_SERVER_URL= BITBUCKET_SERVER_TOKEN= +BITBUCKET_SERVER_TEST_PROJECT_KEY= +BITBUCKET_SERVER_TEST_PROJECT_NAME= diff --git a/.github/workflows/node.js.yaml b/.github/workflows/node.js.yaml index d5f72f2..afa34ee 100644 --- a/.github/workflows/node.js.yaml +++ b/.github/workflows/node.js.yaml @@ -29,3 +29,5 @@ jobs: BITBUCKET_CLOUD_APP_PASSWORD: ${{ secrets.BITBUCKET_CLOUD_APP_PASSWORD }} BITBUCKET_SERVER_URL: ${{ secrets.BITBUCKET_SERVER_URL }} BITBUCKET_SERVER_TOKEN: ${{ secrets.BITBUCKET_SERVER_TOKEN }} + BITBUCKET_SERVER_TEST_PROJECT_KEY: ${{ vars.BITBUCKET_SERVER_TEST_PROJECT_KEY }} + BITBUCKET_SERVER_TEST_PROJECT_NAME: ${{ vars.BITBUCKET_SERVER_TEST_PROJECT_NAME }} diff --git a/.github/workflows/pnpm-publish.yaml b/.github/workflows/pnpm-publish.yaml index ed3c318..85e6772 100644 --- a/.github/workflows/pnpm-publish.yaml +++ b/.github/workflows/pnpm-publish.yaml @@ -42,7 +42,7 @@ jobs: scope: "@coderabbitai" - run: pnpm publish --access public --no-git-checks --provenance env: - NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }} + NODE_AUTH_TOKEN: ${{ secrets.CODERABBIT_NPM_TOKEN }} - run: pnpm pack --pack-gzip-level 9 - name: Sign diff --git a/tests/cloud/client.ts b/tests/cloud/client.ts index baffd3b..34f4261 100644 --- a/tests/cloud/client.ts +++ b/tests/cloud/client.ts @@ -9,7 +9,7 @@ const basic = toBase64( BITBUCKET_CLOUD_USERNAME + ":" + BITBUCKET_CLOUD_APP_PASSWORD, ) -export const cloud = createBitbucketCloudClient({ +export const client = createBitbucketCloudClient({ baseUrl: BITBUCKET_CLOUD_URL.toString(), headers: { Accept: "application/json", Authorization: `Basic ${basic}` }, }) diff --git a/tests/cloud/repositories.test.ts b/tests/cloud/repositories.test.ts index ee6df59..1ccf2d5 100644 --- a/tests/cloud/repositories.test.ts +++ b/tests/cloud/repositories.test.ts @@ -1,8 +1,8 @@ import { test } from "vitest" -import { cloud } from "./client.js" +import { client } from "./client.js" test("GET /repositories", async ({ expect }) => { - const got = await cloud.GET("/repositories") + const got = await client.GET("/repositories") expect(got.data?.next).toBeTypeOf("string") expect(got.data?.pagelen).toBeTypeOf("number") diff --git a/tests/env.ts b/tests/env.ts index 3dd6ab6..a5a7d3f 100644 --- a/tests/env.ts +++ b/tests/env.ts @@ -88,3 +88,11 @@ export const BITBUCKET_CLOUD_APP_PASSWORD = envString( ) export const BITBUCKET_SERVER_URL = envUrl("BITBUCKET_SERVER_URL") export const BITBUCKET_SERVER_TOKEN = envString("BITBUCKET_SERVER_TOKEN") +export const NODE_ENV = parsed.NODE_ENV + +export const BITBUCKET_SERVER_TEST_PROJECT_KEY = envString( + "BITBUCKET_SERVER_TEST_PROJECT_KEY", +) +export const BITBUCKET_SERVER_TEST_PROJECT_NAME = envString( + "BITBUCKET_SERVER_TEST_PROJECT_NAME", +) diff --git a/tests/server/client.ts b/tests/server/client.ts index def3086..5961027 100644 --- a/tests/server/client.ts +++ b/tests/server/client.ts @@ -1,7 +1,7 @@ import { createBitbucketServerClient } from "../../src/index.js" import { BITBUCKET_SERVER_TOKEN, BITBUCKET_SERVER_URL } from "../env.js" -export const server = createBitbucketServerClient({ +export const client = createBitbucketServerClient({ baseUrl: BITBUCKET_SERVER_URL.toString(), headers: { Accept: "application/json", diff --git a/tests/server/projects.test.ts b/tests/server/projects.test.ts new file mode 100644 index 0000000..11cad52 --- /dev/null +++ b/tests/server/projects.test.ts @@ -0,0 +1,31 @@ +import { describe, test } from "vitest" +import { + BITBUCKET_SERVER_TEST_PROJECT_KEY, + BITBUCKET_SERVER_TEST_PROJECT_NAME, +} from "../env.js" +import { client } from "./client.js" + +describe("Projects", () => { + const key = BITBUCKET_SERVER_TEST_PROJECT_KEY + const name = BITBUCKET_SERVER_TEST_PROJECT_NAME + + // The API, even with a token with Project Admin permissions, cannot create + // or delete projects. + + test("Get projects", async ({ expect }) => { + const page = await client.GET("/api/latest/projects", { + params: { query: { name } }, + }) + expect(page.data?.size).toBeTypeOf("number") + + const found = page.data?.values?.find(p => p.key === key) + expect(found).toMatchObject({ key, name }) + }) + + test("Get a project", async ({ expect }) => { + const project = await client.GET("/api/latest/projects/{projectKey}", { + params: { path: { projectKey: key } }, + }) + expect(project.data).toMatchObject({ key, name }) + }) +}) diff --git a/tests/server/repositories.test.ts b/tests/server/repositories.test.ts index b16dfdc..2da0285 100644 --- a/tests/server/repositories.test.ts +++ b/tests/server/repositories.test.ts @@ -1,8 +1,62 @@ -import { test } from "vitest" -import { server } from "./client.js" +import { describe, test } from "vitest" +import { + BITBUCKET_SERVER_TEST_PROJECT_KEY, + BITBUCKET_SERVER_TEST_PROJECT_NAME, +} from "../env.js" +import { client } from "./client.js" -test("GET /api/latest/repos", async ({ expect }) => { - const got = await server.GET("/api/latest/repos") +describe("Repositories", { concurrent: false, sequential: true }, () => { + const projectKey = BITBUCKET_SERVER_TEST_PROJECT_KEY + const projectName = BITBUCKET_SERVER_TEST_PROJECT_NAME + const slug = "test-repository" + const name = "Test Repository" - expect(got.data?.size).toBeTypeOf("number") + test("Create repository", async ({ expect }) => { + const created = await client.POST( + "/api/latest/projects/{projectKey}/repos", + { params: { path: { projectKey } }, body: { name, scmId: "git", slug } }, + ) + + if (created.error) + console.error("Failed to create a repository", created.error) + + expect(created).toMatchObject({ + data: { + slug, + name, + project: { key: projectKey, name: projectName }, + scmId: "git", + }, + response: { status: 201 }, + }) + }) + + test("Get a repository", async ({ expect }) => { + const repository = await client.GET( + "/api/latest/projects/{projectKey}/repos/{repositorySlug}", + { params: { path: { projectKey, repositorySlug: slug } } }, + ) + + if (repository.error) + console.error("Failed to get a repository", repository.error) + + expect(repository.data).toMatchObject({ + slug, + name, + project: { key: projectKey, name: projectName }, + scmId: "git", + }) + }) + + test("Delete a repository", async ({ expect }) => { + const deleted = await client.DELETE( + "/api/latest/projects/{projectKey}/repos/{repositorySlug}", + { params: { path: { projectKey, repositorySlug: slug } } }, + ) + + if (deleted.error) + console.error("Failed to delete a repository", deleted.error) + + expect(deleted.response.status).toBe(202) + }) })