Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2ca7ddb

Browse files
fix: use the [] after key names for array variables
1. If a value is of type ArrayAttribute then append '[]' to the name of the value.This is step 3 in a series of steps of our goal to add fullsupport for the GitLab API data types[1]: * array * hash * array of hashesStep one was: commit5127b15Step two was: commita57334fFixes:#1698[1]https://docs.gitlab.com/ee/api/#encoding-api-parameters-of-array-and-hash-types
1 parent1ecbc7c commit2ca7ddb

File tree

5 files changed

+63
-25
lines changed

5 files changed

+63
-25
lines changed

‎gitlab/types.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ def get(self) -> Any:
6363
defset_from_cli(self,cli_value:Any)->None:
6464
self._value=cli_value
6565

66-
defget_for_api(self)->Any:
67-
returnself._value
66+
defget_for_api(self,*,key:str)->Tuple[str,Any]:
67+
return(key,self._value)
6868

6969

7070
class_ListArrayAttribute(GitlabAttribute):
@@ -76,20 +76,28 @@ def set_from_cli(self, cli_value: str) -> None:
7676
else:
7777
self._value= [item.strip()foritemincli_value.split(",")]
7878

79-
defget_for_api(self)->str:
79+
defget_for_api(self,*,key:str)->Tuple[str,str]:
8080
# Do not comma-split single value passed as string
8181
ifisinstance(self._value,str):
82-
returnself._value
82+
return(key,self._value)
8383

8484
ifTYPE_CHECKING:
8585
assertisinstance(self._value,list)
86-
return",".join([str(x)forxinself._value])
86+
return(key,",".join([str(x)forxinself._value]))
8787

8888

8989
classArrayAttribute(_ListArrayAttribute):
9090
"""To support `array` types as documented in
9191
https://docs.gitlab.com/ee/api/#array"""
9292

93+
defget_for_api(self,*,key:str)->Tuple[str,Any]:
94+
ifisinstance(self._value,str):
95+
return (f"{key}[]",self._value)
96+
97+
ifTYPE_CHECKING:
98+
assertisinstance(self._value,list)
99+
return (f"{key}[]",self._value)
100+
93101

94102
classCommaSeparatedListAttribute(_ListArrayAttribute):
95103
"""For values which are sent to the server as a Comma Separated Values
@@ -98,8 +106,8 @@ class CommaSeparatedListAttribute(_ListArrayAttribute):
98106

99107

100108
classLowercaseStringAttribute(GitlabAttribute):
101-
defget_for_api(self)->str:
102-
returnstr(self._value).lower()
109+
defget_for_api(self,*,key:str)->Tuple[str,str]:
110+
return(key,str(self._value).lower())
103111

104112

105113
classFileAttribute(GitlabAttribute):

‎gitlab/utils.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,22 @@ def _transform_types(
7171
data=data.copy()
7272
files= {}
7373

74-
forattr_name,type_clsincustom_types.items():
74+
forattr_name,attribute_classincustom_types.items():
7575
ifattr_namenotindata:
7676
continue
7777

78-
type_obj=type_cls(data[attr_name])
79-
80-
# if the type if FileAttribute we need to pass the data as file
81-
iftransform_filesandisinstance(type_obj,types.FileAttribute):
82-
key=type_obj.get_file_name(attr_name)
83-
files[attr_name]= (key,data.pop(attr_name))
84-
else:
85-
data[attr_name]=type_obj.get_for_api()
78+
gitlab_attribute=attribute_class(data[attr_name])
79+
ifisinstance(gitlab_attribute,types.GitlabAttribute):
80+
# if the type is FileAttribute we need to pass the data as file
81+
ifisinstance(gitlab_attribute,types.FileAttribute):
82+
iftransform_files:
83+
key=gitlab_attribute.get_file_name(attr_name)
84+
files[attr_name]= (key,data.pop(attr_name))
85+
continue
86+
key,value=gitlab_attribute.get_for_api(key=attr_name)
87+
ifkey!=attr_name:
88+
deldata[attr_name]
89+
data[key]=value
8690

8791
returndata,files
8892

@@ -94,6 +98,8 @@ def copy_dict(
9498
)->None:
9599
fork,vinsrc.items():
96100
ifisinstance(v,dict):
101+
# NOTE(jlvillal): This provides some support for the `hash` type
102+
# https://docs.gitlab.com/ee/api/#hash
97103
# Transform dict values to new attributes. For example:
98104
# custom_attributes: {'foo', 'bar'} =>
99105
# "custom_attributes['foo']": "bar"

‎tests/functional/api/test_groups.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ def test_groups(gl):
9999
assertlen(group1.members.list())==3
100100
assertlen(group2.members.list())==2
101101

102+
# Test `user_ids` array
103+
result=group1.members.list(user_ids=[user.id,99999])
104+
assertlen(result)==1
105+
assertresult[0].id==user.id
106+
102107
group1.members.delete(user.id)
103108
assertusernotingroup1.members.list()
104109
assertgroup1.members_all.list()

‎tests/unit/mixins/test_mixin_methods.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,25 @@ class M(ListMixin, FakeManager):
205205
assertresponses.assert_call_count(url,2)isTrue
206206

207207

208+
@responses.activate
209+
deftest_list_mixin_with_attributes(gl):
210+
classM(ListMixin,FakeManager):
211+
_types= {"my_array":gl_types.ArrayAttribute}
212+
213+
url="http://localhost/api/v4/tests?my_array%5B%5D=1&my_array%5B%5D=2&my_array%5B%5D=3"
214+
responses.add(
215+
method=responses.GET,
216+
headers={},
217+
url=url,
218+
json=[],
219+
status=200,
220+
match=[responses.matchers.query_param_matcher({"my_array[]": ["1","2","3"]})],
221+
)
222+
223+
mgr=M(gl)
224+
mgr.list(iterator=True,my_array=[1,2,3])
225+
226+
208227
@responses.activate
209228
deftest_list_other_url(gl):
210229
classM(ListMixin,FakeManager):

‎tests/unit/test_types.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_gitlab_attribute_get():
7373

7474
o.set_from_cli("whatever2")
7575
asserto.get()=="whatever2"
76-
asserto.get_for_api()=="whatever2"
76+
asserto.get_for_api(key="spam")==("spam","whatever2")
7777

7878
o=types.GitlabAttribute()
7979
asserto._valueisNone
@@ -100,42 +100,42 @@ def test_array_attribute_empty_input():
100100
deftest_array_attribute_get_for_api_from_cli():
101101
o=types.ArrayAttribute()
102102
o.set_from_cli("foo,bar,baz")
103-
asserto.get_for_api()=="foo,bar,baz"
103+
asserto.get_for_api(key="spam")==("spam[]", ["foo","bar","baz"])
104104

105105

106106
deftest_array_attribute_get_for_api_from_list():
107107
o=types.ArrayAttribute(["foo","bar","baz"])
108-
asserto.get_for_api()=="foo,bar,baz"
108+
asserto.get_for_api(key="spam")==("spam[]", ["foo","bar","baz"])
109109

110110

111111
deftest_array_attribute_get_for_api_from_int_list():
112112
o=types.ArrayAttribute([1,9,7])
113-
asserto.get_for_api()=="1,9,7"
113+
asserto.get_for_api(key="spam")==("spam[]", [1,9,7])
114114

115115

116116
deftest_array_attribute_does_not_split_string():
117117
o=types.ArrayAttribute("foo")
118-
asserto.get_for_api()=="foo"
118+
asserto.get_for_api(key="spam")==("spam[]","foo")
119119

120120

121121
# CommaSeparatedListAttribute tests
122122
deftest_csv_string_attribute_get_for_api_from_cli():
123123
o=types.CommaSeparatedListAttribute()
124124
o.set_from_cli("foo,bar,baz")
125-
asserto.get_for_api()=="foo,bar,baz"
125+
asserto.get_for_api(key="spam")==("spam","foo,bar,baz")
126126

127127

128128
deftest_csv_string_attribute_get_for_api_from_list():
129129
o=types.CommaSeparatedListAttribute(["foo","bar","baz"])
130-
asserto.get_for_api()=="foo,bar,baz"
130+
asserto.get_for_api(key="spam")==("spam","foo,bar,baz")
131131

132132

133133
deftest_csv_string_attribute_get_for_api_from_int_list():
134134
o=types.CommaSeparatedListAttribute([1,9,7])
135-
asserto.get_for_api()=="1,9,7"
135+
asserto.get_for_api(key="spam")==("spam","1,9,7")
136136

137137

138138
# LowercaseStringAttribute tests
139139
deftest_lowercase_string_attribute_get_for_api():
140140
o=types.LowercaseStringAttribute("FOO")
141-
asserto.get_for_api()=="foo"
141+
asserto.get_for_api(key="spam")==("spam","foo")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp