问题
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.
来源:https://stackoverflow.com/questions/41670608/how-to-call-the-github-api-form-httpsurlconnection-in-java