Back

Step by Step Guide to Building a BitBucket API Integration in Go

Aug 7, 20246 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of BitBucket API integration using Go? You're in for a treat! We'll be using the awesome go-bitbucket package to make our lives easier. Let's get cracking!

Prerequisites

Before we jump in, make sure you've got:

  • Go installed on your machine (you're a Go dev, right?)
  • A BitBucket account with API credentials (if you don't have these, go grab 'em real quick)

Setting up the project

First things first, let's get our project set up:

mkdir bitbucket-api-project cd bitbucket-api-project go mod init bitbucket-api-project go get github.com/ktrysmt/go-bitbucket

Easy peasy! Now we're ready to rock and roll.

Authenticating with BitBucket API

Let's start by creating a client and setting up our credentials:

import ( "github.com/ktrysmt/go-bitbucket" ) func main() { client := bitbucket.NewBasicAuth("YOUR_USERNAME", "YOUR_APP_PASSWORD") }

Replace those placeholders with your actual credentials, and you're good to go!

Basic API Operations

Now for the fun part - let's interact with the API:

Fetching repository information

repo, err := client.Repositories.Repository.Get(&bitbucket.RepositoryOptions{ Owner: "owner", RepoSlug: "repo-name", }) if err != nil { // Handle error } fmt.Printf("Repo: %+v\n", repo)

Creating a new repository

newRepo, err := client.Repositories.Repository.Create(&bitbucket.RepositoryOptions{ Owner: "owner", RepoSlug: "new-repo-name", IsPrivate: "true", }) if err != nil { // Handle error } fmt.Printf("New repo created: %+v\n", newRepo)

Working with Pull Requests

Pull requests are the bread and butter of collaborative coding. Here's how to handle them:

Listing pull requests

prs, err := client.Repositories.PullRequests.List(&bitbucket.PullRequestsOptions{ Owner: "owner", RepoSlug: "repo-name", }) if err != nil { // Handle error } for _, pr := range prs.PullRequests { fmt.Printf("PR: %s\n", pr.Title) }

Creating a pull request

newPR, err := client.Repositories.PullRequests.Create(&bitbucket.PullRequestsOptions{ Owner: "owner", RepoSlug: "repo-name", Title: "My awesome new feature", SourceBranch: "feature-branch", DestinationBranch: "main", }) if err != nil { // Handle error } fmt.Printf("New PR created: %+v\n", newPR)

Managing Issues

Issues are crucial for tracking work. Let's see how to handle them:

Fetching issues

issues, err := client.Repositories.Issues.List(&bitbucket.IssuesOptions{ Owner: "owner", RepoSlug: "repo-name", }) if err != nil { // Handle error } for _, issue := range issues.Issues { fmt.Printf("Issue: %s\n", issue.Title) }

Creating an issue

newIssue, err := client.Repositories.Issues.Create(&bitbucket.IssuesOptions{ Owner: "owner", RepoSlug: "repo-name", Title: "Bug: Something's not right", Content: "Here's a detailed description of the bug...", }) if err != nil { // Handle error } fmt.Printf("New issue created: %+v\n", newIssue)

Webhooks

Webhooks are super useful for real-time updates. Here's a quick example:

webhook, err := client.Repositories.Webhooks.Create(&bitbucket.WebhooksOptions{ Owner: "owner", RepoSlug: "repo-name", URL: "https://your-webhook-endpoint.com", Events: []string{"repo:push", "issue:created"}, }) if err != nil { // Handle error } fmt.Printf("Webhook created: %+v\n", webhook)

Error Handling and Best Practices

Always check for errors after API calls. It's a good habit:

if err != nil { log.Fatalf("API call failed: %v", err) }

And don't forget about rate limiting! Be a good API citizen:

time.Sleep(time.Second) // Simple rate limiting

Conclusion

And there you have it! You're now equipped to integrate BitBucket into your Go projects like a pro. Remember, this is just scratching the surface - there's so much more you can do with the BitBucket API.

Keep exploring, keep coding, and most importantly, have fun! If you get stuck, the go-bitbucket docs and BitBucket API docs are your best friends. Happy coding!