<commit_before>/// <reference path="../globals.d.ts" />

import appPackage from '../../app/package.json'
import { createPR } from '../pr-api'

const numberToOrdinal = (n: number) => {
  const s = ['th', 'st', 'nd', 'rd']
  const v = n % 100
  return n + (s[(v - 20) % 10] || s[v] || s[0])
}

function getPullRequestBody(fullVersion: string): string {
  const versionComponents = fullVersion.split('-')
  const version = versionComponents[0]

  let releaseDescription = `v${version} production release`
  if (versionComponents.length > 1) {
    const channelVersion = versionComponents[1]
    if (!channelVersion.startsWith('beta')) {
      throw new Error('We should not create release PRs for test builds')
    }

    const buildNumber = parseInt(channelVersion.substring('beta'.length))
    releaseDescription = `${numberToOrdinal(
      buildNumber
    )} beta of the v${version} series`
  }

  return `## Description
Looking for the PR for the upcoming ${releaseDescription}? Well, you've just found it, congratulations!

## Release checklist

- [ ] Check to see if there are any errors in Sentry that have only occurred since the last production release
- [ ] Verify that all feature flags are flipped appropriately
- [ ] If there are any new metrics, ensure that central and desktop.github.com have been updated
`
}

function getPullRequestTitle(fullVersion: string): string {
  return `Release ${fullVersion}`
}

process.on('unhandledRejection', error => {
  console.error(error.message)
})

async function run() {
  const version = appPackage.version
  console.log(`Creating release Pull Request for ${version}...`)
  const title = getPullRequestTitle(version)
  const body = getPullRequestBody(version)
  const response = await createPR(title, body, `releases/${version}`)
  if (response === null) {
    console.error('Failed to create release Pull Request')
    process.exit(1)
  }
  console.log(`Done: ${response.permalink}`)
}

run()<commit_msg>Reuse logic from bash script in yarn draft-release:pr<commit_after>/// <reference path="../globals.d.ts" />

import appPackage from '../../app/package.json'
import { createPR } from '../pr-api'
import { sh } from '../sh'

async function getPullRequestContent(
  contentType: 'title' | 'body',
  fullVersion: string
): Promise<string> {
  const result = await sh(
    './script/draft-release/release-pr-content.sh',
    contentType,
    fullVersion
  )
  return result.trim()
}

const getPullRequestBody = (fullVersion: string) =>
  getPullRequestContent('body', fullVersion)
const getPullRequestTitle = (fullVersion: string) =>
  getPullRequestContent('title', fullVersion)

process.on('unhandledRejection', error => {
  console.error(error.message)
})

async function run() {
  const version = appPackage.version
  if (version.includes('test')) {
    console.error('Cannot create a PR for a test version')
    process.exit(1)
  }

  console.log(`Creating release Pull Request for ${version}...`)
  const title = await getPullRequestTitle(version)
  const body = await getPullRequestBody(version)
  const response = await createPR(title, body, `releases/${version}`)
  if (response === null) {
    console.error('Failed to create release Pull Request')
    process.exit(1)
  }
  console.log(`Done: ${response.permalink}`)
}

run()
{'fraction_non_alphanumeric': 0.13507757833891085, 'fraction_numerical': 0.0042592029205962886, 'mean_word_length': 4.2025316455696204, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2261807', 'n_tokens_mistral': 985, 'n_tokens_neox': 955, 'n_words': 287}