use of org.jenkinsci.plugin.gitea.client.api.GiteaHttpStatusException in project gitea-plugin by jenkinsci.
the class GiteaNotifier method sendNotifications.
/**
* Sends notifications to Bitbucket on Checkout (for the "In Progress" Status).
*/
private static void sendNotifications(Run<?, ?> build, TaskListener listener) throws IOException, InterruptedException {
final SCMSource s = SCMSource.SourceByItem.findSource(build.getParent());
if (!(s instanceof GiteaSCMSource)) {
return;
}
GiteaSCMSource source = (GiteaSCMSource) s;
if (new GiteaSCMSourceContext(null, SCMHeadObserver.none()).withTraits(source.getTraits()).notificationsDisabled()) {
return;
}
String url;
try {
url = DisplayURLProvider.get().getRunURL(build);
} catch (IllegalStateException e) {
listener.getLogger().println("Can not determine Jenkins root URL. Commit status notifications are disabled until a root URL is" + " configured in Jenkins global configuration.");
return;
}
Result result = build.getResult();
GiteaCommitStatus status = new GiteaCommitStatus();
status.setTargetUrl(url);
status.setContext(build.getParent().getFullName());
if (Result.SUCCESS.equals(result)) {
status.setDescription("This commit looks good");
status.setState(GiteaCommitState.SUCCESS);
} else if (Result.UNSTABLE.equals(result)) {
status.setDescription("This commit has test failures");
status.setState(GiteaCommitState.FAILURE);
} else if (Result.FAILURE.equals(result)) {
status.setDescription("There was a failure building this commit");
status.setState(GiteaCommitState.FAILURE);
} else if (result != null) {
// ABORTED etc.
status.setDescription("Something is wrong with the build of this commit");
status.setState(GiteaCommitState.ERROR);
} else {
status.setDescription("Build started...");
status.setState(GiteaCommitState.PENDING);
}
SCMRevision revision = SCMRevisionAction.getRevision(source, build);
String hash;
if (revision instanceof BranchSCMRevision) {
listener.getLogger().format("[Gitea] Notifying branch build status: %s %s%n", status.getState().name(), status.getDescription());
hash = ((BranchSCMRevision) revision).getHash();
} else if (revision instanceof PullRequestSCMRevision) {
listener.getLogger().format("[Gitea] Notifying pull request build status: %s %s%n", status.getState().name(), status.getDescription());
hash = ((PullRequestSCMRevision) revision).getOrigin().getHash();
} else {
// TODO tags
return;
}
JobScheduledListener jsl = ExtensionList.lookup(QueueListener.class).get(JobScheduledListener.class);
if (jsl != null) {
// we are setting the status, so don't let the queue listener background thread change it to pending
synchronized (jsl.resolving) {
jsl.resolving.remove(build.getParent());
}
}
try (GiteaConnection c = source.gitea().open()) {
int tries = 3;
while (true) {
tries--;
try {
c.createCommitStatus(source.getRepoOwner(), source.getRepository(), hash, status);
break;
} catch (GiteaHttpStatusException e) {
if (e.getStatusCode() == 500 && tries > 0) {
// server may be overloaded
continue;
}
throw e;
}
}
listener.getLogger().format("[Gitea] Notified%n");
}
}
use of org.jenkinsci.plugin.gitea.client.api.GiteaHttpStatusException in project gitea-plugin by jenkinsci.
the class DefaultGiteaConnection method post.
private <T> T post(UriTemplate template, Object body, final Class<T> modelClass) throws IOException, InterruptedException {
HttpURLConnection connection = openConnection(template);
withAuthentication(connection);
connection.setRequestMethod("POST");
byte[] bytes;
if (body != null) {
bytes = mapper.writer(new ISO8601DateFormat()).writeValueAsBytes(body);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Content-Length", Integer.toString(bytes.length));
connection.setDoOutput(true);
} else {
bytes = null;
connection.setDoOutput(false);
}
connection.setDoInput(!Void.class.equals(modelClass));
try {
connection.connect();
if (bytes != null) {
try (OutputStream os = connection.getOutputStream()) {
os.write(bytes);
}
}
int status = connection.getResponseCode();
if (status / 100 == 2) {
if (Void.class.equals(modelClass)) {
return null;
}
try (InputStream is = connection.getInputStream()) {
return mapper.readerFor(modelClass).readValue(is);
}
}
throw new GiteaHttpStatusException(status, connection.getResponseMessage(), bytes != null ? new String(bytes, StandardCharsets.UTF_8) : null);
} finally {
connection.disconnect();
}
}
use of org.jenkinsci.plugin.gitea.client.api.GiteaHttpStatusException in project gitea-plugin by jenkinsci.
the class DefaultGiteaConnection method getObject.
private <T> T getObject(UriTemplate template, final Class<T> modelClass) throws IOException, InterruptedException {
HttpURLConnection connection = openConnection(template);
withAuthentication(connection);
try {
connection.connect();
int status = connection.getResponseCode();
if (status == 200) {
try (InputStream is = connection.getInputStream()) {
return mapper.readerFor(modelClass).readValue(is);
}
}
throw new GiteaHttpStatusException(status, connection.getResponseMessage());
} finally {
connection.disconnect();
}
}
use of org.jenkinsci.plugin.gitea.client.api.GiteaHttpStatusException in project gitea-plugin by jenkinsci.
the class DefaultGiteaConnection method patch.
private <T> T patch(UriTemplate template, Object body, final Class<T> modelClass) throws IOException, InterruptedException {
HttpURLConnection connection = openConnection(template);
withAuthentication(connection);
setRequestMethodViaJreBugWorkaround(connection, "PATCH");
byte[] bytes;
if (body != null) {
bytes = mapper.writer(new ISO8601DateFormat()).writeValueAsBytes(body);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Content-Length", Integer.toString(bytes.length));
connection.setDoOutput(true);
} else {
bytes = null;
connection.setDoOutput(false);
}
connection.setDoInput(true);
try {
connection.connect();
if (bytes != null) {
try (OutputStream os = connection.getOutputStream()) {
os.write(bytes);
}
}
int status = connection.getResponseCode();
if (status / 100 == 2) {
if (Void.class.equals(modelClass)) {
return null;
}
try (InputStream is = connection.getInputStream()) {
return mapper.readerFor(modelClass).readValue(is);
}
}
throw new GiteaHttpStatusException(status, connection.getResponseMessage(), bytes != null ? new String(bytes, StandardCharsets.UTF_8) : null);
} finally {
connection.disconnect();
}
}
use of org.jenkinsci.plugin.gitea.client.api.GiteaHttpStatusException in project gitea-plugin by jenkinsci.
the class DefaultGiteaConnection method getList.
private <T> List<T> getList(UriTemplate template, final Class<T> modelClass) throws IOException, InterruptedException {
HttpURLConnection connection = openConnection(template);
withAuthentication(connection);
try {
connection.connect();
int status = connection.getResponseCode();
if (status / 100 == 2) {
try (InputStream is = connection.getInputStream()) {
List<T> list = mapper.readerFor(mapper.getTypeFactory().constructCollectionType(List.class, modelClass)).readValue(is);
// strip null values from the list
for (Iterator<T> iterator = list.iterator(); iterator.hasNext(); ) {
if (iterator.next() == null) {
iterator.remove();
}
}
return list;
}
}
throw new GiteaHttpStatusException(status, connection.getResponseMessage());
} finally {
connection.disconnect();
}
}
Aggregations