Search for code or text in GitLab

前端 未结 2 1195
名媛妹妹
名媛妹妹 2021-02-12 10:42

Is it possible to search for code or text in GitLab inside of files? I can search for files, issues, milestones, etc., but could not find a way to search for code in source file

2条回答
  •  独厮守ぢ
    2021-02-12 11:08

    Alternatively you can use the python-gitlab library to search text in the projects that you need:

    import gitlab
    
    
    def search(gitlab_server, token, file_filter, text, group=None, project_filter=None):
        return_value = []
        gl = gitlab.Gitlab(gitlab_server, private_token=token)
        if (project_filter == '') and (group == ''):
            projects = gl.projects.list(all=True)
        else:
            projects = gl.projects.list(search=project_filter, group=group)
        for project in projects:
            files = []
            try:
                files = project.repository_tree(recursive=True, all=True)
            except Exception as e:
                print(str(e), "Error getting tree in project:", project.name)
            for file in files:
                if file_filter == file['name']:
                    file_content = project.files.raw(file_path=file['path'], ref='master')
                    if text in str(file_content):
                        return_value.append({
                            "project": project.name,
                            "file": file['path']
                        })
        return return_value
    

    Complete example can be found here: gitlab-search

提交回复
热议问题