Skip to content
GitLab
Explore
Projects
Groups
Snippets
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Memri
pymemri
Commits
794738ab
Commit
794738ab
authored
2 years ago
by
Aziz Berkay Yesilyurt
Browse files
Options
Download
Email Patches
Plain Diff
fix: gitlab sluggify logic
parent
4c6122d8
dev
aziz/tweet_schema
disable_cert_verify
eelco/export-schema-json
oauth1_simulator
sz/create_account_when_keys_are_present
v0.1.2
v0.1.0
v0.0.52
v0.0.51
v0.0.50
v0.0.48
v0.0.47
v0.0.46
v0.0.45
v0.0.44
v0.0.43
v0.0.42
v0.0.41
v0.0.40
v0.0.39
v0.0.38
v0.0.37
v0.0.36
v0.0.35
v0.0.34
v0.0.33
v0.0.32
v0.0.31
v0.0.30
1 merge request
!250
fix: gitlab sluggify logic
Pipeline
#10652
failed with stage
in 1 minute and 31 seconds
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
pymemri/gitlab_api.py
+22
-12
pymemri/gitlab_api.py
pymemri/pod/client.py
+8
-10
pymemri/pod/client.py
pymemri/template/formatter.py
+6
-5
pymemri/template/formatter.py
tests/test_gitlab.py
+48
-0
tests/test_gitlab.py
with
84 additions
and
27 deletions
+84
-27
pymemri/gitlab_api.py
+
22
-
12
View file @
794738ab
...
...
@@ -12,7 +12,7 @@ from loguru import logger
from
pymemri.pod.client
import
PodClient
from
.template.formatter
import
_plugin_from_template
,
str_to_gitlab_identifier
from
.template.formatter
import
_plugin_from_template
,
gitlab_slugify
MEMRI_PATH
=
Path
.
home
()
/
".memri"
MEMRI_GITLAB_BASE_URL
=
"https://gitlab.memri.io"
...
...
@@ -36,8 +36,8 @@ class GitlabAPI:
def
init_auth_params
(
self
):
access_token
=
self
.
client
.
get_oauth2_access_token
(
"gitlab"
)
if
access_token
is
None
:
raise
Valu
eError
(
"No gitlab access token found in the pod."
raise
Runtim
eError
(
"No gitlab access token found in the pod.
"
"Please authenticate with gitlab in the frontend."
)
self
.
auth_params
=
{
"access_token"
:
access_token
}
...
...
@@ -73,6 +73,14 @@ class GitlabAPI:
self
.
auth_headers
=
{
"PRIVATE-TOKEN"
:
access_token
}
self
.
auth_initialized
=
True
# test if we are authenticated
res
=
self
.
get
(
f
"
{
GITLAB_API_BASE_URL
}
/projects"
,
headers
=
self
.
auth_headers
,
params
=
self
.
auth_params
)
if
res
.
status_code
not
in
[
200
,
201
]:
logger
.
error
(
res
.
content
)
raise
RuntimeError
(
res
.
content
)
def
write_file_to_package_registry
(
self
,
project_id
,
...
...
@@ -132,23 +140,25 @@ class GitlabAPI:
logger
.
error
(
"Failed to trigger pipeline"
)
def
project_id_from_name
(
self
,
project_name
):
iden
=
str_to_gitlab_identifier
(
project_name
)
iden
=
gitlab_slugify
(
project_name
)
username
=
self
.
get_current_username
()
uri_encoded_name
=
urllib
.
parse
.
quote_plus
(
f
"
{
username
}
/
{
iden
}
"
)
res
=
self
.
get
(
f
"
{
GITLAB_API_BASE_URL
}
/projects"
,
f
"
{
GITLAB_API_BASE_URL
}
/projects
/
{
uri_encoded_name
}
"
,
headers
=
self
.
auth_headers
,
params
=
{
**
self
.
auth_params
,
**
{
"owned"
:
True
,
"search"
:
project_name
}},
params
=
self
.
auth_params
,
)
if
res
.
status_code
not
in
[
200
,
201
]:
logger
.
error
(
res
.
content
)
raise
RuntimeError
(
f
"Failed to get project id for
{
project_name
}
"
)
# we need this extra filter (search is not exact match)
res
=
[
x
.
get
(
"id"
)
for
x
in
res
.
json
()
if
x
.
get
(
"path"
,
None
)
==
iden
]
if
len
(
res
)
==
0
:
project_id
=
res
.
json
().
get
(
"id"
,
None
)
if
project_id
:
return
project_id
else
:
raise
ValueError
(
f
"No plugin found with name
{
project_name
}
, make sure to enter the name as specified in the url of the repo"
)
else
:
return
res
[
0
]
def
get_project_id_from_project_path_unsafe
(
self
,
project_path
):
try
:
...
...
@@ -214,7 +224,7 @@ class GitlabAPI:
# NEVER EXPORT THIS
def
delete_project
(
self
,
path_or_id
,
client
=
None
):
url_escape_id
=
urllib
.
parse
.
quote
(
path_or_id
,
safe
=
""
)
url_escape_id
=
urllib
.
parse
.
quote
(
str
(
path_or_id
)
,
safe
=
""
)
url
=
f
"
{
GITLAB_API_BASE_URL
}
/projects/
{
url_escape_id
}
"
res
=
self
.
delete
(
url
=
url
,
headers
=
self
.
auth_headers
,
params
=
self
.
auth_params
)
if
res
.
status_code
not
in
[
200
,
201
,
202
]:
...
...
This diff is collapsed.
Click to expand it.
pymemri/pod/client.py
+
8
-
10
View file @
794738ab
...
...
@@ -67,7 +67,9 @@ class PodClient:
return
""
.
join
([
str
(
random
.
randint
(
0
,
9
))
for
i
in
range
(
64
)])
def
register_base_schemas
(
self
):
assert
self
.
add_to_schema
(
PluginRun
,
Account
)
result
=
self
.
add_to_schema
(
PluginRun
,
Account
)
if
not
result
:
raise
ValueError
(
"Could not register base schemas"
)
def
add_to_store
(
self
,
item
:
Item
,
priority
:
Priority
=
None
)
->
Item
:
item
.
create_id_if_not_exists
()
...
...
@@ -109,15 +111,11 @@ class PodClient:
raise
ValueError
(
f
"
{
item
}
is not an instance or subclass of Item"
)
create_items
.
extend
(
item
.
pod_schema
())
try
:
self
.
api
.
bulk
(
create_items
=
create_items
)
for
item
in
items
:
item_cls
=
type
(
item
)
if
isinstance
(
item
,
Item
)
else
item
self
.
registered_classes
[
item
.
__name__
]
=
item_cls
return
True
except
Exception
as
e
:
logger
.
error
(
e
)
return
False
self
.
api
.
bulk
(
create_items
=
create_items
)
for
item
in
items
:
item_cls
=
type
(
item
)
if
isinstance
(
item
,
Item
)
else
item
self
.
registered_classes
[
item
.
__name__
]
=
item_cls
return
True
def
_upload_image
(
self
,
img
,
asyncFlag
=
True
,
callback
=
None
):
if
isinstance
(
img
,
np
.
ndarray
):
...
...
This diff is collapsed.
Click to expand it.
pymemri/template/formatter.py
+
6
-
5
View file @
794738ab
...
...
@@ -58,11 +58,12 @@ def str_to_identifier(s, lower=True):
return
result
def
str_to_gitlab_identifier
(
s
,
lower
=
True
):
result
=
re
.
sub
(
"\W|^(?=\d)"
,
"-"
,
s
)
if
lower
:
result
=
result
.
lower
()
return
result
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/utils.rb#L92
def
gitlab_slugify
(
s
):
s
=
s
.
lower
()
s
=
re
.
sub
(
r
"[^a-z0-9]"
,
"-"
,
s
)[:
63
]
s
=
re
.
sub
(
r
"(^-+|-+$)"
,
""
,
s
)
return
s
def
reponame_to_displayname
(
reponame
:
str
)
->
str
:
...
...
This diff is collapsed.
Click to expand it.
tests/test_gitlab.py
0 → 100644
+
48
-
0
View file @
794738ab
import
pytest
from
pymemri.gitlab_api
import
GitlabAPI
from
pymemri.pod.client
import
PodClient
from
pymemri.template.formatter
import
gitlab_slugify
def
test_gitlab_slugify
():
# Lowercased
string
=
"SAMPLE Project"
assert
gitlab_slugify
(
string
)
==
"sample-project"
# First/Last Character is not a hyphen
string
=
"--sample space project--"
assert
gitlab_slugify
(
string
)
==
"sample-space-project"
# Maximum length is 63 bytes
string
=
"s"
*
64
assert
gitlab_slugify
(
string
)
==
"s"
*
63
# Anything not matching [a-z0-9-] is replaced with a -
string
=
"sample project"
assert
gitlab_slugify
(
string
)
==
"sample-project"
string
=
"sample_project"
assert
gitlab_slugify
(
string
)
==
"sample-project"
string
=
"01-09-test"
assert
gitlab_slugify
(
string
)
==
"01-09-test"
def
test_gitlab_missing_oauth
():
# Create a random owner/database key pair
client
=
PodClient
()
with
pytest
.
raises
(
RuntimeError
):
gitlab
=
GitlabAPI
(
client
)
@
pytest
.
mark
.
skip
(
reason
=
"This test requires a valid Gitlab API token, which is not available in CI"
)
def
test_gitlab_project_id
():
owner_key
=
""
database_key
=
""
client
=
PodClient
(
owner_key
=
owner_key
,
database_key
=
database_key
)
gitlab
=
GitlabAPI
(
client
)
project_id
=
gitlab
.
project_id_from_name
(
"test-proj"
)
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Snippets