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

bug: Error when syncing git repositories due to unbound branch variable #1389

Closed
ogenstad opened this issue Nov 10, 2023 · 2 comments
Closed
Labels
group/backend Issue related to the backend (API Server, Git Agent) type/bug Something isn't working as expected
Milestone

Comments

@ogenstad
Copy link
Contributor

Component

Git Integration

Current Behavior

When syncing a repository the process can fail due to a branch variable being unbound.

2023-11-09 17:57:47 Traceback (most recent call last):
2023-11-09 17:57:47   File "/usr/local/bin/infrahub", line 6, in <module>
2023-11-09 17:57:47     sys.exit(app())
2023-11-09 17:57:47              ^^^^^
2023-11-09 17:57:47   File "/usr/local/lib/python3.11/site-packages/typer/main.py", line 328, in __call__
2023-11-09 17:57:47     raise e
2023-11-09 17:57:47   File "/usr/local/lib/python3.11/site-packages/typer/main.py", line 311, in __call__
2023-11-09 17:57:47     return get_command(self)(*args, **kwargs)
2023-11-09 17:57:47            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-11-09 17:57:47   File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1157, in __call__
2023-11-09 17:57:47     return self.main(*args, **kwargs)
2023-11-09 17:57:47            ^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-11-09 17:57:47   File "/usr/local/lib/python3.11/site-packages/typer/core.py", line 778, in main
2023-11-09 17:57:47     return _main(
2023-11-09 17:57:47            ^^^^^^
2023-11-09 17:57:47   File "/usr/local/lib/python3.11/site-packages/typer/core.py", line 216, in _main
2023-11-09 17:57:47     rv = self.invoke(ctx)
2023-11-09 17:57:47          ^^^^^^^^^^^^^^^^
2023-11-09 17:57:47   File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1688, in invoke
2023-11-09 17:57:47     return _process_result(sub_ctx.command.invoke(sub_ctx))
2023-11-09 17:57:47                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-11-09 17:57:47   File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1688, in invoke
2023-11-09 17:57:47     return _process_result(sub_ctx.command.invoke(sub_ctx))
2023-11-09 17:57:47                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-11-09 17:57:47   File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1434, in invoke
2023-11-09 17:57:47     return ctx.invoke(self.callback, **ctx.params)
2023-11-09 17:57:47            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-11-09 17:57:47   File "/usr/local/lib/python3.11/site-packages/click/core.py", line 783, in invoke
2023-11-09 17:57:47     return __callback(*args, **kwargs)
2023-11-09 17:57:47            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-11-09 17:57:47   File "/usr/local/lib/python3.11/site-packages/typer/main.py", line 683, in wrapper
2023-11-09 17:57:47     return callback(**use_params)  # type: ignore
2023-11-09 17:57:47            ^^^^^^^^^^^^^^^^^^^^^^
2023-11-09 17:57:47   File "/source/backend/infrahub/cli/git_agent.py", line 162, in start
2023-11-09 17:57:47     aiorun(_start(interval=interval, debug=debug, port=port))
2023-11-09 17:57:47   File "/usr/local/lib/python3.11/asyncio/runners.py", line 190, in run
2023-11-09 17:57:47     return runner.run(main)
2023-11-09 17:57:47            ^^^^^^^^^^^^^^^^
2023-11-09 17:57:47   File "/usr/local/lib/python3.11/asyncio/runners.py", line 118, in run
2023-11-09 17:57:47     return self._loop.run_until_complete(task)
2023-11-09 17:57:47            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-11-09 17:57:47   File "/usr/local/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
2023-11-09 17:57:47     return future.result()
2023-11-09 17:57:47            ^^^^^^^^^^^^^^^
2023-11-09 17:57:47   File "/source/backend/infrahub/cli/git_agent.py", line 131, in _start
2023-11-09 17:57:47     await initialize_git_agent(client=client)
2023-11-09 17:57:47   File "/source/backend/infrahub/cli/git_agent.py", line 101, in initialize_git_agent
2023-11-09 17:57:47     await sync_remote_repositories(client=client)
2023-11-09 17:57:47   File "/source/backend/infrahub/git/actions.py", line 38, in sync_remote_repositories
2023-11-09 17:57:47     await repo.sync()
2023-11-09 17:57:47   File "/source/backend/infrahub/git/repository.py", line 848, in sync
2023-11-09 17:57:47     await self.create_branch_in_git(branch_name=branch.name, branch_id=branch.id)
2023-11-09 17:57:47                                                 ^^^^^^
2023-11-09 17:57:47 UnboundLocalError: cannot access local variable 'branch' where it is not associated with a value

The problem comes from a method that looks like this:

    async def sync(self):
        """Synchronize the repository with its remote origin and with the database.

        By default the sync will focus only on the branches pulled from origin that have some differences with the local one.
        """

        LOGGER.info(f"{self.name} | Starting the synchronization.")

        await self.fetch()

        new_branches, updated_branches = await self.compare_local_remote()

        if not new_branches and not updated_branches:
            return True

        LOGGER.debug(f"{self.name} | New Branches {new_branches}, Updated Branches {updated_branches} ")

        # TODO need to handle properly the situation when a branch is not valid.
        for branch_name in new_branches:
            is_valid = await self.validate_remote_branch(branch_name=branch_name)
            if not is_valid:
                continue

            try:
                branch = await self.create_branch_in_graph(branch_name=branch_name)
            except GraphQLError as exc:
                if "already exist" not in exc.errors[0]["message"]:
                    raise

            await self.create_branch_in_git(branch_name=branch.name, branch_id=branch.id)

When running self.create_branch_in_graph() we catch a GraphQLError but then continue if the branch already exists we continue to the next line for self.create_branch_in_git() in this case the branch variable is unbound and doesn't exist so the sync fails.

Expected Behavior

No response

Steps to Reproduce

Bug found by @fooelisa, probably a branch that existed in the external repo already existed in Infrahub.

Additional Information

Some things to note is that Pylance warns for this in VS Code, we should check to see if Ruff has a rule to find things like this.

Screenshot 2023-11-10 at 10 09 30

@ogenstad ogenstad added type/bug Something isn't working as expected group/backend Issue related to the backend (API Server, Git Agent) labels Nov 10, 2023
@BeArchiTek
Copy link
Contributor

Rudd doesn’t seem to report unbound at the moment astral-sh/ruff#2493

@ogenstad ogenstad added this to the Alpha #2.1 milestone Nov 10, 2023
@ogenstad
Copy link
Contributor Author

@BeArchiTek, ok thanks for checking! We'll look for something else. I think it would be nice to have that.

Closing this issue, the bug was fixed in #1390.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
group/backend Issue related to the backend (API Server, Git Agent) type/bug Something isn't working as expected
Projects
None yet
Development

No branches or pull requests

2 participants