Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor of dev prerelease auto-update-version workflow #685

Merged
merged 59 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
5058652
.github
rashidnhm Apr 16, 2024
3e4b163
Try with other event
rashidnhm Apr 16, 2024
a910ed1
Try with other event
rashidnhm Apr 16, 2024
d39c06d
Try with other event
rashidnhm Apr 16, 2024
1bdf2fa
Try with other event
rashidnhm Apr 16, 2024
7d79434
Auto update version
ringo-but-quantum Apr 16, 2024
b34ae52
Try invalid token
rashidnhm Apr 16, 2024
d954e96
Merge branch 'sc-63182-version-update' of github.com:PennyLaneAI/penn…
rashidnhm Apr 16, 2024
7916bdd
Try new method
rashidnhm Apr 16, 2024
b58204a
Revert version
rashidnhm Apr 16, 2024
b44aaa8
Try persist creds
rashidnhm Apr 16, 2024
92db27f
Fix token name
rashidnhm Apr 16, 2024
6d2472c
Fix token name
rashidnhm Apr 16, 2024
a2ca5cf
Auto update version
ringo-but-quantum Apr 16, 2024
7f2e046
Fix token name
rashidnhm Apr 16, 2024
8f3c885
Try new method
rashidnhm Apr 16, 2024
fe1e1aa
try pr script
rashidnhm Apr 16, 2024
aecbbeb
Fix bug
rashidnhm Apr 16, 2024
af5c8ca
Auto update version
ringo-but-quantum Apr 16, 2024
ebd7546
Trigger CI
rashidnhm Apr 16, 2024
69e5167
Add some error handling
rashidnhm Apr 16, 2024
524b7a1
Cleanup
rashidnhm Apr 16, 2024
af523b0
Cleanup
rashidnhm Apr 16, 2024
2485df6
Cleanup
rashidnhm Apr 16, 2024
c805e42
Cleanup
rashidnhm Apr 16, 2024
c535d17
Cleanup
rashidnhm Apr 16, 2024
a2f40ff
Cleanup
rashidnhm Apr 16, 2024
4a6e3fc
Auto update version
ringo-but-quantum Apr 16, 2024
dd723ea
Cleanup
rashidnhm Apr 16, 2024
2eeeeb4
Merge branch 'sc-63182-version-update' of github.com:PennyLaneAI/penn…
rashidnhm Apr 16, 2024
6da69bf
Cleanup
rashidnhm Apr 16, 2024
ad4bc84
Auto update version
ringo-but-quantum Apr 16, 2024
ae74bd0
Cleanup
rashidnhm Apr 16, 2024
15f88e1
Auto update version
ringo-but-quantum Apr 16, 2024
5f6c649
Try new method
rashidnhm Apr 16, 2024
5256f8d
Auto update version
ringo-but-quantum Apr 16, 2024
16e4e3a
Try new method
rashidnhm Apr 16, 2024
eead494
Auto update version
ringo-but-quantum Apr 16, 2024
3e849b6
Cleanup
rashidnhm Apr 16, 2024
adaf750
Try new code
rashidnhm Apr 17, 2024
1c27aa1
Merge branch 'master' of github.com:PennyLaneAI/pennylane-lightning i…
rashidnhm Apr 17, 2024
eeed292
Auto update version from '0.36.0-dev29' to '0.36.0-dev30'
ringo-but-quantum Apr 17, 2024
a961b34
Trigger CI
rashidnhm Apr 17, 2024
a735380
Merge branch 'sc-63182-version-update' of github.com:PennyLaneAI/penn…
rashidnhm Apr 17, 2024
79d0c8d
Trigger CI
rashidnhm Apr 17, 2024
8af0449
Add comments. Try high dev version num
rashidnhm Apr 17, 2024
02f6c3e
Auto update version from '0.36.0-dev99' to '0.36.0-dev30'
ringo-but-quantum Apr 17, 2024
413aa36
Address codefactor annotations
rashidnhm Apr 17, 2024
c41463f
Merge branch 'sc-63182-version-update' of github.com:PennyLaneAI/penn…
rashidnhm Apr 17, 2024
60d28bf
Address codefactor annotations
rashidnhm Apr 17, 2024
9570f99
Undo TODOs
rashidnhm Apr 17, 2024
270b525
Address PR comments. Test the new changes
rashidnhm Apr 18, 2024
d158591
Merge branch 'master' of github.com:PennyLaneAI/pennylane-lightning i…
rashidnhm Apr 18, 2024
02aae79
Auto update version from '0.36.0-dev31' to '0.36.0-dev32'
ringo-but-quantum Apr 18, 2024
40a84e8
Set ringo email as secret
rashidnhm Apr 18, 2024
35b9759
Update regex to capture new/old version to be more specific. Added if…
rashidnhm Apr 18, 2024
49bdd18
Swap trigger event back to pr_target
rashidnhm Apr 18, 2024
46222b2
Merge branch 'master' into sc-63182-version-update
rashidnhm Apr 18, 2024
5407f2e
Merge branch 'master' into sc-63182-version-update
mlxd Apr 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 71 additions & 43 deletions .github/workflows/dev_version_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,74 +11,102 @@
# 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.

import re
import argparse
from pathlib import Path
import importlib

import re
try:
from semver import Version
except ImportError as exc:
raise ImportError("Unable to import semver. Install semver by running `pip install semver`") from exc

VERSION_FILE_PATH = "pennylane_lightning/core/_version.py"
DEV_PRERELEASE_TAG_PREFIX = "dev"
DEV_PRERELEASE_TAG_START = "dev0"
VERSION_FILE_PATH = Path("pennylane_lightning/core/_version.py")

rgx_ver = re.compile('^__version__ = "(.*?)"$')
rgx_ver = re.compile(pattern=r"^__version__ = \"(.*)\"$", flags=re.MULTILINE)
mlxd marked this conversation as resolved.
Show resolved Hide resolved

rgx_dev_ver = re.compile("^(\d*\.\d*\.\d*)-dev(\d*)$")

def extract_version(repo_root_path: Path) -> Version:
"""
Given the repository root for pennylane-lightning, this function extracts the semver version from
pennylane_lightning/core/_version.py.

def extract_version(package_path):
with package_path.joinpath(VERSION_FILE_PATH).open("r") as f:
for line in f.readlines():
:param repo_root_path: Path to the repository root.
:return: Extracted version a semver.Version object.
"""
version_file_path = repo_root_path / VERSION_FILE_PATH
if not version_file_path.exists():
raise FileNotFoundError(f"Unable to find version file at location {version_file_path}")

with version_file_path.open() as f:
for line in f:
if line.startswith("__version__"):
line = line.strip()
m = rgx_ver.match(line)
return m.group(1)
if (m := rgx_ver.match(line.strip())) is not None:
if not m.groups():
raise ValueError(f"Unable to find valid semver for __version__. Got: '{line}'")
parsed_semver = m.group(1)
if not Version.is_valid(parsed_semver):
raise ValueError(f"Invalid semver for __version__. Got: '{parsed_semver}' from line '{line}'")
return Version.parse(parsed_semver)
raise ValueError(f"Unable to find valid semver for __version__. Got: '{line}'")
raise ValueError("Cannot parse version")


def is_dev(version_str):
m = rgx_dev_ver.fullmatch(version_str)
return m is not None
def update_prerelease_version(repo_root_path: Path, version: Version):
"""
Updates the version file within pennylane_lightning/core/_version.py.

:param repo_root_path: Path to the repository root.
:param version: The new version to use within the file.
:return:
"""
version_file_path = repo_root_path / VERSION_FILE_PATH
if not version_file_path.exists():
raise FileNotFoundError(f"Unable to find version file at location {version_file_path}")

def update_dev_version(package_path, version_str):
m = rgx_dev_ver.fullmatch(version_str)
if m.group(2) == "":
curr_dev_ver = 0
else:
curr_dev_ver = int(m.group(2))
with version_file_path.open() as f:
lines = [
rgx_ver.sub(f"__version__ = \"{str(version)}\"", line)
for line in f
]

new_version_str = "{}-dev{}".format(m.group(1), str(curr_dev_ver + 1))

lines = []
with package_path.joinpath(VERSION_FILE_PATH).open("r") as f:
for line in f.readlines():
if not line.startswith("__version__"):
lines.append(line)
else:
lines.append(f'__version__ = "{new_version_str}"\n')

with package_path.joinpath(VERSION_FILE_PATH).open("w") as f:
with version_file_path.open("w") as f:
f.write("".join(lines))


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--pr-path", dest="pr", type=str, required=True, help="Path to the PR dir")
parser.add_argument("--pr-path", dest="pr", type=Path, required=True, help="Path to the PR dir")
parser.add_argument(
"--master-path", dest="master", type=str, required=True, help="Path to the master dir"
"--master-path", dest="master", type=Path, required=True, help="Path to the master dir"
)

args = parser.parse_args()

pr_version = extract_version(Path(args.pr))
master_version = extract_version(Path(args.master))
pr_version = extract_version(args.pr)
master_version = extract_version(args.master)

if pr_version == master_version:
if is_dev(pr_version):
print("Automatically update version string.")
update_dev_version(Path(args.pr), pr_version)
print("Got Package Version from 'master' ->", str(master_version))
print("Got Package Version from 'PR' ->", str(pr_version))

# Only attempt to bump the version if the pull_request is:
# - A prerelease, has `X.Y.Z-prerelease` in _version.py
# - The prerelease startswith `dev`. We do not want to auto bump for non-dev prerelease.
mlxd marked this conversation as resolved.
Show resolved Hide resolved
if pr_version.prerelease and pr_version.prerelease.startswith(DEV_PRERELEASE_TAG_PREFIX):
# If master branch does not have a prerelease (for any reason) OR does not have an ending number
# Then default to the starting tag
if not master_version.prerelease or master_version.prerelease == DEV_PRERELEASE_TAG_PREFIX:
next_prerelease_version = DEV_PRERELEASE_TAG_START
else:
# Generate the next prerelease version (eg: dev1 -> dev2). Sourcing from master version.
next_prerelease_version = master_version.next_version("prerelease").prerelease
new_version = master_version.replace(prerelease=next_prerelease_version)
if pr_version != new_version:
print(f"Updating PR package version from -> '{pr_version}', to -> {new_version}")
update_prerelease_version(args.pr, new_version)
else:
print(
"Even though version of this PR is different from the master, as the PR is not dev, we do nothing."
)
print(f"PR is on the expected version '{new_version}' ... Nothing to do!")
else:
print("Version of this PR is already different from master. Do nothing.")
print("PR is not a dev prerelease ... Nothing to do!")
50 changes: 36 additions & 14 deletions .github/workflows/update_dev_version.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: Update dev version automatically
on:
pull_request:
pull_request_target:
branches:
- master

jobs:
update-dev-version:
Expand All @@ -14,31 +16,51 @@ jobs:
ref: master
path: main

- name: Checkout PennyLane-Lightning PR
uses: actions/checkout@v4
with:
token: ${{ secrets.AUTO_UPDATE_VERSION_RINGO_TOKEN }}
path: pr

- name: Checkout PR branch
env:
GITHUB_TOKEN: ${{ secrets.AUTO_UPDATE_VERSION_RINGO_TOKEN }}
run: cd pr && gh pr checkout ${{ github.event.pull_request.number }}

- uses: actions/setup-python@v5
name: Install Python
with:
python-version: '3.9'

- name: Checkout PennyLane-Lightning PR
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
ref: ${{ github.head_ref }}
path: pr
- name: Install Python Dependencies
run: pip install semver

- name: Capture Current Version in Pull Request
id: old_version
run: echo "version=$(grep -o '__version__ = "[^"]\+[0-9]\+"' pr/pennylane_lightning/core/_version.py | cut -d '"' -f 2)" >> $GITHUB_OUTPUT

- name: Run version update script
run: >
python3 pr/.github/workflows/dev_version_script.py
--pr-path "./pr" --master-path "./main"
run: |
python3 \
main/.github/workflows/dev_version_script.py \
--pr-path "${{ github.workspace }}/pr" \
--master-path "${{ github.workspace }}/main"

- name: Capture Changed version
id: new_version
run: echo "version=$(grep -o '__version__ = "[^"]\+[0-9]\+"' pr/pennylane_lightning/core/_version.py | cut -d '"' -f 2)" >> $GITHUB_OUTPUT

- name: Commit and push changes
if:
if: steps.old_version.outputs.version != steps.new_version.outputs.version
env:
OLD_VERSION: ${{ steps.old_version.outputs.version }}
NEW_VERSION: ${{ steps.new_version.outputs.version }}
run: |
cd ./pr
if [[ -n $(git status -s) ]]; then
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git config --global user.name "Dev version update bot"
git config --global user.email '${{ secrets.AUTO_UPDATE_VERSION_RINGO_EMAIL }}'
git config --global user.name "ringo-but-quantum"
git add .
git commit -m 'Auto update version'
git commit -m "Auto update version from '$OLD_VERSION' to '$NEW_VERSION'"
git push
fi
Loading