OverviewFilesCommitsIssuesDocsReleasesPeers

pgh / lib / commands / label.js

35 lines · javascript ref:
/** pgh label add|remove|list <repo> <issue-id> [label] */

export const usage = `pgh label — issue labels

  pgh label list <repo> <issue-id>         List labels on an issue
  pgh label add <repo> <issue-id> <label>
  pgh label remove <repo> <issue-id> <label>`

export async function run (client, args) {
  const sub = args[0]
  if (!sub) throw new Error(usage)

  const repo = args[1]
  const issueId = args[2]
  if (!repo || !issueId) throw new Error('usage: pgh label <sub> <repo> <issue-id> [label]')

  if (sub === 'list') {
    const issue = await client.call('get-issue', { repo, id: issueId })
    return { labels: issue.labels || [] }
  }

  const label = args[3]
  if (!label) throw new Error('usage: pgh label ' + sub + ' <repo> <issue-id> <label>')

  if (sub === 'add') {
    return client.call('update-issue', { repo, id: issueId, addLabels: [label] })
  }

  if (sub === 'remove') {
    return client.call('update-issue', { repo, id: issueId, removeLabels: [label] })
  }

  throw new Error('unknown label subcommand: ' + sub + '\n' + usage)
}