Skip to content

Commit 7b9e77b

Browse files
committed
add update-submodule script
1 parent b313b5e commit 7b9e77b

File tree

3 files changed

+167
-2
lines changed

3 files changed

+167
-2
lines changed

devel/submodules.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ git submodule update --init
2323
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:
2424

2525
```bash
26-
git submodule update --remote
26+
scripts/update-submodule.sh
2727
```
2828

29-
Once updated, you should create a new PR to commit changes to the repository.
29+
Create a commit "generated python-base update" and send a PR to the main repo.

scripts/update-submodule.sh

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/bin/bash
2+
3+
# Copyright 2021 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# Update python-base submodule and collect release notes.
18+
# Usage:
19+
# scripts/update-submodule.sh
20+
#
21+
# Create a commit "generated python-base update" and send a PR to the main repo.
22+
# NOTE: not all python-base changes are user-facing, and you may want to remove
23+
# some of the non-user-facing release notes from CHANGELOG.md.
24+
25+
set -o errexit
26+
set -o nounset
27+
set -o pipefail
28+
29+
repo_root="$(git rev-parse --show-toplevel)"
30+
declare -r repo_root
31+
cd "${repo_root}"
32+
33+
source scripts/util/changelog.sh
34+
35+
# TODO(roycaihw): make the script send a PR by default (standalone mode), and
36+
# have an option to make the script reusable by other release automations.
37+
TARGET_RELEASE=${TARGET_RELEASE:-"v$(grep "^CLIENT_VERSION = \"" scripts/constants.py | sed "s/CLIENT_VERSION = \"//g" | sed "s/\"//g")"}
38+
SUBMODULE_SECTION=${SUBMODULE_SECTION:-"\*\*Submodule Change:\*\*"}
39+
40+
# update submodule
41+
git submodule update --remote
42+
pulls=$(git diff --submodule | egrep -o "^ > Merge pull request #[0-9]+" | sed 's/ > Merge pull request #//g')
43+
44+
# download release notes
45+
release_notes=""
46+
submodule_repo="kubernetes-client/python-base"
47+
echo "Downloading release notes from submodule repo."
48+
for pull in ${pulls}; do
49+
pull_url="https://github.com/$submodule_repo/pull/${pull}"
50+
echo "+++ Downloading python-base patch to /tmp/${pull}.patch"
51+
curl -o "/tmp/${pull}.patch" -sSL "${pull_url}.patch"
52+
subject=$(grep -m 1 "^Subject" "/tmp/${pull}.patch" | sed -e 's/Subject: \[PATCH//g' | sed 's/.*] //')
53+
pull_uid="$submodule_repo#${pull}"
54+
release_note="- ${subject} [${pull_uid}](${pull_url})\n"
55+
release_notes+=${release_note}
56+
# remove the patch file from /tmp
57+
rm -f "/tmp/${pull}.patch"
58+
done
59+
60+
# find the place in the changelog that we want to edit
61+
line_to_edit="1"
62+
if util::changelog::has_release $TARGET_RELEASE; then
63+
# the target release exists
64+
release_first_line=$(util::changelog::find_release_start $TARGET_RELEASE)
65+
release_last_line=$(util::changelog::find_release_end $TARGET_RELEASE)
66+
if util::changelog::has_section_in_range "$SUBMODULE_SECTION" "$release_first_line" "$release_last_line"; then
67+
# prepend to existing section
68+
line_to_edit=$(($(util::changelog::find_section_in_range "$SUBMODULE_SECTION" "$release_first_line" "$release_last_line")+1))
69+
release_notes=${release_notes::-2}
70+
else
71+
# add a new section
72+
line_to_edit=$(($(util::changelog::find_release_start $TARGET_RELEASE)+4))
73+
release_notes="$SUBMODULE_SECTION\n$release_notes"
74+
fi
75+
else
76+
# add a new release
77+
# TODO(roycaihw): ideally a parent script updates the generated client and
78+
# fills in the Kubernetes API Version based on the OpenAPI spec.
79+
release_notes="# $TARGET_RELEASE\n\nKubernetes API Version: TBD\n\n$SUBMODULE_SECTION\n$release_notes"
80+
fi
81+
82+
echo "Writing the following release notes to CHANGELOG line $line_to_edit:"
83+
echo -e $release_notes
84+
85+
# update changelog
86+
sed -i "${line_to_edit}i${release_notes}" CHANGELOG.md
87+
88+
echo "Successfully updated CHANGELOG for submodule."

scripts/util/changelog.sh

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/bin/bash
2+
3+
# Copyright 2021 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
changelog="$(git rev-parse --show-toplevel)/CHANGELOG.md"
18+
19+
function util::changelog::has_release {
20+
local release=$1
21+
return $(grep -q "^# $release$" $changelog)
22+
}
23+
24+
# find_release_start returns the number of the first line of the given release
25+
function util::changelog::find_release_start {
26+
local release=$1
27+
echo $(grep -n "^# $release$" $changelog | head -1 | cut -d: -f1)
28+
}
29+
30+
# find_release_end returns the number of the last line of the given release
31+
function util::changelog::find_release_end {
32+
local release=$1
33+
34+
local release_start=$(util::changelog::find_release_start $release)
35+
local next_release_index=0
36+
local releases=($(grep -n "^# " $changelog | cut -d: -f1))
37+
for i in "${!releases[@]}"; do
38+
if [[ "${releases[$i]}" = "$release_start" ]]; then
39+
next_release_index=$((i+1))
40+
break
41+
fi
42+
done
43+
# return the line before the next release
44+
echo $((${releases[${next_release_index}]}-1))
45+
}
46+
47+
# has_section returns if the given section exists between start and end
48+
function util::changelog::has_section_in_range {
49+
local section="$1"
50+
local start=$2
51+
local end=$3
52+
53+
local lines=($(grep -n "$section" "$changelog" | cut -d: -f1))
54+
for i in "${!lines[@]}"; do
55+
if [[ ${lines[$i]} -ge $start && ${lines[$i]} -le $end ]]; then
56+
return 0
57+
fi
58+
done
59+
return 1
60+
}
61+
62+
# find_section returns the number of the first line of the given section
63+
function util::changelog::find_section_in_range {
64+
local section="$1"
65+
local start=$2
66+
local end=$3
67+
68+
local line="0"
69+
local lines=($(grep -n "$section" "$changelog" | cut -d: -f1))
70+
for i in "${!lines[@]}"; do
71+
if [[ ${lines[$i]} -ge $start && ${lines[$i]} -le $end ]]; then
72+
line=${lines[$i]}
73+
break
74+
fi
75+
done
76+
echo $line
77+
}

0 commit comments

Comments
 (0)