Skip to content

Commit 7f2528b

Browse files
author
Hamed Soleimani
committed
improve the logic
1 parent 59c6146 commit 7f2528b

File tree

4 files changed

+67
-18
lines changed

4 files changed

+67
-18
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"type" : "bugfix",
3-
"description" : "Preserve file permissions in the zip archive, fix bugs in gitignore matching and keep mvm and gradle files"
4-
}
3+
"description" : "Amazon Q can update mvn and gradle build files"
4+
}

plugins/amazonq/chat/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/amazonqFeatureDev/FeatureDevSessionContextTest.kt

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
import com.intellij.openapi.project.guessProjectDir
5+
import com.intellij.openapi.vfs.VfsUtil
46
import com.intellij.openapi.vfs.VirtualFile
7+
import com.intellij.openapi.vfs.VirtualFileVisitor
58
import com.intellij.testFramework.RuleChain
69
import org.junit.Assert.assertFalse
710
import org.junit.Assert.assertTrue
@@ -13,8 +16,21 @@ import org.mockito.kotlin.whenever
1316
import software.aws.toolkits.jetbrains.services.amazonq.FeatureDevSessionContext
1417
import software.aws.toolkits.jetbrains.services.amazonqFeatureDev.FeatureDevTestBase
1518
import software.aws.toolkits.jetbrains.services.amazonqFeatureDev.util.FeatureDevService
19+
import software.aws.toolkits.jetbrains.utils.rules.HeavyJavaCodeInsightTestFixtureRule
20+
import software.aws.toolkits.jetbrains.utils.rules.addFileToModule
21+
import java.nio.file.Paths
22+
import java.util.zip.ZipFile
23+
import kotlin.io.path.Path
24+
import kotlin.io.path.relativeTo
25+
26+
27+
class FeatureDevSessionContextTest : FeatureDevTestBase(HeavyJavaCodeInsightTestFixtureRule()) {
28+
29+
private fun addFilesToProjectModule(vararg path: String) {
30+
val module = projectRule.module
31+
path.forEach { projectRule.fixture.addFileToModule(module, it, it) }
32+
}
1633

17-
class FeatureDevSessionContextTest : FeatureDevTestBase() {
1834
@Rule
1935
@JvmField
2036
val ruleChain = RuleChain(projectRule, disposableRule)
@@ -61,4 +77,42 @@ class FeatureDevSessionContextTest : FeatureDevTestBase() {
6177
assertTrue(featureDevSessionContext.isFileExtensionAllowed(txtFile))
6278
})
6379
}
80+
81+
@Test
82+
fun testZipProject() {
83+
addFilesToProjectModule(
84+
".gradle/cached.jar",
85+
"src/MyClass.java",
86+
"gradlew",
87+
"gradlew.bat",
88+
"README.md",
89+
"settings.gradle",
90+
"build.gradle",
91+
"gradle/wrapper/gradle-wrapper.properties",
92+
)
93+
94+
val zipResult = featureDevSessionContext.getProjectZip()
95+
val zipPath = zipResult.payload.path
96+
97+
val zippedFiles = mutableSetOf<String>()
98+
ZipFile(zipPath).use { zipFile ->
99+
for (entry in zipFile.entries()) {
100+
if (!entry.name.endsWith("/")) {
101+
zippedFiles.add(entry.name)
102+
}
103+
}
104+
}
105+
106+
val expectedFiles = setOf(
107+
"src/MyClass.java",
108+
"gradlew",
109+
"gradlew.bat",
110+
"README.md",
111+
"settings.gradle",
112+
"build.gradle",
113+
"gradle/wrapper/gradle-wrapper.properties",
114+
)
115+
116+
assertTrue(zippedFiles == expectedFiles)
117+
}
64118
}

plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/FeatureDevSessionContext.kt

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import kotlinx.coroutines.runBlocking
1717
import kotlinx.coroutines.withContext
1818
import org.apache.commons.codec.digest.DigestUtils
1919
import org.apache.commons.io.FileUtils
20-
import software.aws.toolkits.jetbrains.core.coroutines.EDT
2120
import software.aws.toolkits.jetbrains.core.coroutines.getCoroutineBgContext
2221
import software.aws.toolkits.jetbrains.services.telemetry.ALLOWED_CODE_EXTENSIONS
2322
import software.aws.toolkits.resources.AwsCoreBundle
@@ -78,15 +77,11 @@ class FeatureDevSessionContext(val project: Project, val maxProjectSizeBytes: Lo
7877
// well known source files that do not have extensions
7978
private val wellKnownSourceFiles = setOf(
8079
"Dockerfile",
81-
"Dockerfile.build"
80+
"Dockerfile.build",
81+
"gradlew",
82+
"mvnw"
8283
)
8384

84-
// patterns to explicitly allow unless matched with gitignore rules
85-
private val allowedPatterns = setOf(
86-
".*mvn.*",
87-
".*gradle.*",
88-
).map { Regex(it) }
89-
9085
// projectRoot: is the directory where the project is located when selected to open a project.
9186
val projectRoot = project.guessProjectDir() ?: error("Cannot guess base directory for project ${project.name}")
9287
private val projectRootPath = Paths.get(projectRoot.path) ?: error("Can not find project root path")
@@ -122,10 +117,6 @@ class FeatureDevSessionContext(val project: Project, val maxProjectSizeBytes: Lo
122117
fun isFileExtensionAllowed(file: VirtualFile): Boolean {
123118
// if it is a directory, it is allowed
124119
if (file.isDirectory) return true
125-
val explicitAllowed = allowedPatterns.map { pattern -> pattern.matches(file.path) }.any { it }
126-
if (explicitAllowed) {
127-
return true
128-
}
129120
val extension = file.extension ?: return false
130121
return ALLOWED_CODE_EXTENSIONS.contains(extension)
131122
}
@@ -217,7 +208,7 @@ class FeatureDevSessionContext(val project: Project, val maxProjectSizeBytes: Lo
217208
val externalFilePermissions = externalFilePath.getPosixFilePermissions()
218209
val relativePath = Path(file.path).relativeTo(projectRootPath)
219210
val zipfsPath = zipfs.getPath("/$relativePath")
220-
withContext(EDT) {
211+
runBlocking {
221212
zipfsPath.createParentDirectories()
222213
Files.copy(externalFilePath, zipfsPath, StandardCopyOption.REPLACE_EXISTING)
223214
Files.setAttribute(zipfsPath, "zip:permissions", externalFilePermissions)
@@ -228,7 +219,7 @@ class FeatureDevSessionContext(val project: Project, val maxProjectSizeBytes: Lo
228219
zipFilePath
229220
}.toFile()
230221

231-
private suspend fun createTemporaryZipFileAsync(block: suspend (FileSystem) -> Unit): Path = withContext(EDT) {
222+
private suspend fun createTemporaryZipFileAsync(block: suspend (FileSystem) -> Unit): Path = withContext(getCoroutineBgContext()) {
232223
// Don't use Files.createTempFile since the file must not be created for ZipFS to work
233224
val tempFilePath: Path = Paths.get(FileUtils.getTempDirectory().absolutePath, "${UUID.randomUUID()}.zip")
234225
val uri = URI.create("jar:file:$tempFilePath")

plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/services/telemetry/TelemetryUtils.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ val ALLOWED_CODE_EXTENSIONS = setOf(
1616
"bash",
1717
"bat",
1818
"boo",
19+
"bms",
1920
"c",
2021
"cbl",
2122
"cc",
@@ -27,6 +28,7 @@ val ALLOWED_CODE_EXTENSIONS = setOf(
2728
"cljs",
2829
"cls",
2930
"cmake",
31+
"cmd",
3032
"cob",
3133
"cobra",
3234
"coffee",
@@ -122,11 +124,13 @@ val ALLOWED_CODE_EXTENSIONS = setOf(
122124
"pike",
123125
"pir",
124126
"pl",
127+
"pli",
125128
"pm",
126129
"pmod",
127130
"pp",
128131
"pro",
129132
"prolog",
133+
"properties",
130134
"ps1",
131135
"psd1",
132136
"psm1",
@@ -200,7 +204,7 @@ val ALLOWED_CODE_EXTENSIONS = setOf(
200204
"xml",
201205
"yaml",
202206
"yml",
203-
"zig"
207+
"zig",
204208
)
205209

206210
fun scrubNames(messageToBeScrubbed: String, username: String? = getSystemUserName()): String {

0 commit comments

Comments
 (0)