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

Fix IndexError in get_last_tag_from_remote Function #196

Merged
merged 1 commit into from
Jan 2, 2024

Conversation

markus-lassfolk
Copy link
Contributor

Summary

This pull request addresses an IndexError: list index out of range that was occurring in the get_last_tag_from_remote function within the git_status.py file. The error emerged when the function attempted to access a specific index in the output of a subprocess command which sometimes returned fewer lines than expected.

Changes

  • Added a check to verify the length of the output list before accessing the second-to-last element.
  • Implemented handling for cases where the output is insufficient, thereby preventing the IndexError.
  • Included additional verbose logging for better debugging and error tracking.

Impact

  • These changes ensure that the function behaves as expected even when the subprocess command returns unexpected or minimal output.
  • Prevents the application from crashing due to unhandled exceptions in scenarios where git tags are not as expected or absent.

Testing

  • The function has been tested with various scenarios, including cases where the git command returns no tags, a single tag, or multiple tags. In all cases, the function now handles the output gracefully without causing an IndexError.

Full Exception when accessing Server Manager:

[2024-01-01 20:24:49,550] ERROR in app: Exception on /settings [GET]
Traceback (most recent call last):
  File "/home/markus/ail-framework/AILENV/lib/python3.10/site-packages/flask/app.py", line 2190, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/markus/ail-framework/AILENV/lib/python3.10/site-packages/flask/app.py", line 1486, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/markus/ail-framework/AILENV/lib/python3.10/site-packages/flask/app.py", line 1484, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/markus/ail-framework/AILENV/lib/python3.10/site-packages/flask/app.py", line 1469, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "/home/markus/ail-framework/AILENV/lib/python3.10/site-packages/flask_login/utils.py", line 290, in decorated_view
    return current_app.ensure_sync(func)(*args, **kwargs)
  File "/home/markus/ail-framework/var/www/./modules/Role_Manager.py", line 84, in decorated_view
    return func(*args, **kwargs)
  File "/home/markus/ail-framework/var/www/blueprints/settings_b.py", line 41, in settings_page
    git_metadata = git_status.get_git_metadata()
  File "/home/markus/ail-framework/bin/packages/git_status.py", line 157, in get_git_metadata
    dict_git['last_remote_tag'] = get_last_tag_from_remote()
  File "/home/markus/ail-framework/bin/packages/git_status.py", line 139, in get_last_tag_from_remote
    res = process.stdout.split(b'\n')[-2].split(b'/')[-1].replace(b'^{}', b'').decode()
IndexError: list index out of range

This commit adds a check to ensure that the output from the subprocess command in the get_last_tag_from_remote function has a sufficient number of lines before attempting to access specific indices. This change prevents the IndexError that occurred when the git command's output was shorter than expected.
@Terrtia Terrtia merged commit dddc691 into ail-project:master Jan 2, 2024
0 of 4 checks passed
@Terrtia
Copy link
Member

Terrtia commented Jan 2, 2024

Good catch !

Thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants