Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* limitations under the License.
*/

import org.apache.http.HttpStatus;
import org.gradle.api.DefaultTask;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
Expand All @@ -28,6 +29,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -113,7 +115,12 @@ public void run() throws IOException, InterruptedException {
URI apiUri = URI.create(CENTRAL_PORTAL_OSSRH_API_URI);

String repositoryKey = findOpenRepository(apiUri, bearer);
uploadRepositoryToPortal(apiUri, bearer, repositoryKey);
System.out.println("Found published repository: " + repositoryKey);

int status = uploadRepository(apiUri, bearer, repositoryKey);
if (status == HttpURLConnection.HTTP_CLIENT_TIMEOUT)
uploadRepository(apiUri, bearer, repositoryKey);

dropRepository(apiUri, bearer, repositoryKey);
}

Expand All @@ -137,8 +144,6 @@ private String findOpenRepository(URI apiUri, String bearer) throws IOException
throw new IllegalStateException("Open repositories is empty!");
}

System.out.println(repositories);

String repositoryKey = null;
String group = groupId.get();
for (int i = 0; i < repositories.length(); i++) {
Expand All @@ -158,21 +163,30 @@ private String findOpenRepository(URI apiUri, String bearer) throws IOException
return repositoryKey;
}

private static void uploadRepositoryToPortal(URI apiUri, String bearer, String repositoryKey) throws IOException {
String endpoint = apiUri.resolve("/manual/upload/repository/" + repositoryKey + "?publishing_type=user_managed").toString();
HttpURLConnection conn = (HttpURLConnection) new URL(endpoint).openConnection();
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setReadTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Bearer " + bearer);
conn.setDoOutput(true);
conn.getOutputStream().close();

int status = conn.getResponseCode();
String body = readBody(conn);
if (status != 200) {
throw new IllegalStateException("Failed to upload repository: repository_key=" + repositoryKey + ", status=" + status + ", response=" + body);
private static int uploadRepository(URI apiUri, String bearer, String repositoryKey) throws IOException {
String response = null;
int status = -1;
HttpURLConnection conn = null;
try {
String endpoint = apiUri.resolve("/manual/upload/repository/" + repositoryKey + "?publishing_type=automatic").toString();
conn = (HttpURLConnection) new URL(endpoint).openConnection();
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setReadTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Bearer " + bearer);
conn.setDoOutput(true);

status = conn.getResponseCode();
response = readBody(conn);
} catch (SocketTimeoutException ex) {
return HttpURLConnection.HTTP_CLIENT_TIMEOUT;
} catch (Exception ex) {
throw new IllegalStateException("Failed to upload repository: repository_key=" + repositoryKey + ", status=" + status + ", response=" + response);
} finally {
conn.disconnect();
}

return status;
}

private static void dropRepository(URI apiUri, String bearer, String repositoryKey) throws IOException {
Expand All @@ -185,7 +199,7 @@ private static void dropRepository(URI apiUri, String bearer, String repositoryK

int status = conn.getResponseCode();
String body = readBody(conn);
if (status != 204) {
if (status != HttpURLConnection.HTTP_NO_CONTENT) {
throw new IllegalStateException("Failed to drop repository: repository_key=" + repositoryKey + ", status=" + status + ", response=" + body);
}
}
Expand Down
Loading