Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impl for SyncApi #166

Merged
merged 7 commits into from
Feb 16, 2019
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
</module>
<module name="AbbreviationAsWordInName">
<property name="ignoreFinal" value="false"/>
<property name="allowedAbbreviationLength" value="5"/>
<property name="allowedAbbreviationLength" value="10"/>
</module>
<module name="OverloadMethodsDeclarationOrder"/>
<module name="VariableDeclarationUsageDistance"/>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/cdancy/bitbucket/rest/BitbucketApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.cdancy.bitbucket.rest.features.ProjectApi;
import com.cdancy.bitbucket.rest.features.PullRequestApi;
import com.cdancy.bitbucket.rest.features.RepositoryApi;
import com.cdancy.bitbucket.rest.features.SyncApi;
import com.cdancy.bitbucket.rest.features.SystemApi;
import com.cdancy.bitbucket.rest.features.TagApi;
import com.cdancy.bitbucket.rest.features.TasksApi;
Expand Down Expand Up @@ -71,6 +72,9 @@ public interface BitbucketApi extends Closeable {
@Delegate
RepositoryApi repositoryApi();

@Delegate
SyncApi syncApi();

@Delegate
SystemApi systemApi();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,33 @@
* limitations under the License.
*/

package com.cdancy.bitbucket.rest.domain.pullrequest;
package com.cdancy.bitbucket.rest.domain.common;

import com.cdancy.bitbucket.rest.BitbucketUtils;
import com.cdancy.bitbucket.rest.domain.pullrequest.MinimalRepository;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.json.SerializedNames;

import com.google.auto.value.AutoValue;

import java.util.List;

@AutoValue
public abstract class Reference {
public abstract class Reference implements ErrorsHolder {

// default to 'refs/heads/master' if null
@Nullable
public abstract String id();

@Nullable
public abstract MinimalRepository repository();

@Nullable
public abstract String state();

@Nullable
public abstract Boolean tag();

@Nullable
public abstract String displayId();

Expand All @@ -55,18 +66,41 @@ public static Reference create(final String id,
final MinimalRepository repository,
final String displayId) {

return create(id, repository, displayId, null);
return create(id, repository, null, null, displayId, null, null);
}

@SerializedNames({"id", "repository", "displayId", "latestCommit"})
public static Reference create(final String id,
final MinimalRepository repository,
final String displayId,
final String latestCommit) {
@Deprecated
public static Reference create(final String id,
final MinimalRepository repository,
final String state,
final Boolean tag,
final String displayId,
final String latestCommit) {

return create(id,
repository,
state,
tag,
displayId,
latestCommit,
null);
}

@SerializedNames({ "id", "repository", "state", "tag", "displayId", "latestCommit", "errors" })
public static Reference create(final String id,
final MinimalRepository repository,
final String state,
final Boolean tag,
final String displayId,
final String latestCommit,
final List<Error> errors) {

return new AutoValue_Reference(id != null ? id : "refs/heads/master",
repository,
displayId,
latestCommit);
return new AutoValue_Reference(BitbucketUtils.nullToEmpty(errors),
id != null ? id : "refs/heads/master",
repository,
state,
tag,
displayId,
latestCommit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public abstract class RequestStatus implements Value<Boolean>, ErrorsHolder {
public static RequestStatus create(@Nullable final Boolean value,
final List<Error> errors) {

return new AutoValue_RequestStatus(value,
return new AutoValue_RequestStatus(value,
BitbucketUtils.nullToEmpty(errors));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.List;

import com.cdancy.bitbucket.rest.domain.common.Reference;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.json.SerializedNames;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public abstract class Repository implements ErrorsHolder, LinksHolder {

public abstract boolean forkable();

@Nullable
public abstract Repository origin();

@Nullable
public abstract Project project();

Expand All @@ -61,15 +64,16 @@ public abstract class Repository implements ErrorsHolder, LinksHolder {
}

@SerializedNames({ "slug", "id", "name", "scmId",
"state", "statusMessage", "forkable", "project",
"public", "links", "errors" })
"state", "statusMessage", "forkable", "origin",
"project", "public", "links", "errors" })
public static Repository create(final String slug,
final int id,
final String name,
final String scmId,
final String state,
final String statusMessage,
final boolean forkable,
final Repository origin,
final Project project,
final boolean _public,
final Links links,
Expand All @@ -83,7 +87,8 @@ public static Repository create(final String slug,
scmId,
state,
statusMessage,
forkable,
forkable,
origin,
project,
_public);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.cdancy.bitbucket.rest.domain.sync;

import com.cdancy.bitbucket.rest.BitbucketUtils;
import com.cdancy.bitbucket.rest.domain.common.Error;
import com.cdancy.bitbucket.rest.domain.common.ErrorsHolder;
import com.cdancy.bitbucket.rest.domain.common.Reference;
import com.google.auto.value.AutoValue;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.json.SerializedNames;

import java.util.List;

@SuppressWarnings("PMD")
@AutoValue
public abstract class SyncStatus implements ErrorsHolder {

@Nullable
public abstract Boolean available();

@Nullable
public abstract Boolean enabled();

@Nullable
public abstract Long lastSync();

public abstract List<Reference> aheadRefs();

public abstract List<Reference> divergedRefs();

public abstract List<Reference> orphanedRefs();

SyncStatus() {
}

@SerializedNames({ "available", "enabled", "lastSync",
"aheadRefs", "divergedRefs", "orphanedRefs", "errors" })
public static SyncStatus create(final Boolean available,
final Boolean enabled,
final Long lastSync,
final List<Reference> aheadRefs,
final List<Reference> divergedRefs,
final List<Reference> orphanedRefs,
final List<Error> errors) {

return new AutoValue_SyncStatus(BitbucketUtils.nullToEmpty(errors),
available != null ? available : true,
enabled != null ? enabled : false,
lastSync != null ? lastSync : 0,
BitbucketUtils.nullToEmpty(aheadRefs),
BitbucketUtils.nullToEmpty(divergedRefs),
BitbucketUtils.nullToEmpty(orphanedRefs));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.cdancy.bitbucket.rest.domain.commit.Commit;
import com.cdancy.bitbucket.rest.domain.commit.CommitPage;
import com.cdancy.bitbucket.rest.domain.common.Error;
import com.cdancy.bitbucket.rest.domain.common.Reference;
import com.cdancy.bitbucket.rest.domain.common.RequestStatus;
import com.cdancy.bitbucket.rest.domain.common.Veto;
import com.cdancy.bitbucket.rest.domain.defaultreviewers.Condition;
Expand All @@ -63,6 +64,7 @@
import com.cdancy.bitbucket.rest.domain.repository.PullRequestSettings;
import com.cdancy.bitbucket.rest.domain.repository.Repository;
import com.cdancy.bitbucket.rest.domain.repository.RepositoryPage;
import com.cdancy.bitbucket.rest.domain.sync.SyncStatus;
import com.cdancy.bitbucket.rest.domain.tags.Tag;
import com.cdancy.bitbucket.rest.domain.tags.TagPage;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -366,6 +368,45 @@ public Object createOrPropagate(final Throwable throwable) throws Exception {
}
}

public static final class SyncStatusOnError implements Fallback<Object> {
@Override
public Object createOrPropagate(final Throwable throwable) throws Exception {
if (checkNotNull(throwable, "throwable") != null) {
final Boolean is204 = returnValueOnCodeOrNull(throwable, true, equalTo(204));
final boolean isAvailable = (is204 != null) ? true : false;
final List<Error> errors = getErrors(throwable.getMessage());
if (errors.size() > 0
&& errors.get(0).context() != null
&& errors.get(0).context().startsWith("Error parsing input: null")) {
return createSyncStatusFromErrors(isAvailable, null);
} else {
return createSyncStatusFromErrors(isAvailable, errors);
}
}
throw propagate(throwable);
}
}

public static final class ReferenceOnError implements Fallback<Object> {
@Override
public Object createOrPropagate(final Throwable throwable) throws Exception {
if (checkNotNull(throwable, "throwable") != null) {
final Boolean is204 = returnValueOnCodeOrNull(throwable, true, equalTo(204));
final String syncedStatus = (is204 != null) ? "SYNCED" : null;
final List<Error> errors = getErrors(throwable.getMessage());
if (errors.size() > 0
&& errors.get(0).context() != null
&& errors.get(0).context().startsWith("Error parsing input: null")) {

return createReferenceFromErrors(syncedStatus, null);
} else {
return createReferenceFromErrors(syncedStatus, errors);
}
}
throw propagate(throwable);
}
}

public static final class ActivitiesPageOnError implements Fallback<Object> {
@Override
public Object createOrPropagate(final Throwable throwable) throws Exception {
Expand Down Expand Up @@ -555,7 +596,7 @@ public static Task createTaskFromErrors(final List<Error> errors) {
}

public static Repository createRepositoryFromErrors(final List<Error> errors) {
return Repository.create(null, -1, null, null, null, null, false, null, false, null, errors);
return Repository.create(null, -1, null, null, null, null, false, null, null, false, null, errors);
}

public static RepositoryPage createRepositoryPageFromErrors(final List<Error> errors) {
Expand Down Expand Up @@ -601,6 +642,14 @@ public static PullRequest createPullRequestFromErrors(final List<Error> errors)
null, null, errors);
}

public static Reference createReferenceFromErrors(final String syncedStatus, final List<Error> errors) {
return Reference.create(null, null, syncedStatus, null, null, null, errors);
}

public static SyncStatus createSyncStatusFromErrors(final boolean isAvailable, final List<Error> errors) {
return SyncStatus.create(isAvailable, false, null, null, null, null, errors);
}

public static ActivitiesPage createActivitiesPageFromErrors(final List<Error> errors) {
return ActivitiesPage.create(-1, -1, -1, -1, true, null, errors);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.rest.annotations.BinderParam;
import org.jclouds.rest.annotations.Fallback;
import org.jclouds.rest.annotations.Payload;
import org.jclouds.rest.annotations.PayloadParam;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.binders.BindToJsonPayload;
import javax.ws.rs.Consumes;
Expand Down Expand Up @@ -71,6 +73,18 @@ Repository create(@PathParam("project") String project,
Repository get(@PathParam("project") String project,
@PathParam("repo") String repo);

@Named("repository:fork")
@Documentation({"https://developer.atlassian.com/static/rest/bitbucket-server/latest/bitbucket-rest.html#idm45888277587248"})
@Consumes(MediaType.APPLICATION_JSON)
@Path("/{project}/repos/{repo}")
@Payload("%7B \"name\": \"{newRepo}\", \"project\": %7B \"key\": \"{newProject}\" %7D %7D")
@Fallback(BitbucketFallbacks.RepositoryOnError.class)
@POST
Repository fork(@PathParam("project") String project,
@PathParam("repo") String repo,
@PayloadParam("newProject") String newProject,
@PayloadParam("newRepo") String newRepo);

@Named("repository:delete")
@Documentation({"https://developer.atlassian.com/static/rest/bitbucket-server/latest/bitbucket-rest.html#idm45888277567792"})
@Consumes(MediaType.APPLICATION_JSON)
Expand Down
Loading