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

Add support for Bitbucket Self-Hosted URL #118

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,15 @@
"help_text": "(Optional) Set to lock the plugin to a single Bitbucket organization.",
"placeholder": "",
"default": null
},
{
"key": "BitbucketSelfHostedUrl",
"display_name": "Bitbucket Self-hosted Url",
"type": "text",
"help_text": "(Optional) Set to change the default bitbucket URL to a self-hosted one.",
panoramix360 marked this conversation as resolved.
Show resolved Hide resolved
"placeholder": "",
"default": null
}
]
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that all files should end in a newline. This red indicator shows that there is not one here. If you use VSCode, you can configure the editor to do this automatically on save with these settings:

    "files.insertFinalNewline": true,
    "files.trimFinalNewlines": true,

Copy link
Author

Choose a reason for hiding this comment

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

Oh, thank you! ;) fixed!

Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like this file and docker-compose.yml both are missing final new line

5 changes: 3 additions & 2 deletions server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ func (p *Plugin) handleSubscribe(_ *plugin.Context, args *model.CommandArgs, par

ctx := context.Background()
bitbucketClient := p.bitbucketConnect(*userInfo.Token)
owner, repo := parseOwnerAndRepo(parameters[0], BitbucketBaseURL)
bitbucketURL := p.getBitbucketBaseURL()
owner, repo := parseOwnerAndRepo(parameters[0], bitbucketURL)
previousSubscribedEvents, err := p.findSubscriptionsEvents(args.ChannelId, owner, repo)
if err != nil {
return err.Error()
Expand All @@ -243,7 +244,7 @@ func (p *Plugin) handleSubscribe(_ *plugin.Context, args *model.CommandArgs, par
return err.Error()
}

repoLink := fmt.Sprintf("%s%s/%s", p.getBaseURL(), owner, repo)
repoLink := fmt.Sprintf("%s%s/%s", bitbucketURL, owner, repo)
panoramix360 marked this conversation as resolved.
Show resolved Hide resolved

msg := fmt.Sprintf("Successfully subscribed to [%s/%s](%s) with events: %s", owner, repo, repoLink, formattedString(features))
if previousSubscribedEvents != "" {
Expand Down
1 change: 1 addition & 0 deletions server/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Configuration struct {
BitbucketOrg string
BitbucketOAuthClientID string
BitbucketOAuthClientSecret string
BitbucketSelfHostedUrl string
panoramix360 marked this conversation as resolved.
Show resolved Hide resolved
WebhookSecret string
EncryptionKey string
}
Expand Down
8 changes: 8 additions & 0 deletions server/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ const manifestStr = `
"help_text": "(Optional) Set to lock the plugin to a single Bitbucket organization.",
"placeholder": "",
"default": null
},
{
"key": "BitbucketSelfHostedUrl",
"display_name": "Bitbucket Self-hosted Url",
"type": "text",
"help_text": "(Optional) Set to change the default bitbucket URL to a self-hosted one.",
"placeholder": "",
"default": null
}
]
}
Expand Down
30 changes: 21 additions & 9 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,10 @@ func (p *Plugin) OnActivate() error {
func (p *Plugin) getOAuthConfig() *oauth2.Config {
config := p.getConfiguration()

authURL, _ := url.Parse(BitbucketBaseURL)
tokenURL, _ := url.Parse(BitbucketBaseURL)
bitbucketURL := p.getBitbucketBaseURL()

authURL, _ := url.Parse(bitbucketURL)
tokenURL, _ := url.Parse(bitbucketURL)
authURL.Path = path.Join(authURL.Path, "site", "oauth2", "authorize")
tokenURL.Path = path.Join(tokenURL.Path, "site", "oauth2", "access_token")

Expand Down Expand Up @@ -303,6 +305,8 @@ func (p *Plugin) PostToDo(info *BitbucketUserInfo) {
}

func (p *Plugin) GetToDo(ctx context.Context, userInfo *BitbucketUserInfo, bitbucketClient *bitbucket.APIClient) (string, error) {
bitbucketURL := p.getBitbucketBaseURL()

userRepos, err := p.getUserRepositories(ctx, bitbucketClient)
if err != nil {
return "", errors.Wrap(err, "error occurred while searching for repositories")
Expand Down Expand Up @@ -331,7 +335,7 @@ func (p *Plugin) GetToDo(ctx context.Context, userInfo *BitbucketUserInfo, bitbu
text += fmt.Sprintf("You have %v assignments:\n", len(yourAssignments))

for _, assign := range yourAssignments {
text += getToDoDisplayText(BitbucketBaseURL, assign.Title, assign.Links.Html.Href, "")
text += getToDoDisplayText(bitbucketURL, assign.Title, assign.Links.Html.Href, "")
}
}

Expand All @@ -343,7 +347,7 @@ func (p *Plugin) GetToDo(ctx context.Context, userInfo *BitbucketUserInfo, bitbu
text += fmt.Sprintf("You have %v pull requests awaiting your review:\n", len(assignedPRs))

for _, assign := range assignedPRs {
text += getToDoDisplayText(BitbucketBaseURL, assign.Title, assign.Links.Html.Href, "")
text += getToDoDisplayText(bitbucketURL, assign.Title, assign.Links.Html.Href, "")
}
}

Expand All @@ -355,7 +359,7 @@ func (p *Plugin) GetToDo(ctx context.Context, userInfo *BitbucketUserInfo, bitbu
text += fmt.Sprintf("You have %v open pull requests:\n", len(yourOpenPrs))

for _, assign := range yourOpenPrs {
text += getToDoDisplayText(BitbucketBaseURL, assign.Title, assign.Links.Html.Href, "")
text += getToDoDisplayText(bitbucketURL, assign.Title, assign.Links.Html.Href, "")
}
}

Expand Down Expand Up @@ -587,10 +591,6 @@ func (p *Plugin) sendRefreshEvent(userID string) {
)
}

func (p *Plugin) getBaseURL() string {
return "https://bitbucket.org/"
}

// getBitBucketAccountIDToMattermostUsernameMapping maps a BitBucket account ID to the corresponding Mattermost username, if any.
func (p *Plugin) getBitBucketAccountIDToMattermostUsernameMapping(bitbucketAccountID string) string {
user, _ := p.API.GetUser(p.getBitbucketAccountIDToMattermostUserIDMapping(bitbucketAccountID))
Expand Down Expand Up @@ -661,3 +661,15 @@ func (p *Plugin) getUsername(mmUserID string) (string, error) {

return "@" + info.BitbucketUsername, nil
}

// getBitbucketBaseURl returns the base URL from the configuration
panoramix360 marked this conversation as resolved.
Show resolved Hide resolved
// if there is a Self Hosted URL configured it returns it
// if not it will return the bitbucket base URL
panoramix360 marked this conversation as resolved.
Show resolved Hide resolved
func (p *Plugin) getBitbucketBaseURL() string {
config := p.getConfiguration()

if config.BitbucketSelfHostedUrl != "" {
return config.BitbucketSelfHostedUrl
}
return BitbucketBaseURL
}
2 changes: 1 addition & 1 deletion server/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (p *Plugin) Unsubscribe(channelID, repo string) (string, error) {
return requiredErrorMessage, nil
}

owner, repo := parseOwnerAndRepo(repo, p.getBaseURL())
owner, repo := parseOwnerAndRepo(repo, p.getBitbucketBaseURL())
if owner == "" && repo == "" {
return requiredErrorMessage, nil
}
Expand Down
2 changes: 1 addition & 1 deletion server/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (p *Plugin) executeHandlers(webhookHandlers []*webhook.HandleWebhook, pl we
}

func (p *Plugin) permissionToRepo(userID string, ownerAndRepo string) bool {
_, owner, repo := parseOwnerAndRepoAndReturnFullAlso(ownerAndRepo, p.getBaseURL())
_, owner, repo := parseOwnerAndRepoAndReturnFullAlso(ownerAndRepo, p.getBitbucketBaseURL())

if owner == "" {
return false
Expand Down
8 changes: 8 additions & 0 deletions webapp/src/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ const manifest = JSON.parse(`
"help_text": "(Optional) Set to lock the plugin to a single Bitbucket organization.",
"placeholder": "",
"default": null
},
{
"key": "BitbucketSelfHostedUrl",
"display_name": "Bitbucket Self-hosted Url",
"type": "text",
"help_text": "(Optional) Set to change the default bitbucket URL to a self-hosted one.",
"placeholder": "",
"default": null
}
]
}
Expand Down