Skip to content

Commit d86b1f2

Browse files
committed
chore: update version script
1 parent 1df01ae commit d86b1f2

File tree

1 file changed

+53
-22
lines changed

1 file changed

+53
-22
lines changed

scripts/bump-package-versions.mjs

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,57 @@ const getPackages = () => {
4343
});
4444
}
4545

46-
const getScopedCommitsFrom = (scope, commitOrTag) => {
47-
const commits = execSync(`git log ${commitOrTag}..HEAD --oneline`, { encoding: 'utf-8' }).split('\n');
46+
const getPkgCommitsFrom = (pkgInfo, commitOrTag) => {
47+
const command = `git log ${commitOrTag}..HEAD --oneline ${pkgInfo.location}`;
48+
const commits = execSync(command,{ encoding: 'utf-8' }).split('\n');
4849

49-
return commits.filter((c) => c.indexOf(`(${scope})`) !== -1);
50+
return commits;
5051
}
5152

53+
/**
54+
*
55+
* @param {Record<string, any>} pkgInfo
56+
* @param {string[]} commits
57+
* @returns {'major' | 'minor' | 'patch'}
58+
*/
59+
const getBumpType = (pkgInfo, commits) => {
60+
const isExperimental = pkgInfo.version.startsWith('0.');
61+
let bumpType = 'patch';
62+
console.log('isExperimental', isExperimental)
63+
for (const commit of commits) {
64+
// commit must be in the proper format
65+
if (commit.indexOf(':') === -1) {
66+
continue;
67+
}
68+
const commitPrefix = commit.split(':').shift().trim();
69+
const isBreaking = commitPrefix.endsWith('!');
70+
const isFeature = commitPrefix.includes('feat');
71+
72+
// Experimental only accpets patch & minor
73+
if (isExperimental && (isBreaking || isFeature)) {
74+
return 'minor';
75+
}
76+
77+
// Stable could be also major
78+
if (!isExperimental) {
79+
if (isBreaking) {
80+
return 'major';
81+
}
82+
if (isFeature) {
83+
bumpType = 'minor';
84+
}
85+
}
86+
}
87+
return bumpType;
88+
};
89+
5290
// -- Main line
5391
const publicPkgList = getPackages().filter(pkg => !pkg.private);
5492
const repoTags = execSync('git tag', { encoding: 'utf-8' }).split('\n');
5593

5694
// Set the latest tag on each package
5795
repoTags.forEach((tag) => {
58-
const nameParts = tag.split('-');
96+
const nameParts = tag.split('-').slice(0, -1);
5997
const pkgName = `@opentelemetry/${nameParts.join('-')}`;
6098
const pkgInfo = publicPkgList.find((pkg) => pkg.name === pkgName);
6199

@@ -71,21 +109,21 @@ repoTags.forEach((tag) => {
71109
publicPkgList.forEach((pkgInfo) => {
72110
const pkgScope = pkgInfo.name.replace('@opentelemetry/', '');
73111

112+
// if (pkgInfo.tag) {
113+
// console.log(`**** commits for ${pkgScope} from ${pkgInfo.tag} ****`)
114+
// const scopedCommits = getPkgCommitsFrom(pkgInfo, pkgInfo.tag);
115+
// console.log('bump type', getBumpType(pkgInfo, scopedCommits))
116+
// }
117+
// return
118+
74119
if (pkgInfo.tag) {
75-
const scopedCommits = getScopedCommitsFrom(pkgScope, pkgInfo.tag);
120+
const scopedCommits = getPkgCommitsFrom(pkgInfo, pkgInfo.tag);
76121

77122
if (scopedCommits.length === 0) {
78123
return;
79124
}
80125

81-
const isExperimental = pkgInfo.version.startsWith('0.');
82-
const bumpMinor = scopedCommits.some((cmt) => {
83-
const pattern = isExperimental ? `(${pkgScope})!:` : `feat(${pkgScope}):`
84-
return cmt.includes(pattern);
85-
});
86-
const bumpMajor = !isExperimental && scopedCommits.some((cmt) => cmt.includes(`(${pkgScope})!:`));
87-
const bumpType = bumpMajor ? 'major' : (bumpMinor ? 'minor' : 'patch');
88-
126+
const bumpType = getBumpType(pkgInfo, scopedCommits);
89127
console.log(`Bumping ${bumpType} version in ${pkgInfo.name}`);
90128
execSync(`npm version ${bumpType} --git-tag-version=false`, { cwd: pkgInfo.location });
91129
} else {
@@ -115,17 +153,10 @@ publicPkgList.forEach((pkgInfo) => {
115153
// - check for commits since then, and do the calculation
116154
const addCommit = execSync(`git log --diff-filter=A -- ${pkgInfo.location}/package.json`, { encoding: 'utf-8' });
117155
const commitSha = addCommit.substring(7, 14);
118-
const scopedCommits = getScopedCommitsFrom(pkgScope, commitSha);
156+
const scopedCommits = getPkgCommitsFrom(pkgInfo, commitSha);
119157

120158
console.log(`Package ${pkgInfo.name} was added in ${commitSha}`);
121-
const isExperimental = pkgInfo.version.startsWith('0.');
122-
const bumpMinor = scopedCommits.some((cmt) => {
123-
const pattern = isExperimental ? `(${pkgScope})!:` : `feat(${pkgScope}):`
124-
return cmt.includes(pattern);
125-
});
126-
const bumpMajor = !isExperimental && scopedCommits.some((cmt) => cmt.includes(`(${pkgScope})!:`));
127-
const bumpType = bumpMajor ? 'major' : (bumpMinor ? 'minor' : 'patch');
128-
159+
const bumpType = getBumpType(pkgInfo, scopedCommits);
129160
console.log(`Bumping ${bumpType} version in ${pkgInfo.name}`);
130161
execSync(`npm version ${bumpType} --git-tag-version=false`, { cwd: pkgInfo.location });
131162
}

0 commit comments

Comments
 (0)