From 7b9e77bfd212c8e4386f284b486be1b1c0ba2946 Mon Sep 17 00:00:00 2001 From: Haowei Cai Date: Mon, 19 Apr 2021 21:03:44 -0700 Subject: [PATCH 1/3] add update-submodule script --- devel/submodules.md | 4 +- scripts/update-submodule.sh | 88 +++++++++++++++++++++++++++++++++++++ scripts/util/changelog.sh | 77 ++++++++++++++++++++++++++++++++ 3 files changed, 167 insertions(+), 2 deletions(-) create mode 100755 scripts/update-submodule.sh create mode 100755 scripts/util/changelog.sh diff --git a/devel/submodules.md b/devel/submodules.md index 68ca4eee0e..bec58b8618 100644 --- a/devel/submodules.md +++ b/devel/submodules.md @@ -23,7 +23,7 @@ git submodule update --init If you changed [kubernetes-client/python-base](https://github.com/kubernetes-client/python-base) and want to pull your changes into this repo run this command: ```bash -git submodule update --remote +scripts/update-submodule.sh ``` -Once updated, you should create a new PR to commit changes to the repository. +Create a commit "generated python-base update" and send a PR to the main repo. diff --git a/scripts/update-submodule.sh b/scripts/update-submodule.sh new file mode 100755 index 0000000000..267dcb41eb --- /dev/null +++ b/scripts/update-submodule.sh @@ -0,0 +1,88 @@ +#!/bin/bash + +# Copyright 2021 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Update python-base submodule and collect release notes. +# Usage: +# scripts/update-submodule.sh +# +# Create a commit "generated python-base update" and send a PR to the main repo. +# NOTE: not all python-base changes are user-facing, and you may want to remove +# some of the non-user-facing release notes from CHANGELOG.md. + +set -o errexit +set -o nounset +set -o pipefail + +repo_root="$(git rev-parse --show-toplevel)" +declare -r repo_root +cd "${repo_root}" + +source scripts/util/changelog.sh + +# TODO(roycaihw): make the script send a PR by default (standalone mode), and +# have an option to make the script reusable by other release automations. +TARGET_RELEASE=${TARGET_RELEASE:-"v$(grep "^CLIENT_VERSION = \"" scripts/constants.py | sed "s/CLIENT_VERSION = \"//g" | sed "s/\"//g")"} +SUBMODULE_SECTION=${SUBMODULE_SECTION:-"\*\*Submodule Change:\*\*"} + +# update submodule +git submodule update --remote +pulls=$(git diff --submodule | egrep -o "^ > Merge pull request #[0-9]+" | sed 's/ > Merge pull request #//g') + +# download release notes +release_notes="" +submodule_repo="kubernetes-client/python-base" +echo "Downloading release notes from submodule repo." +for pull in ${pulls}; do + pull_url="https://github.com/$submodule_repo/pull/${pull}" + echo "+++ Downloading python-base patch to /tmp/${pull}.patch" + curl -o "/tmp/${pull}.patch" -sSL "${pull_url}.patch" + subject=$(grep -m 1 "^Subject" "/tmp/${pull}.patch" | sed -e 's/Subject: \[PATCH//g' | sed 's/.*] //') + pull_uid="$submodule_repo#${pull}" + release_note="- ${subject} [${pull_uid}](${pull_url})\n" + release_notes+=${release_note} + # remove the patch file from /tmp + rm -f "/tmp/${pull}.patch" +done + +# find the place in the changelog that we want to edit +line_to_edit="1" +if util::changelog::has_release $TARGET_RELEASE; then + # the target release exists + release_first_line=$(util::changelog::find_release_start $TARGET_RELEASE) + release_last_line=$(util::changelog::find_release_end $TARGET_RELEASE) + if util::changelog::has_section_in_range "$SUBMODULE_SECTION" "$release_first_line" "$release_last_line"; then + # prepend to existing section + line_to_edit=$(($(util::changelog::find_section_in_range "$SUBMODULE_SECTION" "$release_first_line" "$release_last_line")+1)) + release_notes=${release_notes::-2} + else + # add a new section + line_to_edit=$(($(util::changelog::find_release_start $TARGET_RELEASE)+4)) + release_notes="$SUBMODULE_SECTION\n$release_notes" + fi +else + # add a new release + # TODO(roycaihw): ideally a parent script updates the generated client and + # fills in the Kubernetes API Version based on the OpenAPI spec. + release_notes="# $TARGET_RELEASE\n\nKubernetes API Version: TBD\n\n$SUBMODULE_SECTION\n$release_notes" +fi + +echo "Writing the following release notes to CHANGELOG line $line_to_edit:" +echo -e $release_notes + +# update changelog +sed -i "${line_to_edit}i${release_notes}" CHANGELOG.md + +echo "Successfully updated CHANGELOG for submodule." diff --git a/scripts/util/changelog.sh b/scripts/util/changelog.sh new file mode 100755 index 0000000000..a17d977743 --- /dev/null +++ b/scripts/util/changelog.sh @@ -0,0 +1,77 @@ +#!/bin/bash + +# Copyright 2021 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +changelog="$(git rev-parse --show-toplevel)/CHANGELOG.md" + +function util::changelog::has_release { + local release=$1 + return $(grep -q "^# $release$" $changelog) +} + +# find_release_start returns the number of the first line of the given release +function util::changelog::find_release_start { + local release=$1 + echo $(grep -n "^# $release$" $changelog | head -1 | cut -d: -f1) +} + +# find_release_end returns the number of the last line of the given release +function util::changelog::find_release_end { + local release=$1 + + local release_start=$(util::changelog::find_release_start $release) + local next_release_index=0 + local releases=($(grep -n "^# " $changelog | cut -d: -f1)) + for i in "${!releases[@]}"; do + if [[ "${releases[$i]}" = "$release_start" ]]; then + next_release_index=$((i+1)) + break + fi + done + # return the line before the next release + echo $((${releases[${next_release_index}]}-1)) +} + +# has_section returns if the given section exists between start and end +function util::changelog::has_section_in_range { + local section="$1" + local start=$2 + local end=$3 + + local lines=($(grep -n "$section" "$changelog" | cut -d: -f1)) + for i in "${!lines[@]}"; do + if [[ ${lines[$i]} -ge $start && ${lines[$i]} -le $end ]]; then + return 0 + fi + done + return 1 +} + +# find_section returns the number of the first line of the given section +function util::changelog::find_section_in_range { + local section="$1" + local start=$2 + local end=$3 + + local line="0" + local lines=($(grep -n "$section" "$changelog" | cut -d: -f1)) + for i in "${!lines[@]}"; do + if [[ ${lines[$i]} -ge $start && ${lines[$i]} -le $end ]]; then + line=${lines[$i]} + break + fi + done + echo $line +} From d86a47de26036d7aa2e2b12000d815bce68bb341 Mon Sep 17 00:00:00 2001 From: Haowei Cai Date: Tue, 20 Apr 2021 11:01:16 -0700 Subject: [PATCH 2/3] add PR template copied from k/k. We should add the same template to python-base --- .github/PULL_REQUEST_TEMPLATE.md | 72 ++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 .github/PULL_REQUEST_TEMPLATE.md diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000000..f6af35b429 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,72 @@ + + +#### What type of PR is this? + + + +#### What this PR does / why we need it: + +#### Which issue(s) this PR fixes: + +Fixes # + +#### Special notes for your reviewer: + +#### Does this PR introduce a user-facing change? + +```release-note + +``` + +#### Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.: + + +```docs + +``` From 83d294e2b0119127eeff2fefa8342987517d7d08 Mon Sep 17 00:00:00 2001 From: Haowei Cai Date: Tue, 20 Apr 2021 16:56:30 -0700 Subject: [PATCH 3/3] fixup! add update-submodule script --- devel/submodules.md | 2 +- scripts/update-submodule.sh | 73 +++++++++++++------------------------ scripts/util/changelog.sh | 32 ++++++++++++++++ 3 files changed, 58 insertions(+), 49 deletions(-) diff --git a/devel/submodules.md b/devel/submodules.md index bec58b8618..6edca72752 100644 --- a/devel/submodules.md +++ b/devel/submodules.md @@ -26,4 +26,4 @@ If you changed [kubernetes-client/python-base](https://github.com/kubernetes-cli scripts/update-submodule.sh ``` -Create a commit "generated python-base update" and send a PR to the main repo. +After the script finishes, please create a commit "generated python-base update" and send a PR to this repository. diff --git a/scripts/update-submodule.sh b/scripts/update-submodule.sh index 267dcb41eb..86aa5a4b5a 100755 --- a/scripts/update-submodule.sh +++ b/scripts/update-submodule.sh @@ -14,13 +14,18 @@ # See the License for the specific language governing permissions and # limitations under the License. + # Update python-base submodule and collect release notes. # Usage: -# scripts/update-submodule.sh # -# Create a commit "generated python-base update" and send a PR to the main repo. -# NOTE: not all python-base changes are user-facing, and you may want to remove -# some of the non-user-facing release notes from CHANGELOG.md. +# $ scripts/update-submodule.sh +# +# # To update the release notes for a specific release (e.g. v18.17.0a1): +# $ TARGET_RELEASE="v18.17.0a1" scripts/update-submodule.sh +# +# After the script finishes, please create a commit "generated python-base update" +# and send a PR to this repository. +# TODO(roycaihw): make the script send a PR set -o errexit set -o nounset @@ -31,58 +36,30 @@ declare -r repo_root cd "${repo_root}" source scripts/util/changelog.sh +go get k8s.io/release/cmd/release-notes -# TODO(roycaihw): make the script send a PR by default (standalone mode), and -# have an option to make the script reusable by other release automations. TARGET_RELEASE=${TARGET_RELEASE:-"v$(grep "^CLIENT_VERSION = \"" scripts/constants.py | sed "s/CLIENT_VERSION = \"//g" | sed "s/\"//g")"} -SUBMODULE_SECTION=${SUBMODULE_SECTION:-"\*\*Submodule Change:\*\*"} # update submodule git submodule update --remote -pulls=$(git diff --submodule | egrep -o "^ > Merge pull request #[0-9]+" | sed 's/ > Merge pull request #//g') # download release notes -release_notes="" -submodule_repo="kubernetes-client/python-base" -echo "Downloading release notes from submodule repo." -for pull in ${pulls}; do - pull_url="https://github.com/$submodule_repo/pull/${pull}" - echo "+++ Downloading python-base patch to /tmp/${pull}.patch" - curl -o "/tmp/${pull}.patch" -sSL "${pull_url}.patch" - subject=$(grep -m 1 "^Subject" "/tmp/${pull}.patch" | sed -e 's/Subject: \[PATCH//g' | sed 's/.*] //') - pull_uid="$submodule_repo#${pull}" - release_note="- ${subject} [${pull_uid}](${pull_url})\n" - release_notes+=${release_note} - # remove the patch file from /tmp - rm -f "/tmp/${pull}.patch" -done - -# find the place in the changelog that we want to edit -line_to_edit="1" -if util::changelog::has_release $TARGET_RELEASE; then - # the target release exists - release_first_line=$(util::changelog::find_release_start $TARGET_RELEASE) - release_last_line=$(util::changelog::find_release_end $TARGET_RELEASE) - if util::changelog::has_section_in_range "$SUBMODULE_SECTION" "$release_first_line" "$release_last_line"; then - # prepend to existing section - line_to_edit=$(($(util::changelog::find_section_in_range "$SUBMODULE_SECTION" "$release_first_line" "$release_last_line")+1)) - release_notes=${release_notes::-2} - else - # add a new section - line_to_edit=$(($(util::changelog::find_release_start $TARGET_RELEASE)+4)) - release_notes="$SUBMODULE_SECTION\n$release_notes" - fi -else - # add a new release - # TODO(roycaihw): ideally a parent script updates the generated client and - # fills in the Kubernetes API Version based on the OpenAPI spec. - release_notes="# $TARGET_RELEASE\n\nKubernetes API Version: TBD\n\n$SUBMODULE_SECTION\n$release_notes" -fi - -echo "Writing the following release notes to CHANGELOG line $line_to_edit:" -echo -e $release_notes +start_sha=$(git diff | grep "^-Subproject commit " | sed 's/-Subproject commit //g') +end_sha=$(git diff | grep "^+Subproject commit " | sed 's/+Subproject commit //g') +output="/tmp/python-base-relnote.md" +release-notes --dependencies=false --org kubernetes-client --repo python-base --start-sha $start_sha --end-sha $end_sha --output $output +sed -i 's/(\[\#/(\[kubernetes-client\/python-base\#/g' $output # update changelog -sed -i "${line_to_edit}i${release_notes}" CHANGELOG.md +IFS_backup=$IFS +IFS=$'\n' +sections=($(grep "^### " $output)) +IFS=$IFS_backup +for section in "${sections[@]}"; do + # ignore section titles and empty lines; replace newline with liternal "\n" + release_notes=$(sed -n "/$section/,/###/{/###/!p}" $output | sed -n "{/^$/!p}" | sed ':a;N;$!ba;s/\n/\\n/g') + util::changelog::write_changelog "$TARGET_RELEASE" "$section" "$release_notes" +done +rm -f $output echo "Successfully updated CHANGELOG for submodule." diff --git a/scripts/util/changelog.sh b/scripts/util/changelog.sh index a17d977743..672b823369 100755 --- a/scripts/util/changelog.sh +++ b/scripts/util/changelog.sh @@ -75,3 +75,35 @@ function util::changelog::find_section_in_range { done echo $line } + +# write_changelog writes release_notes to section in target_release +function util::changelog::write_changelog { + local target_release="$1" + local section="$2" + local release_notes="$3" + + # find the place in the changelog that we want to edit + local line_to_edit="1" + if util::changelog::has_release $target_release; then + # the target release exists + release_first_line=$(util::changelog::find_release_start $target_release) + release_last_line=$(util::changelog::find_release_end $target_release) + if util::changelog::has_section_in_range "$section" "$release_first_line" "$release_last_line"; then + # prepend to existing section + line_to_edit=$(($(util::changelog::find_section_in_range "$section" "$release_first_line" "$release_last_line")+1)) + else + # add a new section; plus 4 so that the section is placed below "Kubernetes API Version" + line_to_edit=$(($(util::changelog::find_release_start $target_release)+4)) + release_notes="$section\n$release_notes\n" + fi + else + # add a new release + release_notes="# $target_release\n\nKubernetes API Version: To Be Updated\n\n$section\n$release_notes\n" + fi + + echo "Writing the following release notes to CHANGELOG line $line_to_edit:" + echo -e $release_notes + + # update changelog + sed -i "${line_to_edit}i${release_notes}" $changelog +}