netlify-cli 6.14.19 → 6.14.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,6 +13,14 @@ const { openBrowser } = require('./open-browser')
13
13
 
14
14
  const SERVER_PORT = 3000
15
15
 
16
+ /**
17
+ * @typedef Token
18
+ * @type {object}
19
+ * @property {string} user - The username that is associated with the token
20
+ * @property {string} token - The actual token value starting with `gho_`
21
+ * @property {string} provider - The Provider where the token is associated with ('github').
22
+ */
23
+
16
24
  const promptForAuthMethod = async () => {
17
25
  const authChoiceNetlify = 'Authorize with GitHub through app.netlify.com'
18
26
  const authChoiceToken = 'Authorize with a GitHub personal access token'
@@ -34,14 +42,7 @@ const promptForAuthMethod = async () => {
34
42
 
35
43
  /**
36
44
  * Authenticate with the netlify app
37
- * @returns {Promise<Record<string,string>} Returns a Promise with an object of the following shape
38
- * ```
39
- * {
40
- * user: 'spongebob,
41
- * token: 'gho_some-token',
42
- * provider: 'github'
43
- * }
44
- * ```
45
+ * @returns {Promise<Token>} Returns a Promise with a token object
45
46
  */
46
47
  const authWithNetlify = async () => {
47
48
  const port = await getPort({ port: SERVER_PORT })
@@ -95,6 +96,10 @@ const getPersonalAccessToken = async () => {
95
96
  return { token }
96
97
  }
97
98
 
99
+ /**
100
+ * Authenticate with the netlify app
101
+ * @returns {Promise<Token>} Returns a Promise with a token object
102
+ */
98
103
  const authWithToken = async () => {
99
104
  const { token } = await getPersonalAccessToken()
100
105
  if (token) {
@@ -108,6 +113,10 @@ const authWithToken = async () => {
108
113
  throw error
109
114
  }
110
115
 
116
+ /**
117
+ * Get a github token
118
+ * @returns {Promise<Token>} Returns a Promise with a token object
119
+ */
111
120
  const getGitHubToken = async () => {
112
121
  log('')
113
122
 
@@ -2,10 +2,18 @@ const { Octokit } = require('@octokit/rest')
2
2
  const chalk = require('chalk')
3
3
 
4
4
  const { error: failAndExit, log } = require('../command-helpers')
5
- const ghauth = require('../gh-auth')
5
+ const { getGitHubToken: ghauth } = require('../gh-auth')
6
6
 
7
7
  const { createDeployKey, formatErrorMessage, getBuildSettings, saveNetlifyToml, setupSite } = require('./utils')
8
8
 
9
+ /**
10
+ * @typedef Token
11
+ * @type {object}
12
+ * @property {string} user - The username that is associated with the token
13
+ * @property {string} token - The actual token value.
14
+ * @property {string} provider - The Provider where the token is associated with ('github').
15
+ */
16
+
9
17
  const formatRepoAndOwner = ({ repoName, repoOwner }) => ({
10
18
  name: chalk.magenta(repoName),
11
19
  owner: chalk.magenta(repoOwner),
@@ -13,14 +21,27 @@ const formatRepoAndOwner = ({ repoName, repoOwner }) => ({
13
21
 
14
22
  const PAGE_SIZE = 100
15
23
 
16
- const isValidToken = (token) => token && token.user && token.token
17
-
24
+ /**
25
+ * Get a valid github token
26
+ * @returns {string}
27
+ */
18
28
  const getGitHubToken = async ({ globalConfig }) => {
19
29
  const userId = globalConfig.get('userId')
30
+
31
+ /** @type {Token} */
20
32
  const githubToken = globalConfig.get(`users.${userId}.auth.github`)
21
33
 
22
- if (isValidToken(githubToken)) {
23
- return githubToken.token
34
+ if (githubToken && githubToken.user && githubToken.token) {
35
+ try {
36
+ const octokit = getGitHubClient(githubToken.token)
37
+ const { status } = await octokit.rest.users.getAuthenticated()
38
+ if (status < 400) {
39
+ return githubToken.token
40
+ }
41
+ } catch {
42
+ log(chalk.yellow('Token is expired or invalid!'))
43
+ log('Generating a new Github token...')
44
+ }
24
45
  }
25
46
 
26
47
  const newToken = await ghauth()
@@ -28,12 +49,15 @@ const getGitHubToken = async ({ globalConfig }) => {
28
49
  return newToken.token
29
50
  }
30
51
 
31
- const getGitHubClient = ({ token }) => {
32
- const octokit = new Octokit({
52
+ /**
53
+ * Retrieves the Github octokit client
54
+ * @param {string} token
55
+ * @returns {Octokit}
56
+ */
57
+ const getGitHubClient = (token) =>
58
+ new Octokit({
33
59
  auth: `token ${token}`,
34
60
  })
35
- return octokit
36
- }
37
61
 
38
62
  const addDeployKey = async ({ api, octokit, repoName, repoOwner }) => {
39
63
  log('Adding deploy key to repository...')
@@ -172,7 +196,7 @@ const addNotificationHooks = async ({ api, siteId, token }) => {
172
196
  log(`Netlify Notification Hooks configured!`)
173
197
  }
174
198
 
175
- module.exports = async function configGithub({ context, repoName, repoOwner, siteId }) {
199
+ const configGithub = async ({ context, repoName, repoOwner, siteId }) => {
176
200
  const { netlify } = context
177
201
  const {
178
202
  api,
@@ -193,7 +217,7 @@ module.exports = async function configGithub({ context, repoName, repoOwner, sit
193
217
  })
194
218
  await saveNetlifyToml({ repositoryRoot, config, configPath, baseDir, buildCmd, buildDir, functionsDir })
195
219
 
196
- const octokit = getGitHubClient({ token })
220
+ const octokit = getGitHubClient(token)
197
221
  const [deployKey, githubRepo] = await Promise.all([
198
222
  addDeployKey({ api, octokit, repoOwner, repoName }),
199
223
  getGitHubRepo({ octokit, repoOwner, repoName }),
@@ -223,3 +247,5 @@ module.exports = async function configGithub({ context, repoName, repoOwner, sit
223
247
  log()
224
248
  await addNotificationHooks({ siteId, api, token })
225
249
  }
250
+
251
+ module.exports = { configGithub, getGitHubToken }
@@ -2,7 +2,7 @@ const chalk = require('chalk')
2
2
 
3
3
  const { log } = require('../command-helpers')
4
4
 
5
- const configGithub = require('./config-github')
5
+ const { configGithub } = require('./config-github')
6
6
  const configManual = require('./config-manual')
7
7
 
8
8
  const logSuccess = (repoData) => {