Skip to content

Commit

Permalink
📝 [RUM-152] Add packages name modified next to commit in CHANGELOG (#…
Browse files Browse the repository at this point in the history
…2889)

* Add packages name modified next to commit in CHANGELOG

* factorize package name

* change path packages

* update folder for package directory name
  • Loading branch information
RomanGaignault authored Jul 25, 2024
1 parent 0764e79 commit 44baa33
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 7 deletions.
6 changes: 6 additions & 0 deletions scripts/lib/packages-directory-names.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { readdirSync } = require('fs')
const PACKAGES_DIRECTORY_NAMES = readdirSync('packages')

module.exports = {
packagesDirectoryNames: PACKAGES_DIRECTORY_NAMES,
}
87 changes: 82 additions & 5 deletions scripts/release/generate-changelog.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const util = require('util')
const fs = require('fs')
const readFile = util.promisify(require('fs').readFile)

const emojiNameMap = require('emoji-name-map')
Expand All @@ -9,11 +10,11 @@ const { browserSdkVersion } = require('../lib/browser-sdk-version')
const { spawnCommand, printError, runMain } = require('../lib/execution-utils')
const { command } = require('../lib/command')
const { modifyFile } = require('../lib/files-utils')
const { packagesDirectoryNames } = require('../lib/packages-directory-names')

const CHANGELOG_FILE = 'CHANGELOG.md'
const CONTRIBUTING_FILE = 'CONTRIBUTING.md'
const PUBLIC_EMOJI_PRIORITY = ['💥', '✨', '🐛', '⚡', '📝']

const INTERNAL_EMOJI_PRIORITY = [
'👷',
'🔧',
Expand All @@ -29,6 +30,20 @@ const INTERNAL_EMOJI_PRIORITY = [
'⚗️', // experiment
]
const EMOJI_REGEX = /^\p{Emoji_Presentation}/u

const PACKAGES_REVERSE_DEPENDENCIES = (() => {
const result = new Map()
packagesDirectoryNames.forEach((packageDirectoryName) => {
for (const dependency of getDepenciesRecursively(packageDirectoryName)) {
if (!result.has(dependency)) {
result.set(dependency, new Set())
}
result.get(dependency).add(packageDirectoryName)
}
})
return result
})()

runMain(async () => {
if (!process.env.EDITOR) {
printError('Please configure your environment variable EDITOR')
Expand Down Expand Up @@ -88,7 +103,7 @@ function getChangesList() {
const lastTagHash = command`git rev-list --tags --max-count=1`.run().trim()
const lastTagName = command`git describe --tags ${lastTagHash}`.run()

const commits = command`git log ${lastTagName.trimEnd()}..HEAD --pretty=format:%s`.run()
const commits = command`git log ${[`${lastTagName.trimEnd()}..HEAD`, '--pretty=format:%H %s']}`.run()
const changesWithEmojis = emojiNameToUnicode(commits)

let changes = changesWithEmojis.split('\n').filter(isNotVersionEntry)
Expand All @@ -97,10 +112,18 @@ function getChangesList() {

changes.forEach((entry) => {
let trimmedEntry = entry.trim()
if (PUBLIC_EMOJI_PRIORITY.some((emoji) => trimmedEntry.startsWith(emoji))) {
publicChanges.push(entry)
const hash = trimmedEntry.split(' ')[0]
const message = trimmedEntry.slice(trimmedEntry.indexOf(' ') + 1)
const affectedPackages = getAffectedPackages(hash)

const formattedPackages = affectedPackages
.map((packageDirectoryName) => `[${packageDirectoryName.toUpperCase()}]`)
.join(' ')

if (PUBLIC_EMOJI_PRIORITY.some((emoji) => message.startsWith(emoji))) {
publicChanges.push(`${message} ${formattedPackages}`)
} else {
internalChanges.push(entry)
internalChanges.push(`${message} ${formattedPackages}`)
}
})

Expand All @@ -120,6 +143,38 @@ ${internalChanges.join('\n')}
`.replace(/\(#(\d+)\)/gm, (_, id) => `([#${id}](https://github.com/DataDog/browser-sdk/pull/${id}))`)
}

function getAffectedPackages(hash) {
const changedFiles = command`git diff-tree --no-commit-id --name-only -r ${hash}`.run().trim().split('\n')
const affectedPackages = new Set()

changedFiles.forEach((filePath) => {
const packageDirectoryName = getPackageDirectoryNameFromFilePath(filePath)
if (packageDirectoryName) {
if (!isToplevelPackage(packageDirectoryName)) {
PACKAGES_REVERSE_DEPENDENCIES.get(packageDirectoryName).forEach((dependentPackageDirectoryName) => {
if (isToplevelPackage(dependentPackageDirectoryName)) {
affectedPackages.add(dependentPackageDirectoryName)
}
})
} else {
affectedPackages.add(packageDirectoryName)
}
}
})

return Array.from(affectedPackages)
}

function getPackageDirectoryNameFromFilePath(filePath) {
if (filePath.startsWith('packages/')) {
return filePath.split('/')[1]
}
}

function isToplevelPackage(packageDirectoryName) {
return !PACKAGES_REVERSE_DEPENDENCIES.has(packageDirectoryName)
}

function sortByEmojiPriority(a, b, priorityList) {
const getFirstRelevantEmojiIndex = (text) => {
const matches = text.match(EMOJI_REGEX) || []
Expand All @@ -137,3 +192,25 @@ function emojiNameToUnicode(changes) {
function isNotVersionEntry(line) {
return !/^v\d+\.\d+\.\d+/.test(line)
}
function getPackageDirectoryNameFromPackageName(packageName) {
if (packageName.startsWith('@datadog/browser-')) {
return packageName.slice('@datadog/browser-'.length)
}
}

function getDepenciesRecursively(packageDirectoryName) {
const packageDirectoryNameJson = JSON.parse(
fs.readFileSync(`packages/${packageDirectoryName}/package.json`, { encoding: 'utf-8' })
)
const dependencies = new Set()
if (packageDirectoryNameJson.dependencies) {
for (const dependencyPackageName of Object.keys(packageDirectoryNameJson.dependencies)) {
const packageDirectoryName = getPackageDirectoryNameFromPackageName(dependencyPackageName)
dependencies.add(packageDirectoryName)
for (let transitiveDependency of getDepenciesRecursively(packageDirectoryName)) {
dependencies.add(transitiveDependency)
}
}
}
return dependencies
}
4 changes: 2 additions & 2 deletions scripts/release/update-peer-dependency-versions.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { readdirSync } = require('fs')
const { runMain } = require('../lib/execution-utils')
const { modifyFile } = require('../lib/files-utils')
const { command } = require('../lib/command')
const { browserSdkVersion } = require('../lib/browser-sdk-version')
const { packagesDirectoryNames } = require('../lib/packages-directory-names')

const JSON_FILES = readdirSync('./packages').map((packageName) => `./packages/${packageName}/package.json`)
const JSON_FILES = packagesDirectoryNames.map((packageName) => `./packages/${packageName}/package.json`)

// This script updates the peer dependency versions between rum and logs packages to match the new
// version during a release.
Expand Down

0 comments on commit 44baa33

Please sign in to comment.