簡體   English   中英

通過REST在Java中創建Bitbucket存儲庫

[英]Creating a Bitbucket-Repository in Java via REST

我正在嘗試使用他們的REST-API創建一個Bitbucket存儲庫。 除了設置需要在其中創建存儲庫的“父”項目之外,其他所有事情似乎都可以正常工作。在鏈接上提供了一個cURL示例。 在主體中,參數“ scm”設置為“ git”或“ hg”,均為字符串,參數“ project”似乎是一個包含鍵值對的json對象。 到目前為止,我嘗試的所有操作均無效(json對象,字符串等)

問題:如何在特定項目中創建存儲庫?

我的代碼如下所示:

RestTemplate restTemplate = new RestTemplate();
String url = "https://api.bitbucket.org/2.0/repositories/" + tName + "/" + rName;
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Basic 1234567890qwert");
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

MultiValueMap<String, String> project = new LinkedMultiValueMap();
project.add("key", "aaaaaaaa"); //the repo should be created in the project aaaaaaaa

MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
postParameters.add("scm", "hg"); //hg or git, does not matter
postParameters.add("project", project); //<-- the api ignores the declared project

HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(body, headers);

ResponseEntity<BitbucketRepository> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, BitbucketRepository.class);

System.out.println("createRepository: " + response);
return response;

您可以像這樣使用bitbucket-rest庫來做到這一點:

BitbucketClient client = BitbucketClient.builder()
.endPoint("http://127.0.0.1:7990")
.credentials("admin:password") // will base64 for you if not already done. Can optionally use token auth as well.
.build();

CreateRepository crepo = CreateRepository.create("my-repo", true);
Repository repo = client.api().repositoryApi().create("my-project", crepo);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM