2.gitLabApi 获取分支列表

/**
 * 获取分支列表
 */
public   void  getBranches() throws Exception {
    Map 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 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 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 formparams = new ArrayList();
    /*** 分支名称*/
    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 jsonObj = JSON.parseObject(EntityUtils.toString(entity1));
    System.out.println(JSON.toJSONString(jsonObj));
}

来源api文档

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

你可能感兴趣的:(2.Git相关,git)