Skip to content

Handle non 401/403 gitlab exceptions

For each of the benchmarks, it checks to see if the request failed or not, then returns a SKIP:

def code_approvals(glEntity, glObject, **kwargs):
    """
    id: 1.1.3
    title: Ensure any change to code receives approval of two strongly
           authenticated users
    """

    from gitlab.exceptions import GitlabGetError, GitlabHttpError
    from gitlab.exceptions import GitlabAuthenticationError

    try:
        for approval in glEntity.approvalrules.list(get_all=True):
            if approval.approvals_required >= 2:
                return {True: '2 approvals are required for code changes'}

        return {False: '2 approvals are required for code changes'}

    except (GitlabHttpError, GitlabGetError, GitlabAuthenticationError) as e:
        if e.response_code in [401, 403]:
            return {None: 'Insufficient permissions'}

But the exceptions should also handle when the response_code is not 401/403

We should instead do:

...

    except (GitlabHttpError, GitlabGetError, GitlabAuthenticationError) as e:
        if e.response_code in [401, 403]:
            return {None: 'Insufficient permissions'}
        else:
            return {None: f'Request failed code: {e.response_code}'}
Edited by Neil McDonald