-
Notifications
You must be signed in to change notification settings - Fork 0
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
Хелпер для загрузки BSL LS #3
Draft
otymko
wants to merge
4
commits into
master
Choose a base branch
from
feature/issue2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/main/java/com/github/_1c_syntax/utils/DownloadHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* This file is a part of 1c-syntax utils. | ||
* | ||
* Copyright © 2018-2020 | ||
* Alexey Sosnoviy <[email protected]>, Nikita Gryzlov <[email protected]> and contributors | ||
* | ||
* SPDX-License-Identifier: LGPL-3.0-or-later | ||
* | ||
* 1c-syntax utils is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3.0 of the License, or (at your option) any later version. | ||
* | ||
* 1c-syntax utils is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with 1c-syntax utils. | ||
*/ | ||
package com.github._1c_syntax.utils; | ||
|
||
import lombok.experimental.UtilityClass; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.io.FileUtils; | ||
import org.kohsuke.github.GHAsset; | ||
import org.kohsuke.github.GHRelease; | ||
import org.kohsuke.github.GHRepository; | ||
import org.kohsuke.github.GitHub; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
|
||
@UtilityClass | ||
@Slf4j | ||
public class DownloadHelper { | ||
|
||
private final String REPO_PATH = "1c-syntax/bsl-language-server"; | ||
private final String CONTENT_TYPE = "application/zip"; | ||
private final int CONNECTION_TIMEOUT = 900; | ||
|
||
public final String SUPPLY_FOR_WIN = "win"; | ||
public final String SUPPLY_FOR_MAC = "mac"; | ||
public final String SUPPLY_FOR_LINUX = "nix"; | ||
|
||
public Optional<GHAsset> getLatestRelease(String token, String supplyVariant, Path pathForSave) { | ||
return getLatestRelease(token, supplyVariant, pathForSave, false); | ||
} | ||
|
||
public Optional<GHAsset> getLatestRelease(String token, String supplyVariant, Path pathForSave, boolean prerelease) { | ||
Optional<GHAsset> ghAsset; | ||
try { | ||
GitHub github = GitHub.connectUsingOAuth(token); | ||
ghAsset = Optional.ofNullable(github.getRepository(REPO_PATH)) | ||
.flatMap(repo -> { | ||
try { | ||
return getAssetsFromRelease(repo, supplyVariant, prerelease); | ||
} catch (IOException e) { | ||
LOGGER.error("Error finding asset from release", e); | ||
return Optional.empty(); | ||
} | ||
}); | ||
|
||
} catch (IOException e) { | ||
LOGGER.error("Error finding asset from release", e); | ||
ghAsset = Optional.empty(); | ||
} | ||
if (ghAsset.isPresent() && !downloadFromAsset(ghAsset.get(), pathForSave)) { | ||
return Optional.empty(); | ||
} | ||
return ghAsset; | ||
} | ||
|
||
private boolean downloadFromAsset(GHAsset ghAsset, Path path) { | ||
try { | ||
var file = Path.of(path.toString(), ghAsset.getName()).toFile(); | ||
FileUtils.copyURLToFile(new URL(ghAsset.getBrowserDownloadUrl()), file, CONNECTION_TIMEOUT, 0); | ||
} catch (IOException e) { | ||
LOGGER.error("An error occurred while loading", e); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
private Optional<GHAsset> getAssetsFromRelease(GHRepository repo, String supplyVariant, boolean prerelease) throws IOException { | ||
var endsName = supplyVariant + ".zip"; | ||
var release = getLatestRelease(repo, prerelease); | ||
if (release.isPresent()) { | ||
return release.get().getAssets().stream() | ||
.filter(ghAsset -> ghAsset.getContentType().equals(CONTENT_TYPE) | ||
&& ghAsset.getName().endsWith(endsName)) | ||
.findAny(); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
private Optional<GHRelease> getLatestRelease(GHRepository repo, boolean prerelease) throws IOException { | ||
return repo.listReleases().toList().stream() | ||
.filter(ghRelease -> ghRelease.isPrerelease() == prerelease) | ||
.limit(1) | ||
.findAny(); | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
src/test/java/com/github/_1c_syntax/utils/DownloadHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* This file is a part of 1c-syntax utils. | ||
* | ||
* Copyright © 2018-2020 | ||
* Alexey Sosnoviy <[email protected]>, Nikita Gryzlov <[email protected]> and contributors | ||
* | ||
* SPDX-License-Identifier: LGPL-3.0-or-later | ||
* | ||
* 1c-syntax utils is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3.0 of the License, or (at your option) any later version. | ||
* | ||
* 1c-syntax utils is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with 1c-syntax utils. | ||
*/ | ||
package com.github._1c_syntax.utils; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.kohsuke.github.GHAsset; | ||
|
||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class DownloadHelperTest { | ||
|
||
private final Path BASE_PATH = Path.of("build", "fixture"); | ||
|
||
@Test | ||
public void test_getLatestRelease() { | ||
prepareFolder(BASE_PATH); | ||
var token = System.getenv("GITHUB_TOKEN"); | ||
|
||
var ghAsset = DownloadHelper.getLatestRelease(token, DownloadHelper.SUPPLY_FOR_WIN, BASE_PATH); | ||
checkGHAsset(ghAsset); | ||
|
||
ghAsset = DownloadHelper.getLatestRelease(token, DownloadHelper.SUPPLY_FOR_LINUX, BASE_PATH); | ||
checkGHAsset(ghAsset); | ||
|
||
ghAsset = DownloadHelper.getLatestRelease(token, DownloadHelper.SUPPLY_FOR_MAC, BASE_PATH, true); | ||
checkGHAsset(ghAsset); | ||
} | ||
|
||
void checkGHAsset(Optional<GHAsset> ghAsset) { | ||
assertThat(ghAsset).isPresent(); | ||
var file = Path.of(BASE_PATH.toString(), ghAsset.get().getName()).toFile(); | ||
assertThat(file).exists(); | ||
} | ||
|
||
|
||
private void prepareFolder(Path path) { | ||
path.toFile().mkdir(); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
вот эта логика не совсем верная. если ты посмотришь в реализацию загрузчика для vsc, то в случае если запрашивается последний релиз по каналу prerelease и если релиз вышел после пререлиза, то вернется релиз. а в твоем коде - пререлиз.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Спасибо! Посмотрю