2

I'm using the Gitlab Api v3 to do some task. I'm using Java.

I can do all the Get action and also, create a new project via a Post request (with the private token of the admin user)

Now I'm trying to move this project to a specific group. But I've got some difficulties to understand how to create a post request (in Java) with the info given in the documentation "transfer project to group".

Thank you in advance for your help

Edit: my code

    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();
}
naike
  • 673
  • 4
  • 11
  • 19

2 Answers2

1

You could try and follow the method described in "Java - sending HTTP parameters via POST method easily", except for String urlParameters, you would replace said parameters with the ones mentioned in the "Transfer project to group" section:

String id            = ... // (the right group id);
String project_id    = ... // (the right project id);
String urlParameters = "/groups/:"+id+"/projects/:"+project_id;
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I tried this but the project didn't move to the group. I will edit my post to add my code. – naike May 03 '13 at 06:32
  • @naike then check the logs to see if there is any clue as to why the `POST` failed. – VonC May 03 '13 at 06:33
  • There is nothing that appear in the log when I use this method but when I create a project with following way, it's ok (I see the log too): String urlParameters = "projects?name="+name+"&"; URL url_request = new URL(remote+urlParameters+private_token); // http://mygitlab/api/v3/projects?name=olaola4&private_token=7wHppgzq4HxbxvZVWyso; writer.write(urlParameters); – naike May 03 '13 at 07:22
  • @naike ok. Maybe the parameter is not properly encoded? Otherwise, you might want to open an issue in https://github.com/gitlabhq/gitlabhq/issues – VonC May 03 '13 at 07:25
  • I'm not sure that the problem is related to that. I think there is something with the token. When I use it with Get, it's OK. But with Post, I can't see anything about the operation in my log. Thank you for your help. – naike May 03 '13 at 08:04
  • I fixed it, now it's OK. The problem was related to the path of the Url – naike May 03 '13 at 08:20
1

The solution to my problem was to add a path and adapt the 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();
}
naike
  • 673
  • 4
  • 11
  • 19