Skip to content

Conversation

@mateoHernandez123
Copy link
Contributor

@mateoHernandez123 mateoHernandez123 commented Oct 16, 2025

Summary by CodeRabbit

  • Refactor
    • Standardized and centralized GitHub API error handling across connector components (orgs, teams, users, repos, invitations, enterprise) via a unified error wrapper, replacing ad-hoc per-path rate-limit and error branches to produce more consistent, contextualized error messages.

@coderabbitai
Copy link

coderabbitai bot commented Oct 16, 2025

Walkthrough

This PR centralizes GitHub API error handling by adding wrapGitHubError and replacing many ad-hoc error branches across connector files to consistently convert API responses and errors into gRPC status errors with contextual messages and HTTP response awareness.

Changes

Cohort / File(s) Summary
Error handling centralization
pkg/connector/helpers.go
Adds wrapGitHubError(err, resp, contextMsg) to translate GitHub API errors and responses into gRPC status errors (handles rate-limit, auth, permission, and generic cases).
Connector core & validation
pkg/connector/connector.go
Replaces granular error branches in Validate, validateAppCredentials, and New with wrapGitHubError; propagates HTTP response objects on failures.
API token handling
pkg/connector/api_token.go
Replaces rate-limit-specific handling with wrapGitHubError for ListFineGrainedPersonalAccessTokens errors.
Organization management
pkg/connector/org.go
Propagates HTTP responses and replaces per-path error checks with wrapGitHubError across list, grant, revoke, and app-installation flows.
Repository operations
pkg/connector/repository.go
Replaces direct errors with wrapGitHubError(err, resp, ...) for repo listing, GetByID, collaborator and team operations; captures resp where needed.
Team operations
pkg/connector/team.go
Standardizes error wrapping with wrapGitHubError for team listing, lookup, member listing, and membership add/remove; captures and forwards resp.
User operations
pkg/connector/user.go
Replaces ad-hoc rate-limit/generic errors with wrapGitHubError in List and Delete flows; captures resp and adjusts variable usage.
Enterprise & org roles
pkg/connector/enterprise_role.go, pkg/connector/org_role.go
Replaces isRatelimited and bespoke error formatting with wrapGitHubError in List and Grants paths; unifies error propagation.
Invitations
pkg/connector/invitation.go
Replaces fmt.Errorf returns with wrapGitHubError(err, resp, ...) for ListPendingOrgInvitations and CreateOrgInvitation errors.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant Connector as Connector code
    participant GitHub as GitHub API (client)
    participant Wrapper as wrapGitHubError
    participant gRPC as gRPC status

    Connector->>GitHub: API call (e.g., List/Get/Add)
    GitHub-->>Connector: response (resp) + error (err)
    alt err == nil
        Connector->>Connector: continue normal flow
    else err != nil
        Connector->>Wrapper: wrapGitHubError(err, resp, "context message")
        Wrapper-->>gRPC: map to status (Unavailable / Unauthenticated / PermissionDenied / Unknown)
        Wrapper-->>Connector: return gRPC error
    end

    Note over Wrapper,gRPC: Wrapper inspects resp and helpers (isRatelimited, isAuthError, isPermissionError)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 I hopped through code, a careful sweep,

Wrapping errors, tidy and neat.
One helper now sings, no branches to fear,
Responses and messages all bundled near.
A small rabbit cheer—errors now clear! 🥕

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly identifies the core change of wrapping GitHub API errors with gRPC codes and aligns directly with the modifications throughout the connector code, making it clear and specific to the main purpose of the pull request.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch mateoHernandez123/wrap-github-errors-grpc-codes

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 336fa3d and da94690.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum and included by none
📒 Files selected for processing (10)
  • pkg/connector/api_token.go (1 hunks)
  • pkg/connector/connector.go (4 hunks)
  • pkg/connector/enterprise_role.go (2 hunks)
  • pkg/connector/helpers.go (2 hunks)
  • pkg/connector/invitation.go (2 hunks)
  • pkg/connector/org.go (9 hunks)
  • pkg/connector/org_role.go (3 hunks)
  • pkg/connector/repository.go (7 hunks)
  • pkg/connector/team.go (7 hunks)
  • pkg/connector/user.go (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • pkg/connector/api_token.go
  • pkg/connector/invitation.go
🧰 Additional context used
🧬 Code graph analysis (3)
pkg/connector/helpers.go (1)
vendor/github.com/conductorone/baton-sdk/pkg/uhttp/wrapper.go (1)
  • WrapErrors (316-325)
pkg/connector/enterprise_role.go (1)
vendor/github.com/conductorone/baton-sdk/pkg/uhttp/wrapper.go (1)
  • WrapErrors (316-325)
pkg/connector/connector.go (1)
vendor/github.com/conductorone/baton-sdk/pkg/uhttp/wrapper.go (1)
  • WrapErrors (316-325)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: ci
🔇 Additional comments (14)
pkg/connector/enterprise_role.go (2)

144-144: LGTM!

Correctly wraps the GitHub API error with response context using the new wrapGitHubError helper.


63-63: No action: uhttp.WrapErrors is appropriate. ListEnterpriseConsumedLicenses returns a v2.RateLimitDescription—not a *github.Response—so wrapGitHubError isn’t applicable here.

pkg/connector/user.go (2)

136-136: LGTM!

Correctly migrates to the centralized wrapGitHubError helper for consistent error handling.


251-267: LGTM!

The error handling is now consistent with the PR's centralized pattern. The response object is correctly propagated and error context messages are descriptive.

pkg/connector/org_role.go (1)

91-91: LGTM!

All three error handling paths now use the centralized wrapGitHubError helper, providing consistent error wrapping with HTTP response awareness across list and grant operations.

Also applies to: 178-178, 225-225

pkg/connector/connector.go (2)

221-223: LGTM!

The findInstallation function now returns the response object, enabling richer error context via wrapGitHubError. This change aligns with the PR's goal of centralizing GitHub API error handling.

Also applies to: 275-277


456-456: LGTM!

Correctly replaces multiple per-status checks with the unified wrapGitHubError helper for consistent error handling in getOrgs.

pkg/connector/helpers.go (1)

255-273: LGTM!

The wrapGitHubError helper provides a clean, centralized approach to converting GitHub API errors into gRPC status errors with appropriate codes. The implementation correctly:

  • Early-returns on nil errors
  • Maps rate-limiting to Unavailable
  • Maps authentication failures to Unauthenticated
  • Maps permission denials to PermissionDenied
  • Wraps generic errors with contextual messages

This helper significantly reduces code duplication and improves consistency across the connector.

pkg/connector/team.go (2)

98-98: LGTM!

All error handling in list and grant operations now uses the centralized wrapGitHubError helper for consistent error wrapping with HTTP response awareness.

Also applies to: 109-109, 169-169, 205-205


304-314: LGTM!

The error handling is now consistent with the PR's pattern. Note that the use of variable name er (instead of error) correctly avoids shadowing the predeclared identifier, addressing the lint issues mentioned in past review comments.

Also applies to: 353-360

pkg/connector/org.go (1)

105-105: LGTM!

All GitHub API error handling in this file has been successfully migrated to use the centralized wrapGitHubError helper. The changes:

  • Propagate response objects for richer error context
  • Provide descriptive error messages
  • Eliminate ad-hoc rate limit and error handling in favor of unified wrapping
  • Maintain consistent error handling across List, Grant, and Revoke operations

This significantly improves the maintainability and consistency of error handling throughout the organization resource type.

Also applies to: 129-129, 223-223, 286-288, 301-303, 310-315, 326-328, 378-380, 383-385, 393-395, 400-402

pkg/connector/repository.go (3)

93-93: LGTM!

List operations now consistently use wrapGitHubError for unified error handling with HTTP response awareness.

Also applies to: 184-184, 237-237


296-298: LGTM!

Grant operations have been successfully migrated to use wrapGitHubError. The response objects are correctly propagated, and the use of variable name er (instead of error) avoids shadowing the predeclared identifier.

Also applies to: 316-330, 333-342


367-369: LGTM!

Revoke operations now consistently use wrapGitHubError for error handling. The use of variable name er correctly addresses the lint issue mentioned in past review comments about shadowing the predeclared identifier error.

Also applies to: 381-388, 391-398


Comment @coderabbitai help to get the list of available commands and usage tips.

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: 3

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 45165f4 and 336fa3d.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum and included by none
📒 Files selected for processing (10)
  • pkg/connector/api_token.go (1 hunks)
  • pkg/connector/connector.go (4 hunks)
  • pkg/connector/enterprise_role.go (2 hunks)
  • pkg/connector/helpers.go (2 hunks)
  • pkg/connector/invitation.go (2 hunks)
  • pkg/connector/org.go (9 hunks)
  • pkg/connector/org_role.go (3 hunks)
  • pkg/connector/repository.go (7 hunks)
  • pkg/connector/team.go (7 hunks)
  • pkg/connector/user.go (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (3)
pkg/connector/enterprise_role.go (1)
vendor/github.com/conductorone/baton-sdk/pkg/uhttp/wrapper.go (1)
  • WrapErrors (316-325)
pkg/connector/helpers.go (2)
vendor/github.com/conductorone/baton-sdk/pkg/lambda/grpc/transport.go (1)
  • Response (146-148)
vendor/github.com/conductorone/baton-sdk/pkg/uhttp/wrapper.go (1)
  • WrapErrors (316-325)
pkg/connector/connector.go (1)
vendor/github.com/conductorone/baton-sdk/pkg/uhttp/wrapper.go (1)
  • WrapErrors (316-325)
🪛 GitHub Check: go-lint
pkg/connector/repository.go

[failure] 386-386:
variable error has same name as predeclared identifier (predeclared)

pkg/connector/team.go

[failure] 304-304:
variable error has same name as predeclared identifier (predeclared)


[failure] 357-357:
variable error has same name as predeclared identifier (predeclared)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: ci
🔇 Additional comments (2)
pkg/connector/org.go (1)

103-130: Consistent GitHub error wrapping looks good

Great to see every GitHub call now funneled through wrapGitHubError, including the membership guard that still skips on 403s. This keeps the control flow unchanged while guaranteeing uniform gRPC status conversion. Nicely done.

pkg/connector/connector.go (1)

222-279: Installation error path now surfaces precise gRPC codes

Wrapping both validation and constructor installation lookups with wrapGitHubError is exactly what we needed—callers now get consistent status codes plus context without any behavior change on success. Looks solid.

@mateoHernandez123 mateoHernandez123 force-pushed the mateoHernandez123/wrap-github-errors-grpc-codes branch from 336fa3d to da94690 Compare October 16, 2025 17:05
}

if isRatelimited(resp) {
return uhttp.WrapErrors(codes.Unavailable, "too many requests", err)
Copy link

Choose a reason for hiding this comment

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

this doesn't seem right?

Copy link

Choose a reason for hiding this comment

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

like if its rate limited.... shouldn't we be returning that

Copy link
Contributor

Choose a reason for hiding this comment

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

@pquerna this is wrapping the error with the status code so baton-sdk will wait and retry

Copy link

Choose a reason for hiding this comment

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

Don't we want the rate limit metadata?

Copy link
Contributor

Choose a reason for hiding this comment

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

We do and it seems like the GitHub go SDK does expose it. I have filed a separate Jira issue for it https://conductorone.atlassian.net/browse/BB-1660

return nil
}

if isRatelimited(resp) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We could add the rate limit here from the error as per https://conductorone.atlassian.net/browse/BB-1660 or we could do it in a separate PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it is best to do a specific PR to resolve that

@mateoHernandez123 mateoHernandez123 merged commit feac0ce into main Oct 17, 2025
4 checks passed
@mateoHernandez123 mateoHernandez123 deleted the mateoHernandez123/wrap-github-errors-grpc-codes branch October 17, 2025 19:38
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.

5 participants