import org.apache.commons.fileupload.FileItem
import org.apache.commons.fileupload.FileItemFactory
import org.apache.commons.fileupload.disk.DiskFileItem
import org.apache.commons.fileupload.disk.DiskFileItemFactory
import org.apache.http.entity.FileEntity
import org.apache.http.entity.mime.HttpMultipartMode
import org.apache.http.entity.mime.content.ByteArrayBody
import org.apache.http.entity.mime.content.FileBody
import org.apache.http.entity.mime.content.InputStreamBody
import org.apache.http.entity.mime.content.StringBody
import org.springframework.web.multipart.MultipartFile
import org.springframework.web.multipart.commons.CommonsMultipartFile
import java.io.File
import groovy.json.JsonSlurper
import groovyx.net.http.*
import java.net.URI

//////////////////////
///// Parameters ---------- START
//////////////////////

def releaseversion = "2.1"
// the host where you want to automatically deploy
def githubapihost = "api.github.com"
def githubuploadhost = "uploads.github.com"
def gitreleasesrepositoryowner = "PierrickVouletBonitasoft"
def gitreleasesrepositoryname = "bonita-internal-contrib-releases"
// the Authorization hash
def authorization = "Basic UGllcnJpY2tWb3VsZXRCb25pdGFzb2Z0Oll1Z2kwMDAw"
// the filename of the archive to be automatically deployed
def fileName = "bonita-internal-contrib-releases-" + releaseversion + ".zip"
// the path of the archive file to be automatically deployed
def filepath = "./target/" + fileName

//////////////////////
///// Parameters ----------- END
//////////////////////

def sluper = new JsonSlurper()

def createdreleaseid = "";

//---------- create release

//def github = new RESTClient("https://" + githubapihost + "/")
//github.auth.basic("me", "pwd")

//def createReleaseResult = github.post(
//	path: "/repos/" + gitreleasesrepositoryowner + "/" + gitreleasesrepositoryname + "/releases",
//	requestContentType: ContentType.JSON,
//	headers: ["Accept": "application/vnd.github.v3+json"],
//	body: "{" +
//			  "\"tag_name\": \"" + releaseversion + "\"," +
//			  "\"target_commitish\": \"master\"," +
//			  "\"name\": \"Release-" + releaseversion + "\"," +
//			  "\"body\": \"Release generated by bonita-internal-contrib-releases project\"," +
//			  "\"draft\": false," +
//			  "\"prerelease\": false" +
//			"}"
//)
//
//log.info "Login Status: " + createReleaseResult.status
//log.info 'Login Headers: -----------'


def data = "{" +
			  "\"tag_name\": \"" + releaseversion + "\"," +
			  "\"target_commitish\": \"master\"," +
			  "\"name\": \"Release-" + releaseversion + "\"," +
			  "\"body\": \"Release generated by bonita-internal-contrib-releases project\"," +
			  "\"draft\": false," +
			  "\"prerelease\": false" +
			"}"

def github = new HTTPBuilder("https://" + githubapihost + "/repos/" + gitreleasesrepositoryowner + "/" + gitreleasesrepositoryname + "/releases")
def githubResult = github.request(Method.POST, ContentType.JSON) { req ->
	headers.'Accept' = 'application/vnd.github.v3+json'
	headers.'Authorization' = authorization
	body: data
}

def resultString = "${githubResult}".toString()

log.info "Status: " + resultString


//----------------- upload the assest into the freshly created release
//
//File file = new File(filepath);
//byte[] data = file.getBytes();
//
//def bonitasoftUploadFile = new HTTPBuilder('http://' + host + ':' + port + '/bonita/portal/pageUpload')
//def bonitasoftUploadFileResult = bonitasoftUploadFile.request(Method.POST) { req ->
//	requestContentType: "multipart/form-data"
//	MultipartEntity multiPartContent = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE)
//	multiPartContent.addPart("file", new ByteArrayBody(data, fileName))
//	req.setEntity(multiPartContent)
//	headers['Cookie'] = setcookies
//}
//
//def resultString = "${bonitasoftUploadFileResult}".toString()
//def pageZip = resultString.substring(0, resultString.indexOf("[]"));
//
//log.info "Upload File Status: " + pageZip
