package com.thoughtworks.go.release_tagger.git;

import com.jayway.jsonpath.JsonPath;
import net.minidev.json.JSONObject;
import org.apache.cxf.jaxrs.client.WebClient;
import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;

import javax.json.Json;
import javax.json.JsonObject;
import javax.ws.rs.core.Response;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static org.joda.time.format.ISODateTimeFormat.dateTimeNoMillis;

public class GitRepo {

    private final String GIT_API_URI = "https://api.github.com/repos/";
    private String authToken;
    private String gitRepoUri;

    public GitRepo(String repoUserName, String repoName, String authToken) {
        this.authToken = authToken;
        gitRepoUri = GIT_API_URI + repoUserName + "/" + repoName;
    }

    public void createTag(String userName, String email, String commitHash, String tagName, String message) {
        JsonObject requestBody = tagBody(userName, email, commitHash, tagName, message);
        post(gitRepoUri + "/git/tags?access_token=" + this.authToken, requestBody);
    }

    private JsonObject tagBody(String userName, String email, String commitHash, String tagName, String message) {
        JsonObject tagger = Json.createObjectBuilder()
                .add("name", userName)
                .add("email", email)
                .add("date", dateTimeNoMillis().print(DateTime.now())).build();
        return Json.createObjectBuilder()
                .add("tag", tagName)
                .add("message", message)
                .add("object", commitHash)
                .add("type", "commit")
                .add("tagger", tagger)
                .build();
    }

    private JSONObject post(String url, JsonObject requestBody){
        Response response = WebClient.create(url)
                .accept(APPLICATION_JSON)
                .type(APPLICATION_JSON)
                .post(requestBody.toString());
        if(response.getStatus() != 201)
            throw new RuntimeException(response.getStatus() + " - Git Api Error while posting to " + url + " with " + requestBody.toString() + " . Got " + response.toString() );
        return (JSONObject) JsonPath.parse(response.readEntity(String.class)).json();
    }
}

@@ -1010,16 +1010,47 @@
 %0A       
+ JSONObject tagCreateResponse =
  post(gi
@@ -1118,16 +1118,258 @@
 tBody);%0A
+        post(gitRepoUri + %22/git/refs?access_token=%22 + authToken, Json.createObjectBuilder()%0A                .add(%22ref%22, %22refs/tags/%22 + requestBody.getString(%22tag%22))%0A                .add(%22sha%22, (String) tagCreateResponse.get(%22sha%22)).build());%0A
     %7D%0A%0A 

Revert "[Aswin|Sathish] Looks like creating tags creates refs too. No need for explicit refs creationg."