aic-utils 2.0.6__tar.gz → 2.0.7__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.4
2
2
  Name: aic_utils
3
- Version: 2.0.6
3
+ Version: 2.0.7
4
4
  Summary: AIC API wrapper and GitLab integration framework for pipeline management
5
5
  Home-page: https://github.com/dylandoyle11/aic_utils
6
6
  Author: Dylan D
@@ -8,7 +8,7 @@ from .gitlab_init import GitLabRepositoryInitializer
8
8
  from .dataset_converter import DatasetConverter
9
9
  from .slack_logger import SlackLogger
10
10
 
11
- __version__ = '2.0.6'
11
+ __version__ = '2.0.7'
12
12
  __author__ = 'Dylan D'
13
13
 
14
14
  __all__ = [
@@ -150,65 +150,71 @@ class GitLabManager:
150
150
 
151
151
  def get_token_expiration_date(self):
152
152
  """
153
- Retrieves the expiration date of the current GitLab token.
153
+ Retrieves the expiration date of the GitLab token actually being used
154
+ for this session. Uses GET /personal_access_tokens/self (GitLab 16.0+)
155
+ for an exact match. Falls back to listing all tokens and picking the
156
+ soonest-expiring one only if /self isn't supported by the GitLab
157
+ instance — that fallback is a heuristic and may name the wrong token.
158
+
154
159
  Returns None if expiration cannot be determined.
155
160
  """
161
+ def _parse_exp(exp_str):
162
+ if not exp_str:
163
+ return None
164
+ try:
165
+ if 'T' in exp_str:
166
+ return datetime.strptime(exp_str, '%Y-%m-%dT%H:%M:%S.%fZ')
167
+ return datetime.strptime(exp_str, '%Y-%m-%d')
168
+ except ValueError:
169
+ return None
170
+
156
171
  try:
157
- # First, validate the token works by getting user info
158
- user_url = f"{self.gitlab_base}/user"
159
- user_response = requests.get(user_url, headers=self.headers, timeout=30)
172
+ # Validate the token works
173
+ user_response = requests.get(f"{self.gitlab_base}/user", headers=self.headers, timeout=30)
160
174
  user_response.raise_for_status()
175
+ username = user_response.json().get('username', 'Unknown')
161
176
 
162
- user_info = user_response.json()
163
- username = user_info.get('username', 'Unknown')
164
-
165
- # Try to get personal access tokens
166
- tokens_url = f"{self.gitlab_base}/personal_access_tokens"
167
- tokens_response = requests.get(tokens_url, headers=self.headers, timeout=30)
168
-
169
- if tokens_response.status_code == 200:
170
- tokens = tokens_response.json()
171
-
172
- # Find active tokens with expiration dates
173
- active_tokens_with_expiry = []
174
- for token in tokens:
175
- if token.get('revoked', True):
176
- continue
177
-
178
- exp_str = token.get('expires_at')
179
- if not exp_str:
180
- continue
181
-
182
- try:
183
- # Handle both date formats GitLab might return
184
- if 'T' in exp_str:
185
- exp_date = datetime.strptime(exp_str, '%Y-%m-%dT%H:%M:%S.%fZ')
186
- else:
187
- exp_date = datetime.strptime(exp_str, '%Y-%m-%d')
188
-
189
- active_tokens_with_expiry.append({
190
- 'name': token.get('name', 'Unnamed'),
191
- 'expires_at': exp_date,
192
- 'scopes': token.get('scopes', [])
193
- })
194
- except ValueError as e:
195
- continue
196
-
197
- if active_tokens_with_expiry:
198
- # Sort by expiration date (soonest first)
199
- active_tokens_with_expiry.sort(key=lambda x: x['expires_at'])
200
-
201
- # Return the soonest expiring token
202
- # (assumption: user is likely using their most recent/soon-to-expire token)
203
- token_info = active_tokens_with_expiry[0]
204
- print(f"Checking expiration for token: '{token_info['name']}'")
205
- return token_info['expires_at']
206
- else:
207
- print(f"✅ Token valid for user '{username}' but no expiration date found")
208
- return None
209
- else:
210
- print(f"✅ Token valid for user '{username}' but cannot access token list")
177
+ # Preferred path: ask GitLab which token we're using right now.
178
+ self_resp = requests.get(
179
+ f"{self.gitlab_base}/personal_access_tokens/self",
180
+ headers=self.headers, timeout=30,
181
+ )
182
+ if self_resp.status_code == 200:
183
+ tok = self_resp.json()
184
+ exp_date = _parse_exp(tok.get('expires_at'))
185
+ if exp_date is not None:
186
+ print(f"Checking expiration for token: '{tok.get('name','Unnamed')}'")
187
+ return exp_date
188
+ print(f"✅ Token '{tok.get('name','Unnamed')}' valid for user '{username}' but has no expiration set")
189
+ return None
190
+ elif self_resp.status_code != 404:
191
+ # Some other error — surface a hint but keep going to fallback
192
+ print(f"⚠️ /personal_access_tokens/self returned {self_resp.status_code}; falling back to list-all heuristic.")
193
+
194
+ # Fallback for GitLab < 16.0: list all tokens and pick soonest-expiring.
195
+ # This is best-effort and may report a different token than the one in use.
196
+ tokens_response = requests.get(
197
+ f"{self.gitlab_base}/personal_access_tokens",
198
+ headers=self.headers, timeout=30,
199
+ )
200
+ if tokens_response.status_code != 200:
201
+ print(f"✅ Token valid for user '{username}' but cannot read token metadata")
202
+ return None
203
+ active = []
204
+ for token in tokens_response.json():
205
+ if token.get('revoked', True):
206
+ continue
207
+ exp_date = _parse_exp(token.get('expires_at'))
208
+ if exp_date is None:
209
+ continue
210
+ active.append((exp_date, token.get('name', 'Unnamed')))
211
+ if not active:
212
+ print(f"✅ Token valid for user '{username}' but no expiration date found")
211
213
  return None
214
+ active.sort()
215
+ exp_date, name = active[0]
216
+ print(f"Checking expiration for token: '{name}' (best-guess via list-all fallback)")
217
+ return exp_date
212
218
 
213
219
  except requests.exceptions.RequestException as err:
214
220
  print(f"Error checking token: {err}")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aic_utils
3
- Version: 2.0.6
3
+ Version: 2.0.7
4
4
  Summary: AIC API wrapper and GitLab integration framework for pipeline management
5
5
  Home-page: https://github.com/dylandoyle11/aic_utils
6
6
  Author: Dylan D
@@ -7,7 +7,7 @@ with open('README.md', 'r', encoding='utf-8') as fh:
7
7
 
8
8
  setup(
9
9
  name='aic_utils',
10
- version='2.0.6',
10
+ version='2.0.7',
11
11
  packages=find_packages(),
12
12
  install_requires=[
13
13
  'requests>=2.25.0',
File without changes
File without changes
File without changes