netboxlabs / netbox-branching

Official NetBox Labs plugin that implements git-like branching functionality for NetBox
http://netboxlabs.com
Other
69 stars 1 forks source link

Events outside the main branch should not trigger Event Rules #132

Open PaulR282 opened 2 months ago

PaulR282 commented 2 months ago

Plugin Version

v0.5.0

Proposed functionality

Events outside the main branch should not trigger Event Rules

Use case

A lot of network automation relies on event rules. If you plan e.g. a rebuild of a rack, where you update interfaces of a switch, you don't want those changes to be rolled out immediately. Only the changes on the main branch, like when merging a branch, should trigger the event rules.

External dependencies

No response

mrmrcoleman commented 2 months ago

Hey @PaulR282 we can't switch off Event Rules for branches because other users rely on webhooks from branches for their workflows.

One approach is to call the /api/plugins/branching/changes/ endpoint and check if the object is there. If it is, you know the change is on a branch, so you can ignore it for your use case.

def find_object_branch(object_id, model):
    changes_endpoint = f"{NETBOX_URL}/api/plugins/branching/changes/"
    params = {"object_id": object_id, "model": model}

    response = requests.get(changes_endpoint, headers=headers, params=params)

    if response.status_code == 200:
        changes = response.json().get("results", [])
        if not changes:
            return "main"  # Object was created or modified on the main branch
        else:
            for change in changes:
                branch = change.get("branch", {}).get("name", "main")
                return branch
    else:
        print(f"Error: {response.status_code} - {response.text}")
        return None
PaulR282 commented 2 months ago

Hi @mrmrcoleman. That would be one solution, thanks, I will try that. Alternatively, it would also be nice if you could configure whether branches trigger event rules or not.