# Buggy code
def check(
    module,
    name,
    state,
    service_id,
    integration_key,
    api_key,
    incident_key=None,
    http_call=fetch_url,
):
    url = "https://api.pagerduty.com/incidents"
    headers = {
        "Content-type": "application/json",
        "Authorization": "Token token=%s" % api_key,
        "Accept": "application/vnd.pagerduty+json;version=2",
    }

    params = {
        "service_ids[]": service_id,
        "sort_by": "incident_number:desc",
        "time_zone": "UTC",
    }
    if incident_key:
        params["incident_key"] = incident_key

    url_parts = list(urlparse(url))
    url_parts[4] = urlencode(params, True)

    url = urlunparse(url_parts)

    response, info = http_call(module, url, method="get", headers=headers)

    if info["status"] != 200:
        module.fail_json(
            msg="failed to check current incident status.Reason: %s" % info["msg"]
        )
    json_out = json.loads(response.read())["incidents"][0]

    if state != json_out["status"]:
        return json_out, True
    return json_out, False


# Traceback (IndexError)
task path: /home/oceyral/dev/COMPANY/PROJECT/roles/audit/tasks/user.yml:161
The full traceback is:
Traceback (most recent call last):
File "<stdin>", line 113, in <module>
File "<stdin>", line 105, in _ansiballz_main
File "<stdin>", line 48, in invoke_module
File "/tmp/ansible_pagerduty_alert_payload_BaeLhM/__main__.py", line 248, in <module>
File "/tmp/ansible_pagerduty_alert_payload_BaeLhM/__main__.py", line 238, in main
File "/tmp/ansible_pagerduty_alert_payload_BaeLhM/__main__.py", line 156, in check
IndexError: list index out of range

fatal: [MACHINE_NAME]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"<stdin>\", line 113, in <module>\n  File \"<stdin>\", line 105, in _ansiballz_main\n  File \"<stdin>\", line 48, in invoke_module\n  File \"/tmp/ansible_pagerduty_alert_payload_BaeLhM/__main__.py\", line 248, in <module>\n  File \"/tmp/ansible_pagerduty_alert_payload_BaeLhM/__main__.py\", line 238, in main\n  File \"/tmp/ansible_pagerduty_alert_payload_BaeLhM/__main__.py\", line 156, in check\nIndexError: list index out of range\n",
"module_stdout": "",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 1
}

# Fixed code
def check(
    module,
    name,
    state,
    service_id,
    integration_key,
    api_key,
    incident_key=None,
    http_call=fetch_url,
):
    url = "https://api.pagerduty.com/incidents"
    headers = {
        "Content-type": "application/json",
        "Authorization": "Token token=%s" % api_key,
        "Accept": "application/vnd.pagerduty+json;version=2",
    }

    params = {
        "service_ids[]": service_id,
        "sort_by": "incident_number:desc",
        "time_zone": "UTC",
    }
    if incident_key:
        params["incident_key"] = incident_key

    url_parts = list(urlparse(url))
    url_parts[4] = urlencode(params, True)

    url = urlunparse(url_parts)

    response, info = http_call(module, url, method="get", headers=headers)

    if info["status"] != 200:
        module.fail_json(
            msg="failed to check current incident status.Reason: %s" % info["msg"]
        )

    incidents = json.loads(response.read())["incidents"]
    msg = "No corresponding incident"

    if len(incidents) == 0:
        if state in ("acknowledged", "resolved"):
            return msg, False
        return msg, True
    elif state != incidents[0]["status"]:
        return incidents[0], True

    return incidents[0], False