From 1876d8ce8808ff4ff8366be29add81da709654c9 Mon Sep 17 00:00:00 2001 From: Oksana Kolesnikova <83080415+okolesn@users.noreply.github.com> Date: Wed, 18 Sep 2024 14:06:52 +0300 Subject: [PATCH] Issue 3683 GitHub Support - fixes (#3704) * Issue 3683 GitHub Support - fixes for 1. branches 2. file upload * Issue 3683 GitHub Support - fix syntax --- .../git/bitbucketcloud/BitbucketCloudApi.java | 5 +++-- .../bitbucketcloud/BitbucketCloudClient.java | 5 +++-- .../bitbucketcloud/BitbucketCloudService.java | 2 +- .../pipeline/manager/git/github/GitHubApi.java | 18 ++++++++++++------ .../manager/git/github/GitHubClient.java | 17 ++++++++--------- .../manager/git/github/GitHubService.java | 8 ++++---- 6 files changed, 31 insertions(+), 24 deletions(-) diff --git a/api/src/main/java/com/epam/pipeline/manager/git/bitbucketcloud/BitbucketCloudApi.java b/api/src/main/java/com/epam/pipeline/manager/git/bitbucketcloud/BitbucketCloudApi.java index fe01495811..7eee0361da 100644 --- a/api/src/main/java/com/epam/pipeline/manager/git/bitbucketcloud/BitbucketCloudApi.java +++ b/api/src/main/java/com/epam/pipeline/manager/git/bitbucketcloud/BitbucketCloudApi.java @@ -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 getRepository(@Path(API_VERSION) String apiVersion, @@ -109,10 +109,11 @@ Call 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> 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); diff --git a/api/src/main/java/com/epam/pipeline/manager/git/bitbucketcloud/BitbucketCloudClient.java b/api/src/main/java/com/epam/pipeline/manager/git/bitbucketcloud/BitbucketCloudClient.java index fde9fbcdfa..0316a92723 100644 --- a/api/src/main/java/com/epam/pipeline/manager/git/bitbucketcloud/BitbucketCloudClient.java +++ b/api/src/main/java/com/epam/pipeline/manager/git/bitbucketcloud/BitbucketCloudClient.java @@ -112,8 +112,9 @@ public BitbucketCloudRef createTag(final BitbucketCloudRef tag) { return RestApiUtils.execute(bitbucketServerApi.createTag(apiVersion, projectName, repositoryName, tag)); } - public BitbucketCloudPagedResponse getLastCommit() { - return RestApiUtils.execute(bitbucketServerApi.getCommits(apiVersion, projectName, repositoryName, null, null)); + public BitbucketCloudPagedResponse getLastCommit(final String revision) { + return RestApiUtils.execute(bitbucketServerApi.getCommits(apiVersion, projectName, repositoryName, + revision, null, null)); } public BitbucketCloudCommit getCommit(final String commitId) { diff --git a/api/src/main/java/com/epam/pipeline/manager/git/bitbucketcloud/BitbucketCloudService.java b/api/src/main/java/com/epam/pipeline/manager/git/bitbucketcloud/BitbucketCloudService.java index 42ea4243cb..fd1fa240c9 100644 --- a/api/src/main/java/com/epam/pipeline/manager/git/bitbucketcloud/BitbucketCloudService.java +++ b/api/src/main/java/com/epam/pipeline/manager/git/bitbucketcloud/BitbucketCloudService.java @@ -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 commits = getClient(pipeline).getLastCommit(); + final BitbucketCloudPagedResponse commits = getClient(pipeline).getLastCommit(ref); return Optional.ofNullable(commits) .flatMap(value -> ListUtils.emptyIfNull(value.getValues()).stream() .findFirst() diff --git a/api/src/main/java/com/epam/pipeline/manager/git/github/GitHubApi.java b/api/src/main/java/com/epam/pipeline/manager/git/github/GitHubApi.java index 295d4f10ad..c65ef991a3 100644 --- a/api/src/main/java/com/epam/pipeline/manager/git/github/GitHubApi.java +++ b/api/src/main/java/com/epam/pipeline/manager/git/github/GitHubApi.java @@ -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 getRepository(@Path(WORKSPACE) String workspace, @@ -58,6 +59,7 @@ Call getRepository(@Path(WORKSPACE) String workspace, Call createRepository(@Path(WORKSPACE) String workspace, @Path(REPOSITORY) String repository, @Body GitHubRepository bitbucketRepository); + @PATCH("repos/{workspace}/{repository}") Call updateRepository(@Path(WORKSPACE) String workspace, @Path(REPOSITORY) String repository, @@ -94,6 +96,7 @@ Call createTag(@Path(WORKSPACE) String workspace, Call createRef(@Path(WORKSPACE) String workspace, @Path(REPOSITORY) String repository, @Body GitHubRef ref); + @POST("repos/{workspace}/{repository}/releases") Call createRelease(@Path(WORKSPACE) String workspace, @Path(REPOSITORY) String repository, @@ -102,34 +105,37 @@ Call createRelease(@Path(WORKSPACE) String workspace, @GET("repos/{workspace}/{repository}/commits") Call> 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 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 getTag(@Path(WORKSPACE) String workspace, @Path(REPOSITORY) String repository, @Path(TAG_NAME) String tagName); + @GET("repos/{workspace}/{repository}/contents/{path}") Call> 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 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 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> getBranches(@Path(WORKSPACE) String workspace, diff --git a/api/src/main/java/com/epam/pipeline/manager/git/github/GitHubClient.java b/api/src/main/java/com/epam/pipeline/manager/git/github/GitHubClient.java index cfc31d3732..0a51e00069 100644 --- a/api/src/main/java/com/epam/pipeline/manager/git/github/GitHubClient.java +++ b/api/src/main/java/com/epam/pipeline/manager/git/github/GitHubClient.java @@ -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 call = serverApi.getFile(projectName, repositoryName, path, null); + public boolean fileExists(final String ref, final String path) { + final Call call = serverApi.getFile(projectName, repositoryName, path, ref); try { final Response response = call.execute(); return response.isSuccessful(); @@ -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) @@ -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) @@ -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); } @@ -144,8 +143,8 @@ public void createRelease(final GitHubRelease ref) { RestApiUtils.execute(serverApi.createRelease(projectName, repositoryName, ref)); } - public Response> getLastCommit() { - return RestApiUtils.getResponse(serverApi.getCommits(projectName, repositoryName, null, null)); + public Response> getLastCommit(final String sha) { + return RestApiUtils.getResponse(serverApi.getCommits(projectName, repositoryName, sha, null, null)); } public GitHubCommitNode getCommit(final String commitId) { diff --git a/api/src/main/java/com/epam/pipeline/manager/git/github/GitHubService.java b/api/src/main/java/com/epam/pipeline/manager/git/github/GitHubService.java index 5125c00493..c7b672c229 100644 --- a/api/src/main/java/com/epam/pipeline/manager/git/github/GitHubService.java +++ b/api/src/main/java/com/epam/pipeline/manager/git/github/GitHubService.java @@ -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> commits = getClient(pipeline).getLastCommit(); + final Response> commits = getClient(pipeline).getLastCommit(ref); return Optional.ofNullable(commits.body()).orElse(Collections.emptyList()).stream() .findFirst() .map(mapper::commitToRevision) @@ -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) { @@ -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); } }