From 0134bfb2d3ce8b3caa2bc16970d36f4428a1227d Mon Sep 17 00:00:00 2001 From: Himani1519 Date: Thu, 5 Oct 2023 17:24:52 +0530 Subject: [PATCH 1/3] seperate steps for template comparsion and extract changelog Signed-off-by: Himani1519 --- .github/workflows/build-core.yml | 41 ++++++++++++++++++++++++++++---- CHANGELOG.md | 2 +- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build-core.yml b/.github/workflows/build-core.yml index 6ac88598..fb769935 100644 --- a/.github/workflows/build-core.yml +++ b/.github/workflows/build-core.yml @@ -67,25 +67,56 @@ jobs: echo "ERROR: CHANGELOG.md has not been updated." echo "::set-output name=check_commit::false" fi + + - name: Compare PR description with template + if: steps.check-changelog.outputs.check_commit == 'false' + run: | + PR_DESCRIPTION="${{ github.event.pull_request.body }}" + echo "$PR_DESCRIPTION" > /tmp/pr_description.txt + echo "PR DESCRIPTION saved to /tmp/pr_description.txt." + cat /tmp/pr_description.txt + + # Save the template content to a file + TEMPLATE_CONTENT=$(sed 's/"//g' .github/pull_request_template.md) + echo "$TEMPLATE_CONTENT" > /tmp/template_content.txt + echo "Template content saved to /tmp/template_content.txt." + cat /tmp/template_content.txt + + # Use diff to compare the two files + if diff -wB /tmp/pr_description.txt /tmp/template_content.txt > /dev/null; then + echo "ERROR: PR description is identical to the template." + exit 1 + else + echo "PR description and template are different." + fi + - name: Extract changelog info if: steps.check-changelog.outputs.check_commit == 'false' id: extract-changelog run: | - PR_DESCRIPTION="${{ github.event.pull_request.body }}" + PR_DESCRIPTION=$(cat /tmp/pr_description.txt) # Check if "changelog:" exists in PR description if echo "$PR_DESCRIPTION" | grep -q "VERSION:" && echo "$PR_DESCRIPTION" | grep -q "CHANGELOG:"; then - # Extract text after "changelog:" + # Extract content after "changelog:" CHANGELOG_TEXT=$(echo $PR_DESCRIPTION | sed -n 's/.*CHANGELOG: \(.*\)/\1/p') + + # Check if extracted CHANGELOG_TEXT is empty or identical to the template content + TEMPLATE_CONTENT=$(cat /tmp/template_content.txt) + if [ -z "$CHANGELOG_TEXT" ] || [ "$CHANGELOG_TEXT" == "$TEMPLATE_CONTENT" ]; then + echo "The changelog information after 'CHANGELOG:' cannot be empty or identical to pull_request_template.md." + exit 1 + fi + # Extract VERSION: from PR description - VERSION=$(echo "$PR_DESCRIPTION" | grep -oP 'VERSION:\s*\K\d+\.\d+\.\d+') + VERSION=$(echo "$PR_DESCRIPTION" | grep -oP 'VERSION:\s*\K\d+\.\d+\.\d+') echo "Extracted changelog: $CHANGELOG_TEXT" echo "::set-output name=changelog::$CHANGELOG_TEXT" echo "::set-output name=version::$VERSION" else - echo -e "No changelog and version information found in PR description please add them.\n Expected Format:\n VERSION:X.XX.X\n CHANGELOG:This is changelog note.\n - To re-run the action, just make a push or commit after updating the PR description or updating the changelog via a manual file changing commit." + echo -e "No changelog and version information found in PR description. Please add them.\nExpected Format:\nVERSION:vX.XX.X\nCHANGELOG:This is changelog note.\nTo re-run the action, just make a push or commit after updating the PR description or updating the changelog via a manual file changing commit." exit 1 fi + - name: Check PR body against changelog if: steps.check-changelog.outputs.check_commit == 'false' run: | diff --git a/CHANGELOG.md b/CHANGELOG.md index aea3b8c2..57012abe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# Zlux Server Framework Changelog +# Zlux Server Framework Changelog All notable changes to the Zlux Server Framework package will be documented in this file.. This repo is part of the app-server Zowe Component, and the change logs here may appear on Zowe.org in that section. From 2ed0d74ab2d77571a4d0b42a4f4cd6fcc2188e23 Mon Sep 17 00:00:00 2001 From: Himani1519 Date: Thu, 5 Oct 2023 17:25:58 +0530 Subject: [PATCH 2/3] some fixes Signed-off-by: Himani1519 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57012abe..aea3b8c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# Zlux Server Framework Changelog +# Zlux Server Framework Changelog All notable changes to the Zlux Server Framework package will be documented in this file.. This repo is part of the app-server Zowe Component, and the change logs here may appear on Zowe.org in that section. From b4f928e891cd41f51708aea4a59cc0f7e35b1ac8 Mon Sep 17 00:00:00 2001 From: Himani1519 Date: Mon, 16 Oct 2023 13:24:24 +0530 Subject: [PATCH 3/3] automation to rely upon JS rather than shell Signed-off-by: Himani1519 --- .github/workflows/build-core.yml | 83 ++++++++---------------------- .github/workflows/set-changelog.js | 59 +++++++++++++++++++++ 2 files changed, 81 insertions(+), 61 deletions(-) create mode 100644 .github/workflows/set-changelog.js diff --git a/.github/workflows/build-core.yml b/.github/workflows/build-core.yml index fb769935..be831622 100644 --- a/.github/workflows/build-core.yml +++ b/.github/workflows/build-core.yml @@ -67,80 +67,40 @@ jobs: echo "ERROR: CHANGELOG.md has not been updated." echo "::set-output name=check_commit::false" fi - - name: Compare PR description with template if: steps.check-changelog.outputs.check_commit == 'false' + env: + PR_DESCRIPTION: ${{ github.event.pull_request.body }} run: | - PR_DESCRIPTION="${{ github.event.pull_request.body }}" - echo "$PR_DESCRIPTION" > /tmp/pr_description.txt - echo "PR DESCRIPTION saved to /tmp/pr_description.txt." - cat /tmp/pr_description.txt - - # Save the template content to a file - TEMPLATE_CONTENT=$(sed 's/"//g' .github/pull_request_template.md) - echo "$TEMPLATE_CONTENT" > /tmp/template_content.txt - echo "Template content saved to /tmp/template_content.txt." - cat /tmp/template_content.txt - - # Use diff to compare the two files - if diff -wB /tmp/pr_description.txt /tmp/template_content.txt > /dev/null; then - echo "ERROR: PR description is identical to the template." - exit 1 - else - echo "PR description and template are different." - fi - - - name: Extract changelog info - if: steps.check-changelog.outputs.check_commit == 'false' - id: extract-changelog - run: | - PR_DESCRIPTION=$(cat /tmp/pr_description.txt) - # Check if "changelog:" exists in PR description - if echo "$PR_DESCRIPTION" | grep -q "VERSION:" && echo "$PR_DESCRIPTION" | grep -q "CHANGELOG:"; then - # Extract content after "changelog:" - CHANGELOG_TEXT=$(echo $PR_DESCRIPTION | sed -n 's/.*CHANGELOG: \(.*\)/\1/p') - - # Check if extracted CHANGELOG_TEXT is empty or identical to the template content - TEMPLATE_CONTENT=$(cat /tmp/template_content.txt) - if [ -z "$CHANGELOG_TEXT" ] || [ "$CHANGELOG_TEXT" == "$TEMPLATE_CONTENT" ]; then - echo "The changelog information after 'CHANGELOG:' cannot be empty or identical to pull_request_template.md." - exit 1 - fi - - # Extract VERSION: from PR description - VERSION=$(echo "$PR_DESCRIPTION" | grep -oP 'VERSION:\s*\K\d+\.\d+\.\d+') - echo "Extracted changelog: $CHANGELOG_TEXT" - echo "::set-output name=changelog::$CHANGELOG_TEXT" - echo "::set-output name=version::$VERSION" - else - echo -e "No changelog and version information found in PR description. Please add them.\nExpected Format:\nVERSION:vX.XX.X\nCHANGELOG:This is changelog note.\nTo re-run the action, just make a push or commit after updating the PR description or updating the changelog via a manual file changing commit." + # Safely print the PR description using Node.js + + node -e "const fs=require('fs'); fs.writeFileSync('/tmp/pr_description.txt', process.env.PR_DESCRIPTION);" + # Use diff to compare the two files + if diff -wB /tmp/pr_description.txt .github/pull_request_template.md > /dev/null; then + echo "ERROR: PR description is identical to the template." exit 1 + else + echo "PR description and template are different." fi - name: Check PR body against changelog if: steps.check-changelog.outputs.check_commit == 'false' + id: extract-changelog run: | - ESCAPED_CHANGELOG="${{ steps.extract-changelog.outputs.changelog }}" - ESCAPED_CHANGELOG=$(echo "$ESCAPED_CHANGELOG" | sed "s/'/\\\\'/g") - VERSION="${{ steps.extract-changelog.outputs.version }}" - - if ! grep -Fq "$ESCAPED_CHANGELOG" CHANGELOG.md; then - # Check if version exists in CHANGELOG.md - if grep -q "^## $VERSION" CHANGELOG.md; then - # Append PR description to existing version - sed -i "/^## $VERSION/a - $ESCAPED_CHANGELOG (#${{ github.event.pull_request.number }})" CHANGELOG.md - else - # Append new version and PR description - ANCHOR_LINE=$(awk '/This repo is part of the app-server Zowe Component, and the change logs here may appear on Zowe.org in that section\./ {print NR}' CHANGELOG.md) - sed -i "$ANCHOR_LINE a\\ - \n## $VERSION\n- $ESCAPED_CHANGELOG (#${{ github.event.pull_request.number }})\n" CHANGELOG.md - fi + result=$(node .github/workflows/set-changelog.js ${{ github.event.pull_request.number }}) + if [ "$result" = "Success" ]; then git config --global user.email "zowe-robot@users.noreply.github.com" git config --global user.name "Zowe Robot" git add CHANGELOG.md git commit -s -m "Update changelog with PR #${{ github.event.pull_request.number }} description" git push + echo "Updated CHANGELOG from description" + else + echo $result + echo -e "No changelog and version information found in PR description. Please add them.\nExpected Format:\nVERSION:X.XX.X\nCHANGELOG:This is changelog note.\nTo re-run the action, just make a push or commit after updating the PR description or updating the changelog via a manual file changing commit." + exit 1 fi + - name: check for changes id: check-change run: | @@ -149,7 +109,7 @@ jobs: echo "::set-output name=change_detected::false" else echo "::set-output name=change_detected::true" - fi + fi check_changelog: if: github.event_name == 'pull_request' @@ -166,7 +126,8 @@ jobs: exit 1 else echo "changelog was updated successfully." - fi + fi + build: runs-on: ubuntu-latest diff --git a/.github/workflows/set-changelog.js b/.github/workflows/set-changelog.js new file mode 100644 index 00000000..44868f03 --- /dev/null +++ b/.github/workflows/set-changelog.js @@ -0,0 +1,59 @@ +/* +This program and the accompanying materials are +made available under the terms of the Eclipse Public License v2.0 which accompanies +this distribution, and is available at https://www.eclipse.org/legal/epl-v20.html + +SPDX-License-Identifier: EPL-2.0 + +Copyright Contributors to the Zowe Project. +*/ + +const fs = require('fs'); + +// Must run with args: PR_NUMBER +const PR_NUMBER = process.argv[2]; +const description = fs.readFileSync('/tmp/pr_description.txt', 'utf8'); +let changelogMsg, version; + +if (description.includes('VERSION:') && description.includes('CHANGELOG:')) { + let lines = description.split('\n'); + lines.forEach((line) => { + if (line.startsWith('CHANGELOG:')) { + changelogMsg = line.substring('CHANGELOG:'.length).trim(); + } else if (line.startsWith('VERSION:')) { + version = line.substring('VERSION:'.length).trim(); + } + }); + + if (changelogMsg && version) { + let changelog = fs.readFileSync('CHANGELOG.md', 'utf8'); + let changelogLines = changelog.split('\n'); + let versionIndex = -1; + let anchorIndex = 0; + for (let i = 0; i < changelogLines.length; i++) { + if (changelogLines[i].includes('This repo is part of the app-server Zowe Component, and the change logs here may appear on Zowe.org in that section.')) { + anchorIndex = i; + } else if (changelogLines[i].startsWith('## ' + version)) { // Removed "v" prefix + versionIndex = i; + break; + } + } + if (versionIndex != -1) { + changelogLines.splice(versionIndex + 2, 0, `- ${changelogMsg} (#${PR_NUMBER})`); + } else { + changelogLines.splice(anchorIndex + 1, 0, `\n## ${version}\n- ${changelogMsg} (#${PR_NUMBER})`); + } + const newChangelog = changelogLines.join('\n'); + fs.writeFileSync('CHANGELOG.md', newChangelog); + console.log('Success'); + } else { + if (!changelogMsg) { + console.log('Missing CHANGELOG'); + } + if (!version) { + console.log('Missing VERSION'); + } + } +} else { + console.log('Missing CHANGELOG or VERSION'); +} \ No newline at end of file