gpt-pr 0.3.0__tar.gz → 0.4.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: gpt-pr
3
- Version: 0.3.0
3
+ Version: 0.4.1
4
4
  Summary: Automate your GitHub workflow with GPT-PR: an OpenAI powered library for streamlined PR generation.
5
5
  Home-page: http://github.com/alissonperez/gpt-pr
6
6
  Author: Alisson R. Perez
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: gpt-pr
3
- Version: 0.3.0
3
+ Version: 0.4.1
4
4
  Summary: Automate your GitHub workflow with GPT-PR: an OpenAI powered library for streamlined PR generation.
5
5
  Home-page: http://github.com/alissonperez/gpt-pr
6
6
  Author: Alisson R. Perez
@@ -11,12 +11,14 @@ gpt_pr.egg-info/not-zip-safe
11
11
  gpt_pr.egg-info/requires.txt
12
12
  gpt_pr.egg-info/top_level.txt
13
13
  gptpr/__init__.py
14
+ gptpr/checkversion.py
14
15
  gptpr/config.py
15
16
  gptpr/consolecolor.py
16
17
  gptpr/gh.py
17
18
  gptpr/gitutil.py
18
19
  gptpr/main.py
19
20
  gptpr/prdata.py
21
+ gptpr/test_checkversion.py
20
22
  gptpr/test_config.py
21
23
  gptpr/test_prdata.py
22
24
  gptpr/version.py
@@ -0,0 +1,90 @@
1
+ import requests
2
+ import os
3
+ import json
4
+ import tempfile
5
+ from gptpr.version import __version__
6
+ from datetime import datetime, timedelta
7
+
8
+ from gptpr import consolecolor as cc
9
+
10
+
11
+ PACKAGE_NAME = 'gpt-pr'
12
+ CACHE_FILE = os.path.join(os.path.expanduser("~"), '.gpt_pr_update_cache.json')
13
+ CACHE_DURATION = timedelta(days=1)
14
+
15
+
16
+ def cache_daily_version(func):
17
+ def wrapper(*args, **kwargs):
18
+ cache = load_cache()
19
+ last_checked = cache.get('last_checked')
20
+
21
+ if last_checked:
22
+ last_checked = datetime.fromisoformat(last_checked)
23
+
24
+ if datetime.now() - last_checked < CACHE_DURATION:
25
+ # Use cached version info
26
+ latest_version = cache.get('latest_version')
27
+ if latest_version:
28
+ return latest_version
29
+
30
+ latest_version = func(*args, **kwargs)
31
+ cache = {
32
+ 'last_checked': datetime.now().isoformat(),
33
+ 'latest_version': latest_version
34
+ }
35
+ save_cache(cache)
36
+
37
+ return latest_version
38
+
39
+ return wrapper
40
+
41
+
42
+ def get_cache_file_path():
43
+ temp_dir = tempfile.gettempdir()
44
+ return os.path.join(temp_dir, f'{PACKAGE_NAME}_update_cache.json')
45
+
46
+
47
+ @cache_daily_version
48
+ def get_latest_version():
49
+ url = f'https://pypi.org/pypi/{PACKAGE_NAME}/json'
50
+
51
+ try:
52
+ response = requests.get(url)
53
+ response.raise_for_status()
54
+ data = response.json()
55
+ return data['info']['version']
56
+ except requests.exceptions.RequestException as e:
57
+ print(f"Error fetching latest version info: {e}")
58
+ return None
59
+
60
+
61
+ def load_cache():
62
+ cache_file = get_cache_file_path()
63
+ if os.path.exists(cache_file):
64
+ with open(cache_file, 'r') as file:
65
+ return json.load(file)
66
+
67
+ return {}
68
+
69
+
70
+ def save_cache(data):
71
+ cache_file = get_cache_file_path()
72
+ with open(cache_file, 'w') as file:
73
+ file.write(json.dumps(data))
74
+
75
+
76
+ def check_for_updates():
77
+ latest_version = get_latest_version()
78
+
79
+ if latest_version and latest_version != __version__:
80
+ print('')
81
+ print(cc.yellow(
82
+ f'A new version of {PACKAGE_NAME} is available ({latest_version}). '
83
+ f'You are using version {__version__}. Please update by running'),
84
+ cc.green(f'pip install --upgrade {PACKAGE_NAME}.'))
85
+ print('')
86
+
87
+
88
+ if __name__ == "__main__":
89
+ check_for_updates()
90
+ # Your CLI code here
@@ -7,6 +7,7 @@ from gptpr.prdata import get_pr_data
7
7
  from gptpr.version import __version__
8
8
  from gptpr.config import config, config_command_example, CONFIG_README_SECTION
9
9
  from gptpr import consolecolor as cc
10
+ from gptpr.checkversion import check_for_updates
10
11
 
11
12
 
12
13
  def run(base_branch='main', yield_confirmation=False, version=False):
@@ -81,10 +82,14 @@ def print_config():
81
82
 
82
83
 
83
84
  def main():
85
+ check_for_updates()
86
+
84
87
  fire.Fire(run)
85
88
 
86
89
 
87
90
  def run_config():
91
+ check_for_updates()
92
+
88
93
  fire.Fire({
89
94
  'set': set_config,
90
95
  'get': get_config,
@@ -0,0 +1,126 @@
1
+ import pytest
2
+ import requests
3
+ import json
4
+ from datetime import datetime
5
+ from unittest.mock import patch, mock_open
6
+
7
+ from gptpr.version import __version__
8
+ from gptpr.checkversion import (get_latest_version, load_cache,
9
+ save_cache, check_for_updates,
10
+ CACHE_DURATION)
11
+
12
+
13
+ @pytest.fixture
14
+ def mock_requests_get(mocker):
15
+ return mocker.patch('requests.get')
16
+
17
+
18
+ @pytest.fixture
19
+ def mock_os_path_exists(mocker):
20
+ return mocker.patch('os.path.exists')
21
+
22
+
23
+ @pytest.fixture
24
+ def mock_open_file(mocker):
25
+ return mocker.patch('builtins.open', mock_open())
26
+
27
+
28
+ @pytest.fixture
29
+ def mock_datetime(mocker):
30
+ return mocker.patch('gptpr.checkversion.datetime')
31
+
32
+
33
+ def test_get_latest_version(mock_requests_get, mock_os_path_exists):
34
+ mock_os_path_exists.return_value = False
35
+ mock_response = mock_requests_get.return_value
36
+ mock_response.raise_for_status.return_value = None
37
+ mock_response.json.return_value = {'info': {'version': '2.0.0'}}
38
+
39
+ assert get_latest_version() == '2.0.0'
40
+
41
+
42
+ def test_get_latest_version_error(mock_requests_get, mock_os_path_exists):
43
+ mock_os_path_exists.return_value = False
44
+ mock_requests_get.side_effect = requests.exceptions.RequestException
45
+
46
+ assert get_latest_version() is None
47
+
48
+
49
+ def test_load_cache(mock_os_path_exists, mock_open_file):
50
+ mock_os_path_exists.return_value = True
51
+ mock_open_file.return_value.read.return_value = json.dumps({
52
+ 'last_checked': datetime.now().isoformat(),
53
+ 'latest_version': '2.0.0'
54
+ })
55
+
56
+ cache = load_cache()
57
+ assert cache['latest_version'] == '2.0.0'
58
+
59
+
60
+ def test_load_cache_no_file(mock_os_path_exists):
61
+ mock_os_path_exists.return_value = False
62
+
63
+ cache = load_cache()
64
+ assert cache == {}
65
+
66
+
67
+ def test_save_cache(mock_open_file):
68
+ data = {
69
+ 'last_checked': datetime.now().isoformat(),
70
+ 'latest_version': '2.0.0'
71
+ }
72
+
73
+ save_cache(data)
74
+ mock_open_file.return_value.write.assert_called_once_with(json.dumps(data))
75
+
76
+
77
+ def test_check_for_updates_new_version(mocker, mock_datetime, mock_requests_get, mock_open_file):
78
+ # Set up mocks
79
+ last_checked_str = (datetime(2024, 1, 1) - CACHE_DURATION).isoformat()
80
+ mock_datetime.now.return_value = datetime(2024, 1, 2)
81
+ mock_datetime.fromisoformat.return_value = datetime.fromisoformat(last_checked_str)
82
+ mock_open_file.return_value.read.return_value = json.dumps({
83
+ 'last_checked': last_checked_str,
84
+ 'latest_version': '1.0.0'
85
+ })
86
+ mock_requests_get.return_value.raise_for_status.return_value = None
87
+ mock_requests_get.return_value.json.return_value = {'info': {'version': '2.0.0'}}
88
+
89
+ # Capture the print statements
90
+ with patch('builtins.print') as mocked_print:
91
+ check_for_updates()
92
+ assert mocked_print.call_count == 3
93
+
94
+
95
+ def test_check_for_updates_no_new_version(mocker, mock_datetime, mock_requests_get, mock_open_file):
96
+ # Set up mocks
97
+ last_checked_str = (datetime(2024, 1, 1) - CACHE_DURATION).isoformat()
98
+ mock_datetime.now.return_value = datetime(2024, 1, 2)
99
+ mock_datetime.fromisoformat.return_value = datetime.fromisoformat(last_checked_str)
100
+ mock_open_file.return_value.read.return_value = json.dumps({
101
+ 'last_checked': (datetime(2024, 1, 1) - CACHE_DURATION).isoformat(),
102
+ 'latest_version': __version__
103
+ })
104
+ mock_requests_get.return_value.raise_for_status.return_value = None
105
+ mock_requests_get.return_value.json.return_value = {'info': {'version': __version__}}
106
+
107
+ # Capture the print statements
108
+ with patch('builtins.print') as mocked_print:
109
+ check_for_updates()
110
+ assert mocked_print.call_count == 0
111
+
112
+
113
+ def test_check_for_updates_cache_valid(mock_datetime, mock_open_file):
114
+ # Set up mocks
115
+ last_checked_str = datetime(2024, 1, 2).isoformat()
116
+ mock_datetime.now.return_value = datetime(2024, 1, 2)
117
+ mock_datetime.fromisoformat.return_value = datetime.fromisoformat(last_checked_str)
118
+ mock_open_file.return_value.read.return_value = json.dumps({
119
+ 'last_checked': last_checked_str,
120
+ 'latest_version': __version__
121
+ })
122
+
123
+ # Capture the print statements
124
+ with patch('builtins.print') as mocked_print:
125
+ check_for_updates()
126
+ assert mocked_print.call_count == 0
@@ -0,0 +1 @@
1
+ __version__ = "0.4.1"
@@ -1 +0,0 @@
1
- __version__ = "0.3.0"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes