summaryrefslogtreecommitdiff
path: root/tools/libgithub/field.py
blob: aabc70b2232b7534dae5537d41b1b16a2e7a0bcb (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import sys

from .option import Option
from .graphql import GraphQLClient
from logger import LOG

class Field:
    def __init__(self, id, name, options):
        self.id = id
        self.name = name
        self.options = options

    @staticmethod
    def get_status_field(project_id: str) -> 'Field':
        LOG.debug(f'Getting status field for project ID {project_id}')
        query = '''
        query ($projectId: ID!) {
            node(id: $projectId) {
                ... on ProjectV2 {
                    fields(first: 100) {
                        nodes {
                            ... on ProjectV2SingleSelectField {
                                name
                                id
                            }
                        }
                    }
                }
            }
        }
        '''

        variables = {
            "projectId": project_id
        }

        data = GraphQLClient.get_instance().make_request(query, variables)
        if data:
            fields = data['data']['node']['fields']['nodes']
            for field in fields:
                if 'name' in field and field['name'] == 'Status':
                    field_id = field['id']
                    LOG.info(f'Status Field ID: {field_id}')
                    return Field(
                        id=field_id, 
                        name='Status', 
                        options=Option.get_all_options(field_id)
                    )
        else:
            LOG.critica(f'No field found with name "Status"!')
            sys.exit(1)
            
    def create_option(self, option_name: str):
        LOG.debug(f'Creating option with name {option_name} for field {self.name}')
        query = '''
        mutation ($fieldId: ID!, $optionName: String!) {
            createProjectOption(input: {projectId: $fieldId, name: $optionName}) {
                projectOption {
                    id
                    name
                }
            }
        }
        '''
        variables = {
            "fieldId": self.id,
            "optionName": option_name
        }

        data = GraphQLClient.get_instance().make_request(query, variables)
        if data:
            option_id = data['data']['createProjectOption']['projectOption']['id']
            LOG.info(f'Created option with name {option_name} and ID {option_id}')
            return Option(option_id, option_name)
        else:
            LOG.warning(f'Could not create option with name {option_name}')
            return None
    
    # Finish later if we decide to add more fields other than the default Status field
    def create(self, project_id: str):
        pass

    # Finish later if we decide to add more fields other than the default Status field
    def delete(self):
        pass