Skip to content

Commit

Permalink
fix(graph): regressions introduced during JWT migration (#4802)
Browse files Browse the repository at this point in the history
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
anmonteiro and kodiakhq[bot] committed Jul 15, 2022
1 parent 5558446 commit 44245c0
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions src/lib/one-graph/cli-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,17 +403,15 @@ const handleCliSessionEvent = async ({
/**
*
* @param {object} input
* @param {string} input.netlifyToken The (typically netlify) access token that is used for authentication, if any
* @param {string} input.jwt The GraphJWT string
* @param {string} input.oneGraphSessionId The id of the cli session to fetch the current metadata for
* @param {object} input.siteId The site object that contains the root file path for the site
*/
const getCLISession = async ({ netlifyToken, oneGraphSessionId, siteId }) => {
const graphJwt = await OneGraphClient.getGraphJwtForSite({ siteId, nfToken: netlifyToken })

const getCLISession = async ({ jwt, oneGraphSessionId, siteId }) => {
const input = {
appId: siteId,
sessionId: oneGraphSessionId,
jwt: graphJwt.jwt,
jwt,
desiredEventCount: 1,
}
return await OneGraphClient.fetchCliSession(input)
Expand All @@ -422,12 +420,12 @@ const getCLISession = async ({ netlifyToken, oneGraphSessionId, siteId }) => {
/**
*
* @param {object} input
* @param {string} input.netlifyToken The (typically netlify) access token that is used for authentication, if any
* @param {string} input.jwt The GraphJWT string
* @param {string} input.oneGraphSessionId The id of the cli session to fetch the current metadata for
* @param {string} input.siteId The site object that contains the root file path for the site
*/
const getCLISessionMetadata = async ({ netlifyToken, oneGraphSessionId, siteId }) => {
const { errors, session } = await getCLISession({ netlifyToken, oneGraphSessionId, siteId })
const getCLISessionMetadata = async ({ jwt, oneGraphSessionId, siteId }) => {
const { errors, session } = await getCLISession({ jwt, oneGraphSessionId, siteId })
return { metadata: session && session.metadata, errors }
}

Expand Down Expand Up @@ -464,15 +462,15 @@ const detectLocalCLISessionMetadata = ({ siteRoot }) => {
* Fetch the existing cli session metadata if it exists, and mutate it remotely with the passed in metadata
* @param {object} input
* @param {NetlifyGraph.NetlifyGraphConfig} input.netlifyGraphConfig The (typically netlify) access token that is used for authentication, if any
* @param {string} input.netlifyToken The (typically netlify) access token that is used for authentication, if any
* @param {string} input.jwt The Graph JWT string
* @param {string} input.oneGraphSessionId The id of the cli session to fetch the current metadata for
* @param {string} input.siteId The site object that contains the root file path for the site
* @param {string} input.siteRoot The root file path for the site
* @param {object} input.newMetadata The metadata to merge into (with priority) the existing metadata
* @returns {Promise<object>}
*/
const upsertMergeCLISessionMetadata = async ({ netlifyToken, newMetadata, oneGraphSessionId, siteId, siteRoot }) => {
const { errors, metadata } = await getCLISessionMetadata({ netlifyToken, oneGraphSessionId, siteId })
const upsertMergeCLISessionMetadata = async ({ jwt, newMetadata, oneGraphSessionId, siteId, siteRoot }) => {
const { errors, metadata } = await getCLISessionMetadata({ jwt, oneGraphSessionId, siteId })
if (errors) {
warn(`Error fetching cli session metadata: ${JSON.stringify(errors, null, 2)}`)
}
Expand All @@ -481,7 +479,6 @@ const upsertMergeCLISessionMetadata = async ({ netlifyToken, newMetadata, oneGra

// @ts-ignore
const finalMetadata = { ...metadata, ...detectedMetadata, ...newMetadata }
const { jwt } = await OneGraphClient.getGraphJwtForSite({ siteId, nfToken: netlifyToken })

return OneGraphClient.updateCLISessionMetadata(jwt, siteId, oneGraphSessionId, finalMetadata)
}
Expand All @@ -495,16 +492,16 @@ const persistNewOperationsDocForSession = async ({
siteRoot,
}) => {
const { branch } = gitRepoInfo()
const { jwt } = await OneGraphClient.getGraphJwtForSite({ siteId, nfToken: netlifyToken })
const persistedResult = await executeCreatePersistedQueryMutation(
{
nfToken: netlifyToken,
appId: siteId,
description: 'Temporary snapshot of local queries',
query: operationsDoc,
tags: ['netlify-cli', `session:${oneGraphSessionId}`, `git-branch:${branch}`, `local-change`],
},
{
accessToken: netlifyToken,
accessToken: jwt,
siteId,
},
)
Expand All @@ -522,7 +519,7 @@ const persistNewOperationsDocForSession = async ({
const newMetadata = { docId: persistedDoc.id }
const result = await upsertMergeCLISessionMetadata({
netlifyGraphConfig,
netlifyToken,
jwt,
siteId,
oneGraphSessionId,
newMetadata,
Expand Down Expand Up @@ -580,7 +577,7 @@ const startOneGraphCLISession = async (input) => {
const newOperationsDoc = readGraphQLOperationsSourceFile(netlifyGraphConfig)
await persistNewOperationsDocForSession({
netlifyGraphConfig,
netlifyToken,
jwt,
oneGraphSessionId,
operationsDoc: newOperationsDoc,
siteId: site.id,
Expand All @@ -593,7 +590,7 @@ const startOneGraphCLISession = async (input) => {
const newOperationsDoc = readGraphQLOperationsSourceFile(netlifyGraphConfig)
await persistNewOperationsDocForSession({
netlifyGraphConfig,
netlifyToken,
jwt,
oneGraphSessionId,
operationsDoc: newOperationsDoc,
siteId: site.id,
Expand Down Expand Up @@ -680,12 +677,11 @@ const ensureCLISession = async (input) => {
const { metadata, netlifyToken, site, state } = input
let oneGraphSessionId = input.oneGraphSessionId ? input.oneGraphSessionId : loadCLISession(state)
let parentCliSessionId = null
const { jwt } = await OneGraphClient.getGraphJwtForSite({ siteId: site.id, nfToken: netlifyToken })

// Validate that session still exists and we can access it
try {
if (oneGraphSessionId) {
const { jwt } = await OneGraphClient.getGraphJwtForSite({ siteId: site.id, nfToken: netlifyToken })

const sessionEvents = await OneGraphClient.fetchCliSessionEvents({
appId: site.id,
jwt,
Expand Down Expand Up @@ -728,7 +724,7 @@ const ensureCLISession = async (input) => {

state.set('oneGraphSessionId', oneGraphSessionId)
const { errors: markCLISessionActiveErrors } = await executeMarkCliSessionActiveHeartbeat(
netlifyToken,
jwt,
site.id,
oneGraphSessionId,
)
Expand Down

1 comment on commit 44245c0

@github-actions
Copy link

Choose a reason for hiding this comment

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

📊 Benchmark results

Package size: 221 MB

Please sign in to comment.