Skip to content
Open
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 @@ -20,7 +20,6 @@
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Nonnull;
import java.util.List;
import java.util.Optional;

@ConditionalOnProperty(prefix = "source-control", name = "type", havingValue = "BITBUCKET")
Expand Down Expand Up @@ -102,12 +101,11 @@ default PullRequest createPullRequest(String repoFullName, PullRequestToCreate n
return BitbucketReferenceClient.createPullRequest(newPr);
}

@GetMapping(value = "/repos/{repositorySlug}/browse/{path}?at={branch}&blame=true&noContent=true",
@GetMapping(value = "/repos/{repositorySlug}/commits?until={branch}&limit=1",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
@Nonnull
List<Blame> fetchCommits(@PathVariable("repositorySlug") String repositorySlug, @PathVariable("path") String path,
@PathVariable("branch") String branch);
Commits fetchCommits(@PathVariable("repositorySlug") String repositorySlug, @PathVariable("branch") String branch);

@Data
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ public RemoteForBitbucketBulkActionsWrapper(FeignRemoteForBitbucketBulkActions f
public ResourceContent fetchContent(String repoFullName, String path, String branch) {

var bitbucketResourceContent=feignRemoteForBitbucketBulkActions.fetchContent(repoFullName,path,branch);
var blames = feignRemoteForBitbucketBulkActions.fetchCommits(repoFullName, path, branch);
var findLatestCommitHash = blames.stream().max(Comparator.comparing(Blame::getCommitterTimestamp)).orElse(null);
return ResourceContent.builder().base64EncodedContent(Base64.getEncoder().encodeToString(bitbucketResourceContent.getBytes(StandardCharsets.UTF_8)))
.sha(findLatestCommitHash.getCommitHash())
.build();
var findFirstCommitHash = feignRemoteForBitbucketBulkActions.fetchCommits(repoFullName, branch).getValues().stream()
.findFirst()
.map(com.societegenerale.cidroid.tasks.consumer.infrastructure.bitbucket.model.Commit::getId)
.orElse(null);

return ResourceContent.builder().base64EncodedContent(Base64.getEncoder()
.encodeToString(bitbucketResourceContent.getBytes(StandardCharsets.UTF_8)))
.sha(findFirstCommitHash)
.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
package com.societegenerale.cidroid.tasks.consumer.infrastructure.bitbucket.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Commit {

private String id;

private String url;

private User author;

com.societegenerale.cidroid.tasks.consumer.services.model.Commit toStandardCommit(){

return com.societegenerale.cidroid.tasks.consumer.services.model.Commit.builder()
.id(this.id)
.url(this.url)
.author(com.societegenerale.cidroid.tasks.consumer.services.model.User.builder()
.login(author.getLogin())
.email(author.getEmail()).build())
.build();

}



}

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.societegenerale.cidroid.tasks.consumer.infrastructure.bitbucket.model;


import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.annotation.Nonnull;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Commits {

@Nonnull
private List<Commit> values;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.societegenerale.cidroid.tasks.consumer.infrastructure.bitbucket;

import com.societegenerale.cidroid.tasks.consumer.infrastructure.bitbucket.model.Blame;
import com.societegenerale.cidroid.tasks.consumer.infrastructure.bitbucket.model.DirectCommit;
import com.societegenerale.cidroid.tasks.consumer.infrastructure.bitbucket.model.Project;
import com.societegenerale.cidroid.tasks.consumer.infrastructure.bitbucket.model.UpdatedResource;
import com.societegenerale.cidroid.tasks.consumer.infrastructure.bitbucket.model.*;
import com.societegenerale.cidroid.tasks.consumer.services.exceptions.BranchAlreadyExistsException;
import com.societegenerale.cidroid.tasks.consumer.services.exceptions.RemoteSourceControlAuthorizationException;
import com.societegenerale.cidroid.tasks.consumer.services.model.Commit;
Expand All @@ -28,11 +25,8 @@ class RemoteForBitbucketBulkActionsWrapperTest {
@Test
void should_fetch_encoded_content_and_latest_commit_id() {
when(feignRemoteForBitbucketBulkActions.fetchContent("CI-Repo", "jenkin", "master")).thenReturn("Raw content");
ZonedDateTime now = ZonedDateTime.now();
when(feignRemoteForBitbucketBulkActions.fetchCommits("CI-Repo", "jenkin", "master"))
.thenReturn(List.of(new Blame(now, "commitHashToday"),new Blame(now.minusDays(1), "commitHashYesterday"),
new Blame(now.minusDays(2), "commitHash2daysBefore")
));
when(feignRemoteForBitbucketBulkActions.fetchCommits("CI-Repo", "master"))
.thenReturn(new Commits(List.of(new com.societegenerale.cidroid.tasks.consumer.infrastructure.bitbucket.model.Commit( "commitHashToday"))));
ResourceContent resourceContent = remoteForBitbucketBulkActionsWrapper.fetchContent("CI-Repo", "jenkin", "master");
assertThat(resourceContent.getBase64EncodedContent()).isEqualTo("UmF3IGNvbnRlbnQ=");
assertThat(resourceContent.getSha()).isEqualTo("commitHashToday");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.societegenerale.cidroid.tasks.consumer.infrastructure.bitbucket.model.Blame;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
Expand All @@ -11,8 +10,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Objects;

import static org.mockserver.model.HttpRequest.request;
Expand Down Expand Up @@ -89,12 +86,11 @@ protected void initRoutes() {
mockServer
.when(request()
.withMethod("GET")
.withPath("/api/projects/public-project/repos/my-repo/browse/Jenkinsfile")
.withQueryStringParameter("at", "newJavaImageForJenkinsBuild")
.withQueryStringParameter("blame", "true")
.withQueryStringParameter("noContent", "true"))
.withPath("/api/projects/public-project/repos/my-repo/commits")
.withQueryStringParameter("until", "newJavaImageForJenkinsBuild")
.withQueryStringParameter("limit", "1"))

.respond(returnBlames());
.respond(returnCommits());
mockServer
.when(request()
.withMethod("PUT")
Expand Down Expand Up @@ -129,12 +125,9 @@ private HttpResponse returnReference() {
}

@SneakyThrows
private HttpResponse returnBlames() {
ZonedDateTime now = ZonedDateTime.now();
private HttpResponse returnCommits() {
return response()
// No mock response found https://developer.atlassian.com/server/bitbucket/rest/v804/api-group-repository/#api-api-latest-projects-projectkey-repos-repositoryslug-browse-path-get
.withBody(objectMapper.writeValueAsString(List.of(new Blame(now.minusDays(2), "commitHash2daysBefore"),
new Blame(now, "commitHashToday"),new Blame(now.minusDays(1), "commitHashYesterday"))))
.withBody(readFromFile("commits.json"))
.withHeader("Content-Type", "application/json")
.withStatusCode(200);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"values": [
{
"message": "More work on feature 1",
"author": {
"name": "Charlie",
"emailAddress": "charlie@example.com"
},
"parents": [
{
"id": "abcdef0123abcdef4567abcdef8987abcdef6543",
"displayId": "abcdef0"
}
],
"authorTimestamp": 1359075920,
"committer": {
"name": "Charlie",
"emailAddress": "charlie@example.com"
},
"committerTimestamp": 1449075830,
"id": "abcdef0123abcdef4567abcdef8987abcdef6543",
"displayId": "abcdef0"
}
],
"size": 1,
"limit": 25,
"start": 2154,
"isLastPage": true,
"nextPageStart": 2154
}