-
Notifications
You must be signed in to change notification settings - Fork 405
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
feat: create apex action from selected method/this class #5950
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7340e91
feat: create apex action from selected method
CristiCanizales 805e939
chore: refactor
CristiCanizales de6ac37
Merge branch 'develop' into cristi/apex-actions
mingxuanzhangsfdx 7b28663
feat: create apex action from this class
CristiCanizales 85003b6
chore: messaging and when clause
CristiCanizales 17fc570
Merge branch 'cristi/apex-actions' of github.com:forcedotcom/salesfor…
CristiCanizales b7ca6c2
chore: refactor, comments and tests
CristiCanizales 68f5daf
chore: revert changes
CristiCanizales b2f92d0
chore: add sourceUri param
CristiCanizales 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
133 changes: 133 additions & 0 deletions
133
packages/salesforcedx-vscode-apex/src/commands/apexActionController.ts
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,133 @@ | ||
/* | ||
* Copyright (c) 2024, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
import { Progress } from '@salesforce/apex-node-bundle'; | ||
import { notificationService, workspaceUtils } from '@salesforce/salesforcedx-utils-vscode'; | ||
import * as fs from 'fs'; | ||
import { OpenAPIV3 } from 'openapi-types'; | ||
import * as path from 'path'; | ||
import * as vscode from 'vscode'; | ||
import { stringify } from 'yaml'; | ||
import { nls } from '../messages'; | ||
import { getTelemetryService } from '../telemetry/telemetry'; | ||
import { MetadataOrchestrator, MethodMetadata } from './metadataOrchestrator'; | ||
|
||
export class ApexActionController { | ||
constructor(private metadataOrchestrator: MetadataOrchestrator) {} | ||
|
||
/** | ||
* Creates an Apex Action. | ||
* @param isClass - Indicates if the action is for a class or a method. | ||
*/ | ||
public createApexAction = async (isClass: boolean): Promise<void> => { | ||
const type = isClass ? 'Class' : 'Method'; | ||
const command = isClass | ||
? 'SFDX: Create Apex Action from This Class' | ||
: 'SFDX: Create Apex Action from Selected Method'; | ||
let metadata; | ||
let name; | ||
const telemetryService = await getTelemetryService(); | ||
try { | ||
await vscode.window.withProgress( | ||
{ | ||
location: vscode.ProgressLocation.Notification, | ||
title: command, | ||
cancellable: true | ||
}, | ||
async progress => { | ||
// Step 1: Extract Metadata | ||
progress.report({ message: nls.localize('extract_metadata') }); | ||
metadata = isClass | ||
? this.metadataOrchestrator.extractAllMethodsMetadata() | ||
: this.metadataOrchestrator.extractMethodMetadata(); | ||
if (!metadata) { | ||
throw new Error(nls.localize('extraction_failed', type)); | ||
} | ||
|
||
// Step 3: Generate OpenAPI Document | ||
progress.report({ message: nls.localize('generate_openapi_document') }); | ||
const openApiDocument = this.generateOpenAPIDocument(Array.isArray(metadata) ? metadata : [metadata]); | ||
|
||
// Step 4: Write OpenAPI Document to File | ||
name = Array.isArray(metadata) ? metadata[0].className : metadata.name; | ||
const openApiFileName = `${name}_openapi.yml`; | ||
progress.report({ message: nls.localize('write_openapi_document_to_file') }); | ||
await this.saveAndOpenDocument(openApiFileName, openApiDocument); | ||
} | ||
); | ||
|
||
// Step 5: Notify Success | ||
notificationService.showInformationMessage(nls.localize('apex_action_created', type.toLowerCase(), name)); | ||
telemetryService.sendEventData(`ApexAction${type}Created`, { method: name! }); | ||
} catch (error: any) { | ||
void this.handleError(error, `ApexAction${type}CreationFailed`); | ||
} | ||
}; | ||
|
||
/** | ||
* Saves and opens the OpenAPI document to a file. | ||
* @param fileName - The name of the file. | ||
* @param content - The content of the file. | ||
*/ | ||
private saveAndOpenDocument = async (fileName: string, content: string): Promise<void> => { | ||
const openAPIdocumentsPath = path.join(workspaceUtils.getRootWorkspacePath(), 'OpenAPIdocuments'); | ||
if (!fs.existsSync(openAPIdocumentsPath)) { | ||
fs.mkdirSync(openAPIdocumentsPath); | ||
} | ||
const saveLocation = path.join(openAPIdocumentsPath, fileName); | ||
fs.writeFileSync(saveLocation, content); | ||
await vscode.workspace.openTextDocument(saveLocation).then((newDocument: vscode.TextDocument) => { | ||
void vscode.window.showTextDocument(newDocument); | ||
}); | ||
}; | ||
|
||
/** | ||
* Generates an OpenAPI document from the provided metadata. | ||
* @param metadata - The metadata of the methods. | ||
* @returns The OpenAPI document as a string. | ||
*/ | ||
private generateOpenAPIDocument = (metadata: MethodMetadata[]): string => { | ||
const paths: OpenAPIV3.PathsObject = {}; | ||
|
||
metadata?.forEach(method => { | ||
paths[`/apex/${method.name}`] = { | ||
post: { | ||
operationId: method.name, | ||
summary: `Invoke ${method.name}`, | ||
parameters: method.parameters as unknown as (OpenAPIV3.ReferenceObject | OpenAPIV3.ParameterObject)[], | ||
responses: { | ||
200: { | ||
description: 'Success', | ||
content: { | ||
'application/json': { schema: { type: method.returnType as OpenAPIV3.NonArraySchemaObjectType } } | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
}); | ||
|
||
const openAPIDocument: OpenAPIV3.Document = { | ||
openapi: '3.0.0', | ||
info: { title: 'Apex Actions', version: '1.0.0' }, | ||
paths | ||
}; | ||
// Convert the OpenAPI document to YAML | ||
return stringify(openAPIDocument); | ||
}; | ||
|
||
/** | ||
* Handles errors by showing a notification and sending telemetry data. | ||
* @param error - The error to handle. | ||
* @param telemetryEvent - The telemetry event name. | ||
*/ | ||
private handleError = async (error: any, telemetryEvent: string): Promise<void> => { | ||
const telemetryService = await getTelemetryService(); | ||
const errorMessage = error instanceof Error ? error.message : String(error); | ||
notificationService.showErrorMessage(`${nls.localize('create_apex_action_failed')}: ${errorMessage}`); | ||
telemetryService.sendException(telemetryEvent, errorMessage); | ||
}; | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/salesforcedx-vscode-apex/src/commands/createApexAction.ts
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,27 @@ | ||
/* | ||
* Copyright (c) 2024, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
import { ApexActionController } from './apexActionController'; | ||
import { MetadataOrchestrator } from './metadataOrchestrator'; | ||
|
||
const metadataOrchestrator = new MetadataOrchestrator(); | ||
const controller = new ApexActionController(metadataOrchestrator); | ||
|
||
/** | ||
* Creates an Apex Action from the method at the current cursor position. | ||
*/ | ||
export const createApexActionFromMethod = async (): Promise<void> => { | ||
// Call Controller | ||
await controller.createApexAction(false); | ||
}; | ||
|
||
/** | ||
* Creates Apex Actions from all methods in the current class. | ||
*/ | ||
export const createApexActionFromClass = async (): Promise<void> => { | ||
CristiCanizales marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Call Controller | ||
await controller.createApexAction(true); | ||
}; |
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
Oops, something went wrong.
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.
@CristiCanizales please verify the 3pp status of the new modules