使用Gitlab Api v3将项目移动到一个组

时间:2013-05-03 04:53:54

标签: java api post gitlab

我正在使用 Gitlab Api v3 来完成一些任务。我正在使用Java。

我可以执行所有Get操作,还可以通过Post请求(使用admin用户的私人令牌)创建新的项目

现在我正在尝试将此项目移至特定组。但是我很难理解如何使用文档"transfer project to group"中给出的信息创建一个post请求(使用Java)。

提前感谢您的帮助

编辑:我的代码

    public void moveProjectToGroup(String projectName, String groupName) throws IOException
{
    int id_project = getProjectId(projectName); //32
    int id_group = getGroupId(groupName); //14

    System.out.println("project id:"+id_project+"\t group id:"+id_group);


    String urlParameters = "groups/:"+id_group+"/projects/:"+id_project;


    System.out.println(remote); // http://mygitlab/api/v3/
    System.out.println(remote+urlParameters); //http://mygitlab/api/v3/groups/:14/projects/:32

    URL url = new URL(remote); 
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();           
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setInstanceFollowRedirects(false); 
    connection.setRequestMethod("POST"); 
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
    connection.setRequestProperty("charset", "utf-8");
    connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
    connection.setRequestProperty("PRIVATE-TOKEN", "7wHppgzq4HxbxvZVWyso"); // my admin token
    connection.setUseCaches (false);

    DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
    wr.writeBytes(urlParameters);
    wr.flush();
    wr.close();
    connection.disconnect();
}

2 个答案:

答案 0 :(得分:1)

您可以尝试按照“Java - sending HTTP parameters via POST method easily”中描述的方法进行操作,但String urlParameters除外,您可以使用“Transfer project to group”部分中提到的参数替换所述参数:

String id            = ... // (the right group id);
String project_id    = ... // (the right project id);
String urlParameters = "/groups/:"+id+"/projects/:"+project_id;

答案 1 :(得分:1)

我的问题的解决方案是添加路径并调整 urlParameters

public void moveProjectToGroup(String projectName, String groupName) throws IOException
{
    int id_project = getProjectId(projectName); //32
    int id_group = getGroupId(groupName); //14

    String path = "groups/"+id_group+"/projects/"+id_project;
    String urlParameters = "id="+id_group+"&projectid="+id_project;



    //String urlParameters = "name="+name;

    URL url_request = new URL(remote+path);
    System.out.println(url_request.toString());

    HttpURLConnection url_con = (HttpURLConnection) url_request.openConnection();           

    url_con.setDoOutput(true);
    url_con.setDoInput(true);
    url_con.setInstanceFollowRedirects(false); 
    url_con.setRequestMethod("POST"); 
    url_con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
    url_con.setRequestProperty("charset", "utf-8");
    url_con.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
    url_con.setRequestProperty("PRIVATE-TOKEN", "7wHppgzq4HxbxvZVWyso"); // my admin token
    url_con.setUseCaches (false);

    OutputStreamWriter writer = new OutputStreamWriter(url_con.getOutputStream());

    writer.write(urlParameters);
    writer.flush();

    String line;
    BufferedReader reader = new BufferedReader(new InputStreamReader(url_con.getInputStream()));

    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
    writer.close();
    reader.close(); 
    url_con.disconnect();
}