Skip to content

Commit

Permalink
Merge pull request #1615 from mrickard/NR-96240/automate-post-release…
Browse files Browse the repository at this point in the history
…-docs-pr

chore: Added create-release-pr job to post-release action
  • Loading branch information
bizob2828 committed May 4, 2023
2 parents 8d6e9d1 + f2dd6d3 commit e271eee
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/post-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ jobs:
run: node ./bin/update-system-config-pages.js --version ${{ steps.get_tag.outputs.latest_tag }} --staging-key ${{ secrets.NEW_RELIC_API_KEY_STAGING }} --prod-key ${{ secrets.NEW_RELIC_API_KEY_PRODUCTION }}
- name: Publish API Docs
run: npm run publish-docs
- name: Create Docs Website PR
run: node ./bin/create-docs-pr.js --tag ${{ steps.get_tag.outputs.latest_tag }}
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
42 changes: 35 additions & 7 deletions bin/create-docs-pr.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ program.option(
'Name of changelog(defaults to NEWS.md)',
DEFAULT_FILE_NAME
)
program.option('-f --force', 'bypass validation')
program.option('--dry-run', 'executes script but does not commit nor create PR')
program.option(
'--repo-path <path',
'Path to the docs-website fork on local machine',
'docs-website'
'/tmp/docs-website'
)

program.option('--repo-owner <owner>', 'Owner of the target repo', 'newrelic')

const RELEASE_NOTES_PATH =
'./src/content/docs/release-notes/agent-release-notes/nodejs-release-notes'

Expand All @@ -48,6 +50,9 @@ async function createReleaseNotesPr() {

console.log(`Script running with following options: ${JSON.stringify(options)}`)

const username = options.username || process.env.GITHUB_ACTOR
const repoOwner = options.repoOwner

try {
const version = options.tag.replace('refs/tags/', '')
console.log(`Getting version from tag: ${version}`)
Expand All @@ -56,6 +61,10 @@ async function createReleaseNotesPr() {
validateTag(version, options.force)
logStep('Get Release Notes from File')
const { body, releaseDate } = await getReleaseNotes(version, options.changelog)

logStep('Set up docs repo')
await cloneDocsRepo(options.repoPath, repoOwner)

logStep('Branch Creation')
const branchName = await createBranch(options.repoPath, version, options.dryRun)
logStep('Format release notes file')
Expand All @@ -65,7 +74,7 @@ async function createReleaseNotesPr() {
logStep('Commit Release Notes')
await commitReleaseNotes(version, options.remote, branchName, options.dryRun)
logStep('Create Pull Request')
await createPR(options.username, version, branchName, options.dryRun)
await createPR(username, version, branchName, options.dryRun, repoOwner)
console.log('*** Full Run Successful ***')
} catch (err) {
if (err.status && err.status === 404) {
Expand Down Expand Up @@ -128,14 +137,29 @@ async function readReleaseNoteFile(file) {
return new Promise((resolve, reject) => {
fs.readFile(file, 'utf8', (err, data) => {
if (err) {
return reject(err)
reject(err)
}

return resolve(data)
resolve(data)
})
})
}

/**
* Clones docs repo
*
* @param repoPath
* @param repoOwner
* @returns {boolean} success or failure
*/
async function cloneDocsRepo(repoPath, repoOwner) {
const branch = 'develop'
const url = `https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${repoOwner}/docs-website.git`
const cloneOptions = [`--branch=${branch}`, '--single-branch']

return git.clone(url, repoPath, cloneOptions)
}

/**
* Creates a branch in your local `docs-website` fork
* That follows the pattern `add-node-<new agent version>`
Expand Down Expand Up @@ -227,6 +251,9 @@ async function commitReleaseNotes(version, remote, branch, dryRun) {
console.log('Dry run indicated (--dry-run), skipping committing release notes.')
return
}
const GITHUB_ACTOR = process.env.GITHUB_ACTOR
const GITHUB_EMAIL = `gh-actions-${GITHUB_ACTOR}@github.com`
await git.setUser(GITHUB_ACTOR, GITHUB_EMAIL)

console.log(`Adding release notes for ${version}`)
const files = [getFileName(version)]
Expand All @@ -243,14 +270,15 @@ async function commitReleaseNotes(version, remote, branch, dryRun) {
* @param {string} version version number
* @param {string} branch github branch
* @param {boolean} dryRun whether or not we should actually create the PR
* @param {string} repoOwner Owner of the docs-website repo, if targeting a fork instead of newrelic
*/
async function createPR(username, version, branch, dryRun) {
async function createPR(username, version, branch, dryRun, repoOwner) {
if (!process.env.GITHUB_TOKEN) {
console.log('GITHUB_TOKEN required to create a pull request')
stopOnError()
}

const github = new Github('newrelic', 'docs-website')
const github = new Github(repoOwner, 'docs-website')
const title = `Node.js Agent ${version} Release Notes`
const prOptions = {
head: `${username}:${branch}`,
Expand Down
13 changes: 10 additions & 3 deletions bin/git-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,22 @@ async function sparseCloneRepo(repoInfo, checkoutFiles) {
process.chdir('..')
}

async function setUser(name, email) {
const setName = await execAsPromise(`git config user.name ${name}`)
const setEmail = await execAsPromise(`git config user.email ${email}`)
return [setName, setEmail].join(' ')
}

function execAsPromise(command) {
return new Promise((resolve, reject) => {
console.log(`Executing: '${command}'`)

exec(command, (err, stdout) => {
if (err) {
return reject(err)
reject(err)
}

return resolve(stdout)
resolve(stdout)
})
})
}
Expand All @@ -143,5 +149,6 @@ module.exports = {
checkout,
clone,
sparseCloneRepo,
addFiles
addFiles,
setUser
}

0 comments on commit e271eee

Please sign in to comment.