sub check_kibana_links()

in build_docs.pl [345:463]


sub check_kibana_links {
#===================================
    my $build_dir    = shift;
    my $link_checker = shift;
    my $branch;
    my $version;

    say "Checking Kibana links";

    my $extractor = sub {
        my $contents = shift;
        return sub {
            while ( $contents =~ m!`(\$\{(?:baseUrl|ELASTIC.+|KIBANA_DOCS|PLUGIN_DOCS|FLEET_DOCS|APM_DOCS|STACK_DOCS|SECURITY_SOLUTION_DOCS|STACK_GETTING_STARTED|APP_SEARCH_DOCS|ENTERPRISE_SEARCH_DOCS|INTEGRATIONS_DEV_DOCS|WORKPLACE_SEARCH_DOCS|SERVERLESS_DOCS)\}[^`]+)`!g ) {
                my $path = $1;
                $path =~ s/\$\{(?:DOC_LINK_VERSION|urlVersion)\}/$version/;
                $path =~ s/\$\{(?:ECS_VERSION)\}/current/;
                # In older versions, the variable `${ELASTIC_DOCS}` referred to
                # the Elasticsearch Guide. In newer branches, the
                # variable is called `${ELASTICSEARCH_DOCS}`
                $path =~ s!\$\{ELASTIC_DOCS\}!en/elasticsearch/reference/$version/!;
                $path =~ s!\$\{ELASTICSEARCH_DOCS\}!en/elasticsearch/reference/$version/!;
                $path =~ s!\$\{KIBANA_DOCS\}!en/kibana/$version/!;
                $path =~ s!\$\{PLUGIN_DOCS\}!en/elasticsearch/plugins/$version/!;
                $path =~ s!\$\{OBSERVABILITY_DOCS\}!en/observability/$version/!;
                $path =~ s!\$\{FLEET_DOCS\}!en/fleet/$version/!;
                $path =~ s!\$\{APM_DOCS\}!en/apm/!;
                $path =~ s!\$\{STACK_DOCS\}!en/elastic-stack/$version/!;
                $path =~ s!\$\{SECURITY_SOLUTION_DOCS\}!en/security/$version/!;
                $path =~ s!\$\{STACK_GETTING_STARTED\}!en/elastic-stack-get-started/$version/!;
                $path =~ s!\$\{APP_SEARCH_DOCS\}!en/app-search/$version/!;
                $path =~ s!\$\{ENTERPRISE_SEARCH_DOCS\}!en/enterprise-search/$version/!;
                $path =~ s!\$\{WORKPLACE_SEARCH_DOCS\}!en/workplace-search/$version/!;
                $path =~ s!\$\{MACHINE_LEARNING_DOCS\}!en/machine-learning/$version/!;
                $path =~ s!\$\{INTEGRATIONS_DEV_DOCS}!en/integrations-developer/current/!;
                $path =~ s!\$\{SERVERLESS_DOCS}!/en/serverless/current/!;
                # Replace the "https://www.elastic.co/guide/" URL prefix so that
                # it becomes a file path in the built docs.
                $path =~ s!\$\{(?:baseUrl|ELASTIC_WEBSITE_URL)\}guide/!!;
                # We don't want to check any links to www.elastic.co that aren't
                # part of the docs.
                return "" if $path =~ m/\$\{(?:baseUrl|ELASTIC_WEBSITE_URL|ELASTIC_GITHUB|API_DOCS|ELASTICSEARCH_APIS|ELASTICSEARCH_SERVERLESS_APIS|KIBANA_APIS|KIBANA_SERVERLESS_APIS)\}.*/;
                # Otherwise, return the link to check
                return ( split /#/, $path );
            }
            return;
        };

    };

    my $src_path = 'src/ui/public/documentation_links/documentation_links';
    my $legacy_path = 'src/legacy/ui/public/documentation_links/documentation_links';
    my $repo     = ES::Repo->get_repo('kibana');

    my @versions = sort map { $_->basename }
        grep { $_->is_dir } $build_dir->subdir('en/kibana')->children;

    my $link_check_name = 'link-check-kibana';

    for (@versions) {
        $version = $_;
        next if $version eq 'current' || $version =~ /^\d/ && $version lt 5;
        # @versions is looping through the directories in the output (which
        # still contains `master`), but we need to look in the `main` branch of
        # the Kibana repo for this file.
        #
        # TODO: remove as part of
        # https://github.com/elastic/docs/issues/2264
        if ($version eq "master") {
            $branch = "main"
        }
        else {
            if ($version eq "8.x") {
                $branch = "8.19"
            }
            else {
                $branch = $version
            }
        }
        # $branch = $version eq "master" ? "main" : $version;
        say "  Branch: $branch, Version: $version";
        my $links_file;
        my $source = eval {
            $links_file = "src/platform/packages/shared/kbn-doc-links/src/get_doc_links.ts";
            $repo->show_file( $link_check_name, $branch, $links_file );
        } || eval {
            $links_file = "packages/kbn-doc-links/src/get_doc_links.ts";
            $repo->show_file( $link_check_name, $branch, $links_file );
        } || eval {
            $links_file = $src_path . ".js";
            $repo->show_file( $link_check_name, $branch, $links_file );
        } || eval {
            $links_file = $src_path . ".ts";
            $repo->show_file( $link_check_name, $branch, $links_file );
        } || eval {
            $links_file = $legacy_path . ".js";
            $repo->show_file( $link_check_name, $branch, $links_file );
        } || eval {
            $links_file = $legacy_path . ".ts";
            $repo->show_file( $link_check_name, $branch, $links_file );
        } || eval {
            $links_file = "src/core/packages/doc-links/core-doc-links-browser-internal/src/doc_links_service.ts";
            $repo->show_file( $link_check_name, $branch, $links_file );
        } || eval {
            $links_file = "packages/core/doc-links/core-doc-links-browser-internal/src/doc_links_service.ts";
            $repo->show_file( $link_check_name, $branch, $links_file );
        } || eval {
            $links_file = "src/core/public/doc_links/doc_links_service.ts";
            $repo->show_file( $link_check_name, $branch, $links_file );
        };
        die "failed to find kibana links file;\n$@" unless $source;

        $link_checker->check_source( $source, $extractor,
            "Kibana [$version]: $links_file" );

        # Mark the file that we need for the link check done so we can use
        # --keep_hash with it during some other build.
        $repo->mark_done( $link_check_name, $branch, $links_file, 0 );
    }
}