Skip to content

Instantly share code, notes, and snippets.

@SeyfSV
Last active September 20, 2020 19:56
Show Gist options
  • Save SeyfSV/8934aef482f32054dd1ffb3fceb8bfe6 to your computer and use it in GitHub Desktop.
Save SeyfSV/8934aef482f32054dd1ffb3fceb8bfe6 to your computer and use it in GitHub Desktop.
Export and import group and project environment variables from GitLab
''' Export GitLab variables to .propoerties file
Usage:
gitlab-export-env.py <CONFIG_ID> <OBJECT_ID> [<FILE_NAME>] [<OBJECT_TYPE>]
CONFIG_ID - configuration id from python-gitlab
config file (https://python-gitlab.readthedocs.io/en/stable/cli.html#cli-configuration)
OBJECT_ID - id of project or group or project name
FILE_NAME (optional) - .properties file for export
OBJECT_TYPE (optional) [project|group] - type of environment variables, project is default.
Any other value interpreted as group
'''
import sys
import gitlab
from configparser import ConfigParser, NoSectionError
CONFIG_ID = None
if len(sys.argv) > 1:
CONFIG_ID = sys.argv[1]
else:
sys.exit('Please, provide config id')
OBJECT_ID = None
if len(sys.argv) > 2:
try:
OBJECT_ID = int(sys.argv[2])
except:
if sys.argv[2]:
OBJECT_ID = sys.argv[2]
else:
sys.exit('Please, group/project id should be `int` or project name')
else:
sys.exit('Please, provide group/project id')
FILE_NAME = None
if len(sys.argv) > 3:
FILE_NAME = sys.argv[3]
else:
sys.exit('Please, provide file name')
OBJECT_TYPE = 'project'
if len(sys.argv) > 4:
if sys.argv[4] != OBJECT_TYPE:
OBJECT_TYPE = 'group'
if __name__ == '__main__':
config = ConfigParser()
config.optionxform = lambda option: option
gl = gitlab.Gitlab.from_config(CONFIG_ID)
list_kwargs = {}
if OBJECT_TYPE == 'project':
gitlab_object_class = gl.projects
list_kwargs = {'all':True}
else:
gitlab_object_class = gl.groups
if isinstance(OBJECT_ID, int):
gitlab_object = gitlab_object_class.get(OBJECT_ID)
else:
gitlab_objects = gitlab_object_class.list(**{'query_parameters':{'search':OBJECT_ID}})
if len(gitlab_objects) != 1:
sys.exit('Found {} {}(s)'.format(len(gitlab_objects), OBJECT_TYPE))
else:
gitlab_object = gitlab_objects[0]
variables = gitlab_object.variables.list(**list_kwargs)
for variable in variables:
if variable.masked:
_value = '********'
else:
_value = variable.value
if OBJECT_TYPE == 'project':
_scope = variable.environment_scope
else:
_scope = '*'
print(_scope, variable.key, _value, variable.protected, variable.masked)
try:
config.set(_scope, variable.key, variable.value)
except NoSectionError as ex:
config.add_section(_scope)
config.set(_scope, variable.key, variable.value)
if FILE_NAME:
with open(FILE_NAME, 'w') as configfile:
config.write(configfile)
else:
print(config)
''' Import GitLab variables to .propoerties file
Usage:
gitlab-import-env.py <CONFIG_ID> <OBJECT_ID> <FILE_NAME> [<OBJECT_TYPE>]
CONFIG_ID - configuration id from python-gitlab
config file (https://python-gitlab.readthedocs.io/en/stable/cli.html#cli-configuration)
OBJECT_ID - id of project or group or project name
FILE_NAME - .properties file for import
OBJECT_TYPE (optional) [project|group] - type of environment variables, project is default
Any other value interpreted as group
'''
import sys
import gitlab
from configparser import ConfigParser, NoSectionError
CONFIG_ID = None
if len(sys.argv) > 1:
CONFIG_ID = sys.argv[1]
else:
sys.exit('Please, provide config id')
OBJECT_ID = None
if len(sys.argv) > 2:
try:
OBJECT_ID = int(sys.argv[2])
except:
if sys.argv[2]:
OBJECT_ID = sys.argv[2]
else:
sys.exit('Please, group/project id should be `int` or project name')
else:
sys.exit('Please, provide group/project id')
FILE_NAME = None
if len(sys.argv) > 3:
FILE_NAME = sys.argv[3]
else:
sys.exit('Please, provide file name')
OBJECT_TYPE = 'project'
if len(sys.argv) > 4:
if sys.argv[4] != OBJECT_TYPE:
OBJECT_TYPE = 'group'
if __name__ == '__main__':
config = ConfigParser(default_section='')
config.optionxform = lambda option: option
config.read(FILE_NAME)
gl = gitlab.Gitlab.from_config(CONFIG_ID)
if OBJECT_TYPE == 'project':
gitlab_object_class = gl.projects
else:
gitlab_object_class = gl.groups
if isinstance(OBJECT_ID, int):
gitlab_object = gitlab_object_class.get(OBJECT_ID)
else:
gitlab_objects = gitlab_object_class.list(**{'query_parameters':{'search':OBJECT_ID}})
if len(gitlab_objects) != 1:
sys.exit('Found {} {}(s)'.format(len(gitlab_objects), OBJECT_TYPE))
else:
gitlab_object = gitlab_objects[0]
for section in config.sections():
kwargs = {}
if OBJECT_TYPE == 'project':
kwargs = {'query_parameters':{'filter[environment_scope]':section}}
for variable in config.items(section):
print(section, variable[0], variable[1])
try:
_variable = gitlab_object.variables.get(variable[0], **kwargs)
_variable.value = variable[1]
_variable.save(**kwargs)
except gitlab.exceptions.GitlabGetError as ex:
if OBJECT_TYPE == 'project':
gitlab_object.variables.create({'key': variable[0],
'value': variable[1],
'environment_scope': section})
else:
gitlab_object.variables.create({'key': variable[0],
'value': variable[1]})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment