Skip to content

Instantly share code, notes, and snippets.

@HauptJ
Created February 12, 2026 01:52
Show Gist options
  • Select an option

  • Save HauptJ/774d8a11ee2e13399b608872bdba060b to your computer and use it in GitHub Desktop.

Select an option

Save HauptJ/774d8a11ee2e13399b608872bdba060b to your computer and use it in GitHub Desktop.
/*
Using repository_access_token_test.go as an example generate integration test cases for the following issues functions;
Gets, Get, Create, Update, Delete.
*/
package tests
import (
"fmt"
"testing"
"github.com/ktrysmt/go-bitbucket"
"github.com/stretchr/testify/assert"
)
Note: Run test with test_setup.go for util function dependencies
go test tests/issues_test.go tests/test_setup.go
Prerequisites:
- The repository must have the issue tracker enabled (has_issues = true)
- Set environment variables: BITBUCKET_TEST_OWNER, BITBUCKET_TEST_REPOSLUG, BITBUCKET_TEST_ACCESS_TOKEN
func TestCreateGetUpdateAndDeleteIssue(t *testing.T) {
client, err := SetupBearerToken(t)
if err != nil {
t.Error(err)
return
}
issue := &bitbucket.IssuesOptions{
Owner: ownerEnv,
RepoSlug: repoEnv,
Title: "Test Issue - Integration Test",
Content: "This is a test issue created by integration tests.",
Kind: "bug",
Priority: "major",
}
testIssueApiCalls(t, client, issue)
}
func TestCreateGetUpdateAndDeleteIssueWithAllFields(t *testing.T) {
client, err := SetupBearerToken(t)
if err != nil {
t.Error(err)
return
}
issue := &bitbucket.IssuesOptions{
Owner: ownerEnv,
RepoSlug: repoEnv,
Title: "Test Issue - Full Fields",
Content: "This is a test issue with all fields populated.",
Kind: "enhancement",
Priority: "critical",
State: "new",
}
testIssueApiCalls(t, client, issue)
}
func TestGetsIssues(t *testing.T) {
client, err := SetupBearerToken(t)
if err != nil {
t.Error(err)
return
}
opts := &bitbucket.IssuesOptions{
Owner: ownerEnv,
RepoSlug: repoEnv,
}
res, err := client.Repositories.Issues.Gets(opts)
if err != nil {
t.Errorf("Failed to get issues list: %v", err)
return
}
assert.NotNil(t, res, "Issues list should not be nil")
}
func TestGetsIssuesWithStatesFilter(t *testing.T) {
client, err := SetupBearerToken(t)
if err != nil {
t.Error(err)
return
}
opts := &bitbucket.IssuesOptions{
Owner: ownerEnv,
RepoSlug: repoEnv,
States: []string{"new", "open"},
}
res, err := client.Repositories.Issues.Gets(opts)
if err != nil {
t.Errorf("Failed to get issues list with states filter: %v", err)
return
}
assert.NotNil(t, res, "Issues list should not be nil")
}
func TestGetsIssuesWithQuery(t *testing.T) {
client, err := SetupBearerToken(t)
if err != nil {
t.Error(err)
return
}
opts := &bitbucket.IssuesOptions{
Owner: ownerEnv,
RepoSlug: repoEnv,
Query: "priority=\"major\"",
}
res, err := client.Repositories.Issues.Gets(opts)
if err != nil {
t.Errorf("Failed to get issues list with query: %v", err)
return
}
assert.NotNil(t, res, "Issues list should not be nil")
}
func testIssueApiCalls(t *testing.T, c *bitbucket.Client, issueOpts *bitbucket.IssuesOptions) {
// 1. Create Issue
createRes, err := c.Repositories.Issues.Create(issueOpts)
if err != nil {
t.Errorf("Failed to create issue: %v", err)
return
}
assert.NotNil(t, createRes, "Created issue should not be nil")
// Extract issue ID from response
issueMap, ok := createRes.(map[string]interface{})
if !ok {
t.Error("Failed to parse create response as map")
return
}
issueID, ok := issueMap["id"].(float64)
if !ok {
t.Error("Failed to extract issue ID from create response")
return
}
issueIDStr := fmt.Sprintf("%d", int(issueID))
// 2. Get Issue
getOpts := &bitbucket.IssuesOptions{
Owner: issueOpts.Owner,
RepoSlug: issueOpts.RepoSlug,
ID: issueIDStr,
}
getRes, err := c.Repositories.Issues.Get(getOpts)
if err != nil {
t.Errorf("Failed to get issue: %v", err)
// Attempt cleanup even if get fails
cleanupIssue(t, c, getOpts)
return
}
assert.NotNil(t, getRes, "Retrieved issue should not be nil")
getResMap, ok := getRes.(map[string]interface{})
if ok {
assert.Equal(t, issueOpts.Title, getResMap["title"], "Issue title should match")
}
// 3. Update Issue
updateOpts := &bitbucket.IssuesOptions{
Owner: issueOpts.Owner,
RepoSlug: issueOpts.RepoSlug,
ID: issueIDStr,
Title: "Updated Test Issue - Integration Test",
State: "open",
Priority: "critical",
}
updateRes, err := c.Repositories.Issues.Update(updateOpts)
if err != nil {
t.Errorf("Failed to update issue: %v", err)
// Attempt cleanup even if update fails
cleanupIssue(t, c, getOpts)
return
}
assert.NotNil(t, updateRes, "Updated issue should not be nil")
updateResMap, ok := updateRes.(map[string]interface{})
if ok {
assert.Equal(t, updateOpts.Title, updateResMap["title"], "Updated issue title should match")
}
// 4. Verify update by getting the issue again
verifyRes, err := c.Repositories.Issues.Get(getOpts)
if err != nil {
t.Errorf("Failed to verify updated issue: %v", err)
cleanupIssue(t, c, getOpts)
return
}
verifyResMap, ok := verifyRes.(map[string]interface{})
if ok {
assert.Equal(t, updateOpts.Title, verifyResMap["title"], "Verified issue title should match update")
}
// 5. Delete Issue
deleteOpts := &bitbucket.IssuesOptions{
Owner: issueOpts.Owner,
RepoSlug: issueOpts.RepoSlug,
ID: issueIDStr,
}
_, err = c.Repositories.Issues.Delete(deleteOpts)
if err != nil {
t.Errorf("Failed to delete issue: %v", err)
return
}
// 6. Verify deletion by attempting to get the deleted issue (should fail)
_, err = c.Repositories.Issues.Get(getOpts)
assert.NotNil(t, err, "Getting deleted issue should return an error")
}
// cleanupIssue attempts to delete an issue, logging any errors but not failing the test
func cleanupIssue(t *testing.T, c *bitbucket.Client, opts *bitbucket.IssuesOptions) {
_, err := c.Repositories.Issues.Delete(opts)
if err != nil {
t.Logf("Warning: Failed to cleanup issue %s: %v", opts.ID, err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment