|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * Bumps the package versions according the the git commits present |
| 19 | + * after the latest tag of the package |
| 20 | + */ |
| 21 | + |
| 22 | +import { exec, execSync } from 'child_process'; |
| 23 | +import path from 'path'; |
| 24 | +import { readFileSync } from 'fs'; |
| 25 | +import { globSync } from 'glob'; |
| 26 | + |
| 27 | +// TODO: move this into a common file |
| 28 | +const readJson = (filePath) => { |
| 29 | + return JSON.parse(readFileSync(filePath)); |
| 30 | +}; |
| 31 | + |
| 32 | +const getPackages = () => { |
| 33 | + const TOP = process.cwd(); |
| 34 | + const pj = readJson(path.join(TOP, 'package.json')); |
| 35 | + return pj.workspaces |
| 36 | + .map((wsGlob) => globSync(path.join(wsGlob, 'package.json'))) |
| 37 | + .flat() |
| 38 | + .map((p) => { |
| 39 | + const pkgInfo = readJson(p); |
| 40 | + pkgInfo.location = path.dirname(p); |
| 41 | + return pkgInfo; |
| 42 | + }); |
| 43 | +} |
| 44 | +const publicPkgList = getPackages().filter(pkg => !pkg.private); |
| 45 | +const repoTags = execSync('git tag', { encoding: 'utf-8' }).split('\n'); |
| 46 | + |
| 47 | +// Set the latest tag on each package |
| 48 | +repoTags.forEach((tag) => { |
| 49 | + const nameParts = tag.split('-'); |
| 50 | + const version = nameParts.pop(); |
| 51 | + const pkgName = `@opentelemetry/${nameParts.join('-')}`; |
| 52 | + const pkgInfo = publicPkgList.find((pkg) => pkg.name === pkgName); |
| 53 | + |
| 54 | + // Assumption: `git tag` rueturs tags from old to new so we care about |
| 55 | + // the last occurrence |
| 56 | + if (pkgInfo) { |
| 57 | + pkgInfo.tag = tag; |
| 58 | + } |
| 59 | +}); |
| 60 | + |
| 61 | +// Check for commits on each package |
| 62 | +// Assumption: current version is latest on npm (so no checking it) |
| 63 | +publicPkgList.forEach((pkgInfo) => { |
| 64 | + if (pkgInfo.tag) { |
| 65 | + // need to calculate next version |
| 66 | + const commitList = execSync(`git log ${pkgInfo.tag}..HEAD --oneline`, { encoding: 'utf-8' }).split('\n'); |
| 67 | + const pkgScope = pkgInfo.name.replace('@opentelemetry/', ''); |
| 68 | + const commitsForPackage = commitList.filter((c) => c.indexOf(`(${pkgScope})`) !== -1); |
| 69 | + |
| 70 | + if (commitsForPackage.length === 0) { |
| 71 | + return; |
| 72 | + } |
| 73 | + console.log(pkgInfo.tag) |
| 74 | + console.log(commitsForPackage) |
| 75 | + const [major, minor, patch] = pkgInfo.version.split('.').map(n => parseInt(n, 10)); |
| 76 | + const isExperimental = major === 0; |
| 77 | + const bumpMinor = commitsForPackage.some((cmt) => { |
| 78 | + const pattern = isExperimental ? `(${pkgScope})!:` : `feat(${pkgScope}):` |
| 79 | + return cmt.includes(pattern); |
| 80 | + }); |
| 81 | + const bumpMajor = !isExperimental && commitsForPackage.some((cmt) => cmt.includes(`(${pkgScope})!:`)); |
| 82 | + |
| 83 | + let command |
| 84 | + if (bumpMajor) { |
| 85 | + command = 'npm version major'; |
| 86 | + } else if (bumpMinor) { |
| 87 | + command = 'npm version minor'; |
| 88 | + } else { |
| 89 | + command = 'npm version patch'; |
| 90 | + } |
| 91 | + console.log(`executing ${command}`) |
| 92 | + execSync(`${command} --git-tag-version=false`, { cwd: pkgInfo.location }); |
| 93 | + |
| 94 | + } else { |
| 95 | + // is the 1st time so no new version needed |
| 96 | + console.log(pkgInfo.name, 'has no tag') |
| 97 | + } |
| 98 | +}) |
0 commit comments