Skip to content

Commit

Permalink
feat: Get the branches from a Tuleap Git repository and get all (#270)
Browse files Browse the repository at this point in the history
* feat: Get the branches from a Tuleap Git repository and get all
repositories from a project

Part of [request #22618](https://tuleap.net/plugins/tracker/?aid=22618) Stop using the deprecated client in Jenkins plugin

* Make spotbug happy

Co-authored-by: Clarck Robinson <clarck.robinson@enalean.com>
  • Loading branch information
2 people authored and Erwyn committed Sep 26, 2022
1 parent d3e02c2 commit 73d09cc
Show file tree
Hide file tree
Showing 12 changed files with 366 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public interface GitApi {
String TREE = "/tree";
String FILES = "/files";
String PULL_REQUEST = "/pull_requests";
String BRANCHES = "/branches";

void sendBuildStatus(String repositoryId, String commitReference, TuleapBuildStatus status, StringCredentials credentials);

Expand All @@ -28,4 +29,6 @@ public interface GitApi {
GitFileContent getFileContent(String repositoryId, String path, String commitReference, TuleapAccessToken token) throws FileContentNotFoundException;

List<GitPullRequest> getPullRequests(String repositoryId, TuleapAccessToken token);

List<GitBranch> getBranches(String repositoryId, TuleapAccessToken token);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.jenkins.plugins.tuleap_api.client;

public interface GitBranch {
String getName();

GitCommit getCommit();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.jenkins.plugins.tuleap_api.client;

import java.util.List;

public interface GitRepositories {
List<? extends GitRepository> getGitRepositories();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.jenkins.plugins.tuleap_api.client;

public interface GitRepository {
String getName();

Integer getId();

String getPath();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ public interface ProjectApi {

String PROJECT_API = "/projects";
String PROJECT_GROUPS = "/user_groups";
String PROJECT_GIT = "/git";

String PROJECT_MEMBER_OF_QUERY = "{\"is_member_of\":true}";

Project getProjectByShortname(String shortname, AccessToken token) throws ProjectNotFoundException;
Project getProjectById(String projectId, TuleapAccessToken token) ;
List<UserGroup> getProjectUserGroups(Integer projectId, AccessToken token);
List<GitRepository> getGitRepositories(Integer projectId, TuleapAccessToken token);
}
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,57 @@ public List<UserGroup> getProjectUserGroups(Integer projectId, AccessToken token

return new ArrayList<>(this.objectMapper.readValue(
Objects.requireNonNull(response.body()).string(),
new TypeReference<List<MinimalUserGroupEntity>>() {}
new TypeReference<List<MinimalUserGroupEntity>>() {
}
));
} catch (IOException | InvalidTuleapResponseException e) {
throw new RuntimeException("Error while contacting Tuleap server", e);
}
}

@Override
@SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE") // see https://github.com/spotbugs/spotbugs/issues/651
public List<GitRepository> getGitRepositories(Integer projectId, TuleapAccessToken token) {
int offset = 0;
int totalSize;

List<GitRepository> allRepositories = new ArrayList<>();

do {
HttpUrl urlGetProjectRepositories = Objects.requireNonNull(HttpUrl.parse(this.tuleapConfiguration.getApiBaseUrl() + this.PROJECT_API + "/" + projectId + this.PROJECT_GIT))
.newBuilder()
.addEncodedQueryParameter("limit", Integer.toString(PAGE_SIZE))
.addEncodedQueryParameter("offset", Integer.toString(offset))
.build();

Request request = new Request.Builder()
.url(urlGetProjectRepositories)
.addHeader(this.AUTHORIZATION_HEADER, token.getToken().getPlainText())
.get()
.build();

try (Response response = this.client.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new InvalidTuleapResponseException(response);
}

GitRepositories repositories = this.objectMapper.readValue(
Objects.requireNonNull(response.body()).string(),
GitRepositoriesEntity.class
);

allRepositories.addAll(repositories.getGitRepositories());

totalSize = Integer.parseInt(Objects.requireNonNull(response.header(COLLECTION_LENGTH_HEADER)));
offset = offset + PAGE_SIZE;

} catch (InvalidTuleapResponseException | IOException e) {
throw new RuntimeException("Error while contacting Tuleap server", e);
}
} while (offset < totalSize);
return allRepositories;
}

@Override
public void sendTTMResults(String campaignId, String buildUrl, List<String> results, Secret secret) {
Request request;
Expand All @@ -300,7 +344,7 @@ public void sendTTMResults(String campaignId, String buildUrl, List<String> resu
request = new Request.Builder()
.url(this.tuleapConfiguration.getApiBaseUrl() + this.TEST_CAMPAIGN_API + "/" + campaignId)
.addHeader(this.AUTHORIZATION_HEADER, secret.getPlainText())
.patch(RequestBody.create(this.objectMapper.writer(SerializationFeature.WRAP_ROOT_VALUE).writeValueAsString(new SendTTMResultsEntity(buildUrl,results)), JSON))
.patch(RequestBody.create(this.objectMapper.writer(SerializationFeature.WRAP_ROOT_VALUE).writeValueAsString(new SendTTMResultsEntity(buildUrl, results)), JSON))
.build();
} catch (JsonProcessingException exception) {
throw new RuntimeException("Error while trying to create request for TTM results", exception);
Expand Down Expand Up @@ -494,6 +538,51 @@ public List<GitPullRequest> getPullRequests(String repositoryId, TuleapAccessTok
}
}

@Override
@SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE") // see https://github.com/spotbugs/spotbugs/issues/651
public List<GitBranch> getBranches(String repositoryId, TuleapAccessToken token) {
int offset = 0;
int totalSize;

List<GitBranch> branches = new ArrayList<>();

do {
HttpUrl urlGetPR = Objects.requireNonNull(HttpUrl.parse(this.tuleapConfiguration.getApiBaseUrl() + this.GIT_API + "/" + repositoryId + this.BRANCHES))
.newBuilder()
.addEncodedQueryParameter("limit", Integer.toString(PAGE_SIZE))
.addEncodedQueryParameter("offset", Integer.toString(offset))
.build();

Request request = new Request.Builder()
.url(urlGetPR)
.addHeader(this.AUTHORIZATION_HEADER, token.getToken().getPlainText())
.get()
.build();

try (Response response = this.client.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new InvalidTuleapResponseException(response);
}

List<GitBranch> currentPageBranches = new ArrayList<>(this.objectMapper.readValue(
Objects.requireNonNull(response.body()).string(),
new TypeReference<List<GitBranchEntity>>() {
}
));

branches.addAll(currentPageBranches);

totalSize = Integer.parseInt(Objects.requireNonNull(response.header(COLLECTION_LENGTH_HEADER)));
offset = offset + PAGE_SIZE;

} catch (InvalidTuleapResponseException | IOException e) {
throw new RuntimeException("Error while contacting Tuleap server", e);
}
} while (offset < totalSize);

return branches;
}

@Override
@SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE") // see https://github.com/spotbugs/spotbugs/issues/651
public PullRequest getPullRequest(String pullRequestId, TuleapAccessToken token) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.jenkins.plugins.tuleap_api.client.internals.entities;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.jenkins.plugins.tuleap_api.client.GitBranch;
import io.jenkins.plugins.tuleap_api.client.GitCommit;

final public class GitBranchEntity implements GitBranch {

private final String name;
private final GitCommitEntity commit;

public GitBranchEntity(@JsonProperty("name") String name, @JsonProperty("commit") GitCommitEntity commit){
this.name = name;
this.commit = commit;
}

@Override
public String getName() {
return this.name;
}

@Override
public GitCommit getCommit() {
return this.commit;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.jenkins.plugins.tuleap_api.client.internals.entities;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.jenkins.plugins.tuleap_api.client.GitRepositories;
import io.jenkins.plugins.tuleap_api.client.GitRepository;

import java.util.List;

final public class GitRepositoriesEntity implements GitRepositories {

private List<GitRepositoryEntity> repositories;

public GitRepositoriesEntity(@JsonProperty("repositories") List<GitRepositoryEntity> repositories){
this.repositories = repositories;
}

@Override
public List<? extends GitRepository> getGitRepositories() {
return this.repositories;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.jenkins.plugins.tuleap_api.client.internals.entities;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.jenkins.plugins.tuleap_api.client.GitRepository;

final public class GitRepositoryEntity implements GitRepository {

private final String name;
private final Integer id;
private final String path;

public GitRepositoryEntity(@JsonProperty("name") String name,
@JsonProperty("id") Integer id,
@JsonProperty("path") String path)
{
this.name = name;
this.id = id;
this.path = path;
}

@Override
public String getName() {
return this.name;
}

@Override
public Integer getId() {
return this.id;
}

@Override
public String getPath() {
return this.path;
}
}
Loading

0 comments on commit 73d09cc

Please sign in to comment.