Skip to content

Commit

Permalink
fix: add correct publish tag for backports
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Jul 10, 2023
1 parent f04a76d commit ee30e61
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 68 deletions.
27 changes: 23 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
npx --offline template-oss-release-please "${{ github.ref_name }}" "${{ inputs.release-pr }}"
npx --offline template-oss-release-please --branch="${{ github.ref_name }}" --force-pr="${{ inputs.release-pr }}" --backport=false
- name: Post Pull Request Comment
if: steps.release.outputs.pr-number
uses: actions/github-script@v6
Expand Down Expand Up @@ -169,7 +169,7 @@ jobs:
RELEASE_COMMENT_ID: ${{ needs.release.outputs.comment-id }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
npm exec --offline -- template-oss-release-manager --lockfile=false --publish=true
npm exec --offline -- template-oss-release-manager --lockfile=false --publish=true --backport=false
npm run rp-pull-request --ignore-scripts -ws -iwr --if-present
- name: Commit
id: commit
Expand Down Expand Up @@ -326,18 +326,37 @@ jobs:
uses: actions/checkout@v3
with:
ref: ${{ fromJSON(needs.release.outputs.release).tagName }}
- name: Setup Git User
run: |
git config --global user.email "npm-cli+bot@github.com"
git config --global user.name "npm CLI robot"
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 18.x
- name: Install npm@latest
run: npm i --prefer-online --no-fund --no-audit -g npm@latest
- name: npm Version
run: npm -v
- name: Set Publish Token
run: |
npm i --prefer-online --no-fund --no-audit -g npm@latest
npm config set '//registry.npmjs.org/:_authToken'=\${PUBLISH_TOKEN}
- name: Publish
env:
PUBLISH_TOKEN: ${{ secrets.PUBLISH_TOKEN }}
run: npm publish --provenance
run: |
for release in $(echo '${{ needs.release.outputs.releases }}' | jq -r '.[] | @base64'); do
function decode {
echo "$release" | base64 --decode | jq -r ".$1"
}
PUBLISH_FLAGS="--provenance --tag=$(decode "publishTag")"
if [[ "$(decode "isWorkspace")" == "false" ]]; then
PUBLISH_FLAGS="$PUBLISH_FLAGS --workspace=$(decode "pkgName")"
fi
npm publish $PUBLISH_FLAGS
done
post-release-integration:
needs: [ release, release-integration ]
Expand Down
4 changes: 3 additions & 1 deletion bin/release-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { Octokit } = require('@octokit/rest')
const semver = require('semver')
const mapWorkspaces = require('@npmcli/map-workspaces')
const { join } = require('path')
const getPublishTag = require('../lib/release-please/get-publish-tag.js')

const log = (...logs) => console.error('LOG', ...logs)

Expand Down Expand Up @@ -127,6 +128,7 @@ const getPrReleases = async (pr) => {
const prerelease = !!version.prerelease.length
const tag = `${name ? `${name}-` : ''}v${rawVersion}`
const workspace = workspaces[name]
const publishTag = getPublishTag({ backport: args.backport, version: rawVersion })

return {
name,
Expand All @@ -135,7 +137,7 @@ const getPrReleases = async (pr) => {
version: rawVersion,
major: version.major,
url: `https://github.com/${pr.base.repo.full_name}/releases/tag/${tag}`,
flags: `${name ? `-w ${workspace}` : ''} ${prerelease ? `--tag prerelease` : ''}`.trim(),
flags: `${name ? `-w ${workspace}` : ''} --tag ${publishTag}`.trim(),
}
}

Expand Down
19 changes: 16 additions & 3 deletions bin/release-please.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@ const core = require('@actions/core')
const main = require('../lib/release-please/index.js')

const dryRun = !process.env.CI
const [branch, forcePullRequest] = process.argv.slice(2)
const args = process.argv.slice(2).reduce((acc, a) => {
const [k, v = ''] = a.replace(/^--/, '').split('=')
if (v === 'true') {
acc[k] = true
} else if (v === 'false') {
acc[k] = false
} else if (/^\d+$/.test(v)) {
acc[k] = Number.parseInt(v.trim())
} else if (v) {
acc[k] = v.trim()
}
return acc
}, {})

const debugPr = (val) => {
if (dryRun) {
Expand Down Expand Up @@ -44,8 +56,9 @@ main({
token: process.env.GITHUB_TOKEN,
repo: process.env.GITHUB_REPOSITORY,
dryRun,
branch,
forcePullRequest: forcePullRequest ? +forcePullRequest : null,
branch: args.branch,
backport: args.backport,
forcePullRequest: args['force-pr'],
}).then(({ pr, release, releases }) => {
if (pr) {
debugPr(pr)
Expand Down
7 changes: 4 additions & 3 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const getFullConfig = async ({
const branches = uniq([...pkgConfig.branches ?? [], pkgConfig.releaseBranch]).filter(Boolean)
const gitBranches = await git.getBranches(rootPkg.path, branches)
const currentBranch = await git.currentBranch(rootPkg.path)
const isReleaseBranch = currentBranch ? minimatch(currentBranch, pkgConfig.releaseBranch) : false
const isBackport = currentBranch ? minimatch(currentBranch, pkgConfig.releaseBranch) : false
const defaultBranch = await git.defaultBranch(rootPkg.path) ?? 'main'

// all derived keys
Expand All @@ -180,10 +180,11 @@ const getFullConfig = async ({
isMonoPublic: isMono && !!publicPkgs.filter(p => p.path !== rootPkg.path).length,
// git
defaultBranch,
baseBranch: isReleaseBranch ? currentBranch : defaultBranch,
baseBranch: isBackport ? currentBranch : defaultBranch,
branches: gitBranches.branches,
branchPatterns: gitBranches.patterns,
isReleaseBranch,
// publish
isBackport,
// dependabot
dependabot: parseDependabot(pkgConfig, defaultConfig, gitBranches.branches),
// repo
Expand Down
45 changes: 26 additions & 19 deletions lib/content/_job-release-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,27 @@ permissions:
deployments: write
id-token: write
steps:
- name: Checkout
uses: actions/checkout@v3
with:
ref: $\{{ fromJSON(needs.release.outputs.release).tagName }}
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 18.x
- name: Install npm@latest
{{> stepGit jobCheckout=(obj ref="${{ fromJSON(needs.release.outputs.release).tagName }}") }}
{{> stepNode lockfile=false }}
- name: Set Publish Token
run: |
npm i --prefer-online --no-fund --no-audit -g npm@latest
npm config set '//registry.npmjs.org/:_authToken'=\${PUBLISH_TOKEN}
- name: Publish
env:
PUBLISH_TOKEN: $\{{ secrets.PUBLISH_TOKEN }}
run: npm publish --provenance
run: |
for release in $(echo '$\{{ needs.release.outputs.releases }}' | jq -r '.[] | @base64'); do
function decode {
echo "$release" | base64 --decode | jq -r ".$1"
}
PUBLISH_FLAGS="--provenance --tag=$(decode "publishTag")"
if [[ "$(decode "isWorkspace")" == "false" ]]; then
PUBLISH_FLAGS="$PUBLISH_FLAGS --workspace=$(decode "pkgName")"
fi
npm publish $PUBLISH_FLAGS
done
{{else}}
runs-on: ubuntu-latest
defaults:
Expand All @@ -43,15 +48,17 @@ steps:
}
for release in $(echo '$\{{ needs.release.outputs.releases }}' | jq -r '.[] | @base64'); do
name=$(echo "$release" | base64 --decode | jq -r .pkgName)
version=$(echo "$release" | base64 --decode | jq -r .version)
spec="$name@$version"
status=$(is_published "$spec")
if [[ "$status" -eq 1 ]]; then
echo "$spec ERROR"
EXIT_CODE=$status
function decode {
echo "$release" | base64 --decode | jq -r ".$1"
}
PKG_SPEC=$(decode "pkgName")@$(decode "version")
if [[ "$(is_published "$PKG_SPEC")" -eq 1 ]]; then
echo "$PKG_SPEC ERROR"
EXIT_CODE=$PKG_STATUS
else
echo "$spec OK"
echo "$PKG_SPEC OK"
fi
done
Expand Down
4 changes: 2 additions & 2 deletions lib/content/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const sharedRootAdd = (name) => ({
},
'.github/settings.yml': {
file: 'settings.yml',
filter: (p) => !p.config.isReleaseBranch,
filter: (p) => !p.config.isBackport,
},
})

Expand All @@ -54,7 +54,7 @@ const sharedRootRm = () => ({
filter: (p) => p.config.allPrivate,
},
'.github/settings.yml': {
filter: (p) => p.config.isReleaseBranch,
filter: (p) => p.config.isBackport,
},
})

Expand Down
4 changes: 2 additions & 2 deletions lib/content/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
env:
GITHUB_TOKEN: $\{{ secrets.GITHUB_TOKEN }}
run: |
{{ rootNpxPath }} --offline template-oss-release-please "$\{{ github.ref_name }}" "$\{{ inputs.release-pr }}"
{{ rootNpxPath }} --offline template-oss-release-please --branch="$\{{ github.ref_name }}" --force-pr="$\{{ inputs.release-pr }}" --backport={{ isBackport }}
- name: Post Pull Request Comment
if: steps.release.outputs.pr-number
uses: actions/github-script@v6
Expand Down Expand Up @@ -84,7 +84,7 @@ jobs:
RELEASE_COMMENT_ID: $\{{ needs.release.outputs.comment-id }}
GITHUB_TOKEN: $\{{ secrets.GITHUB_TOKEN }}
run: |
{{ rootNpmPath }} exec --offline -- template-oss-release-manager --lockfile={{ lockfile }} --publish={{ publish }}
{{ rootNpmPath }} exec --offline -- template-oss-release-manager --lockfile={{ lockfile }} --publish={{ publish }} --backport={{ isBackport }}
{{ rootNpmPath }} run rp-pull-request --ignore-scripts {{~#if allFlags}} {{ allFlags }}{{else}} --if-present{{/if}}
- name: Commit
id: commit
Expand Down
13 changes: 13 additions & 0 deletions lib/release-please/get-publish-tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const semver = require('semver')

// When the CLI is auto published, this will have to account for tagging it
// `next-MAJOR` before publishing it as `latest`
module.exports = ({ backport, version }) => {
const sVersion = semver.parse(version)

if (backport) {
return `latest-${sVersion.major}`
}

return sVersion.prerelease.length ? 'prerelease' : 'latest'
}
7 changes: 6 additions & 1 deletion lib/release-please/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { CheckpointLogger } = require('release-please/build/src/util/logger.js')
const ChangelogNotes = require('./changelog.js')
const Version = require('./version.js')
const NodeWs = require('./node-workspace.js')
const getPublishTag = require('./get-publish-tag.js')

const logger = new CheckpointLogger(true, true)
RP.setLogger(logger)
Expand Down Expand Up @@ -128,7 +129,7 @@ const touchPullRequest = async ({ octokit, owner, repo, releasePr }) => {
})
}

const main = async ({ repo: fullRepo, token, dryRun, branch, forcePullRequest }) => {
const main = async ({ repo: fullRepo, token, dryRun, branch, forcePullRequest, backport }) => {
if (!token) {
throw new Error('Token is required')
}
Expand Down Expand Up @@ -247,6 +248,10 @@ const main = async ({ repo: fullRepo, token, dryRun, branch, forcePullRequest })

release.prNumber = releasePrNumber
release.pkgName = releasePkgName
release.publishTag = getPublishTag({ backport, version: release.version })
release.isRoot = isRoot
release.isWorkspace = !isRoot

if (isRoot) {
rootRelease = release
}
Expand Down
6 changes: 3 additions & 3 deletions lib/util/dependabot.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ module.exports = (config, defaultConfig, branches) => {
return branches
.filter((b) => dependabot[b] !== false)
.map(branch => {
const isReleaseBranch = minimatch(branch, config.releaseBranch)
const isBackport = minimatch(branch, config.releaseBranch)
return {
branch,
allowNames: isReleaseBranch ? [NAME] : [],
labels: isReleaseBranch ? ['Backport', branch] : [],
allowNames: isBackport ? [NAME] : [],
labels: isBackport ? ['Backport', branch] : [],
...parseDependabotConfig(defaultDependabot),
...parseDependabotConfig(dependabot),
...parseDependabotConfig(dependabot[branch]),
Expand Down
Loading

0 comments on commit ee30e61

Please sign in to comment.