File: banner.py

package info (click to toggle)
azure-devops-cli-extension 1.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,384 kB
  • sloc: python: 160,782; xml: 198; makefile: 56; sh: 51
file content (132 lines) | stat: -rw-r--r-- 5,892 bytes parent folder | download | duplicates (4)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from azext_devops.dev.common.arguments import convert_date_string_to_iso8601
from .setting import setting_add_or_update, setting_list, setting_remove, GLOBAL_MESSAGE_BANNERS_KEY, USER_SCOPE_HOST


def banner_list(organization=None, detect=None):
    """List banners.
    """
    return setting_list(user_scope='host', key=GLOBAL_MESSAGE_BANNERS_KEY,
                        organization=organization, detect=detect)


def banner_show(id, organization=None, detect=None):  # pylint: disable=redefined-builtin
    """Show details for a banner.
    :param id: Identifier for the banner.
    :type id: str
    :rtype: [object]
    """
    existing_entries = setting_list(user_scope='host', key=GLOBAL_MESSAGE_BANNERS_KEY,
                                    organization=organization, detect=detect)
    if id not in existing_entries:
        raise ValueError('The following banner was not found: %s' % id)
    return {id: existing_entries[id]}


def banner_add(message, banner_type=None, id=None, expiration=None, organization=None, detect=None):  # pylint: disable=redefined-builtin
    """Add a new banner and immediately show it.
    :param message: Message (string) to show in the banner.
    :type message: str
    :param banner_type: Type of banner to present. Defaults is "info".
    :type banner_type: str
    :param id: Identifier for the new banner. This identifier is needed to change or remove the message later. A
                       unique identifier is automatically created if one is not specified.
    :type id: str
    :param expiration: Date/time when the banner should no longer be presented to users. If not set, the banner does not
                       automatically expire and must be removed with the remove command.
                       Example : "2019-06-10 17:21:00 UTC", "2019-06-10"
    :type expiration: date
    :rtype: [object]
    """
    if expiration is not None:
        expiration_iso8601 = convert_date_string_to_iso8601(value=expiration, argument='expiration')
    else:
        expiration_iso8601 = None
    if id is None or id == '':
        import uuid
        id = str(uuid.uuid4())
    setting_key = _get_banner_key(id)
    entries = {
        setting_key: {
            "message": message
        }
    }
    if banner_type is not None:
        entries[setting_key]['level'] = banner_type
    if expiration_iso8601 is not None:
        entries[setting_key]['expirationDate'] = expiration_iso8601
    setting_add_or_update(entries=entries, user_scope=USER_SCOPE_HOST,
                          organization=organization, detect=detect)
    return {id: entries[setting_key]}


def banner_update(id, message=None, banner_type=None, expiration=None, organization=None, detect=None):  # pylint: disable=redefined-builtin
    """Update the message, level, or expiration date for a banner.
    :param message: Message (string) to show in the banner.
    :type message: str
    :param banner_type: Type of banner to present. Defaults is "info".
    :type banner_type: str
    :param id: ID of the banner to update.
    :type id: str
    :param expiration: Date/time when the banner should no longer be presented to users. To unset the expiration for the
                       banner, supply an empty value to this argument.
                       Example : "2019-06-10 17:21:00 UTC", "2019-06-10"
    :type expiration: date
    :rtype: [object]
    """
    if message is None and banner_type is None and expiration is None:
        raise ValueError('At least one of the following arguments need to be supplied: --message, --type, '
                         '--expiration.')
    if expiration is not None and expiration != '':
        expiration_iso8601 = convert_date_string_to_iso8601(value=expiration, argument='expiration')
    else:
        expiration_iso8601 = expiration
    existing_entries = setting_list(user_scope='host',
                                    key=GLOBAL_MESSAGE_BANNERS_KEY,
                                    organization=organization,
                                    detect=detect)
    if id not in existing_entries:
        raise ValueError('The following banner was not found: %s' % id)
    existing_entry = existing_entries[id]
    setting_key = _get_banner_key(id)
    entries = {
        setting_key: {
            "message": message
        }
    }
    if message is not None:
        entries[setting_key]['message'] = message
    elif 'message' in existing_entry:
        entries[setting_key]['message'] = existing_entry['message']

    if banner_type is not None:
        entries[setting_key]['level'] = banner_type
    elif 'level' in existing_entry:
        entries[setting_key]['level'] = existing_entry['level']

    if expiration_iso8601 is not None:
        entries[setting_key]['expirationDate'] = expiration_iso8601
    elif 'expirationDate' in existing_entry:
        entries[setting_key]['expirationDate'] = existing_entry['expirationDate']

    setting_add_or_update(entries=entries, user_scope=USER_SCOPE_HOST, organization=organization,
                          detect=detect)
    return {id: entries[setting_key]}


def banner_remove(id, organization=None, detect=None):  # pylint: disable=redefined-builtin
    """Remove a banner.
    :param id: ID of the banner to remove.
    :type id: str
    :rtype: [object]
    """
    setting_key = _get_banner_key(id)
    setting_remove(key=setting_key, user_scope='host', organization=organization, detect=detect)


def _get_banner_key(message_id):
    return GLOBAL_MESSAGE_BANNERS_KEY + '/' + message_id