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

Spaces + rate limits #11

Merged
merged 7 commits into from
Aug 6, 2024
Merged

Spaces + rate limits #11

merged 7 commits into from
Aug 6, 2024

Conversation

ggreer
Copy link
Contributor

@ggreer ggreer commented Aug 6, 2024

Summary by CodeRabbit

  • New Features

    • Added functionality for managing Confluence space permissions, including methods to get, add, and remove space permissions.
    • Introduced a comprehensive API for space management with the spaceBuilder struct, allowing listing and entitlement management of spaces.
    • Enhanced rate limit handling for Confluence API requests, ensuring better control over request throttling.
  • Bug Fixes

    • Streamlined URL generation for API requests, improving functionality and reducing redundancy.
  • Tests

    • Implemented unit tests for space listing and grant retrieval to ensure reliability and maintainability.

Copy link

coderabbitai bot commented Aug 6, 2024

Walkthrough

The recent changes significantly enhance the ConfluenceClient by refactoring URL generation and introducing new functionalities for managing spaces and permissions. A unified URL parsing method improves code clarity, while new methods bolster the client’s capabilities in handling spaces and permissions robustly. Additionally, rate-limiting features and comprehensive testing ensure reliable interactions with the Confluence API, collectively streamlining the client and enhancing its extensibility.

Changes

Files Change Summary
pkg/connector/client/confluence.go Refactored URL generation with a unified parse method; added new space management methods and enhanced error handling; improved control flow in ConfluenceClient.
pkg/connector/client/models.go Introduced new types for Confluence space management, enhancing response structures for API interactions.
pkg/connector/client/path.go Removed duplicate SearchUrlPath declaration; renamed WithPaginationCursor to withPaginationCursor, changing its visibility to unexported.
pkg/connector/client/ratelimit.go Implemented rate limit handling with WithConfluenceRatelimitData and isRatelimited functions to manage API request limits effectively.
pkg/connector/client/request.go Created a structured HTTP request client with methods for GET, POST, DELETE, and centralized error handling.
pkg/connector/connector.go Added new spaceResourceType for managing Confluence spaces; updated synchronization logic to accommodate this new resource type.
pkg/connector/spaces.go Implemented spaceBuilder with methods for managing Confluence spaces and permissions, enhancing API functionality.
pkg/connector/spaces_test.go Introduced unit tests for spaceBuilder, verifying functionality for listing spaces and managing grants.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ConfluenceClient
    participant API
    
    User->>ConfluenceClient: Request Space List
    ConfluenceClient->>API: GET /spaces
    API-->>ConfluenceClient: Return Space Data
    ConfluenceClient-->>User: List of Spaces
Loading
sequenceDiagram
    participant User
    participant ConfluenceClient
    participant API
    
    User->>ConfluenceClient: Manage Space Permissions
    ConfluenceClient->>API: POST /spaces/{spaceId}/permissions
    API-->>ConfluenceClient: Confirm Permission Change
    ConfluenceClient-->>User: Permission Updated
Loading

Poem

🐇 In a world of spaces, vast and wide,
A client hops with joyful stride.
With URLs now clear and bright,
Permissions granted, oh what a sight!
Through tunnels of code, it deftly weaves,
Celebrating changes, the rabbit believes! 🌿


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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.

@ggreer ggreer changed the title Rate limits Spaces + rate limits Aug 6, 2024
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: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2110877 and 89726a1.

Files ignored due to path filters (1)
  • README.md is excluded by none and included by none
Files selected for processing (8)
  • pkg/connector/client/confluence.go (6 hunks)
  • pkg/connector/client/models.go (1 hunks)
  • pkg/connector/client/path.go (2 hunks)
  • pkg/connector/client/ratelimit.go (1 hunks)
  • pkg/connector/client/request.go (1 hunks)
  • pkg/connector/connector.go (2 hunks)
  • pkg/connector/spaces.go (1 hunks)
  • pkg/connector/spaces_test.go (1 hunks)
Additional comments not posted (51)
pkg/connector/client/ratelimit.go (1)

27-45: LGTM!

The function correctly checks for rate limit status and status codes.

pkg/connector/spaces_test.go (2)

14-50: LGTM!

The test case correctly sets up the context, client, and server, and includes appropriate assertions for listing spaces.


52-63: LGTM!

The sub-test correctly sets up the space and includes appropriate assertions for listing grants.

pkg/connector/client/request.go (4)

15-21: LGTM!

The function correctly makes a GET request using the makeRequest helper function.


23-30: LGTM!

The function correctly makes a POST request using the makeRequest helper function.


32-38: LGTM!

The function correctly makes a DELETE request using the makeRequest helper function.


40-86: LGTM!

The function correctly handles the details of making an HTTP request, including rate limit handling and error processing.

pkg/connector/connector.go (2)

33-37: LGTM!

The definition of spaceResourceType is consistent with other resource types.


102-102: LGTM! But verify the implementation of newSpaceBuilder.

The addition of newSpaceBuilder(c.client) is correctly placed.

However, ensure that the implementation of newSpaceBuilder is correct and aligns with the expected functionality.

Verification successful

Implementation of newSpaceBuilder and spaceBuilder looks correct.

  • The newSpaceBuilder function initializes a spaceBuilder struct with the provided client.
  • The spaceBuilder struct and its methods (ResourceType and List) are correctly defined and align with the expected functionality.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of `newSpaceBuilder`.

# Test: Search for the function definition. Expect: Correct implementation of `newSpaceBuilder`.
rg --type go 'func newSpaceBuilder' -A 10

Length of output: 614


Script:

#!/bin/bash
# Description: Verify the `spaceBuilder` struct and its methods.

# Search for the definition of `spaceBuilder`.
rg --type go 'type spaceBuilder struct' -A 10

# Search for methods associated with `spaceBuilder`.
rg --type go 'func (s \*spaceBuilder)' -A 10

Length of output: 646

pkg/connector/client/path.go (2)

14-14: LGTM!

The removal of the duplicate declaration of SearchUrlPath eliminates redundancy.


Line range hint 59-66: LGTM! But verify the usage of withPaginationCursor.

The change in visibility of WithPaginationCursor to withPaginationCursor is correct for internal use.

However, ensure that the function is used correctly within the package.

Verification successful

The function withPaginationCursor is used correctly within the package.

The change in visibility to make it internal does not negatively impact its functionality, and it is consistently used to pass pagination parameters to the c.parse method.

  • pkg/connector/client/confluence.go: Verified multiple usages of withPaginationCursor within the c.parse method calls.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of `withPaginationCursor`.

# Test: Search for the function usage. Expect: Correct usage of `withPaginationCursor`.
rg --type go 'withPaginationCursor'

Length of output: 536


Script:

#!/bin/bash
# Description: Extract the surrounding lines of the usages of `withPaginationCursor` in `pkg/connector/client/confluence.go`.

# Test: Extract context lines around the usage of `withPaginationCursor`.
rg --type go --context 5 'withPaginationCursor' pkg/connector/client/confluence.go

Length of output: 1020

pkg/connector/client/models.go (14)

58-61: LGTM!

The definition of ConfluenceSpaceDescriptionValue is consistent with other types.


63-65: LGTM!

The definition of ConfluenceSpaceDescription is consistent with other types.


67-70: LGTM!

The definition of ConfluenceMeta is consistent with other types.


72-76: LGTM!

The definition of ConfluenceSpaceOperationsResponse is consistent with other types.


78-81: LGTM!

The definition of ConfluenceSpacePermissionResponse is consistent with other types.


83-87: LGTM!

The definition of ConfluenceSpacePermission is consistent with other types.


89-92: LGTM!

The definition of ConfluenceSpacePermissionPrincipal is consistent with other types.


94-97: LGTM!

The definition of ConfluenceSpaceOperation is consistent with other types.


99-102: LGTM!

The definition of ConfluenceSpacePermissionOperation is consistent with other types.


104-116: LGTM!

The definition of ConfluenceSpace is consistent with other types.


118-121: LGTM!

The definition of confluenceSpaceList is consistent with other types.


123-126: LGTM!

The definition of SpacePermissionSubject is consistent with other types.


128-131: LGTM!

The definition of SpacePermissionOperation is consistent with other types.


133-136: LGTM!

The definition of CreateSpacePermissionRequestBody is consistent with other types.

pkg/connector/spaces.go (10)

21-28: LGTM!

The function correctly generates an entitlement name by concatenating operation details with a separator.


30-34: LGTM!

The function correctly splits an entitlement name into its components.


40-42: LGTM!

The function correctly returns the resource type for spaces.


44-75: LGTM!

The function correctly lists all spaces as resource objects, handles pagination, and converts spaces to resources. Error handling and rate limit annotations are correctly implemented.


78-131: LGTM!

The function correctly retrieves entitlements for a given resource, handles pagination, and converts permissions to entitlements. Error handling, rate limit annotations, and logging are appropriately implemented.


134-187: LGTM!

The function correctly retrieves grants for a given resource, handles pagination, and converts permissions to grants. Error handling and rate limit annotations are correctly implemented.


190-207: LGTM!

The function correctly grants a permission to a principal. Error handling and rate limit annotations are correctly implemented.


209-225: LGTM!

The function correctly revokes a permission from a principal. Error handling and rate limit annotations are correctly implemented.


227-231: LGTM!

The function correctly creates a new spaceBuilder instance.


233-244: LGTM!

The function correctly converts a Confluence space to a resource. Error handling is correctly implemented.

pkg/connector/client/confluence.go (16)

Line range hint 76-85:
LGTM!

The function correctly verifies the current user by retrieving user information and checking for validity. Error handling is correctly implemented.


Line range hint 109-122:
LGTM!

The function correctly retrieves groups with pagination, handles pagination, and converts the response to a list of groups. Error handling and rate limit annotations are correctly implemented.


Line range hint 178-191:
LGTM!

The function correctly adds a user to a group by constructing the URL, preparing the request body, and sending a POST request. Error handling and rate limit annotations are correctly implemented.


Line range hint 208-217:
LGTM!

The function correctly removes a user from a group by constructing the URL and sending a DELETE request. Error handling and rate limit annotations are correctly implemented.


226-234: LGTM!

The function correctly increments a pagination token by converting it to an integer, incrementing it by the count, and returning the new token as a string. Error handling is correctly implemented.


237-242: LGTM!

The function correctly converts a string to an integer and returns 0 if there's an error. Error handling is correctly implemented.


245-273: LGTM!

The function correctly retrieves spaces with pagination, handles pagination, and converts the response to a list of spaces. Error handling and rate limit annotations are correctly implemented.


276-314: LGTM!

The function correctly retrieves operations for a specific space with pagination, handles pagination, and converts the response to a list of operations. Error handling, rate limit annotations, and logging are correctly implemented.


317-349: LGTM!

The function correctly retrieves permissions for a specific space with pagination, handles pagination, and converts the response to a list of permissions. Error handling and rate limit annotations are correctly implemented.


352-363: LGTM!

The function correctly maps principal types to subject types and returns an error for unsupported types.


366-418: LGTM!

The function correctly adds a permission to a space by constructing the URL, preparing the request body, and sending a POST request. Error handling and rate limit annotations are correctly implemented.


421-472: LGTM!

The function correctly finds a space permission by listing all permissions. Error handling and rate limit annotations are correctly implemented.


475-504: LGTM!

The function correctly retrieves a space by its ID. Error handling and rate limit annotations are correctly implemented.


507-557: LGTM!

The function correctly removes a permission from a space by finding the permission and the space, constructing the URL, and sending a DELETE request. Error handling and rate limit annotations are correctly implemented.


560-567: LGTM!

The function correctly parses the URL and extracts the pagination cursor.


Line range hint 570-606:
LGTM!

The function correctly retrieves users using a search query with pagination, handles pagination, and converts the response to a list of users. Error handling and rate limit annotations are correctly implemented.

Comment on lines 16 to 24
func WithConfluenceRatelimitData(resource *v2.RateLimitDescription) uhttp.DoOption {
return func(response *uhttp.WrapperResponse) error {
rateLimitData, err := helpers.ExtractRateLimitData(response.StatusCode, &response.Header)
if err != nil {
return err
}
resource = rateLimitData
return nil
}
Copy link

@coderabbitai coderabbitai bot Aug 6, 2024

Choose a reason for hiding this comment

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

Pass the resource parameter by reference.

The resource parameter should be passed by reference to ensure the extracted rate limit data is correctly assigned.

- func WithConfluenceRatelimitData(resource *v2.RateLimitDescription) uhttp.DoOption {
+ func WithConfluenceRatelimitData(resource **v2.RateLimitDescription) uhttp.DoOption {
	return func(response *uhttp.WrapperResponse) error {
		rateLimitData, err := helpers.ExtractRateLimitData(response.StatusCode, &response.Header)
		if err != nil {
			return err
		}
- 		resource = rateLimitData
+ 		*resource = rateLimitData
		return nil
	}
}
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func WithConfluenceRatelimitData(resource *v2.RateLimitDescription) uhttp.DoOption {
return func(response *uhttp.WrapperResponse) error {
rateLimitData, err := helpers.ExtractRateLimitData(response.StatusCode, &response.Header)
if err != nil {
return err
}
resource = rateLimitData
return nil
}
func WithConfluenceRatelimitData(resource **v2.RateLimitDescription) uhttp.DoOption {
return func(response *uhttp.WrapperResponse) error {
rateLimitData, err := helpers.ExtractRateLimitData(response.StatusCode, &response.Header)
if err != nil {
return err
}
*resource = rateLimitData
return nil
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can't change the function definition without breaking other stuff. I think it's fine to set the attributes on resource directly. What would that code look like?

Copy link

Choose a reason for hiding this comment

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

@ggreer, here's how you can set the attributes on the resource directly without changing the function definition:

func WithConfluenceRatelimitData(resource *v2.RateLimitDescription) uhttp.DoOption {
	return func(response *uhttp.WrapperResponse) error {
		rateLimitData, err := helpers.ExtractRateLimitData(response.StatusCode, &response.Header)
		if err != nil {
			return err
		}
		*resource = *rateLimitData
		return nil
	}
}

This code sets the attributes of resource directly by dereferencing both resource and rateLimitData and assigning the values.

if err != nil {
return err
}
resource = rateLimitData
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Instead of making response a double pointer, it's probably easier to set the attributes on the struct one by one.

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 89726a1 and 004c88d.

Files selected for processing (1)
  • pkg/connector/client/ratelimit.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • pkg/connector/client/ratelimit.go

@ggreer ggreer merged commit 0d6963f into main Aug 6, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants