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

Avoid caching when getting a raw file from raw.githubusercontent.com #376

Merged
merged 1 commit into from
Nov 14, 2022

Conversation

vitaliy-guliy
Copy link
Contributor

@vitaliy-guliy vitaliy-guliy commented Oct 27, 2022

Signed-off-by: Vitaliy Gulyy [email protected]

What does this PR do?

At the moment it's difficult to test the flow on custom git servers, so the functionality is temporary enabled only for github.com.

To prevent caching of raw file content, when addressing the file via https://raw.githubusercontent.com it needs to use SHA of the last commit instead of the branch name.
It gives us the unique URI on each file change.

E.g., use

https://raw.githubusercontent.com/vitaliy-guliy/web-nodejs-sample/ce7b7ab0aa069e0bda52eac761e2d763ae78bd71/devfile.yaml

instead of

https://raw.githubusercontent.com/vitaliy-guliy/web-nodejs-sample/devfilev2/devfile.yaml

When client asks the content of devfile.yaml in devfilev2 branch, che-server

  • gets the last commit for devfilev2 branch using GitHub rest API
https://api.github.com/repos/che-samples/web-nodejs-sample/commits?sha=devfilev2&page=1&per_page=1
  • forms new URI to the raw file, where the branch name is replaced on the commit SHA
https://raw.githubusercontent.com/vitaliy-guliy/web-nodejs-sample/ce7b7ab0aa069e0bda52eac761e2d763ae78bd71/devfile.yaml

Screenshot/screencast of this PR

What issues does this PR fix or reference?

eclipse-che/che#21184

Potentially fixes https://issues.redhat.com/browse/CRW-3449

How to test this PR?

  1. Deploy Che with
chectl server:deploy -p=minikube -i=quay.io/vgulyy/che-server:test-fetching-git-files
  1. Create a workspace from git repository
  2. Change the devfile, push changes
  3. Within a minute, delete workspace and create a new one using the same factory link
  4. Check if the changes have been applied

PR Checklist

As the author of this Pull Request I made sure that:

Reviewers

Reviewers, please comment how you tested the PR when approving it.

@vitaliy-guliy vitaliy-guliy marked this pull request as draft October 27, 2022 12:06
@vitaliy-guliy vitaliy-guliy marked this pull request as ready for review October 27, 2022 14:09
@l0rd l0rd changed the title chore: Avoid caching when getting a raw file from raw.githubusercontent.com Avoid caching when getting a raw file from raw.githubusercontent.com Oct 28, 2022
@l0rd
Copy link
Contributor

l0rd commented Oct 28, 2022

@vinokurig @vitaliy-guliy I have removed the prefix Chore from the PR title. Chore means not important, a routine task. To write the release notes we review all the PR of the week but we ignore those prefixed with Chore. So it's important that you use it only for routine tasks (and this is definitely not a routine task but an important change).

@vitaliy-guliy vitaliy-guliy marked this pull request as draft November 3, 2022 13:05
@vitaliy-guliy vitaliy-guliy marked this pull request as ready for review November 3, 2022 18:44
"Accept",
"application/vnd.github.v3+json")
.timeout(DEFAULT_HTTP_TIMEOUT)
.build();
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a code duplication, why not to define the headers array out of the return statement?

String[] headers = {"Accept", "application/vnd.github.v3+json"};
if (!isNullOrEmpty(authenticationToken)) {
  headers[2] = "Authorization";
  headers[3] = "token " + authenticationToken;
}
return HttpRequest.newBuilder(uri)
        .headers(headers)
        .timeout(DEFAULT_HTTP_TIMEOUT)
        .build();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure it's a duplication.
There is only one if statement, which makes the code easy to read.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a duplication because there are several identical code lines:

  private HttpRequest buildGithubApiRequest(URI uri, @Nullable String authenticationToken) {
    if (isNullOrEmpty(authenticationToken)) {
>     return HttpRequest.newBuilder(uri)
          .headers("Accept", "application/vnd.github.v3+json")
>         .timeout(DEFAULT_HTTP_TIMEOUT)
>         .build();
    } else {
>     return HttpRequest.newBuilder(uri)
          .headers(
              "Authorization",
              "token " + authenticationToken,
              "Accept",
              "application/vnd.github.v3+json")
>         .timeout(DEFAULT_HTTP_TIMEOUT)
>         .build();
        }
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a question of taste and it should not block merging of this pull request.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't block the merging, I just express my point.

Copy link
Contributor

Choose a reason for hiding this comment

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

Or a little bit simpler with wrapping to list:

private HttpRequest buildGithubApiRequest(URI uri, String authenticationToken) {
  List<String> headers = Arrays.asList("Accept", "application/vnd.github.v3+json");
  if (!isNullOrEmpty(authenticationToken)) {
    headers.addAll(Arrays.asList("Authorization", "token " + authenticationToken));
  }
  return HttpRequest.newBuilder(uri)
      .headers(headers.toArray(String[]::new))
      .timeout(DEFAULT_HTTP_TIMEOUT)
      .build();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This looks much better. Let's go with that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It fails with java.lang.UnsupportedOperationException on

headers.addAll(Arrays.asList("Authorization", "token " + authenticationToken));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a code duplication, why not to define the headers array out of the return statement?

String[] headers = {"Accept", "application/vnd.github.v3+json"};
if (!isNullOrEmpty(authenticationToken)) {
  headers[2] = "Authorization";

Throws ArrayIndexOutOfBoundsException

java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2

headers[3] = "token " + authenticationToken;
}
return HttpRequest.newBuilder(uri)
.headers(headers)
.timeout(DEFAULT_HTTP_TIMEOUT)
.build();

Copy link
Contributor

Choose a reason for hiding this comment

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

Throws ArrayIndexOutOfBoundsException

I haven't tested it but I think wrapping to a list should work:

List<String> headers = Arrays.asList("Accept", "application/vnd.github.v3+json");
if (!isNullOrEmpty(authenticationToken)) {
  headers.addAll(Arrays.asList("Authorization", "token " + authenticationToken));
}

Copy link
Contributor

@svor svor left a comment

Choose a reason for hiding this comment

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

LGTM
I haven't detected a problem with caching. Tested with quay.io/vgulyy/che-server:test-fetching-git-files che-server image and my public git repository

@vinokurig
Copy link
Contributor

vinokurig commented Nov 8, 2022

I've tested the flow and confirm that it works.
I still expect the next changes to be applied:

@vitaliy-guliy
Copy link
Contributor Author

I've tested the flow and confirm that it works. I still expect the next changes to be applied:

Have been changed on private.

The current if statement in buildGithubApiRequest() is simple and understandable. As we have a different meaning on it, let's leave it for future improvements.

@vitaliy-guliy vitaliy-guliy merged commit cf508f0 into main Nov 14, 2022
@vitaliy-guliy vitaliy-guliy deleted the che-21184 branch November 14, 2022 07:20
@vitaliy-guliy vitaliy-guliy restored the che-21184 branch November 14, 2022 07:30
@che-bot che-bot added this to the 7.57 milestone Nov 14, 2022
@ibuziuk
Copy link
Member

ibuziuk commented Nov 14, 2022

@vitaliy-guliy we should probably backport the fix to 7.56.x wdyt?

@vitaliy-guliy vitaliy-guliy deleted the che-21184 branch November 14, 2022 12:12
@nickboldt
Copy link
Contributor

If backported to 7.56.x, please update https://issues.redhat.com/browse/CRW-3449 with link to the successful build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants