Skip to content

Commit 90a3c05

Browse files
authored
Merge pull request #18 from openlawlibrary/ndusan/run-post-install-notifications
fix: add post install notifications
2 parents e37ee99 + 4cb3f42 commit 90a3c05

3 files changed

Lines changed: 81 additions & 23 deletions

File tree

.github/workflows/ci.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@ jobs:
88
matrix:
99
os: [macos-latest, windows-latest, ubuntu-latest]
1010
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
11+
exclude:
12+
- os: macos-latest
13+
python-version: "3.8"
14+
- os: macos-latest
15+
python-version: "3.9"
16+
- os: macos-latest
17+
python-version: "3.10"
1118

1219
steps:
13-
- uses: actions/checkout@v3
20+
- uses: actions/checkout@v4
1421
- name: Set up Python
15-
uses: actions/setup-python@v4
22+
uses: actions/setup-python@v5
1623
with:
1724
python-version: "${{ matrix.python-version }}"
1825
architecture: x64

CHANGELOG.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,34 @@ and this project adheres to a _modified_ form of _[Semantic Versioning][semver]_
1616

1717
### Removed
1818

19+
## [0.7.2]
20+
21+
### Added
22+
23+
### Changed
24+
25+
### Fixed
26+
27+
- Send slack notification when post-install fails ([#18])
28+
29+
[#18]: https://github.com/openlawlibrary/upgrade-python-package/pull/18
30+
31+
### Removed
32+
33+
## [0.7.1]
34+
35+
### Added
36+
37+
### Changed
38+
39+
### Fixed
40+
41+
- Fix get latest wheel if `version_cmd` is missing ([#17])
42+
43+
### Removed
44+
45+
[#17]: https://github.com/openlawlibrary/upgrade-python-package/pull/17
46+
1947
## [0.7.0]
2048

2149
### Added
@@ -127,7 +155,9 @@ and this project adheres to a _modified_ form of _[Semantic Versioning][semver]_
127155
[#5]: https://github.com/openlawlibrary/upgrade-python-package/pull/5
128156
[#6]: https://github.com/openlawlibrary/upgrade-python-package/pull/6
129157

130-
[Unreleased]: https://github.com/openlawlibrary/upgrade-python-package/compare/0.7.0...HEAD
158+
[Unreleased]: https://github.com/openlawlibrary/upgrade-python-package/compare/0.7.2...HEAD
159+
[0.7.2]: https://github.com/openlawlibrary/upgrade-python-package/compare/0.7.1...0.7.2
160+
[0.7.1]: https://github.com/openlawlibrary/upgrade-python-package/compare/0.7.0...0.7.1
131161
[0.7.0]: https://github.com/openlawlibrary/upgrade-python-package/compare/0.6.0...0.7.0
132162
[0.6.0]: https://github.com/openlawlibrary/upgrade-python-package/compare/0.5.0...0.6.0
133163
[0.5.0]: https://github.com/openlawlibrary/upgrade-python-package/compare/0.4.0...0.5.0

upgrade/scripts/upgrade_python_package.py

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ def upgrade_and_run(
7171
)
7272
if not skip_post_install and (was_updated or force):
7373
module_name = package_name.replace("-", "_")
74-
try_running_module(module_name, *args)
74+
try_running_module(
75+
module_name,
76+
cloudsmith_url=cloudsmith_url,
77+
slack_webhook_url=slack_webhook_url,
78+
*args,
79+
)
7580
return was_updated, response_err
7681

7782

@@ -217,7 +222,9 @@ def install_wheel(
217222
]
218223
wheel_mapping = {k: v for (k, v) in zip(parsed_wheel_versions, wheel_paths)}
219224
if version_cmd is not None:
220-
versions = filter_versions(SpecifierSet(version_cmd), parsed_wheel_versions)
225+
versions = filter_versions(
226+
SpecifierSet(version_cmd), parsed_wheel_versions
227+
)
221228
wheel = wheel_mapping.get(versions[-1])
222229
else:
223230
wheel = parsed_wheel_versions[-1]
@@ -270,22 +277,11 @@ def install_wheel(
270277
)
271278
except:
272279
if slack_webhook_url is not None:
273-
try:
274-
environment = (
275-
"dev" if is_development_cloudsmith(cloudsmith_url) else "prod"
276-
)
277-
log_filepath = get_log_file_path().as_posix() or "log file"
278-
server_metadata = get_server_metadata()
279-
send_slack_notification(
280-
f"Failed to upgrade package {package_name}",
281-
f"{environment.upper()} - For more details, please audit {str(log_filepath)} at ({server_metadata}).",
282-
slack_webhook_url,
283-
)
284-
except Exception as e:
285-
logging.error(
286-
f"Failed to send slack notification due to error: {e}"
287-
)
288-
raise
280+
send_upgrade_notification(
281+
f"Failed to upgrade package {package_name}",
282+
cloudsmith_url,
283+
slack_webhook_url,
284+
)
289285
# if install with constraints fails or the installation caused broken dependencies
290286
# revert back to old package version
291287
if version is not None:
@@ -435,6 +431,22 @@ def run_module_and_reload_uwsgi_app(module_name, *args):
435431
reload_uwsgi_app(package_name)
436432

437433

434+
def send_upgrade_notification(header, cloudsmith_url, slack_webhook_url):
435+
try:
436+
log_filepath = get_log_file_path().as_posix() or "log file"
437+
server_metadata = get_server_metadata()
438+
environment = "dev" if is_development_cloudsmith(cloudsmith_url) else "prod"
439+
text = f"{environment.upper()} - For more details, please audit {str(log_filepath)} at ({server_metadata})."
440+
send_slack_notification(
441+
header,
442+
text,
443+
slack_webhook_url,
444+
)
445+
except Exception as e:
446+
logging.error(f"Failed to send upgrade notification due to error: {e}")
447+
raise
448+
449+
438450
def split_package_name_and_extra(package_install_cmd):
439451
extra_start = package_install_cmd.find("[")
440452
if extra_start != -1:
@@ -446,14 +458,23 @@ def split_package_name_and_extra(package_install_cmd):
446458
return package_name, extra
447459

448460

449-
def try_running_module(wheel, *args):
461+
def try_running_module(wheel, cloudsmith_url=None, slack_webhook_url=None, *args):
450462
file_name = os.path.basename(wheel)
451463
module_name = file_name.split("-", 1)[0]
452464
# don't try running the module if it does not exists
453465
# prevents errors from being printed in case of trying
454466
# to run e.g. oll-core or oll-partners
455467
if util.find_spec(module_name) and util.find_spec(".__main__", package=module_name):
456-
run_module_and_reload_uwsgi_app(module_name, *args)
468+
try:
469+
run_module_and_reload_uwsgi_app(module_name, *args)
470+
except Exception:
471+
if slack_webhook_url is not None:
472+
send_upgrade_notification(
473+
f"Failed to run module {module_name}",
474+
cloudsmith_url,
475+
slack_webhook_url,
476+
)
477+
raise
457478
else:
458479
logging.info("No module named %s", module_name)
459480
print(f"No module named {module_name}")

0 commit comments

Comments
 (0)