Search in sources :

Example 1 with Status

use of org.gitlab4j.api.models.ImportStatus.Status in project catma by forTEXT.

the class GitlabManagerRestricted method forkResource.

@Override
public ForkStatus forkResource(String resourceId, String sourceProjectId, String targetProjectId) throws IOException {
    try {
        Project sourceResourceProject = restrictedGitLabApi.getProjectApi().getProject(sourceProjectId, resourceId);
        Optional<Project> optionalTargetResource = restrictedGitLabApi.getProjectApi().getOptionalProject(targetProjectId, resourceId);
        if (optionalTargetResource.isPresent()) {
            return ForkStatus.resourceAlreadyExists();
        }
        restrictedGitLabApi.getProjectApi().forkProject(sourceResourceProject, targetProjectId);
        Project targetProject = restrictedGitLabApi.getProjectApi().getProject(targetProjectId, resourceId);
        Status importStatus = targetProject.getImportStatus();
        int tries = 10;
        while (importStatus != Status.FINISHED && tries > 0) {
            tries--;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                break;
            }
            logger.info(String.format("Trying to retrieve forked resource status for %1$s from group %2$s (try %3$d)", resourceId, targetProjectId, 10 - tries));
            importStatus = targetProject.getImportStatus();
        }
        if (importStatus != Status.FINISHED) {
            logger.warning(String.format("Status is still '%1$s' and not 'finished'! Trying to continue anyway!", importStatus));
        }
        return ForkStatus.success();
    } catch (GitLabApiException e) {
        throw new IOException(String.format("Failed to fork resource %1$s from group %2$s into group %3$s", resourceId, sourceProjectId, targetProjectId), e);
    }
}
Also used : Status(org.gitlab4j.api.models.ImportStatus.Status) ForkStatus(de.catma.project.ForkStatus) Project(org.gitlab4j.api.models.Project) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException)

Aggregations

ForkStatus (de.catma.project.ForkStatus)1 IOException (java.io.IOException)1 GitLabApiException (org.gitlab4j.api.GitLabApiException)1 Status (org.gitlab4j.api.models.ImportStatus.Status)1 Project (org.gitlab4j.api.models.Project)1