# Buggy code
def main():
    module = AnsibleModule(
        argument_spec=dict(
            organization=dict(required=True),
            repo=dict(required=True),
            issue=dict(required=True),
            action=dict(required=False, choices=["get_status"]),
        ),
        supports_check_mode=True,
    )

    if not HAS_GITHUB_PACKAGE:
        module.fail_json(
            msg=missing_required_lib("github3.py >= 1.0.0a4"), exception=GITHUB_IMP_ERR
        )

    organization = module.params["organization"]
    repo = module.params["repo"]
    issue = module.params["issue"]
    action = module.params["action"]

    result = dict()

    gh_obj = github3.issue(organization, repo, issue)
    if gh_obj is None:
        module.fail_json(
            msg="Failed to get details about issue specified. "
            "Please check organization, repo and issue "
            "details and try again."
        )

    if action == "get_status" or action is None:
        if module.check_mode:
            result.update(changed=True)
        else:
            result.update(changed=True, issue_status=gh_obj.state)

    module.exit_json(**result)


# Traceback (github3.exceptions.NotFoundError)
TASK [github_issue : Check if GitHub issue is closed or not] *******************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: github3.exceptions.NotFoundError: 404 Not Found
fatal: [testhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_htCo77/ansible_module_github_issue.py\", line 117, in <module>\n    main()\n  File \"/tmp/ansible_htCo77/ansible_module_github_issue.py\", line 101, in main\n    gh_obj = github3.issue(organization, repo, issue)\n  File \"/usr/local/lib/python2.7/dist-packages/github3/api.py\", line 376, in issue\n    return gh.issue(owner, repository, number)\n  File \"/usr/local/lib/python2.7/dist-packages/github3/github.py\", line 774, in issue\n    json = self._json(self._get(url), 200)\n  File \"/usr/local/lib/python2.7/dist-packages/github3/models.py\", line 156, in _json\n    raise exceptions.error_for(response)\ngithub3.exceptions.NotFoundError: 404 Not Found\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 1}
...ignoring

TASK [github_issue : assert] ***************************************************
fatal: [testhost]: FAILED! => {
"assertion": false,
"changed": false,
"evaluated_to": false
}

# Fixed code
def main():
    module = AnsibleModule(
        argument_spec=dict(
            organization=dict(required=True),
            repo=dict(required=True),
            issue=dict(type="int", required=True),
            action=dict(choices=["get_status"], default="get_status"),
        ),
        supports_check_mode=True,
    )

    organization = module.params["organization"]
    repo = module.params["repo"]
    issue = module.params["issue"]
    action = module.params["action"]

    result = dict()

    headers = {
        "Content-Type": "application/json",
        "Accept": "application/vnd.github.v3+json",
    }

    url = "https://api.github.com/repos/%s/%s/issues/%s" % (organization, repo, issue)

    response, info = fetch_url(module, url, headers=headers)
    if not (200 <= info["status"] < 400):
        if info["status"] == 404:
            module.fail_json(msg="Failed to find issue %s" % issue)
        module.fail_json(msg="Failed to send request to %s: %s" % (url, info["msg"]))

    gh_obj = json.loads(response.read())

    if action == "get_status" or action is None:
        if module.check_mode:
            result.update(changed=True)
        else:
            result.update(changed=True, issue_status=gh_obj["state"])

    module.exit_json(**result)