|
5 | 5 | "testing" |
6 | 6 |
|
7 | 7 | _ "github.com/k0kubun/pp" |
| 8 | + |
8 | 9 | "github.com/ktrysmt/go-bitbucket" |
9 | 10 | ) |
10 | 11 |
|
@@ -45,6 +46,149 @@ func TestGetRepositoryRepositories(t *testing.T) { |
45 | 46 | } |
46 | 47 | } |
47 | 48 |
|
| 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 | + |
48 | 192 | func TestGetRepositoryPipelineVariables(t *testing.T) { |
49 | 193 |
|
50 | 194 | user := os.Getenv("BITBUCKET_TEST_USERNAME") |
|
0 commit comments