-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add graph:init command (#4711)
* feat: add graph:init command * feat: add new command * chore: update docs * chore: address PR comments * Apply suggestions from code review Co-authored-by: Kyle Rollins <kyleblankrollins@gmail.com> * fix: better error messages, and fix authentication * fix: #4868 * chore: generate docs Co-authored-by: Antonio Nuno Monteiro <anmonteiro@gmail.com> Co-authored-by: Kyle Rollins <kyleblankrollins@gmail.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
7203fe5
commit 22ac960
Showing
11 changed files
with
182 additions
and
9 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// @ts-check | ||
const { Buffer } = require('buffer') | ||
|
||
const { OneGraphClient } = require('netlify-onegraph-internal') | ||
const { v4: uuidv4 } = require('uuid') | ||
|
||
const { OneGraphCliClient, ensureCLISession } = require('../../lib/one-graph/cli-client') | ||
const { NETLIFYDEVERR, chalk, error, exit, getToken, log } = require('../../utils') | ||
const { msg } = require('../login/login') | ||
|
||
const { ensureAppForSite, executeCreateApiTokenMutation } = OneGraphCliClient | ||
|
||
/** | ||
* Creates the `netlify graph:init` command | ||
* @param {import('commander').OptionValues} options | ||
* @param {import('../base-command').BaseCommand} command | ||
* @returns | ||
*/ | ||
const graphInit = async (options, command) => { | ||
const { api, site, state } = command.netlify | ||
const siteId = site.id | ||
|
||
if (!siteId) { | ||
error( | ||
`${NETLIFYDEVERR} Warning: no siteId defined, unable to start Netlify Graph. To enable, run ${chalk.yellow( | ||
'netlify init', | ||
)} or ${chalk.yellow('netlify link')}`, | ||
) | ||
} | ||
|
||
let [netlifyToken, loginLocation] = await getToken() | ||
if (!netlifyToken) { | ||
netlifyToken = await command.authenticate() | ||
} | ||
|
||
let siteData = null | ||
try { | ||
// @ts-ignore: we need better types for our api object | ||
siteData = await api.getSite({ siteId }) | ||
} catch (error_) { | ||
if (netlifyToken && error_.status === 401) { | ||
log(`Already logged in ${msg(loginLocation)}`) | ||
log() | ||
log(`Run ${chalk.cyanBright('netlify status')} for account details`) | ||
log() | ||
log(`or run ${chalk.cyanBright('netlify switch')} to switch accounts`) | ||
log() | ||
log(`To see all available commands run: ${chalk.cyanBright('netlify help')}`) | ||
log() | ||
return exit() | ||
} | ||
throw error_ | ||
} | ||
|
||
if (netlifyToken == null) { | ||
error( | ||
`${NETLIFYDEVERR} Error: Unable to start Netlify Graph without a login. To enable, run ${chalk.yellow( | ||
'netlify login', | ||
)} first`, | ||
) | ||
return exit() | ||
} | ||
|
||
await ensureAppForSite(netlifyToken, siteId) | ||
|
||
await ensureCLISession({ | ||
metadata: {}, | ||
netlifyToken, | ||
site, | ||
state, | ||
}) | ||
|
||
let envChanged = false | ||
|
||
// Get current environment variables set in the UI | ||
const { | ||
build_settings: { env = {} }, | ||
} = siteData | ||
|
||
const newEnv = { | ||
...env, | ||
} | ||
|
||
if (!env.NETLIFY_GRAPH_WEBHOOK_SECRET) { | ||
envChanged = true | ||
const secret = Buffer.from(uuidv4()).toString('base64') | ||
newEnv.NETLIFY_GRAPH_WEBHOOK_SECRET = secret | ||
} | ||
|
||
if (!env.NETLIFY_GRAPH_PERSIST_QUERY_TOKEN) { | ||
const variables = { | ||
input: { | ||
appId: siteId, | ||
scopes: ['MODIFY_SCHEMA', 'PERSIST_QUERY'], | ||
}, | ||
} | ||
|
||
const { jwt } = await OneGraphClient.getGraphJwtForSite({ siteId, nfToken: netlifyToken }) | ||
const result = await executeCreateApiTokenMutation(variables, { | ||
siteId, | ||
accessToken: jwt, | ||
}) | ||
|
||
const token = | ||
result.data && | ||
result.data.oneGraph && | ||
result.data.oneGraph.createApiToken && | ||
result.data.oneGraph.createApiToken.accessToken && | ||
result.data.oneGraph.createApiToken.accessToken.token | ||
|
||
if (token) { | ||
envChanged = true | ||
newEnv.NETLIFY_GRAPH_PERSIST_QUERY_TOKEN = token | ||
} else { | ||
error(`Unable to create Netlify Graph persist query token: ${JSON.stringify(result.errors, null, 2)}`) | ||
} | ||
} | ||
|
||
if (envChanged) { | ||
// Apply environment variable updates | ||
// @ts-ignore | ||
await api.updateSite({ | ||
siteId, | ||
body: { | ||
build_settings: { | ||
env: newEnv, | ||
}, | ||
}, | ||
}) | ||
|
||
log(`Finished updating Graph-related environment variables for site ${siteData.name}`) | ||
} | ||
} | ||
|
||
/** | ||
* Creates the `netlify graph:init` command | ||
* @param {import('../base-command').BaseCommand} program | ||
* @returns | ||
*/ | ||
const createGraphInitCommand = (program) => | ||
program | ||
.command('graph:init') | ||
.description('Initialize all the resources for Netlify Graph') | ||
.action(async (options, command) => { | ||
await graphInit(options, command) | ||
}) | ||
|
||
module.exports = { createGraphInitCommand } |
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
Binary file modified
BIN
+26 Bytes
(100%)
tests/integration/snapshots/220.command.graph.test.js.snap
Binary file not shown.
22ac960
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.
📊 Benchmark results
Package size: 230 MB