-
Notifications
You must be signed in to change notification settings - Fork 274
Preserve file permissions in the zip archive, fix bugs in gitignore matching and keep mvm and gradle files #5224
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
Closed
Closed
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
dbc2651
fix: Preserve file permissions in the zip archive, fix bugs in gitign…
8766b9f
update tests after rebase
a258118
change context
c26fed8
fix tests
af4ca75
Merge branch 'main' into main
osdemah 59c6146
Merge branch 'aws:main' into main
osdemah 7f2528b
improve the logic
ee599bf
remove unused imports
0482010
fix test
9c9c3b4
fix the path for windows
083fb66
only set permissions on posix machines
1a48ed1
use platform specific separators in tests
0be03c9
fix detekt errors
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
4 changes: 4 additions & 0 deletions
4
.changes/next-release/bugfix-2df57233-310e-4788-be6c-f3b37767e60a.json
This file contains hidden or 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,4 @@ | ||
| { | ||
| "type" : "bugfix", | ||
| "description" : "Preserve file permissions in the zip archive, fix bugs in gitignore matching and keep mvm and gradle files" | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -16,21 +16,27 @@ import kotlinx.coroutines.launch | |
| import kotlinx.coroutines.runBlocking | ||
| import kotlinx.coroutines.withContext | ||
| import org.apache.commons.codec.digest.DigestUtils | ||
| import software.aws.toolkits.core.utils.outputStream | ||
| import software.aws.toolkits.core.utils.putNextEntry | ||
| import org.apache.commons.io.FileUtils | ||
| import software.aws.toolkits.jetbrains.core.coroutines.EDT | ||
| import software.aws.toolkits.jetbrains.core.coroutines.getCoroutineBgContext | ||
| import software.aws.toolkits.jetbrains.services.telemetry.ALLOWED_CODE_EXTENSIONS | ||
| import software.aws.toolkits.resources.AwsCoreBundle | ||
| import software.aws.toolkits.telemetry.AmazonqTelemetry | ||
| import java.io.File | ||
| import java.io.FileInputStream | ||
| import java.net.URI | ||
| import java.nio.file.FileSystem | ||
| import java.nio.file.FileSystems | ||
| import java.nio.file.Files | ||
| import java.nio.file.Path | ||
| import java.nio.file.Paths | ||
| import java.nio.file.StandardCopyOption | ||
| import java.util.Base64 | ||
| import java.util.zip.ZipOutputStream | ||
| import java.util.UUID | ||
| import kotlin.coroutines.coroutineContext | ||
| import kotlin.io.path.Path | ||
| import kotlin.io.path.createParentDirectories | ||
| import kotlin.io.path.getPosixFilePermissions | ||
| import kotlin.io.path.relativeTo | ||
|
|
||
| interface RepoSizeError { | ||
|
|
@@ -41,41 +47,49 @@ class RepoSizeLimitError(override val message: String) : RuntimeException(), Rep | |
| class FeatureDevSessionContext(val project: Project, val maxProjectSizeBytes: Long? = null) { | ||
| // TODO: Need to correct this class location in the modules going further to support both amazonq and codescan. | ||
|
|
||
| private val ignorePatterns = setOf( | ||
| "\\.aws-sam", | ||
| "\\.svn", | ||
| "\\.hg/?", | ||
| "\\.rvm", | ||
| "\\.git/?", | ||
| "\\.gitignore", | ||
| "\\.project", | ||
| "\\.gem", | ||
| "/\\.idea/?", | ||
| "\\.zip$", | ||
| "\\.bin$", | ||
| "\\.png$", | ||
| "\\.jpg$", | ||
| "\\.svg$", | ||
| "\\.pyc$", | ||
| "/license\\.txt$", | ||
| "/License\\.txt$", | ||
| "/LICENSE\\.txt$", | ||
| "/license\\.md$", | ||
| "/License\\.md$", | ||
| "/LICENSE\\.md$", | ||
| "node_modules/?", | ||
| "build/?", | ||
| "dist/?" | ||
| ).map { Regex(it) } | ||
| private val additionalGitIgnoreRules = setOf( | ||
| ".aws-sam", | ||
| ".gem", | ||
| ".git", | ||
| ".gitignore", | ||
| ".gradle", | ||
| ".hg", | ||
| ".idea", | ||
| ".project", | ||
| ".rvm", | ||
| ".svn", | ||
| "*.zip", | ||
| "*.bin", | ||
| "*.png", | ||
| "*.jpg", | ||
| "*.svg", | ||
| "*.pyc", | ||
| "license.txt", | ||
| "License.txt", | ||
| "LICENSE.txt", | ||
| "license.md", | ||
| "License.md", | ||
| "LICENSE.md", | ||
| "node_modules", | ||
| "build", | ||
| "dist" | ||
| ) | ||
|
|
||
| // well known source files that do not have extensions | ||
| private val wellKnownSourceFiles = setOf( | ||
| "Dockerfile", | ||
| "Dockerfile.build" | ||
| ) | ||
|
|
||
| // patterns to explicitly allow unless matched with gitignore rules | ||
| private val allowedPatterns = setOf( | ||
| ".*mvn.*", | ||
| ".*gradle.*", | ||
| ).map { Regex(it) } | ||
|
|
||
| // projectRoot: is the directory where the project is located when selected to open a project. | ||
| val projectRoot = project.guessProjectDir() ?: error("Cannot guess base directory for project ${project.name}") | ||
| private val projectRootPath = Paths.get(projectRoot.path) ?: error("Can not find project root path") | ||
|
|
||
| // selectedSourceFolder": is the directory selected in replacement of the root, this happens when the project is too big to bundle for uploading. | ||
| private var _selectedSourceFolder = projectRoot | ||
|
|
@@ -85,10 +99,10 @@ class FeatureDevSessionContext(val project: Project, val maxProjectSizeBytes: Lo | |
| init { | ||
| ignorePatternsWithGitIgnore = try { | ||
| buildList { | ||
| addAll(ignorePatterns) | ||
| parseGitIgnore().mapNotNull { pattern -> | ||
| runCatching { Regex(pattern) }.getOrNull() | ||
| }.let { addAll(it) } | ||
| addAll(additionalGitIgnoreRules.map { convertGitIgnorePatternToRegex(it) }) | ||
| addAll(parseGitIgnore()) | ||
| }.mapNotNull { pattern -> | ||
| runCatching { Regex(pattern) }.getOrNull() | ||
| } | ||
| } catch (e: Exception) { | ||
| emptyList() | ||
|
|
@@ -108,7 +122,10 @@ class FeatureDevSessionContext(val project: Project, val maxProjectSizeBytes: Lo | |
| fun isFileExtensionAllowed(file: VirtualFile): Boolean { | ||
| // if it is a directory, it is allowed | ||
| if (file.isDirectory) return true | ||
|
|
||
| val explicitAllowed = allowedPatterns.map { pattern -> pattern.matches(file.path) }.any { it } | ||
| if (explicitAllowed) { | ||
| return true | ||
| } | ||
| val extension = file.extension ?: return false | ||
| return ALLOWED_CODE_EXTENSIONS.contains(extension) | ||
| } | ||
|
|
@@ -122,7 +139,13 @@ class FeatureDevSessionContext(val project: Project, val maxProjectSizeBytes: Lo | |
| // this method reads like something a JS dev would write and doesn't do what the author thinks | ||
| val deferredResults = ignorePatternsWithGitIgnore.map { pattern -> | ||
| withContext(coroutineContext) { | ||
| async { pattern.containsMatchIn(path) } | ||
| // avoid partial match (pattern.containsMatchIn) since it causes us matching files | ||
| // against folder patterns. (e.g. settings.gradle ignored by .gradle rule!) | ||
| // we convert the glob rules to regex, add a trailing /* to all rules and then match | ||
| // entries against them by adding a trailing /. | ||
| // TODO: Add unit tests for gitignore matching | ||
| val relative = if (path.startsWith(projectRootPath.toString())) Paths.get(path).relativeTo(projectRootPath) else path | ||
| async { pattern.matches("$relative/") } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -187,18 +210,34 @@ class FeatureDevSessionContext(val project: Project, val maxProjectSizeBytes: Lo | |
| } | ||
| } | ||
|
|
||
| createTemporaryZipFileAsync { zipOutput -> | ||
| val zipFilePath = createTemporaryZipFileAsync { zipfs -> | ||
| filesToIncludeFlow.collect { file -> | ||
| val relativePath = Path(file.path).relativeTo(projectRoot.toNioPath()) | ||
| zipOutput.putNextEntry(relativePath.toString(), Path(file.path)) | ||
| if (!file.isDirectory) { | ||
| val externalFilePath = Path(file.path) | ||
| val externalFilePermissions = externalFilePath.getPosixFilePermissions() | ||
| val relativePath = Path(file.path).relativeTo(projectRootPath) | ||
| val zipfsPath = zipfs.getPath("/$relativePath") | ||
| withContext(EDT) { | ||
| zipfsPath.createParentDirectories() | ||
| Files.copy(externalFilePath, zipfsPath, StandardCopyOption.REPLACE_EXISTING) | ||
| Files.setAttribute(zipfsPath, "zip:permissions", externalFilePermissions) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| zipFilePath | ||
| }.toFile() | ||
|
|
||
| private suspend fun createTemporaryZipFileAsync(block: suspend (ZipOutputStream) -> Unit): Path = withContext(EDT) { | ||
| val file = Files.createTempFile(null, ".zip") | ||
| ZipOutputStream(file.outputStream()).use { zipOutput -> block(zipOutput) } | ||
| file | ||
| private suspend fun createTemporaryZipFileAsync(block: suspend (FileSystem) -> Unit): Path = withContext(EDT) { | ||
|
||
| // Don't use Files.createTempFile since the file must not be created for ZipFS to work | ||
| val tempFilePath: Path = Paths.get(FileUtils.getTempDirectory().absolutePath, "${UUID.randomUUID()}.zip") | ||
| val uri = URI.create("jar:file:$tempFilePath") | ||
| val env = hashMapOf("create" to "true") | ||
| val zipfs = FileSystems.newFileSystem(uri, env) | ||
| zipfs.use { | ||
| block(zipfs) | ||
| } | ||
| tempFilePath | ||
| } | ||
|
|
||
| private fun parseGitIgnore(): Set<String> { | ||
|
|
@@ -216,7 +255,7 @@ class FeatureDevSessionContext(val project: Project, val maxProjectSizeBytes: Lo | |
| private fun convertGitIgnorePatternToRegex(pattern: String): String = pattern | ||
| .replace(".", "\\.") | ||
| .replace("*", ".*") | ||
| .let { if (it.endsWith("/")) "$it?" else it } // Handle directory-specific patterns by optionally matching trailing slash | ||
| .let { if (it.endsWith("/")) "$it.*" else "$it/.*" } // Add a trailing /* to all patterns. (we add a trailing / to all files when matching) | ||
|
|
||
| var selectedSourceFolder: VirtualFile | ||
| set(newRoot) { | ||
|
|
||
Oops, something went wrong.
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.
Is the change user facing, ie is a changelog required here?
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.
yep, it does have a user facing change. updated the messaging.