Skip to content

Commit 3b8dd11

Browse files
authored
Fix issue with fork_policy not being updated (#168)
Due to an undocumented quirk in Bitbucket's API, when updating a repository, you have to pass in two additional fields which are then used together, to match the string representation of the fork_policy (which the API returns - the other two fields are not returned). Here is a link to an oustanding bug ticket filed with Atlassian: https://jira.atlassian.com/browse/BCLOUD-13093 Also added tests to cover the behaviour above, as well as an additional one for creating a repository, to ensure this functionality was not broken with the changes introduced.
1 parent 379c28d commit 3b8dd11

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed

repository.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,20 @@ func (r *Repository) buildRepositoryBody(ro *RepositoryOptions) string {
774774
}
775775
if ro.ForkPolicy != "" {
776776
body["fork_policy"] = ro.ForkPolicy
777+
778+
// Due to this undocumented asymmetric behaviour (https://jira.atlassian.com/browse/BCLOUD-13093)
779+
// we have to do this, to allow `fork_policy` to be updated after initial creation (i.e. PUT/POST requests)
780+
switch ro.ForkPolicy {
781+
case "allow_forks":
782+
body["no_forks"] = false
783+
body["no_public_forks"] = false
784+
case "no_public_forks":
785+
body["no_forks"] = false
786+
body["no_public_forks"] = true
787+
case "no_forks":
788+
body["no_forks"] = true
789+
body["no_public_forks"] = true
790+
}
777791
}
778792
if ro.Language != "" {
779793
body["language"] = ro.Language

tests/repository_test.go

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
_ "github.com/k0kubun/pp"
8+
89
"github.com/ktrysmt/go-bitbucket"
910
)
1011

@@ -45,6 +46,149 @@ func TestGetRepositoryRepositories(t *testing.T) {
4546
}
4647
}
4748

49+
func TestCreateRepositoryRepositories(t *testing.T) {
50+
user := os.Getenv("BITBUCKET_TEST_USERNAME")
51+
pass := os.Getenv("BITBUCKET_TEST_PASSWORD")
52+
owner := os.Getenv("BITBUCKET_TEST_OWNER")
53+
54+
if user == "" {
55+
t.Error("BITBUCKET_TEST_USERNAME is empty.")
56+
}
57+
if pass == "" {
58+
t.Error("BITBUCKET_TEST_PASSWORD is empty.")
59+
}
60+
if owner == "" {
61+
t.Error("BITBUCKET_TEST_OWNER is empty.")
62+
}
63+
64+
c := bitbucket.NewBasicAuth(user, pass)
65+
66+
// Create project - needed prior to creating repo
67+
projOpt := &bitbucket.ProjectOptions{
68+
Owner: owner,
69+
Name: "go-bitbucket-test-project",
70+
Key: "GO_BB_TEST_PROJECT",
71+
IsPrivate: true,
72+
}
73+
project, err := c.Workspaces.CreateProject(projOpt)
74+
if err != nil {
75+
t.Error("The project could not be created.", err)
76+
}
77+
78+
repoSlug := "go-bb-test-repo-create"
79+
forkPolicy := "no_forks"
80+
repoOpt := &bitbucket.RepositoryOptions{
81+
Owner: owner,
82+
RepoSlug: repoSlug,
83+
ForkPolicy: forkPolicy,
84+
Project: project.Key,
85+
IsPrivate: "true",
86+
}
87+
88+
res, err := c.Repositories.Repository.Create(repoOpt)
89+
if err != nil {
90+
t.Error("The project could not be created.", err)
91+
}
92+
93+
if res.Full_name != owner+"/"+repoSlug {
94+
t.Error("The repository `Full_name` attribute does not match the expected value.")
95+
}
96+
if res.Fork_policy != forkPolicy {
97+
t.Error("The repository `Fork_policy` attribute does not match the expected value.")
98+
}
99+
100+
// Clean up
101+
_, err = c.Repositories.Repository.Delete(repoOpt)
102+
if err != nil {
103+
t.Error("The repository could not be deleted.", err)
104+
}
105+
106+
_, err = c.Workspaces.DeleteProject(projOpt)
107+
if err != nil {
108+
t.Error("The project could not be deleted.", err)
109+
}
110+
}
111+
112+
func TestRepositoryUpdateForkPolicy(t *testing.T) {
113+
user := os.Getenv("BITBUCKET_TEST_USERNAME")
114+
pass := os.Getenv("BITBUCKET_TEST_PASSWORD")
115+
owner := os.Getenv("BITBUCKET_TEST_OWNER")
116+
repo := os.Getenv("BITBUCKET_TEST_REPOSLUG")
117+
118+
if user == "" {
119+
t.Error("BITBUCKET_TEST_USERNAME is empty.")
120+
}
121+
if pass == "" {
122+
t.Error("BITBUCKET_TEST_PASSWORD is empty.")
123+
}
124+
if owner == "" {
125+
t.Error("BITBUCKET_TEST_OWNER is empty.")
126+
}
127+
if repo == "" {
128+
t.Error("BITBUCKET_TEST_REPOSLUG is empty.")
129+
}
130+
131+
c := bitbucket.NewBasicAuth(user, pass)
132+
133+
opt := &bitbucket.RepositoryOptions{
134+
Owner: owner,
135+
RepoSlug: repo,
136+
}
137+
138+
res, err := c.Repositories.Repository.Get(opt)
139+
if err != nil {
140+
t.Error("The repository is not found.", err)
141+
}
142+
143+
forkPolicy := "allow_forks"
144+
opt = &bitbucket.RepositoryOptions{
145+
Uuid: res.Uuid,
146+
Owner: owner,
147+
RepoSlug: res.Slug,
148+
ForkPolicy: forkPolicy,
149+
}
150+
res, err = c.Repositories.Repository.Update(opt)
151+
if err != nil {
152+
t.Error("The repository could not be updated.", err)
153+
}
154+
155+
if res.Fork_policy != forkPolicy {
156+
t.Errorf("The repository's fork_policy did not match the expected: '%s'.", forkPolicy)
157+
}
158+
159+
forkPolicy = "no_public_forks"
160+
opt = &bitbucket.RepositoryOptions{
161+
Uuid: res.Uuid,
162+
Owner: owner,
163+
RepoSlug: res.Slug,
164+
ForkPolicy: forkPolicy,
165+
}
166+
res, err = c.Repositories.Repository.Update(opt)
167+
if err != nil {
168+
t.Error("The repository could not be updated.", err)
169+
}
170+
171+
if res.Fork_policy != forkPolicy {
172+
t.Errorf("The repository's fork_policy did not match the expected: '%s'.", forkPolicy)
173+
}
174+
175+
forkPolicy = "no_forks"
176+
opt = &bitbucket.RepositoryOptions{
177+
Uuid: res.Uuid,
178+
Owner: owner,
179+
RepoSlug: res.Slug,
180+
ForkPolicy: forkPolicy,
181+
}
182+
res, err = c.Repositories.Repository.Update(opt)
183+
if err != nil {
184+
t.Error("The repository could not be updated.", err)
185+
}
186+
187+
if res.Fork_policy != forkPolicy {
188+
t.Errorf("The repository's fork_policy did not match the expected: '%s'.", forkPolicy)
189+
}
190+
}
191+
48192
func TestGetRepositoryPipelineVariables(t *testing.T) {
49193

50194
user := os.Getenv("BITBUCKET_TEST_USERNAME")

0 commit comments

Comments
 (0)