-
Notifications
You must be signed in to change notification settings - Fork 275
fix(amazonq): file premissions are now preserved and mvn and gradle files are kept in the repo archives #5235
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c8bdf95
fix(amazonq): archives now preserve file premissions and amazon q can…
efa81d3
Merge branch 'aws:main' into main
osdemah 3f6b265
fix test failures on windows
221fbfa
Merge branch 'main' into main
osdemah ba1360e
Merge branch 'main' into main
osdemah 9140bd8
change FS operations to use BG context
b5abe76
Merge branch 'main' into main
osdemah bef0167
Merge branch 'main' into main
osdemah ba63b93
Merge branch 'main' into main
osdemah 3a7d7ae
Merge branch 'main' into main
manodnyab 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" : "Amazon Q can update mvn and gradle build 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,26 @@ 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 software.aws.toolkits.jetbrains.core.coroutines.EDT | ||
| import org.apache.commons.io.FileUtils | ||
| 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 +46,45 @@ 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" | ||
| "Dockerfile.build", | ||
| "gradlew", | ||
| "mvnw" | ||
| ) | ||
|
|
||
| // 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 +94,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,21 +117,26 @@ 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 extension = file.extension ?: return false | ||
| return ALLOWED_CODE_EXTENSIONS.contains(extension) | ||
| } | ||
|
|
||
| private fun ignoreFileByExtension(file: VirtualFile) = | ||
| !isFileExtensionAllowed(file) | ||
|
|
||
| suspend fun ignoreFile(file: VirtualFile): Boolean = ignoreFile(file.path) | ||
| suspend fun ignoreFile(file: VirtualFile): Boolean = ignoreFile(file.presentableUrl) | ||
|
|
||
| suspend fun ignoreFile(path: String): Boolean { | ||
| // 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/") } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -132,7 +146,7 @@ class FeatureDevSessionContext(val project: Project, val maxProjectSizeBytes: Lo | |
| return deferredResults.any { it.await() } | ||
| } | ||
|
|
||
| private fun wellKnown(file: VirtualFile): Boolean = wellKnownSourceFiles.contains(file.name) | ||
| fun wellKnown(file: VirtualFile): Boolean = wellKnownSourceFiles.contains(file.name) | ||
|
|
||
| suspend fun zipFiles(projectRoot: VirtualFile): File = withContext(getCoroutineBgContext()) { | ||
| val files = mutableListOf<VirtualFile>() | ||
|
|
@@ -187,22 +201,41 @@ class FeatureDevSessionContext(val project: Project, val maxProjectSizeBytes: Lo | |
| } | ||
| } | ||
|
|
||
| createTemporaryZipFileAsync { zipOutput -> | ||
| val zipFilePath = createTemporaryZipFileAsync { zipfs -> | ||
| val isPosix = FileSystems.getDefault().supportedFileAttributeViews().contains("posix") | ||
| filesToIncludeFlow.collect { file -> | ||
| try { | ||
| val relativePath = Path(file.path).relativeTo(projectRoot.toNioPath()) | ||
| zipOutput.putNextEntry(relativePath.toString(), Path(file.path)) | ||
| } catch (e: NoSuchFileException) { | ||
| // Noop: Skip if file was deleted | ||
|
|
||
| if (!file.isDirectory) { | ||
| val externalFilePath = Path(file.path) | ||
| val relativePath = Path(file.path).relativeTo(projectRootPath) | ||
| val zipfsPath = zipfs.getPath("/$relativePath") | ||
| runBlocking { | ||
|
||
| zipfsPath.createParentDirectories() | ||
| try { | ||
| Files.copy(externalFilePath, zipfsPath, StandardCopyOption.REPLACE_EXISTING) | ||
| if (isPosix) { | ||
| Files.setAttribute(zipfsPath, "zip:permissions", externalFilePath.getPosixFilePermissions()) | ||
| } | ||
| } catch (e: NoSuchFileException) { | ||
| // Noop: Skip if file was deleted | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| 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(getCoroutineBgContext()) { | ||
| // 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:${tempFilePath.toUri()}") | ||
| val env = hashMapOf("create" to "true") | ||
| val zipfs = FileSystems.newFileSystem(uri, env) | ||
| zipfs.use { | ||
| block(zipfs) | ||
| } | ||
| tempFilePath | ||
| } | ||
|
|
||
| private fun parseGitIgnore(): Set<String> { | ||
|
|
@@ -220,7 +253,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) { | ||
|
|
||
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
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.
nit: can we assign the string to a variable?