summaryrefslogtreecommitdiff
path: root/buildscripts/client/jiraclient.py
blob: 6251d043e25c130dc1e2d57ec2b880d44150f885 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""Module to access a JIRA server."""
from enum import Enum

import jira
from pydantic import BaseSettings


class SecurityLevel(Enum):
    """Security level of SERVER tickets."""

    MONGO_INTERNAL = "Mongo Internal"
    NONE = "None"


class JiraAuth(BaseSettings):
    """OAuth information to connect to Jira."""

    access_token: str
    access_token_secret: str
    consumer_key: str
    key_cert: str

    class Config:
        """Configuration for JiraAuth."""

        env_prefix = "JIRA_AUTH_"


class JiraClient(object):
    """A client for JIRA."""

    def __init__(self, server: str, jira_auth: JiraAuth) -> None:
        """
        Initialize the JiraClient with the server URL and user credentials.

        :param server: Jira Server to connect to.
        :param jira_auth: OAuth connection information.
        """
        opts = {"server": server, "verify": True}
        self._jira = jira.JIRA(options=opts, oauth=jira_auth.dict(), validate=True)

    def get_ticket_security_level(self, key: str) -> SecurityLevel:
        """
        Lookup the security level of the given ticket.

        :param key: Key of ticket to query.
        :return: Security level of the given ticket.
        """
        ticket = self._jira.issue(key)
        if hasattr(ticket.fields, "security"):
            security_level = ticket.fields.security
            return SecurityLevel(security_level.name)
        return SecurityLevel.NONE