Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🏷️ Override Bitbucket Cloud's schema to create new branches #41

Merged
merged 2 commits into from
Mar 4, 2025

Conversation

NatoBoram
Copy link
Collaborator

@NatoBoram NatoBoram commented Mar 4, 2025

πŸ“ Description

Bitbucket Cloud's documentation is as odd with its schema.

Overriding is the simplest way to get over it.

  • Override in "/repositories/{workspace}/{repo_slug}/refs/branches" the post's requestBody with a type taken straight from its docs.

πŸ““ References

Summary by CodeRabbit

  • New Features

    • Improved the branch creation API to provide a clearer, more robust structure for making branch requests via Bitbucket Cloud.
    • Expanded the available interface to support more resilient and integrated development experiences.
  • Tests

    • Added automated tests to verify the successful operation of the branch creation process.

@NatoBoram NatoBoram self-assigned this Mar 4, 2025
Copy link

coderabbitai bot commented Mar 4, 2025

πŸ“ Walkthrough

Walkthrough

The pull request restructures the module organisation related to Bitbucket Cloud’s API types. It updates the import path for the paths type in the client, adds export statements in index files to expose additional types, introduces a new types file that redefines the branch creation endpoint, and adds a corresponding test case. These changes adjust the codebase’s type definitions without modifying core functionality.

Changes

File(s) Change Summary
src/cloud/client.ts Updated the import of the paths type from ./openapi/index.ts to ./interfaces/paths.ts.
src/cloud/index.ts, src/cloud/interfaces/index.ts Added export statements to include type definitions from ./interfaces/index.ts and re-export types from ./paths.ts.
src/cloud/interfaces/paths.ts Introduced new type definitions overriding the OpenAPI schema for branch creation, including interfaces for paths, CreateBranchRequest, and Target.
src/cloud/interfaces/paths.test.ts Added a test case for the CreateBranchRequest that uses a mock fetch to simulate a POST request to the Bitbucket API endpoint for creating a branch.

Sequence Diagram(s)

sequenceDiagram
    participant Test
    participant Client
    participant API

    Test->>Client: Invoke CreateBranchRequest
    Client->>API: POST /repositories/{workspace}/{repo_slug}/refs/branches with new payload structure
    API-->>Client: Return HTTP 200 response
    Client-->>Test: Deliver successful response
Loading

Possibly Related PRs

Suggested Labels

enhancement, javascript

✨ Finishing Touches
  • πŸ“ Generate Docstrings

πŸͺ§ Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@NatoBoram NatoBoram added the bug Something isn't working label Mar 4, 2025
@NatoBoram NatoBoram marked this pull request as ready for review March 4, 2025 18:59
@NatoBoram NatoBoram requested a review from a team as a code owner March 4, 2025 18:59
@NatoBoram NatoBoram requested review from alexcoderabbitai and nehal-a2z and removed request for a team March 4, 2025 18:59
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (3)
src/cloud/interfaces/paths.test.ts (2)

5-8: Consider enhancing the mock implementation.

The mock fetch function currently returns an empty object with a 200 status. For better test coverage, consider returning a realistic response body that matches Bitbucket's actual API response structure.

- async function fetch() {
- 	const response = new Response(JSON.stringify({}), { status: 200 })
- 	return Promise.resolve(response)
- }
+ async function fetch() {
+ 	const response = new Response(JSON.stringify({
+ 		name: "smf/create-feature",
+ 		target: {
+ 			hash: "abc123def456",
+ 			type: "commit"
+ 		},
+ 		links: {
+ 			self: { href: "https://api.bitbucket.org/2.0/repositories/workspace/repo_slug/refs/branches/smf/create-feature" }
+ 		}
+ 	}), { status: 201 })
+ 	return Promise.resolve(response)
+ }

15-30: Test case looks good but could be expanded.

The test case appropriately validates the basic functionality of creating a branch using the custom request type. However, consider expanding test coverage:

  1. Verify response status 201 (created) instead of 200
  2. Add expectations for the request payload format
  3. Test different branch name patterns
  4. Include error case testing
test("CreateBranchRequest", async ({ expect }) => {
	const example: CreateBranchRequest = {
		name: "smf/create-feature",
		target: { hash: "default" },
	}

	const { response } = await client.POST(
		"/repositories/{workspace}/{repo_slug}/refs/branches",
		{
			params: { path: { repo_slug: "repo_slug", workspace: "workspace" } },
			body: example,
		},
	)

-	expect(response.status).toBe(200)
+	expect(response.status).toBe(201) // Bitbucket typically returns 201 for resource creation
+	
+	// Additional test cases could be added here
})

+ test("CreateBranchRequest with validation errors", async ({ expect }) => {
+   // Test with invalid branch name or target to verify error handling
+ })
src/cloud/interfaces/paths.ts (1)

33-35: Consider adding type information to Target interface.

The Target interface could benefit from a more specific type definition for the hash property, potentially distinguishing between commit hashes and branch/tag references.

export interface Target {
-	readonly hash: string
+	/**
+	 * Commit hash, branch name, or tag to point the new branch at.
+	 * For example: "master", "develop", "a1b2c3d4e5f6", etc.
+	 */
+	readonly hash: string
+	/** Optional type property that Bitbucket sometimes expects */
+	readonly type?: "commit"
}
πŸ“œ Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between 9a7ae79 and 97a59c7.

πŸ“’ Files selected for processing (5)
  • src/cloud/client.ts (1 hunks)
  • src/cloud/index.ts (1 hunks)
  • src/cloud/interfaces/index.ts (1 hunks)
  • src/cloud/interfaces/paths.test.ts (1 hunks)
  • src/cloud/interfaces/paths.ts (1 hunks)
βœ… Files skipped from review due to trivial changes (2)
  • src/cloud/interfaces/index.ts
  • src/cloud/client.ts
πŸ”‡ Additional comments (4)
src/cloud/index.ts (1)

2-2: Excellent addition to expose the interface types.

The additional export statement makes types from the interfaces module accessible to consumers, which is essential for exposing the newly defined branch creation types.

src/cloud/interfaces/paths.ts (3)

3-5: Good documentation on the purpose of this override.

The comment clearly states the purpose of overriding Bitbucket Cloud's OpenAPI schema.


6-23: Well-structured type extension for schema override.

The approach to extend and modify the OpenAPI schema is clean and type-safe. The code properly:

  1. Extends the original schema
  2. Omits the specific path to override
  3. Reconstructs the path with the modified request body structure

This implementation maintains type safety while adapting to Bitbucket's actual API requirements.


25-31: Clear and well-documented interface for branch creation.

The CreateBranchRequest interface is appropriately documented with comments explaining each field's purpose. The structure matches Bitbucket's API requirements for branch creation.

@NatoBoram NatoBoram merged commit 26db646 into main Mar 4, 2025
3 checks passed
@NatoBoram NatoBoram deleted the feature/override-create-branch branch March 4, 2025 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant