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

[Bitbucket Server] Implements the GetMe method #128

Open
wants to merge 19 commits into
base: bitbucket-server
Choose a base branch
from

Conversation

panoramix360
Copy link

@panoramix360 panoramix360 commented Nov 10, 2023

Summary

This is the first PR to begin the implementation of the Bitbucket Server in the Plugin.

It creates the client_server.go to integrate the Bitbucket Server methods to the Plugin, I implemented the first one GetMe this one does two requests to complete:

  • Gets the logged Bitbucket User using an OAuth endpoint
  • Uses the logged user name to GetUser using the go-bitbucket-v1 library

This flow is needed because the Bitbucket Server API doesn't have the same methods of the Bitbucket Cloud like /user that already accounts for the logged user.

Ticket Link

Github Issue

@mattermost-build
Copy link

Hello @panoramix360,

Thanks for your pull request! A Core Committer will review your pull request soon. For code contributions, you can learn more about the review process here.

@codecov-commenter
Copy link

codecov-commenter commented Nov 16, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

❗ No coverage uploaded for pull request base (bitbucket-server@3be0785). Click here to learn what that means.

Additional details and impacted files
@@                 Coverage Diff                 @@
##             bitbucket-server     #128   +/-   ##
===================================================
  Coverage                    ?   15.58%           
===================================================
  Files                       ?       15           
  Lines                       ?     2439           
  Branches                    ?        0           
===================================================
  Hits                        ?      380           
  Misses                      ?     2035           
  Partials                    ?       24           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@mickmister mickmister left a comment

Choose a reason for hiding this comment

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

This is looking great @panoramix360! I left some comments on relocating BitBucket Server code to its own package, and managing the token for the client via the oauth2.Client instead of creating a http.Client directly. Otherwise LGTM 👍

server/client_server.go Outdated Show resolved Hide resolved
server/client_server.go Outdated Show resolved Hide resolved
server/client_server.go Outdated Show resolved Hide resolved
server/client_server.go Outdated Show resolved Hide resolved
server/client_server.go Outdated Show resolved Hide resolved
server/client_server.go Outdated Show resolved Hide resolved
}

w.WriteHeader(http.StatusOK)
w.Write([]byte("mocked-user"))
Copy link
Contributor

Choose a reason for hiding this comment

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

You can do this to intentionally ignore the error, and avoid the lint issue:

Suggested change
w.Write([]byte("mocked-user"))
_ = w.Write([]byte("mocked-user"))

Comment on lines 13 to 21
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authHeader := r.Header.Get("Authorization")
if authHeader != "Bearer valid-token" {
w.WriteHeader(http.StatusUnauthorized)
return
}

w.WriteHeader(http.StatusOK)
w.Write([]byte("mocked-user"))
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we have a similar test for the GetUser call? Maybe have one main "test" for ourBitbucketClient.GetMe, and have one of the test cases be a success case that returns a real user from the http server. Nice testing!

@mattermost-build
Copy link

This PR has been automatically labelled "stale" because it hasn't had recent activity.
A core team member will check in on the status of the PR to help with questions.
Thank you for your contribution!

@panoramix360
Copy link
Author

hey @mickmister

I added a Factory Pattern as we discussed, I think this approach is cleaner, can you review it again?

Meanwhile, I'll take a look and solve the other comments.

Can you take a look at some comments that I made as well?

Thanks!

server/client_server.go Outdated Show resolved Hide resolved
Comment on lines 9 to 14
func GetBitbucketClient(clientType string, selfHostedURL string, selfHostedAPIURL string, apiClient *bitbucketv1.APIClient) (Client, error) {
if clientType == "server" {
return newServerClient(selfHostedURL, selfHostedAPIURL, apiClient), nil
}
return nil, fmt.Errorf("wrong client passed")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

If this package only creates a Bitbucket Server client, and not a Bitbucket Cloud client, what benefit do we get from having this clientType parameter?

Copy link
Author

@panoramix360 panoramix360 Dec 5, 2023

Choose a reason for hiding this comment

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

Oh, the idea is to create the Bitbucket Cloud as well! I didn't insert this yet because we plan to do this more consistently on this issue.

But if you want, I can add the condition here. Let me know what you think!

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the function to create the BB Cloud client will be in a separate package though like bitbucket_cloud. What do you think? We'll see how the code feels when instantiating either client from the main package

Copy link
Author

Choose a reason for hiding this comment

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

We can change the name of this package to just package bitbucket and have both on the same package.

so we have:

  • client.go: generic client with the necessary methods
  • client_server: concrete server client with the implementation of the Bitbucket Server
  • client_cloud: concrete cloud client with the implementation of the Bitbucket Cloud

What do you think of this approach?

Copy link
Contributor

Choose a reason for hiding this comment

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

If we think that everything to do with Bitbucket Server can/should fit in its own file, then this works well. But if there are multiple files for each client type, I think we should have separate packages for each client type. What do you think?

So either this that I think you're suggesting

client := bitbucket.NewBBServerClient()

or

client := bitbucket_server.NewClient()

server/client_server.go Outdated Show resolved Hide resolved
server/client_server.go Outdated Show resolved Hide resolved
@mickmister mickmister self-requested a review January 9, 2024 17:03
@mickmister
Copy link
Contributor

Hi @panoramix360, this is looking good 👍

Are you able to resolve the lint issues CI is reporting? Thank you!

@panoramix360
Copy link
Author

hey @mickmister, done!

I needed to change the name of the package, and it made more sense to call it bitbucketclient since it will have BitbucketServer and BitbucketCloud implementations.

Copy link
Contributor

@mickmister mickmister left a comment

Choose a reason for hiding this comment

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

LGTM 👍 Just some comments for discussion. Great work @panoramix360!!

Comment on lines +11 to +12
SelfHostedURL string
SelfHostedAPIURL string
Copy link
Contributor

Choose a reason for hiding this comment

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

I've been thinking that this SelfHostedAPIURL is derived from SelfHostedURL (we just add some suffix to it), so maybe we can have just one config setting SelfHostedURL, and derive SelfHostedAPIURL at runtime?

Comment on lines +19 to +24
func GetBitbucketClient(clientType string, config ClientConfiguration) (Client, error) {
if clientType == "server" {
return newServerClient(config), nil
}
return nil, fmt.Errorf("wrong client passed")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Just an idea, but I'm thinking having two different functions for creating the two clients may be more Go-idiomatic. So having a NewBBServerClient() function for instance to create the BB Server client

BitbucketClient
}

func newServerClient(config ClientConfiguration) Client {
Copy link
Contributor

Choose a reason for hiding this comment

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

So we would make this function capitalized/exported if we want to expose it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants