Skip to content
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

fix(command-link): retrieve all sites using pagination params #1899

Merged
merged 2 commits into from
Feb 17, 2021
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
10 changes: 7 additions & 3 deletions src/commands/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const process = require('process')
const { flags: flagsLib } = require('@oclif/command')
const chalk = require('chalk')

const { listSites } = require('../lib/api')
const Command = require('../utils/command')
const { ensureNetlifyIgnore } = require('../utils/gitignore')
const linkPrompt = require('../utils/link/link-by-prompt')
Expand Down Expand Up @@ -77,9 +78,12 @@ class LinkCommand extends Command {
if (flags.name) {
let results
try {
results = await api.listSites({
name: flags.name,
filter: 'all',
results = await listSites({
api,
options: {
name: flags.name,
filter: 'all',
},
})
} catch (error) {
if (error.status === 404) {
Expand Down
3 changes: 2 additions & 1 deletion src/commands/sites/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { flags: flagsLib } = require('@oclif/command')
const chalk = require('chalk')
const { cli } = require('cli-ux')

const { listSites } = require('../../lib/api')
const Command = require('../../utils/command')

class SitesListCommand extends Command {
Expand All @@ -20,7 +21,7 @@ class SitesListCommand extends Command {
},
})

const sites = await api.listSites({ filter: 'all' })
const sites = await listSites({ api, options: { filter: 'all' } })
if (!flags.json) {
cli.action.stop()
}
Expand Down
15 changes: 14 additions & 1 deletion src/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,17 @@ const cancelDeploy = async ({ api, deployId, warn }) => {
}
}

module.exports = { uploadEdgeHandlers, cancelDeploy }
const FIRST_PAGE = 1
const MAX_PAGES = 10
const MAX_PER_PAGE = 100
const listSites = async ({ api, options }) => {
const { page = FIRST_PAGE, maxPages = MAX_PAGES, ...rest } = options
const sites = await api.listSites({ page, per_page: MAX_PER_PAGE, ...rest })
// TODO: use pagination headers when js-client returns them
if (sites.length === MAX_PER_PAGE && page + 1 <= maxPages) {
return [...sites, ...(await listSites({ api, options: { page: page + 1, maxPages, ...rest } }))]
}
return sites
}

module.exports = { uploadEdgeHandlers, cancelDeploy, listSites }
11 changes: 6 additions & 5 deletions src/utils/link/link-by-prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const chalk = require('chalk')
const inquirer = require('inquirer')
const isEmpty = require('lodash/isEmpty')

const { listSites } = require('../../lib/api')
const { getRepoData } = require('../get-repo-data')
const { track } = require('../telemetry')

Expand Down Expand Up @@ -46,7 +47,7 @@ module.exports = async function linkPrompts(context, flags = {}) {
context.log()
context.log(`Looking for sites connected to '${repoData.httpsUrl}'...`)
context.log()
const sites = await api.listSites({ filter: 'all' })
const sites = await listSites({ api, options: { filter: 'all' } })

if (isEmpty(sites)) {
context.error(
Expand Down Expand Up @@ -112,9 +113,9 @@ Run ${chalk.cyanBright('git remote -v')} to see a list of your git remotes.`)

let matchingSites
try {
matchingSites = await api.listSites({
name: searchTerm,
filter: 'all',
matchingSites = await listSites({
api,
options: { name: searchTerm, filter: 'all' },
})
} catch (error) {
if (error.status === 404) {
Expand Down Expand Up @@ -159,7 +160,7 @@ or run ${chalk.cyanBright('netlify sites:create')} to create a site.`)

let sites
try {
sites = await api.listSites({ filter: 'all' })
sites = await listSites({ api, options: { maxPages: 1, filter: 'all' } })
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Limit to 1 page of results since the option here is Fetching recently updated sites

} catch (error) {
context.error(error)
}
Expand Down