Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Allows using renovate.json as file to update baseBranches #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 7 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const argv = require('yargs')
type: 'string'
})
.option('file', {
Copy link
Owner

Choose a reason for hiding this comment

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

the script only supports README.md or renovate.json, so I would add the .choices param (didn't test it out)

  .option('file', {
    description: 'append empty space to README.md or update renovate.json (one needs a file diff to create a bump PR)',
    type: 'string',
    choices: ['README.md', 'renovate.json']
  })

Copy link
Author

Choose a reason for hiding this comment

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

I was thinking that you can add a new line in any file (doesn't have to be README.md) but we can limit the script to just work with README.md or renovate.json. I will try with choices :)

description: 'append an empty space space to this file (one needs a file diff to create a bump PR)',
description: 'append an empty space to this file (one needs a file diff to create a bump PR)',
type: 'string',
default: 'README.md'
})
Expand All @@ -15,6 +15,11 @@ const argv = require('yargs')
type: 'string',
default: 'master'
})
.option('release', {
description: 'adds release to `baseBranches` in renovate.json',
type: 'string',
default: 'release-2023-01'
})
.help(false)
.version(false)
.argv
Expand All @@ -30,4 +35,4 @@ run(argv)
.catch((e) => {
console.log(e.message)
process.exit(1)
})
})
32 changes: 20 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,31 @@ const getHighestTag = async ({repo, owner, token}) => {
}

// main application
module.exports = async ({owner, repo, ghToken, ghApprovalToken, file, targetBranch}) => {
module.exports = async ({owner, repo, ghToken, ghApprovalToken, file, targetBranch, release}) => {
const token = ghToken

if (token === ghApprovalToken) throw new Error('gh-approval-token token must be different to the gh-token')
const baseTagCommit = await getHighestTag({repo, owner, token})

// get README.md
const readmeBase64Obj = await gitGetContent({owner, repo, token, path: file})
console.log('readmeBase64Obj', readmeBase64Obj)
const fileBase64Obj = await gitGetContent({owner, repo, token, path: file})
console.log('fileBase64Obj', fileBase64Obj)
const content = Buffer.from(fileBase64Obj.content, 'base64').toString('ascii')

// add an empty line to the readme to have a code diff for the upcoming pull request
const readme = Buffer.from(readmeBase64Obj.content, 'base64').toString('ascii')
const newLineReadme = `\n ${readme}`
const newLineReadmeEncoded = Buffer.from(newLineReadme).toString('base64')
let newContent
if (file === "renovate.json") {
// Replace baseBranches with new release branch
Copy link
Owner

Choose a reason for hiding this comment

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

can you extend the comment and describe how it looks before and after?

const contentJson = JSON.parse(content)
contentJson.baseBranches.splice(1, 0, release)
contentJson.baseBranches = contentJson.baseBranches.slice(0,4)
newContent = JSON.stringify(contentJson)
} else {
// add an empty line to the readme to have a code diff for the upcoming pull request
newContent = `\n ${content}`
}
const newContentEncoded = Buffer.from(newContent).toString('base64')

// create new release-branch
const branchName = `bump-to-next-minor-version-${Date.now()}`
const branchName = `bump-to-next-minor-version-${release}`
Copy link
Owner

Choose a reason for hiding this comment

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

the consequence is that you can only do 1 bump per release (which is most probably ok).

console.log(`try to create branch "${branchName}"`)
const branch = await gitCreateBranch({
owner,
Expand All @@ -59,10 +67,10 @@ module.exports = async ({owner, repo, ghToken, ghApprovalToken, file, targetBran
owner,
repo,
token,
path: readmeBase64Obj.path,
path: fileBase64Obj.path,
message: `feat(release-management): Bump minor version for release management`,
content: newLineReadmeEncoded,
sha: readmeBase64Obj.sha,
content: newContentEncoded,
sha: fileBase64Obj.sha,
branch: branchName
})

Expand Down