Search for code or text in GitLab

前端 未结 2 1191
名媛妹妹
名媛妹妹 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 10:57

    It was announced in 5.2:

    https://about.gitlab.com/2013/05/22/gitlab-5-dot-2-released/

    And I can confirm code search is working with 8.4

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题