Skip to content
Snippets Groups Projects
Commit 1e083ddf authored by marcoemi.poleggi's avatar marcoemi.poleggi
Browse files

Added memberid filter to skip project bots

parent 18b580f3
No related branches found
No related tags found
No related merge requests found
...@@ -126,7 +126,7 @@ What do you mean? ;-) ...@@ -126,7 +126,7 @@ What do you mean? ;-)
* API requests that return a list of items do not support pagination yet -- * API requests that return a list of items do not support pagination yet --
max 100 items in one single page are returned. Command `userlist``is one such max 100 items in one single page are returned. Command `userlist``is one such
offender. offender.
* There's no configuration file. * There's no configuration file. TO-DO: use configparse for this.
Notes Notes
...@@ -265,13 +265,14 @@ def handle_command(command, g, api_url, src_project, dst_path, ...@@ -265,13 +265,14 @@ def handle_command(command, g, api_url, src_project, dst_path,
(api_url_endpoint, data) = pre_cmnd_ref['handler']( (api_url_endpoint, data) = pre_cmnd_ref['handler'](
pre_command, g, api_url, src_project, dst_path, users pre_command, g, api_url, src_project, dst_path, users
) )
skip_dryrun = command_xref[pre_command]['skip_dryrun'] if command_xref[pre_command]['skip_dryrun'] else false;
(pstatus, pdata) = send_rest_request( (pstatus, pdata) = send_rest_request(
pre_cmnd_ref['method'], pre_cmnd_ref['method'],
api_url_endpoint, api_url_endpoint,
json.dumps(data) if data else None, json.dumps(data) if data else None,
dry_run dry_run and not skip_dryrun
) )
if dry_run: if dry_run and not skip_dryrun:
logger.info("[DRY RUN] {}: Using fake results as loop data".format(pre_command)) logger.info("[DRY RUN] {}: Using fake results as loop data".format(pre_command))
pdata = command_xref[pre_command]['fake_results'] pdata = command_xref[pre_command]['fake_results']
else: else:
...@@ -541,8 +542,6 @@ def _memberid(command, g, api_url, src_project, dst_path, users=None, jdata=None ...@@ -541,8 +542,6 @@ def _memberid(command, g, api_url, src_project, dst_path, users=None, jdata=None
"""Handle 'userdel' command calls as per `GitLab project """Handle 'userdel' command calls as per `GitLab project
mem API <https://docs.gitlab.com/ee/api/invitations.html>`_ mem API <https://docs.gitlab.com/ee/api/invitations.html>`_
Call :py:func: `_userlist` to get the members' ID and
:returns tuple: 2 elements :returns tuple: 2 elements
:tuple member_id, member_name: extracted from :param jdata: or None if :tuple member_id, member_name: extracted from :param jdata: or None if
filtered out filtered out
...@@ -556,12 +555,24 @@ def _memberid(command, g, api_url, src_project, dst_path, users=None, jdata=None ...@@ -556,12 +555,24 @@ def _memberid(command, g, api_url, src_project, dst_path, users=None, jdata=None
:param obj jdata: JSON data to process :param obj jdata: JSON data to process
the remaining ones are ignored. the remaining ones are ignored.
Filters
*******
So far, two filters (AND-wise) are supported:
:int access_level: must be > than given
:str username: regex must match username -- usually a negative
lookahead to skip certain members, like bots, etc.
""" """
cmnd_ref = command_xref[command] cmnd_ref = command_xref[command]
member_id = jdata['id'] member_id = jdata['id']
member_name = jdata['username'] member_name = jdata['username']
max_access_level = cmnd_ref['filter']['access_level'] max_access_level = cmnd_ref['filter']['access_level']
keep = True if jdata['access_level'] <= max_access_level else False uname_regex = cmnd_ref['filter']['username']
keep = True \
if ( jdata['access_level'] <= max_access_level and re.match(uname_regex, jdata['username']) ) else False
logger.debug("Member #{} ({}) is filtered {}".format( logger.debug("Member #{} ({}) is filtered {}".format(
member_id, member_name, '_in_' if keep else '_out_') member_id, member_name, '_in_' if keep else '_out_')
...@@ -679,6 +690,7 @@ command_xref = { ...@@ -679,6 +690,7 @@ command_xref = {
'api_rendpoint' : 'members/all?per_page=100', # Warning! No pagination 'api_rendpoint' : 'members/all?per_page=100', # Warning! No pagination
# handled. # handled.
'force' : False, 'force' : False,
'skip_dryrun' : True, # this is always safe to run
'fake_results' : [ # for dry-run/debugging purpose 'fake_results' : [ # for dry-run/debugging purpose
{ {
"id": 0, "id": 0,
...@@ -722,8 +734,10 @@ command_xref = { ...@@ -722,8 +734,10 @@ command_xref = {
# ignored. This is supposed to be found in # ignored. This is supposed to be found in
# some input data, usually coming from a # some input data, usually coming from a
# pre-handler call # pre-handler call
'username' : '(?!project.+bot)' # negative lookahead regex: skip "bot" members
}, },
'force' : False, 'force' : False,
'skip_dryrun' : True,
}, },
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment