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

Fix history displays irrelevant changes #922

Closed
wants to merge 5 commits into from
Closed
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 @@ -30,8 +30,12 @@ public final class HttpApiV1Constants {

public static final String PROJECTS_PREFIX = API_V1_PATH_PREFIX + PROJECTS;

public static final String PROJECTS_VO_PREFIX = API_V0_PATH_PREFIX + PROJECTS;

public static final String REPOS = "/repos";

public static final String REPOSITORIES = "/repositories";

public static final String COMMITS = "/commits";

public static final String COMPARE = "/compare";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,11 @@ public CompletionStage<List<CommitDto>> getHistory(@Param String projectName,
@Param String path,
@Param @Default("-1") String from,
@Param @Default("1") String to) {
final String pathPattern = "/".equals(path) ? "/**" : path + "/**";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other APIs we have (e.g. watch*, diff*) don't add a slash at the end.
For consistency, I think it would be better that all APIs that accept a pathPattern have the same behavior.
Also, I think it is perfectly valid to watch /a** depending on the use-case.

Question) What do you think of fixing this issue from the client side instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jrhee17, about fixing this issue from the client side, do you mean we receive the list result from the client and filter?
From my side, it is not good to do logic on the client side.
@minwoox How do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about the late reply. Yeah, I also thought we should fix the UI (which is the client) to call the path correctly when I first created the issue.
We should let users retrieve whatever history they want so I think we should touch it on the server-side.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sound good @minwoox, could you provide me the screenshot to explain expected from client? I am happy to finish this 🚀

return projectApiManager.getProject(projectName).repos().get(repoName)
.history(new Revision(from),
new Revision(to),
path + "**")
pathPattern)
.thenApply(commits -> commits.stream()
.map(DtoConverter::convert)
.collect(toList()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* Copyright 2024 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.linecorp.centraldogma.server.internal.admin.service;

import static com.linecorp.centraldogma.internal.api.v1.HttpApiV1Constants.PROJECTS_PREFIX;
import static com.linecorp.centraldogma.internal.api.v1.HttpApiV1Constants.PROJECTS_VO_PREFIX;
import static com.linecorp.centraldogma.internal.api.v1.HttpApiV1Constants.REPOS;
import static com.linecorp.centraldogma.internal.api.v1.HttpApiV1Constants.REPOSITORIES;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.util.stream.StreamSupport;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import com.fasterxml.jackson.databind.JsonNode;

import com.linecorp.armeria.client.WebClient;
import com.linecorp.armeria.client.WebClientBuilder;
import com.linecorp.armeria.common.AggregatedHttpResponse;
import com.linecorp.armeria.common.HttpHeaderNames;
import com.linecorp.armeria.common.HttpMethod;
import com.linecorp.armeria.common.HttpStatus;
import com.linecorp.armeria.common.MediaType;
import com.linecorp.armeria.common.RequestHeaders;
import com.linecorp.armeria.common.ResponseHeaders;
import com.linecorp.centraldogma.internal.Jackson;
import com.linecorp.centraldogma.server.CentralDogmaBuilder;
import com.linecorp.centraldogma.testing.junit.CentralDogmaExtension;

class RepositoryServiceTest {

@RegisterExtension
static final CentralDogmaExtension dogma = new CentralDogmaExtension() {
@Override
protected void configure(CentralDogmaBuilder builder) {
builder.webAppEnabled(true);
}

@Override
protected void configureHttpClient(WebClientBuilder builder) {
builder.addHeader(HttpHeaderNames.AUTHORIZATION, "Bearer anonymous");
}
};

private static final String REPOS_PREFIX = PROJECTS_PREFIX + "/myPro" + REPOS;
private static final String REPOSITORIES_PREFIX = PROJECTS_VO_PREFIX + "/myPro" + REPOSITORIES;
private static final String FILES_PATH = "/files/revisions/head";

@BeforeAll
static void setUp() {
createProject(dogma);
}

@Test
void getHistory() throws IOException {
final WebClient client = dogma.httpClient();
final String repoName = "myRepo";
final AggregatedHttpResponse myRepoCreatedResponse = createRepository(client, repoName);
final ResponseHeaders repoResponseHeaders = ResponseHeaders.of(myRepoCreatedResponse.headers());
assertThat(repoResponseHeaders.status()).isEqualTo(HttpStatus.CREATED);

final String repoNamePrefix = "/myRepo";
final AggregatedHttpResponse barCreatedResponse = createFile(client,
repoNamePrefix,
"/foo/bar.txt",
"bar.txt",
"Bar");
final ResponseHeaders fileResponseHeader1 = ResponseHeaders.of(barCreatedResponse.headers());
assertThat(fileResponseHeader1.status()).isEqualTo(HttpStatus.CREATED);

final AggregatedHttpResponse bar2Created = createFile(client,
repoNamePrefix,
"/foo2/bar2.txt",
"bar2.txt",
"Bar 2");
final ResponseHeaders file2ResponseHeaders = ResponseHeaders.of(bar2Created.headers());
assertThat(file2ResponseHeaders.status()).isEqualTo(HttpStatus.CREATED);

final AggregatedHttpResponse myRepoHistoryResponse = getHistory(client, repoNamePrefix, "/");
final JsonNode myRepoHistory = Jackson.readTree(myRepoHistoryResponse.contentUtf8());
assertThat(myRepoHistory.size()).isEqualTo(3);
assertThat(myRepoHistory.get(0).get("summary").asText()).isEqualTo("Add /foo2/bar2.txt");
assertThat(myRepoHistory.get(1).get("summary").asText()).isEqualTo("Add /foo/bar.txt");
assertThat(myRepoHistory.get(2).get("summary").asText()).isEqualTo("Create a new repository");

final AggregatedHttpResponse fooHistoryResponse = getHistory(client, repoNamePrefix, "/foo");
final JsonNode fooHistory = Jackson.readTree(fooHistoryResponse.contentUtf8());
assertThat(fooHistory.get(0).get("summary").asText()).isEqualTo("Add /foo/bar.txt");
final boolean containsUndesiredString = StreamSupport.stream(fooHistory.spliterator(), false)
.anyMatch(node -> "Add /foo2/bar2.txt".equals(node.get("summary").asText()));
assertThat(containsUndesiredString).isFalse();

final AggregatedHttpResponse foo2HistoryResponse = getHistory(client, repoNamePrefix, "/foo2");
final JsonNode foo2History = Jackson.readTree(foo2HistoryResponse.contentUtf8());
assertThat(foo2History.get(0).get("summary").asText()).isEqualTo("Add /foo2/bar2.txt");
}

private static AggregatedHttpResponse getHistory(WebClient client, String repoName, String childRepoName) {
final RequestHeaders headers = RequestHeaders.of(HttpMethod.GET,
REPOSITORIES_PREFIX + repoName + "/history" + childRepoName + "?from=head&to=1",
HttpHeaderNames.CONTENT_TYPE, MediaType.JSON);
return client.execute(headers).aggregate().join();
}

private static AggregatedHttpResponse createRepository(WebClient client, String repoName) {
final RequestHeaders headers = RequestHeaders.of(HttpMethod.POST, REPOS_PREFIX,
HttpHeaderNames.CONTENT_TYPE, MediaType.JSON);
final String body = "{\"name\": \"" + repoName + "\"}";
return client.execute(headers, body).aggregate().join();
}

private static AggregatedHttpResponse createFile(WebClient client,
String repoName,
String path,
String fileName,
String content) {
final RequestHeaders headers = RequestHeaders.of(HttpMethod.POST,
REPOSITORIES_PREFIX + repoName + FILES_PATH,
HttpHeaderNames.CONTENT_TYPE, MediaType.JSON);
final String body = "{\n" +
" \"file\": {\n" +
" \"name\": \"" + fileName + "\",\n" +
" \"type\": \"TEXT\",\n" +
" \"content\": \"" + content + "\",\n" +
" \"path\": \"" + path + "\"\n" +
" },\n" +
" \"commitMessage\": {\n" +
" \"summary\": \"Add " + path + "\",\n" +
" \"detail\": {\n" +
" \"content\": \"\",\n" +
" \"markup\": \"PLAINTEXT\"\n" +
" }\n" +
" }\n" +
'}';
return client.execute(headers, body).aggregate().join();
}

private static void createProject(CentralDogmaExtension dogma) {
final String body = "{\"name\": \"myPro\"}";
final RequestHeaders headers = RequestHeaders.of(HttpMethod.POST, PROJECTS_PREFIX,
HttpHeaderNames.CONTENT_TYPE, MediaType.JSON);
final WebClient client = dogma.httpClient();
client.execute(headers, body).aggregate().join();
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@minwoox here, I try to add tests for the class RepositoryServiceTest, but it always returns 404 for /api/v0 (I try users/me to make sure I am correct ^^)
Do you know how to start the /api/v0 dogma for the test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's activated only when the webAppEnabled is true.


By the way, could you tell me why you need to retrieve the user information?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @minwoox, I can call /api/v0 now.
I just do the simplest test with api/v0 to explain to you my problem 🤩

}
Loading