Skip to content

Commit

Permalink
handle empty diff
Browse files Browse the repository at this point in the history
  • Loading branch information
David Hasani committed Nov 8, 2024
1 parent 9dc38e0 commit 3bd08b2
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ export class GumbyController {
private async transformInitiated(message: any) {
// feature flag for SQL transformations
if (!isSQLTransformReady) {
// TODO: if (!projectContainsEmbeddedSQLUsingOracle)
await this.handleLanguageUpgrade(message)
return
}
Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/codewhisperer/models/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,8 +584,6 @@ export const jobPartiallyCompletedNotification = `Amazon Q ${getTransformationAc

export const noPomXmlFoundChatMessage = `Sorry, I couldn\'t find a project that I can upgrade. I couldn\'t find a pom.xml file in any of your open projects. Currently, I can only upgrade Java 8 or Java 11 projects built on Maven. For more information, see the [Amazon Q documentation](${codeTransformPrereqDoc}).`

export const noPomXmlFoundNotification = `None of your open projects are supported by Amazon Q Code Transformation. Amazon Q could not find a pom.xml file in any of your open projects. Currently, Amazon Q can only upgrade Java 8 or Java 11 projects built on Maven. For more information, see the [Amazon Q documentation](${codeTransformPrereqDoc}).`

export const noJavaHomeFoundChatMessage = `Sorry, I couldn\'t locate your Java installation. For more information, see the [Amazon Q documentation](${codeTransformPrereqDoc}).`

export const dependencyVersionsErrorMessage =
Expand Down Expand Up @@ -622,9 +620,9 @@ export const changesAppliedChatMessage = 'I applied the changes to your project.

export const changesAppliedNotification = 'Amazon Q applied the changes to your project.'

export const noOpenProjectsFoundChatMessage = `Sorry, I couldn\'t find a project that I can upgrade. Currently, I can only upgrade Java 8 or Java 11 projects built on Maven. For more information, see the [Amazon Q documentation](${codeTransformPrereqDoc}).`
export const noOpenProjectsFoundChatMessage = `I couldn't find a project that I can transform. Please open a Java project and try again. For more information, see the [Amazon Q documentation](${codeTransformPrereqDoc}).`

export const noJavaProjectsFoundChatMessage = `Sorry, I couldn\'t find a project that I can upgrade. Currently, I can only upgrade Java 8 or Java 11 projects built on Maven. For more information, see the [Amazon Q documentation](${codeTransformPrereqDoc}).`
export const noJavaProjectsFoundChatMessage = `I couldn't find a project that I can transform. Please open a Java project and try again. For more information, see the [Amazon Q documentation](${codeTransformPrereqDoc}).`

export const linkToDocsHome = 'https://docs.aws.amazon.com/amazonq/latest/aws-builder-use-ug/code-transformation.html'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ export async function uploadPayload(payloadFileName: string, uploadContext?: Upl
*/
const mavenExcludedExtensions = ['.repositories', '.sha1']

const sourceExcludedExtensions = ['.DS_Store']

/**
* Determines if the specified file path corresponds to a Maven metadata file
* by checking against known metadata file extensions. This is used to identify
Expand All @@ -243,24 +245,22 @@ function isExcludedDependencyFile(path: string): boolean {
return mavenExcludedExtensions.some((extension) => path.endsWith(extension))
}

/**
* Gets all files in dir. We use this method to get the source code, then we run a mvn command to
* copy over dependencies into their own folder, then we use this method again to get those
* dependencies. If isDependenciesFolder is true, then we are getting all the files
* of the dependencies which were copied over by the previously-run mvn command, in which case
* we DO want to include any dependencies that may happen to be named "target", hence the check
* in the first part of the IF statement. The point of excluding folders named target is that
* "target" is also the name of the folder where .class files, large JARs, etc. are stored after
* building, and we do not want these included in the ZIP so we exclude these when calling
* getFilesRecursively on the source code folder.
*/
// do not zip the .DS_Store file as it may appear in the diff.patch
function isExcludedSourceFile(path: string): boolean {
return sourceExcludedExtensions.some((extension) => path.endsWith(extension))
}

// zip all dependency files and all source files excluding "target" (contains large JARs) plus ".git" and ".idea" (may appear in diff.patch)
function getFilesRecursively(dir: string, isDependenciesFolder: boolean): string[] {
const entries = nodefs.readdirSync(dir, { withFileTypes: true })
const files = entries.flatMap((entry) => {
const res = path.resolve(dir, entry.name)
// exclude 'target' directory from ZIP (except if zipping dependencies) due to issues in backend
if (entry.isDirectory()) {
if (isDependenciesFolder || entry.name !== 'target') {
if (isDependenciesFolder) {
// include all dependency files
return getFilesRecursively(res, isDependenciesFolder)
} else if (entry.name !== 'target' && entry.name !== '.git' && entry.name !== '.idea') {
// exclude the above directories when zipping source code
return getFilesRecursively(res, isDependenciesFolder)
} else {
return []
Expand Down Expand Up @@ -309,8 +309,8 @@ export async function zipCode(
const sourceFiles = getFilesRecursively(projectPath, false)
let sourceFilesSize = 0
for (const file of sourceFiles) {
if (nodefs.statSync(file).isDirectory()) {
getLogger().info('CodeTransformation: Skipping directory, likely a symlink')
if (nodefs.statSync(file).isDirectory() || isExcludedSourceFile(file)) {
getLogger().info('CodeTransformation: Skipping file')
continue
}
const relativePath = path.relative(projectPath, file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ export class DiffModel {
*/
public parseDiff(pathToDiff: string, pathToWorkspace: string): ProposedChangeNode[] {
const diffContents = fs.readFileSync(pathToDiff, 'utf8')

if (!diffContents.trim()) {
getLogger().error(`CodeTransformation: diff.patch file is empty`)
throw new Error('No changes were made as a part of this transformation.')
}

const changedFiles = parsePatch(diffContents)
// path to the directory containing copy of the changed files in the transformed project
const pathToTmpSrcDir = this.copyProject(pathToWorkspace, changedFiles)
Expand Down Expand Up @@ -372,13 +378,12 @@ export class ProposedTransformationExplorer {
pathContainingArchive = path.dirname(pathToArchive)
const zip = new AdmZip(pathToArchive)
zip.extractAllTo(pathContainingArchive)

// TODO: below only needed if the backend cannot fix the "b/" diff.patch issue
// read in the diff.patch
const diffPatch = fs.readFileSync(
path.join(pathContainingArchive, ExportResultArchiveStructure.PathToDiffPatch),
'utf-8'
)
// go through each line, and replace "b" with "b/" on lines that start with "diff"
const lines = diffPatch.split('\n')
const newLines = lines.map((line) => {
if (line.trim().startsWith('diff') || line.trim().startsWith('+++')) {
Expand All @@ -391,6 +396,7 @@ export class ProposedTransformationExplorer {
path.join(pathContainingArchive, ExportResultArchiveStructure.PathToDiffPatch),
newDiffPatch
)

diffModel.parseDiff(
path.join(pathContainingArchive, ExportResultArchiveStructure.PathToDiffPatch),
transformByQState.getProjectPath()
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/dev/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ export const betaUrl = {
}

// feature flag for SQL transformations
export const isSQLTransformReady = false
export const isSQLTransformReady = true

0 comments on commit 3bd08b2

Please sign in to comment.