Skip to content

fix(site): Prompting user for missing variables #7002

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 3 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion site/src/api/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ describe("api.ts", () => {

expect(error).toBeInstanceOf(api.MissingBuildParameters)
// Verify if the correct missing parameters are being passed
// It should not require immutable parameters
expect((error as api.MissingBuildParameters).parameters).toEqual([
MockTemplateVersionParameter1,
{ ...MockTemplateVersionParameter2, mutable: false },
])
})

Expand Down
51 changes: 32 additions & 19 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,27 @@ import { DeploymentConfig } from "./types"
import * as TypesGen from "./typesGenerated"

// Adds 304 for the default axios validateStatus function
// https://github.com/axios/axios#handling-errors
// Check status here https://httpstatusdogs.com/
// https://github.com/axios/axios#handling-errors Check status here
// https://httpstatusdogs.com/
axios.defaults.validateStatus = (status) => {
return (status >= 200 && status < 300) || status === 304
}

export const hardCodedCSRFCookie = (): string => {
// This is a hard coded CSRF token/cookie pair for local development.
// In prod, the GoLang webserver generates a random cookie with a new token for
// each document request. For local development, we don't use the Go webserver for static files,
// so this is the 'hack' to make local development work with remote apis.
// The CSRF cookie for this token is "JXm9hOUdZctWt0ZZGAy9xiS/gxMKYOThdxjjMnMUyn4="
// This is a hard coded CSRF token/cookie pair for local development. In prod,
// the GoLang webserver generates a random cookie with a new token for each
// document request. For local development, we don't use the Go webserver for
// static files, so this is the 'hack' to make local development work with
// remote apis. The CSRF cookie for this token is
// "JXm9hOUdZctWt0ZZGAy9xiS/gxMKYOThdxjjMnMUyn4="
const csrfToken =
"KNKvagCBEHZK7ihe2t7fj6VeJ0UyTDco1yVUJE8N06oNqxLu5Zx1vRxZbgfC0mJJgeGkVjgs08mgPbcWPBkZ1A=="
axios.defaults.headers.common["X-CSRF-TOKEN"] = csrfToken
return csrfToken
}

// withDefaultFeatures sets all unspecified features to not_entitled and disabled.
// withDefaultFeatures sets all unspecified features to not_entitled and
// disabled.
export const withDefaultFeatures = (
fs: Partial<TypesGen.Entitlements["features"]>,
): TypesGen.Entitlements["features"] => {
Expand All @@ -40,9 +42,8 @@ export const withDefaultFeatures = (
return fs as TypesGen.Entitlements["features"]
}

// Always attach CSRF token to all requests.
// In puppeteer the document is undefined. In those cases, just
// do nothing.
// Always attach CSRF token to all requests. In puppeteer the document is
// undefined. In those cases, just do nothing.
const token =
typeof document !== "undefined"
? document.head.querySelector('meta[property="csrf-token"]')
Expand Down Expand Up @@ -978,7 +979,8 @@ export class MissingBuildParameters extends Error {
* - Get the latest template to access the latest active version
* - Get the current build parameters
* - Get the template parameters
* - Update the build parameters and check if there are missed parameters for the newest version
* - Update the build parameters and check if there are missed parameters for
* the newest version
* - If there are missing parameters raise an error
* - Create a build with the latest version and updated build parameters
*/
Expand Down Expand Up @@ -1017,12 +1019,22 @@ const getMissingParameters = (
templateParameters: TypesGen.TemplateVersionParameter[],
) => {
const missingParameters: TypesGen.TemplateVersionParameter[] = []
const requiredParameters = templateParameters.filter(
// It is required
// and it can be changed
// and it is not from a legacy variable
(p) => p.required && p.mutable && p.legacy_variable_name === undefined,
)
const requiredParameters: TypesGen.TemplateVersionParameter[] = []

templateParameters.forEach((p) => {
// Legacy parameters should be required. So we can migrate them.
const isLegacy = p.legacy_variable_name === undefined
// It is mutable and required. Mutable values can be changed after so we
// don't need to ask them if they are not required.
const isMutableAndRequired = p.mutable && p.required
// Is immutable, so we can check if it is its first time on the build
const isImmutable = !p.mutable

if (isLegacy || isMutableAndRequired || isImmutable) {
requiredParameters.push(p)
return
}
})
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mtojek @bpmct is this logic correct? I must admit that asking or not for missing parameters is pretty challenging to understand for me 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me try it locally...


for (const parameter of requiredParameters) {
// Check if there is a new value
Expand All @@ -1049,7 +1061,8 @@ const getMissingParameters = (
/**
*
* @param agentId
* @returns An EventSource that emits agent metadata event objects (ServerSentEvent)
* @returns An EventSource that emits agent metadata event objects
* (ServerSentEvent)
*/
export const watchAgentMetadata = (agentId: string): EventSource => {
return new EventSource(
Expand Down