Skip to content

Commit

Permalink
Dev workflow: Bump version with the first PR that introduces change (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
psrok1 committed Apr 28, 2021
1 parent 817327f commit f590986
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 7 deletions.
16 changes: 15 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,21 @@ https://gist.github.com/Chaser324/ce0505fbed06b947d962).
npx prettier --write src/
```

6. When you feel like you're done, commit the files:
6. If setup.py version matches the latest release version:
include version bump in your commit according to the [semantic versioning](https://semver.org)

e.g.
```
python3 dev/bump_version 1.2.3
```

- Bump MAJOR version (2.2.2 => 3.0.0) when your change breaks the API compatibility.
- Bump MINOR version (2.2.2 => 2.3.0) when you add functionality in a backwards compatible manner.
- Bump PATCH version (2.2.2 => 2.2.3) when you provide a bug fix.

Version bump should be included in the same PR.

7. When you feel like you're done, commit the files:

```bash
$ git add -A
Expand Down
11 changes: 11 additions & 0 deletions dev/bump_version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"files": {
"setup.py": "version=\"$VERSION\"",
"mwdb/version.py": "app_version = \"$VERSION\"",
"mwdb/web/package.json": "\"version\": \"$VERSION\"",
"mwdb/web/package-lock.json": "\"version\": \"$VERSION\"",
"mwdb/web/src/commons/package.json": "\"version\": \"$VERSION\"",
"docs/conf.py": "release = '$VERSION'"
},
"regex": "(\\d+[.]\\d+[.]\\d+(?:-(post|dev|alpha|beta|rc)[0-9])?)"
}
79 changes: 79 additions & 0 deletions dev/bump_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#! /usr/bin/env python3
# bump_version.py 0.1.0
import difflib
import json
import re
import sys
from pathlib import Path

CONFIG = json.loads((Path(__file__).parent / "bump_version.json").read_text())
CURRENT_DIR = Path.cwd()
VERSION_FILES = {
(CURRENT_DIR / path): pattern for path, pattern in CONFIG["files"].items()
}
VERSION_REGEX = CONFIG["regex"]


def main(new_version):
input_files = {}
output_files = {}
old_version = None

if not re.match(fr"^{VERSION_REGEX}$", new_version):
print(f"[!] '{new_version}' doesn't match the regex: {VERSION_REGEX}")
return

def subst_version(repl):
return (
repl.string[repl.start(0) : repl.start(1)]
+ new_version
+ repl.string[repl.end(1) : repl.end(0)]
)

for path in VERSION_FILES.keys():
if not path.exists():
print(f"[!] File {str(path)} is missing. Are you in project root dir?")
return False

with open(path, "r") as f:
content = input_files[path] = f.read()

pattern = VERSION_FILES[path].replace("$VERSION", VERSION_REGEX)
version = next(re.finditer(pattern, content)).group(1)
output_files[path] = re.sub(pattern, subst_version, content, count=1)

if old_version is not None and version != old_version:
print(
f"[!] {str(path)} contains different version than other files "
f"({version} != {old_version})"
)
old_version = version

for path in VERSION_FILES.keys():
input_lines = input_files[path].splitlines()
output_lines = output_files[path].splitlines()
if input_lines == output_lines:
print("[*] No changes detected.")
return
print("=== " + str(path))
for line in difflib.unified_diff(input_lines, output_lines, lineterm=""):
print(line)

response = ""
while response.lower() not in {"y", "n", "yes", "no"}:
response = input("[*] Check above diff ^ Is it correct? (y/n): ")

if response.lower() in {"y", "yes"}:
for path, content in output_files.items():
with open(path, "w") as f:
f.write(content)
print("[+] Changes applied!")
else:
print("[-] Changes discarded.")


if __name__ == "__main__":
if not sys.argv[1:]:
print("Usage: bump_version.py [new_version]")
else:
main(sys.argv[1])
1 change: 1 addition & 0 deletions docs/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
_build/
venv/
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
author = 'CERT Polska'

# The full version, including alpha/beta/rc tags
release = '2.2.2'
release = '2.3.0'


# -- General configuration ---------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion mwdb/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
except IOError:
git_revision = ""

app_version = "2.2.2"
app_version = "2.3.0"
app_build_version = f"{app_version}+{git_revision}" if git_revision else app_version
2 changes: 1 addition & 1 deletion mwdb/web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mwdb/web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mwdb-web",
"version": "2.2.2",
"version": "2.3.0",
"private": true,
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^1.2.0-14",
Expand Down
2 changes: 1 addition & 1 deletion mwdb/web/src/commons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@mwdb-web/commons",
"main": "index.js",
"private": true,
"version": "2.2.2",
"version": "2.3.0",
"peerDependencies": {
"@fortawesome/react-fontawesome": "*",
"lodash": "*",
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""

setup(name="mwdb-core",
version="2.2.2",
version="2.3.0",
description="MWDB Core malware database",
long_description=LONG_DESCRIPTION,
author="CERT Polska",
Expand Down

0 comments on commit f590986

Please sign in to comment.