Source code for pydolphinscheduler.resources_plugin.gitlab
# Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License."""DolphinScheduler gitlab resource plugin."""from__future__importannotationsfromurllib.parseimporturljoin,urlparseimportgitlabimportrequestsfrompydolphinscheduler.constantsimportSymbolfrompydolphinscheduler.core.resource_pluginimportResourcePluginfrompydolphinscheduler.resources_plugin.base.gitimportGit,GitLabFileInfo
[docs]classGitLab(ResourcePlugin,Git):"""GitLab object, declare GitLab resource plugin for task and workflow to dolphinscheduler. :param prefix: A string representing the prefix of GitLab. :param private_token: A string used for identity authentication of GitLab private or Internal repository. :param oauth_token: A string used for identity authentication of GitLab private or Internal repository. :param username: A string representing the user of the repository. :param password: A string representing the user password. """def__init__(self,prefix:str,private_token:str|None=None,oauth_token:str|None=None,username:str|None=None,password:str|None=None,*args,**kwargs,):super().__init__(prefix,*args,**kwargs)self.private_token=private_tokenself.oauth_token=oauth_tokenself.username=usernameself.password=password
[docs]defget_git_file_info(self,path:str):"""Get file information from the file url, like repository name, user, branch, and file path."""self.get_index(path,Symbol.SLASH,8)result=urlparse(path)elements=result.path.split(Symbol.SLASH)self._git_file_info=GitLabFileInfo(host=f"{result.scheme}://{result.hostname}",repo_name=elements[2],branch=elements[5],file_path=Symbol.SLASH.join(str(elements[i])foriinrange(6,len(elements))),user=elements[1],)
[docs]defread_file(self,suf:str):"""Get the content of the file. The address of the file is the prefix of the resource plugin plus the parameter suf. """path=urljoin(self.prefix,suf)self.get_git_file_info(path)gl=self.authentication()project=gl.projects.get(f"{self._git_file_info.user}/{self._git_file_info.repo_name}")return(project.files.get(file_path=self._git_file_info.file_path,ref=self._git_file_info.branch).decode().decode())