netlify-cli 6.14.21 → 6.15.0

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.
@@ -6,6 +6,14 @@ 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) => {