-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Add live validation #2211
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
Add live validation #2211
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. See License in the project root for license information. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const utils = require('../test/util/utils'), | ||
| request = require('request-promise-native'), | ||
| zlib = require('zlib'); | ||
|
|
||
| const repoUrl = utils.getRepoUrl(), | ||
| validationService = "https://app.azure-devex-tools.com/api/validations", | ||
| branch = utils.getSourceBranch(), | ||
| processingDelay = 20, | ||
| isRunningInTravisCI = process.env.MODE === 'liveValidation' && process.env.PR_ONLY === 'true', | ||
| specsPaths = utils.getFilesChangedInPR(), | ||
| regex = /resource-manager[\\|\/](.*?)[\\|\/].*?[\\|\/](.*?)[\\|\/]/, | ||
| successThreshold = 90, | ||
| validationModels = new Map(); | ||
|
|
||
| let durationInSeconds = parseInt(process.env.LIVE_VALIDATION_DURATION_IN_MINUTES) * 60; | ||
| if (isNaN(durationInSeconds)) { | ||
| durationInSeconds = 180; | ||
| } | ||
|
|
||
| async function runScript() { | ||
| // See whether script is in Travis CI context | ||
| console.log(`isRunningInTraviSCI: ${isRunningInTravisCI}`); | ||
| for (const specPath of specsPaths) { | ||
| let matchResult = specPath.match(regex); | ||
|
|
||
| if (matchResult === null) { | ||
| continue; | ||
| } | ||
|
|
||
| let resourceProvider = matchResult[1]; | ||
| let apiVersion = matchResult[2]; | ||
|
|
||
| if (!validationModels.has(resourceProvider)) { | ||
| validationModels.set(resourceProvider, new Set()); | ||
| } | ||
|
|
||
| validationModels.get(resourceProvider).add(apiVersion); | ||
| } | ||
|
|
||
| if (validationModels.size === 0) { | ||
| console.log("Change didn't affect any swagger specs. No validation to be done."); | ||
| return; | ||
| } else if (validationModels.size > 1) { | ||
| console.log("WARNING: Multiple resource provider have changes, only the first one will be validated."); | ||
| } | ||
|
|
||
| let resourceProvider = validationModels.keys().next().value; | ||
|
|
||
| if (validationModels.get(resourceProvider).size > 1) { | ||
| console.log("WARNING: Multiple api versions have changes, only the first one will be validated."); | ||
| } | ||
|
|
||
| let apiVersion = validationModels.get(resourceProvider).values().next().value; | ||
|
|
||
| console.log(`Changes detected in a swagger spec.`); | ||
| console.log(`RP is: ${resourceProvider}`); | ||
| console.log(`ApiVersion is: ${apiVersion}`); | ||
| console.log(`Source repo is: ${repoUrl}`); | ||
| console.log(`Branch is: ${branch}`); | ||
|
|
||
| console.log(`Making the request to the validation service...`); | ||
|
|
||
| let response = await request.post(validationService).form({ | ||
| repoUrl: repoUrl, | ||
| branch: branch, | ||
| resourceProvider: resourceProvider, | ||
| apiVersion: apiVersion, | ||
| duration: durationInSeconds | ||
| }); | ||
| let validationId = JSON.parse(response).validationId; | ||
|
|
||
| let validationResultUrl = `${validationService}/${validationId}`; | ||
| console.log(`Request done, results will in ${durationInSeconds} seconds...`); | ||
|
|
||
| await timeout((durationInSeconds + processingDelay) * 1000); | ||
| let validationResult = JSON.parse(await request(validationResultUrl)); | ||
|
|
||
| console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); | ||
| console.log(`Results of validation ${validationId}:`); | ||
|
|
||
| let analyticsUrl = await createAnalyticsLink(validationId); | ||
|
|
||
| let failingOperations = []; | ||
| let noTrafficOperations = []; | ||
| for (const [operationId, operationResult] of Object.entries(validationResult.operationResults)) { | ||
|
|
||
| if (operationResult.operationCount === 0) { | ||
| noTrafficOperations.push(operationResult.operationId) | ||
| } else if (operationResult.successRate < successThreshold) { | ||
| failingOperations.push(operationResult.operationId); | ||
| } | ||
|
|
||
| console.log(JSON.stringify(operationResult)); | ||
| } | ||
|
|
||
| console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); | ||
| if (failingOperations.length > 0 || noTrafficOperations.length > 0) { | ||
| console.log(`The changes in the specs introduced by this PR potentially do not reflect the Service API.`); | ||
|
|
||
| console.log(`Active traffic and success rate > ${successThreshold}% FOR EACH OPERATION is required. Please review the following operations before moving forward.`); | ||
| console.log(`SUCCESS RATE < ${successThreshold}%: | ||
| ${JSON.stringify(failingOperations)}`); | ||
|
|
||
| if (noTrafficOperations.length > 0) { | ||
| console.log(`NO TRAFFIC: | ||
| ${JSON.stringify(noTrafficOperations)} | ||
| `); | ||
| } | ||
| console.log(`To inspect the individual failures go to the url (add '| where customDimensions.operationId == "<OPERATION_ID>"' to filter for individual operations.): | ||
| ${analyticsUrl} | ||
| `); | ||
| process.exitCode = 1; | ||
| } else { | ||
| console.log(`SUCCESS RATE: ${validationResult.SuccessRate} > ${successThreshold}. You can move forward:`); | ||
| } | ||
| } | ||
|
|
||
| function timeout(ms) { | ||
| return new Promise(resolve => setTimeout(resolve, ms)); | ||
| } | ||
|
|
||
| function createAnalyticsLink(validationId) { | ||
| return new Promise(resolve => { | ||
| const query = ` | ||
| traces | ||
| | where customDimensions.validationId == "${validationId}" | ||
| | where customDimensions.logType == "data" | ||
| | where customDimensions.isSuccess == "false" | ||
| | project timestamp, message, customDimensions | ||
| `; | ||
|
|
||
| zlib.deflate(query, (err, buffer) => { | ||
| if (!err) { | ||
| let queryParams = buffer.toString('base64'); | ||
| let analyticsLink = `https://analytics.applicationinsights.io/subscriptions/6b085460-5f21-477e-ba44-1035046e9101/resourcegroups/openapi-platform-logs/components/openapiAI?q=${queryParams}&apptype=Node.JS×pan=P1D`; | ||
| resolve(analyticsLink); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| runScript().then(success => { | ||
| console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); | ||
| console.log(`Thanks for using live validation.`); | ||
| console.log(`If you encounter any issue(s), please open issue(s) at https://github.com/Azure/openapi-platform/issues .`); | ||
| }).catch(err => { | ||
| console.log(err); | ||
| process.exitCode = 1; | ||
| }); |
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.
should we remove "env: MODE=linter PR_ONLY=false" since you're cleaning up the file anyway?
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.
yes if we don't need it
In reply to: 159792088 [](ancestors = 159792088)