/**
 * 获取分支列表
 */
public   void  getBranches() throws Exception {
    Map<String,Object> user = this.getUser();
    String url =  gitIp+"/api/v3/projects/258/repository/branches";
    CloseableHttpClient httpclient = HttpClients.createDefault();
    // 创建http GET请求
    HttpGet httpGet = new HttpGet(url);
    httpGet.setHeader("PRIVATE-TOKEN", user.get("private_token")+"");
    CloseableHttpResponse response = null;
    try {
        response = httpclient.execute(httpGet);
        // 判断状态码
        if (response.getStatusLine().getStatusCode() == 200) {
            //获取返回内容
            String content = EntityUtils.toString(response.getEntity(), "UTF-8");
        }
    } finally {
        if (response != null) {
            response.close();
        }
        httpclient.close();
    }

}
/**
 * 获取固定分支
 */
public   void  getBranchesSingle() throws Exception {
    Map<String,Object> user = this.getUser();
    String url =  gitIp+"/api/v3/projects/258/repository/branches?branch=master";
    CloseableHttpClient httpclient = HttpClients.createDefault();
    // 创建http GET请求
    HttpGet httpGet = new HttpGet(url);
    httpGet.setHeader("PRIVATE-TOKEN", user.get("private_token")+"");
    CloseableHttpResponse response = null;
    try {
        response = httpclient.execute(httpGet);
        // 判断状态码
        if (response.getStatusLine().getStatusCode() == 200) {
            //获取返回内容
            String content = EntityUtils.toString(response.getEntity(), "UTF-8");
            System.out.println(content);
        }
    } finally {
        if (response != null) {
            response.close();
        }
        httpclient.close();
    }

}
/**
 * 创建分支
 */
public   void  createBranche() throws Exception {
    Map<String,Object> user = this.getUser();
    String url =  gitIp+"/api/v3/projects/258/repository/branches";
    CloseableHttpClient httpClient =  HttpClients.createDefault();
    HttpPost httppost = new HttpPost(url);
    httppost.setHeader("PRIVATE-TOKEN", user.get("private_token")+"");
    List<NameValuePair> formparams = new ArrayList<NameValuePair>();
    /*** 分支名称*/
    formparams.add(new BasicNameValuePair("branch","newBranch"));
    /*** 来源分支*/
    formparams.add(new BasicNameValuePair("ref","master"));

    UrlEncodedFormEntity entity= new UrlEncodedFormEntity(formparams, Consts.UTF_8);

    httppost.setEntity(entity);
    CloseableHttpResponse response = httpClient.execute(httppost);
    HttpEntity entity1 = response.getEntity();
    Map<String,Object> jsonObj = JSON.parseObject(EntityUtils.toString(entity1));
    System.out.println(JSON.toJSONString(jsonObj));
}

来源api文档

https://docs.gitlab.com/ee/api/branches.html

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐