-
Notifications
You must be signed in to change notification settings - Fork 11
fix: add tokowaka auto-deploy api #1357
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 8 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
30fe0b9
fix: add tokowaka auto-deploy api
dipratap 28c2dc2
fix: deploy on personal ns
dipratap f48dbdf
fix: deploy on personal ns
dipratap 5ab680b
fix: update libs
dipratap 1ab1246
fix: update libs
dipratap 66be7bf
fix: update libs
dipratap 757c394
fix: update libs
dipratap c0414e7
fix: update libs
dipratap 04189da
fix: update libs
dipratap d52d938
fix: update libs
dipratap 195369d
fix: update libs
dipratap 2c8f455
fix: update libs
dipratap 4cb6a48
fix: tests
dipratap f142992
fix: update libs
dipratap fb6ffc0
fix: update libs
dipratap 26dbe62
fix: merge main
dipratap b3d526d
fix: custom ns sanity
dipratap 14d5428
fix: remove temp code
dipratap f240882
Merge branch 'main' of github.com:adobe/spacecat-api-service into tok…
dipratap 1010fd7
fix: update shared libs
dipratap 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ import { | |
| } from '@adobe/spacecat-shared-utils'; | ||
|
|
||
| import { ValidationError, Suggestion as SuggestionModel, Site as SiteModel } from '@adobe/spacecat-shared-data-access'; | ||
| import TokowakaClient from '@adobe/spacecat-shared-tokowaka-client'; | ||
| import { SuggestionDto } from '../dto/suggestion.js'; | ||
| import { FixDto } from '../dto/fix.js'; | ||
| import { sendAutofixMessage, getCSPromiseToken, ErrorWithStatusCode } from '../support/utils.js'; | ||
|
|
@@ -704,9 +705,158 @@ function SuggestionsController(ctx, sqs, env) { | |
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Deploys suggestions through Tokowaka edge delivery | ||
| * @param {Object} context of the request | ||
| * @returns {Promise<Response>} Deployment response | ||
| */ | ||
| const deploySuggestionToEdge = async (context) => { | ||
| const siteId = context.params?.siteId; | ||
| const opportunityId = context.params?.opportunityId; | ||
|
|
||
| if (!isValidUUID(siteId)) { | ||
| return badRequest('Site ID required'); | ||
| } | ||
|
|
||
| if (!isValidUUID(opportunityId)) { | ||
| return badRequest('Opportunity ID required'); | ||
| } | ||
|
|
||
| // validate request body | ||
| if (!isNonEmptyObject(context.data)) { | ||
| return badRequest('No data provided'); | ||
| } | ||
| const { suggestionIds } = context.data; | ||
| if (!isArray(suggestionIds) || suggestionIds.length === 0) { | ||
| return badRequest('Request body must contain a non-empty array of suggestionIds'); | ||
| } | ||
|
|
||
| const site = await Site.findById(siteId); | ||
| if (!site) { | ||
| return notFound('Site not found'); | ||
| } | ||
|
|
||
| if (!await accessControlUtil.hasAccess(site)) { | ||
| return forbidden('User does not belong to the organization'); | ||
| } | ||
|
|
||
| const opportunity = await Opportunity.findById(opportunityId); | ||
| if (!opportunity || opportunity.getSiteId() !== siteId) { | ||
| return notFound('Opportunity not found'); | ||
| } | ||
|
|
||
| // Fetch all suggestions for this opportunity | ||
| const allSuggestions = await Suggestion.allByOpportunityId(opportunityId); | ||
|
|
||
| // Track valid, failed, and missing suggestions | ||
| const validSuggestions = []; | ||
| const failedSuggestions = []; | ||
|
|
||
| // Check each requested suggestion (basic validation only) | ||
| suggestionIds.forEach((suggestionId, index) => { | ||
| const suggestion = allSuggestions.find((s) => s.getId() === suggestionId); | ||
|
|
||
| if (!suggestion) { | ||
| failedSuggestions.push({ | ||
| uuid: suggestionId, | ||
| index, | ||
| message: 'Suggestion not found', | ||
| statusCode: 404, | ||
| }); | ||
| } else if (suggestion.getStatus() !== SuggestionModel.STATUSES.NEW) { | ||
| failedSuggestions.push({ | ||
| uuid: suggestionId, | ||
| index, | ||
| message: 'Suggestion is not in NEW status', | ||
| statusCode: 400, | ||
| }); | ||
| } else { | ||
| validSuggestions.push(suggestion); | ||
| } | ||
| }); | ||
|
|
||
| let succeededSuggestions = []; | ||
|
|
||
| // Only attempt deployment if we have valid suggestions | ||
| if (isNonEmptyArray(validSuggestions)) { | ||
| try { | ||
| const tokowakaClient = TokowakaClient.createFrom(context); | ||
| const deploymentResult = await tokowakaClient.deploySuggestions( | ||
| site, | ||
| opportunity, | ||
| validSuggestions, | ||
| ); | ||
|
|
||
| // Process deployment results | ||
| const { | ||
| succeededSuggestions: deployedSuggestions, | ||
| failedSuggestions: ineligibleSuggestions, | ||
| } = deploymentResult; | ||
|
|
||
| // Update successfully deployed suggestions with deployment timestamp | ||
| const deploymentTimestamp = Date.now(); | ||
| succeededSuggestions = await Promise.all( | ||
| deployedSuggestions.map(async (suggestion) => { | ||
| const currentData = suggestion.getData(); | ||
| suggestion.setData({ | ||
| ...currentData, | ||
| tokowakaDeployed: deploymentTimestamp, | ||
| }); | ||
| suggestion.setUpdatedBy('tokowaka-deployment'); | ||
| return suggestion.save(); | ||
| }), | ||
| ); | ||
|
|
||
| // Add ineligible suggestions to failed list | ||
| ineligibleSuggestions.forEach((item) => { | ||
| failedSuggestions.push({ | ||
| uuid: item.suggestion.getId(), | ||
| index: suggestionIds.indexOf(item.suggestion.getId()), | ||
| message: item.reason, | ||
| statusCode: 400, | ||
| }); | ||
| }); | ||
|
|
||
| context.log.info(`Successfully deployed ${succeededSuggestions.length} suggestions to Edge`); | ||
| } catch (error) { | ||
| context.log.error(`Error deploying to Tokowaka: ${error.message}`, error); | ||
| // If deployment fails, mark all valid suggestions as failed | ||
| validSuggestions.forEach((suggestion) => { | ||
| failedSuggestions.push({ | ||
| uuid: suggestion.getId(), | ||
| index: suggestionIds.indexOf(suggestion.getId()), | ||
| message: 'Deployment failed: Internal server error', | ||
| statusCode: 500, | ||
| }); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| const response = { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets make sure these suggestion props are not overwritten by audit worker while updating the suggestions. |
||
| suggestions: [ | ||
| ...succeededSuggestions.map((suggestion) => ({ | ||
| uuid: suggestion.getId(), | ||
| index: suggestionIds.indexOf(suggestion.getId()), | ||
| statusCode: 200, | ||
| suggestion: SuggestionDto.toJSON(suggestion), | ||
| })), | ||
| ...failedSuggestions, | ||
| ], | ||
| metadata: { | ||
| total: suggestionIds.length, | ||
| success: succeededSuggestions.length, | ||
| failed: failedSuggestions.length, | ||
| }, | ||
| }; | ||
| response.suggestions.sort((a, b) => a.index - b.index); | ||
|
|
||
| return createResponse(response, 207); | ||
| }; | ||
|
|
||
| return { | ||
| autofixSuggestions, | ||
| createSuggestions, | ||
| deploySuggestionToEdge, | ||
| getAllForOpportunity, | ||
| getByID, | ||
| getByStatus, | ||
|
|
||
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
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.
any reason to downgrade?
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.
It was a previous package.json that I used to deploy on personal namespace. I have updated the latest package.json now.