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

Issue 3683 GitHub Support - fixes #3704

Merged
merged 2 commits into from
Sep 18, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public interface BitbucketCloudApi {
String PAGE = "page";
String PAGE_LENGTH = "pagelen";
String BRANCH = "branch";

String REVISION = "revision";

@GET("{api_version}/repositories/{workspace}/{repository}")
Call<BitbucketCloudRepository> getRepository(@Path(API_VERSION) String apiVersion,
Expand Down Expand Up @@ -109,10 +109,11 @@ Call<BitbucketCloudRef> createTag(@Path(API_VERSION) String apiVersion,
@Path(REPOSITORY) String repository,
@Body BitbucketCloudRef tag);

@GET("{api_version}/repositories/{workspace}/{repository}/commits")
@GET("{api_version}/repositories/{workspace}/{repository}/commits/{revision}")
Call<BitbucketCloudPagedResponse<BitbucketCloudCommit>> getCommits(@Path(API_VERSION) String apiVersion,
@Path(WORKSPACE) String workspace,
@Path(REPOSITORY) String repository,
@Path(REVISION) String revision,
@Query(PAGE) Integer page,
@Query(PAGE_LENGTH) Integer pageLength);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ public BitbucketCloudRef createTag(final BitbucketCloudRef tag) {
return RestApiUtils.execute(bitbucketServerApi.createTag(apiVersion, projectName, repositoryName, tag));
}

public BitbucketCloudPagedResponse<BitbucketCloudCommit> getLastCommit() {
return RestApiUtils.execute(bitbucketServerApi.getCommits(apiVersion, projectName, repositoryName, null, null));
public BitbucketCloudPagedResponse<BitbucketCloudCommit> getLastCommit(final String revision) {
return RestApiUtils.execute(bitbucketServerApi.getCommits(apiVersion, projectName, repositoryName,
revision, null, null));
}

public BitbucketCloudCommit getCommit(final String commitId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public Revision createTag(final Pipeline pipeline, final String tagName, final S

@Override
public Revision getLastRevision(final Pipeline pipeline, final String ref) {
final BitbucketCloudPagedResponse<BitbucketCloudCommit> commits = getClient(pipeline).getLastCommit();
final BitbucketCloudPagedResponse<BitbucketCloudCommit> commits = getClient(pipeline).getLastCommit(ref);
return Optional.ofNullable(commits)
.flatMap(value -> ListUtils.emptyIfNull(value.getValues()).stream()
.findFirst()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ public interface GitHubApi {
String COMMIT_ID = "commitId";
String TAG_NAME = "tagName";
String PAGE = "page";
String SHA = "sha";
String PAGE_LENGTH = "per_page";

String REF = "ref";

@GET("repos/{workspace}/{repository}")
Call<GitHubRepository> getRepository(@Path(WORKSPACE) String workspace,
Expand All @@ -58,6 +59,7 @@ Call<GitHubRepository> getRepository(@Path(WORKSPACE) String workspace,
Call<GitHubRepository> createRepository(@Path(WORKSPACE) String workspace,
@Path(REPOSITORY) String repository,
@Body GitHubRepository bitbucketRepository);

@PATCH("repos/{workspace}/{repository}")
Call<GitHubRepository> updateRepository(@Path(WORKSPACE) String workspace,
@Path(REPOSITORY) String repository,
Expand Down Expand Up @@ -94,6 +96,7 @@ Call<GitHubTag> createTag(@Path(WORKSPACE) String workspace,
Call<GitHubRef> createRef(@Path(WORKSPACE) String workspace,
@Path(REPOSITORY) String repository,
@Body GitHubRef ref);

@POST("repos/{workspace}/{repository}/releases")
Call<GitHubRelease> createRelease(@Path(WORKSPACE) String workspace,
@Path(REPOSITORY) String repository,
Expand All @@ -102,34 +105,37 @@ Call<GitHubRelease> createRelease(@Path(WORKSPACE) String workspace,
@GET("repos/{workspace}/{repository}/commits")
Call<List<GitHubCommitNode>> getCommits(@Path(WORKSPACE) String workspace,
@Path(REPOSITORY) String repository,
@Query(SHA) String sha,
@Query(PAGE) Integer page,
@Query(PAGE_LENGTH) Integer pageLength);

@GET("repos/{workspace}/{repository}/commits/{commitId}")
Call<GitHubCommitNode> getCommit(@Path(WORKSPACE) String workspace,
@Path(REPOSITORY) String repository,
@Path(COMMIT_ID) String commitId);
@Path(REPOSITORY) String repository,
@Path(COMMIT_ID) String commitId);

@GET("repos/{workspace}/{repository}/releases/tags/{tagName}")
Call<GitHubRelease> getTag(@Path(WORKSPACE) String workspace,
@Path(REPOSITORY) String repository,
@Path(TAG_NAME) String tagName);

@GET("repos/{workspace}/{repository}/contents/{path}")
Call<List<GitHubContent>> getContents(@Path(WORKSPACE) String workspace,
@Path(REPOSITORY) String repository,
@Path(value = PATH) String path,
@Query("ref") String commit);
@Query(REF) String commit);

@GET("repos/{workspace}/{repository}/git/trees/{sha}")
Call<GitHubTree> getTree(@Path(WORKSPACE) String workspace,
@Path(REPOSITORY) String repository,
@Path(value = "sha") String sha,
@Path(value = SHA) String sha,
@Query("recursive") Boolean recursive);

@GET("repos/{workspace}/{repository}/contents/{path}")
Call<GitHubContent> getFile(@Path(WORKSPACE) String workspace,
@Path(REPOSITORY) String repository,
@Path(value = PATH) String path,
@Query("ref") String commit);
@Query(REF) String ref);

@GET("repos/{workspace}/{repository}/branches")
Call<List<GitHubRef>> getBranches(@Path(WORKSPACE) String workspace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public GitHubContent getRawFileContent(final String commit, final String path) {
return RestApiUtils.execute(serverApi.getFile(projectName, repositoryName, path, commit));
}

public boolean fileExists(final String path) {
final Call<GitHubContent> call = serverApi.getFile(projectName, repositoryName, path, null);
public boolean fileExists(final String ref, final String path) {
final Call<GitHubContent> call = serverApi.getFile(projectName, repositoryName, path, ref);
try {
final Response<GitHubContent> response = call.execute();
return response.isSuccessful();
Expand All @@ -97,9 +97,8 @@ public GitHubSource createFile(final String path, final String content, final St
return createFile(path, request);
}

public GitHubSource updateFile(final String path, final String content, final String message,
final String branch) {
final GitHubContent file = RestApiUtils.execute(serverApi.getFile(projectName, repositoryName, path, null));
public GitHubSource updateFile(final String path, final String content, final String message, final String branch) {
final GitHubContent file = RestApiUtils.execute(serverApi.getFile(projectName, repositoryName, path, branch));
final GitHubContent request = GitHubContent.builder()
.sha(file.getSha())
.message(message)
Expand All @@ -110,7 +109,7 @@ public GitHubSource updateFile(final String path, final String content, final St
}

public GitHubSource deleteFile(final String path, final String message, final String branch) {
final GitHubContent file = RestApiUtils.execute(serverApi.getFile(projectName, repositoryName, path, null));
final GitHubContent file = RestApiUtils.execute(serverApi.getFile(projectName, repositoryName, path, branch));
final GitHubContent request = GitHubContent.builder()
.sha(file.getSha())
.message(message)
Expand All @@ -123,7 +122,7 @@ public GitHubSource createFile(final String path, final byte[] content, final St
final GitHubContent request = GitHubContent.builder()
.message(message)
.branch(branch)
.content(Base64.getMimeEncoder().encodeToString(content))
.content(Base64.getEncoder().encodeToString(content))
.build();
return createFile(path, request);
}
Expand All @@ -144,8 +143,8 @@ public void createRelease(final GitHubRelease ref) {
RestApiUtils.execute(serverApi.createRelease(projectName, repositoryName, ref));
}

public Response<List<GitHubCommitNode>> getLastCommit() {
return RestApiUtils.getResponse(serverApi.getCommits(projectName, repositoryName, null, null));
public Response<List<GitHubCommitNode>> getLastCommit(final String sha) {
return RestApiUtils.getResponse(serverApi.getCommits(projectName, repositoryName, sha, null, null));
}

public GitHubCommitNode getCommit(final String commitId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public Revision createTag(final Pipeline pipeline, final String tagName, final S

@Override
public Revision getLastRevision(final Pipeline pipeline, final String ref) {
final Response<List<GitHubCommitNode>> commits = getClient(pipeline).getLastCommit();
final Response<List<GitHubCommitNode>> commits = getClient(pipeline).getLastCommit(ref);
return Optional.ofNullable(commits.body()).orElse(Collections.emptyList()).stream()
.findFirst()
.map(mapper::commitToRevision)
Expand Down Expand Up @@ -342,7 +342,7 @@ public GitCommitEntry uploadFiles(final Pipeline pipeline,

@Override
public boolean fileExists(final Pipeline pipeline, final String filePath) {
return fileExists(getClient(pipeline), filePath);
return fileExists(getClient(pipeline), pipeline.getBranch(), filePath);
}

private static String getContentSha(final String rawPath, final String version, final GitHubClient client) {
Expand Down Expand Up @@ -412,7 +412,7 @@ private String getContentType(final String type) {
}
}

private boolean fileExists(final GitHubClient client, final String path) {
return client.fileExists(path);
private boolean fileExists(final GitHubClient client, final String ref, final String path) {
return client.fileExists(ref, path);
}
}
Loading