Skip to content

Commit

Permalink
Merge branch 'master' into bug/block-variation-and-display-sync
Browse files Browse the repository at this point in the history
  • Loading branch information
rezaelahidev authored Sep 14, 2024
2 parents 937f8da + 23aec1a commit 200ff1d
Show file tree
Hide file tree
Showing 66 changed files with 1,089 additions and 451 deletions.
18 changes: 16 additions & 2 deletions .github/workflows/build-plugin-zip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ jobs:
git fetch --depth=1 origin "${{ steps.get_version.outputs.release_branch }}"
git checkout "${{ steps.get_version.outputs.release_branch }}"
- name: Setup Node.js and install dependencies
uses: ./.github/setup-node

- name: Update the changelog of packages
id: update_packages
run: |
npm run update:changelogs -- --version="${{ steps.get_version.outputs.new_version }}"
CHANGELOG=$(cat changelog.txt | awk '{printf "%s\\n", $0}')
echo "changelog=$CHANGELOG" >> $GITHUB_OUTPUT
git add .
git commit -m "Update Changelog for ${{ steps.get_version.outputs.new_version }}"
echo "has_commit=$(git rev-parse --verify --short HEAD)" >> $GITHUB_OUTPUT
- name: Update plugin version
env:
VERSION: ${{ steps.get_version.outputs.new_version }}
Expand Down Expand Up @@ -145,6 +158,7 @@ jobs:
git pull
MASTER_VERSION=$(jq --raw-output '.version' package.json)
if [[ ${{ steps.get_version.outputs.old_version }} == "$MASTER_VERSION" ]]; then
git cherry-pick "${{ steps.update_packages.outputs.has_commit }}"
git cherry-pick "${{ steps.get_version.outputs.release_branch }}"
git push
echo "version_bump_commit=$(git rev-parse --verify --short HEAD)" >> $GITHUB_OUTPUT
Expand Down Expand Up @@ -194,8 +208,8 @@ jobs:
run: |
IFS='.' read -r -a VERSION_ARRAY <<< "${VERSION}"
MILESTONE="Blockera ${VERSION_ARRAY[0]}.${VERSION_ARRAY[1]}"
npm run update:changelogs
npm run other:changelog -- --milestone="$MILESTONE" --unreleased > release-notes.txt
echo ${{needs.update_packages.outputs.changelog}} >> release-note.txt
npm run other:changelog -- --milestone="$MILESTONE" --unreleased --file="release-note.txt" --version="${{ needs.bump-version.outputs.new_version }}" > release-notes.txt
sed -ie '1,6d' release-notes.txt
if [[ ${{ needs.bump-version.outputs.new_version }} != *"rc"* ]]; then
# Include previous RCs' release notes, if any
Expand Down
78 changes: 2 additions & 76 deletions .github/workflows/upload-release-to-plugin-repo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,85 +78,11 @@ jobs:
RELEASE_BRANCH="release/${VERSION_ARRAY[0]}.${VERSION_ARRAY[1]}"
echo "release_branch=${RELEASE_BRANCH}" >> $GITHUB_OUTPUT
update-changelog:
name: Update Changelog on ${{ matrix.branch }} branch
runs-on: ubuntu-latest
if: |
github.event.release.assets[0]
needs: get-release-branch
env:
TAG: ${{ github.event.release.tag_name }}
strategy:
matrix:
include:
- branch: trunk
label: trunk
- branch: ${{ needs.get-release-branch.outputs.release_branch }}
label: release

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ matrix.branch }}
token: ${{ secrets.BLOCKERA_TOKEN }}
show-progress: ${{ runner.debug == '1' && 'true' || 'false' }}

- name: Update the Changelog to include the release notes
run: |
# First, determine where to insert the new Changelog entry.
SERIES="${RELEASE_BRANCH#release/}"
SERIES_REGEX="=\s${SERIES}\.[0-9]+\s="
CUT_MARKS=$( grep -nP -m 1 "${SERIES_REGEX}" changelog.txt | cut -d: -f1 )
if [[ -z "${CUT_MARKS}" ]]; then
CHANGELOG_REGEX="=\s[0-9]+\.[0-9]+\.[0-9]+(-rc\.[0-9]+)?\s="
RC_REGEX="=\s${TAG#v}(-rc\.[0-9]+)?\s="
CUT_MARKS=$( awk "/${RC_REGEX}/ {print NR; next}; /${CHANGELOG_REGEX}/ {print NR; exit}" changelog.txt )
fi
BEFORE=$( echo "$CUT_MARKS" | head -n 1 )
AFTER=$( echo "$CUT_MARKS" | tail -n 1 )
# Okay, we have all we need to build the new Changelog.
head -n $(( "${BEFORE}" - 1 )) changelog.txt > new_changelog.txt
printf '= %s =\n\n' "${TAG#v}" >> new_changelog.txt
# Need to use a heredoc in order to preserve special characters.
cat <<- "EOF" > release_notes.txt
${{ github.event.release.body }}
EOF
# Normalize empty lines: Trim them from beginning and end of file...
awk 'NF {p=1} p' <<< "$(< release_notes.txt)" >> new_changelog.txt
# ...then add two empty lines at the end.
printf '\n\n' >> new_changelog.txt
tail -n +"${AFTER}" changelog.txt >> new_changelog.txt
mv new_changelog.txt changelog.txt
- name: Configure git user name and email
run: |
git config user.name "blockerabot"
git config user.email [email protected]
- name: Commit the Changelog update
run: |
git add changelog.txt
# Remove files that are not meant to be committed
# ie. release_notes.txt created on the previous step.
git clean -fd
# Only attempt to commit changelog if it has been modified.
if ! git diff-index --quiet HEAD --; then
git commit -m "Update Changelog for ${TAG#v}"
git push --set-upstream origin "${{ matrix.branch }}"
fi
- name: Upload Changelog artifact
uses: actions/upload-artifact@v4
with:
name: changelog ${{ matrix.label }}
path: ./changelog.txt

upload:
name: Publish as trunk (and tag)
runs-on: ubuntu-latest
environment: wp.org plugin
needs: [compute-should-update-trunk, update-changelog]
needs: [compute-should-update-trunk]
if: |
needs.compute-should-update-trunk.outputs.should_update_trunk == 'true' && github.event.release.assets[0]
env:
Expand Down Expand Up @@ -220,7 +146,7 @@ jobs:
name: Publish as tag
runs-on: ubuntu-latest
environment: wp.org plugin
needs: [compute-should-update-trunk, update-changelog]
needs: [compute-should-update-trunk]
if: |
needs.compute-should-update-trunk.outputs.should_update_trunk == 'false' && github.event.release.assets[0]
env:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
</p>

<h1 align="center">Blockera</h1>
<h3 align="center">The Advanced Mode for Gutenberg</h3>
<h3 align="center">The Advanced Mode for Block Editor</h3>

<p align="center">
Transform WordPress Block Editor into a Powerful Page Builder with Blockera
Expand Down
3 changes: 3 additions & 0 deletions bin/plugin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ const { updatePackagesChangelog } = require('./commands/packages');

program
.command('update-packages-changelog')
.option('-v, --version <version>', 'Version')
.option(...releaseType)
.description('Blockera plugin and packages changelogs publishes to git.')
.action(catchException(updatePackagesChangelog));

program
.command('release-plugin-changelog')
.alias('changelog')
.option('-f, --file <file>', 'File')
.option('-v, --version <version>', 'Version')
.option('-m, --milestone <milestone>', 'Milestone')
.option('-t, --token <token>', 'GitHub token')
.option(
Expand Down
159 changes: 152 additions & 7 deletions bin/plugin/commands/changelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const { log, formats } = require('../lib/logger');
const config = require('../config');
// @ts-ignore
const manifest = require('../../../package.json');
const fs = require('fs');
const glob = require('fast-glob');
const path = require('path');

const UNKNOWN_FEATURE_FALLBACK_NAME = 'Uncategorized';

Expand Down Expand Up @@ -120,6 +123,8 @@ const GROUP_TITLE_ORDER = [
'Features',
'Enhancements',
'New APIs',
'New Features',
'Improvements',
'Bug Fixes',
`Accessibility`,
'Performance',
Expand All @@ -137,6 +142,8 @@ const GROUP_TITLE_ORDER = [
* @type {Map<RegExp,string>}
*/
const TITLE_TYPE_PATTERNS = new Map([
[/feat?(:|\/ )?/i, 'New Features'],
[/improve?\s*ment(s)?(:|\/ )?/i, 'Improvements'],
[/^(\w+:)?(bug)?\s*fix(es)?(:|\/ )?/i, 'Bug Fixes'],
]);

Expand Down Expand Up @@ -475,6 +482,7 @@ const createOmitByLabelPrefix = (prefixes) => (text, issue) =>
)
? undefined
: text;

/**
* Given an issue title and issue, returns the title with redundant grouping
* type details removed. The prefix is redundant since it would already be clear
Expand Down Expand Up @@ -692,14 +700,144 @@ async function fetchAllPullRequests(octokit, settings) {
}

/**
* Formats the changelog string for a given list of pull requests.
* Combines repeated sections in a changelog string.
*
* @param {string} changelog the changelog text.
*
* @returns {string} the combined changelog same sections.
*/
function combineChangelogSections(changelog) {
// Split the changelog into lines
const lines = changelog.split('\n');

// Initialize an object to hold each section's content
const sections = {};
let currentSection = '';

// Loop through each line
lines.forEach((line) => {
// Check if the line starts with a section heading (e.g., ### Bug Fixes)
const sectionMatch = line.match(/^### (.+)$/);
if (sectionMatch) {
currentSection = sectionMatch[1];
// Initialize the section in the object if it doesn't exist
if (!sections[currentSection]) {
sections[currentSection] = [];
}
} else if (currentSection) {
// Add the line to the current section
sections[currentSection].push(line);
}
});

// Define the priority order for sections
const priorityOrder = ['New Features', 'Improvements', 'Bug Fixes'];

// Reconstruct the changelog by priority
let combinedChangelog = '';

// Add sections based on priority first
priorityOrder.forEach((section) => {
if (sections[section]) {
combinedChangelog += `\n### ${section}\n`;
combinedChangelog += sections[section]
.filter((line) => line.trim() !== '')
.join('\n');
combinedChangelog += '\n';
}
});

// Add any other sections that were not prioritized
Object.keys(sections).forEach((section) => {
if (!priorityOrder.includes(section)) {
combinedChangelog += `\n### ${section}\n`;
combinedChangelog += sections[section]
.filter((line) => line.trim() !== '')
.join('\n');
combinedChangelog += '\n';
}
});

return combinedChangelog.trim();
}

/**
* Formats the changelog string for a given list of packages.
*
* @param {string[]} changelogPath the changelog path.
* @param {string} version The version number if it has value to update changelog.txt!
*
* @return {string} The formatted changelog string.
*/
function getMainChangelog(changelogPath, version = '') {
let start =
'<details>\n' + '<summary>\n\n' + '## Changelog\n\n' + '</summary>\n\n';
let changelog = '';
const end = '\n\n</details>';

// Read the changelog file
const content = fs.readFileSync(changelogPath, 'utf8');

// Remove redundant headings or descriptions of changelog.
changelog = content
.replace(/== Changelog ==/g, '')
.replace(/=\s[0-9]+\.[0-9]+\.[0-9]+(-rc\.[0-9]+)?\s=/g, '')
.trim();

return start + changelog + end;
}

/**
* Formats the changelog string for a given list of packages.
*
* @param {string[]} changelogs List of pull requests.
* @param {string} version The version number to update changelog.txt!
*
* @return {string} The formatted changelog string.
*/
function updateChangelog(changelogs, version) {
const start = '== Changelog ==\n\n= ' + version.trim() + ' =\n\n';
let changelog = '';
const end =
'\n\n## More\n\nTo read the changelog for older Blockera releases, please navigate to the [[release page](https://community.blockera.ai/changelog-9l8hbrv0)].';

for (const changelogPath of changelogs) {
// Read the changelog file
const content = fs.readFileSync(changelogPath, 'utf8');

// Use a regular expression to extract the ## Unreleased section
const unreleasedSection = content.match(
/## Unreleased[\s\S]+?(?=\n## |\n$)/
);

if (unreleasedSection) {
changelog += unreleasedSection[0].replace(/##\sUnreleased/g, '');
}
}

// Combine same sections.
changelog = combineChangelogSections(changelog);

// Update the changelog.txt file to include combined changes of all packages.
fs.writeFileSync(
path.resolve(process.cwd(), 'changelog.txt'),
start + changelog + end
);
}

/**
* Formats the development changelog string for a given list of pull requests.
*
* @param {IssuesListForRepoResponseItem[]} pullRequests List of pull requests.
*
* @return {string} The formatted changelog string.
*/
function getChangelog(pullRequests) {
let changelog = '## Changelog\n\n';
function getDevelopmentChangelog(pullRequests) {
let changelog =
'<details>\n' +
'<summary>\n\n' +
'## Development Changelog\n\n' +
'</summary>\n\n';

const groupedPullRequests = skipCreatedByBots(pullRequests).reduce(
(
Expand Down Expand Up @@ -784,7 +922,7 @@ function getChangelog(pullRequests) {
changelog += '\n';
}

return changelog;
return changelog + '\n\n</details>';
}

/**
Expand Down Expand Up @@ -984,12 +1122,16 @@ async function createChangelog(settings) {
try {
const pullRequests = await fetchAllPullRequests(octokit, settings);

const changelog = getChangelog(pullRequests);
const developmentChangelog = getDevelopmentChangelog(pullRequests);
const contributorProps = getContributorProps(pullRequests);
const contributorsList = getContributorsList(pullRequests);

releaselog = releaselog.concat(
changelog,
getMainChangelog(
path.resolve(process.cwd(), settings.file),
settings.version
),
developmentChangelog,
contributorProps,
contributorsList
);
Expand Down Expand Up @@ -1024,6 +1166,8 @@ async function getReleaseChangelog(options) {
})
: options.milestone,
unreleased: options.unreleased,
file: options?.file || '',
version: options?.version || '',
});
}

Expand All @@ -1044,7 +1188,8 @@ async function getReleaseChangelog(options) {
getFormattedItemDescription,
getContributorProps,
getContributorsList,
getChangelog,
updateChangelog,
getDevelopmentChangelog,
getUniqueByUsername,
skipCreatedByBots,
mapLabelsToFeatures,
Expand Down
Loading

0 comments on commit 200ff1d

Please sign in to comment.