I need to change my branch named testingProtectedBranch1
as a protected branch with providing the following parameters
on
required_status_check : include_admins= true, strict= true, context= continuous-integration/travis-ci
restrictions: null
required_pull_request_reviews: include_admins=false
here is my code and the access token ( the variable token
) is provided by the user at the runtime.
public void setMasterBranchAsProtected() throws Exception{
String URLForCallingTheBranchAPI="https://api.github.com/repos/kasunsiyambalapitiya/testingProtectedBranch1/branches/master/protection";
String jsonInput="{\"required_status_checks\":{\"include_admins\":true,\"strict\":true,\"contexts\":[\"continuous-integration/travis-ci\"]},"
+ "\"restrictions\":null,"
+ "\"required_pull_request_reviews\":{\"include_admins\":false} ";
try {
URL urlObject= new URL(URLForCallingTheBranchAPI);
HttpsURLConnection httpsURLCon= (HttpsURLConnection)urlObject.openConnection();
httpsURLCon.setDoOutput(true);
httpsURLCon.setRequestMethod("PUT");
httpsURLCon.setRequestProperty("User-Agent", "Mozilla/5.0");
httpsURLCon.setRequestProperty("Accept","application/vnd.github.loki-preview+json");
httpsURLCon.setRequestProperty("Authorization", "Bearer "+token);
OutputStreamWriter outputStream= new OutputStreamWriter(httpsURLCon.getOutputStream());
outputStream.write(jsonInput);
int responseCode= httpsURLCon.getResponseCode();
outputStream.flush();
outputStream.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
but for the response code I receive 422
which resembles unprocessable entity. What I am doing wrong in here, please help me to figure this out. Thanks in advance.
curl
for a different repotestingProtectedBranch
it worked. But I need a java code for it, that's why I wrote the above code, but I cannot figure out what is wrong with it. theresponse message
isUnprocessable Entity
– ErkhttpsURLCon.getErrorStream()
. – Systemsun.net.www.protocol.http.HttpURLConnection$HttpInputStream@26f67b76
– Erk{"message":"Invalid request.\n\nFor 'links/1/schema', nil is not an object.","documentation_url":"https://developer.github.com/v3/repos/branches/#update-branch-protection"}
– Erk