{% extends "coprs/detail.html" %}
{% from "coprs/detail/_builds_forms.html" import
   copr_build_cancel_form,
   copr_build_repeat_form,
   copr_resubmit_allow_user_ssh_form,
   copr_build_delete_form
%}
{% from "coprs/detail/_describe_source.html" import describe_source %}
{% from "coprs/detail/_describe_failure.html" import describe_failure %}
{% from "_helpers.html" import
    chroot_to_os_logo,
    build_state_text,
    build_state,
    copr_name,
    log_detective_ai_link
%}
{% block title %}Build {{ build.id }} in {{ copr_name(build.copr) }}{% endblock %}

{% set selected_tab = "builds" %}

{%block project_breadcrumb %}
<li>
  <a href="{{ copr_url('coprs_ns.copr_builds', copr) }}">Builds</a>
</li>
<li class="active">
  {{build.id}}
</li>
{%endblock%}

{% block detail_body %}

{% if build.copr != copr %}
<h2 class="build-detail"> Build {{ build.id }} doesn't belong to this project. </h2>
<p> You can go to <a href="{{ copr_url('coprs_ns.copr_build', build.copr, build_id=build.id) }}"> {{ copr_name(build.copr) }}/build/{{build.id}} </a>to see this build. </p>
{% else %}

{% if g.user and g.user.can_build_in(copr) and build.finished and not build.persistent %}
{% if g.user.can_edit(copr) or build.user == g.user %}
    {{ copr_build_delete_form(build, page, class="pull-right button-build-action") }}
{% endif %}
{% endif %}

{% if g.user and g.user.can_build_in(copr) and build.cancelable %}
    {{ copr_build_cancel_form(build, page, class="pull-right button-build-action") }}
{% endif %}

{% if g.user and g.user.can_build_in(copr) and build.repeatable %}
    {{ copr_resubmit_allow_user_ssh_form(build, page, class="pull-right button-build-action") }}
    {{ copr_build_repeat_form(build, page, class="pull-right button-build-action") }}
{% endif %}

<h2 class="page-title">Build {{ build.id }}
    {% if build.is_background %}
           <span class="badge">background</span>
    {% endif %}
</h2>

<div class="row">
  <div class="col-sm-12">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">General Information</h3>
      </div>
      <div class="panel-body">
        <dl class="dl-horizontal">
          <dt> Status: </dt>
          <dd>
            {{ build_state(build) }} - {{ build.status | state_from_num | build_state_description }}
          </dd>
          <dt> Submitted: </dt>
          <dd>
            {% if g.user %}
                {{ build.submitted_on|localized_time(g.user.timezone) }}
            {% else %}
                {{ build.submitted_on|localized_time("UTC") }}
            {% endif %}
            ({{ build.submitted_on|time_ago }} ago)
          </dd>
          <dt> Started: </dt>
          <dd>
            {% if g.user %}
                {{ build.min_started_on|localized_time(g.user.timezone) }}
            {% else %}
                {{ build.min_started_on|localized_time("UTC") }}
            {% endif %}
            ({{ build.min_started_on|time_ago }} ago)
          </dd>
          <dt> Finished: </dt>
          <dd>
            {% if g.user %}
                {{ build.max_ended_on|localized_time(g.user.timezone) }}
            {% else %}
                {{ build.max_ended_on|localized_time("UTC") }}
            {% endif %}
            ({{ build.max_ended_on|time_ago }} ago)
          </dd>
          <dt> Build time: </dt>
          <dd>
            {{ build.min_started_on|time_ago(build.max_ended_on) }}
          </dd>
          <dt> Build timeout: </dt>
          <dd>
            {{ build.timeout|natural_time_delta() }}
          </dd>
          <dt> Networking enabled: </dt>
          <dd>
              {{ build.enable_net }}
          </dd>
          {% if build.scm_object_type == 'commit' %}
          <dt> Commit: </dt>
          <dd>
            <a href="{{ build.scm_object_url }}">
              {{ build.scm_object_id | truncate(7, True, "") }}
            </a>
          </dd>
          {% endif %}
          {% if build.scm_object_type == 'pull-request' %}
          <dt> Pull Request: </dt>
          <dd>
            PR#{{ build.scm_object_id }}
          </dd>
          {% endif %}
          {% if build.batch_id %}
            <dt> Batch: </dt>
            <dd>
              <a href="{{ url_for('batches_ns.coprs_batch_detail', batch_id=build.batch_id) }}">
                {{ build.batch_id }}
              </a>
            </dd>
          {% endif %}
          <dt> Directory: </dt>
          <dd>
            <a href="{{ build.copr_dir.repo_url }}">
              {{ build.copr_dir.name }}
            </a>
          </dd>
          {% if build.resubmitted_from_id %}
          <dt> Resubmitted from: </dt>
          <dd>
              {% if build.resubmitted_from %}
                <a href="{{ copr_url('coprs_ns.copr_build', build.copr, build_id=build.resubmitted_from.id) }}">{{ build.resubmitted_from.id }}</a>
              {% else %}
                {{ build.resubmitted_from_id }}
              {% endif %}
          </dd>
          {% endif %}
          <dt> Built by: </dt>
          <dd>
            {% if build.submitter[1] %}
            <a href="{{ build.submitter[1] }}">{{ build.submitter[0] }}</a>
            {% else %}
            {{ build.submitter[0] or "unknown user" }}
            {% endif %}
          </dd>
        </dl>
      </div>
    </div>

    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">Source</h3>
      </div>
      <div class="panel-body">
        <dl class="dl-horizontal">
          <dt> Package:</dt>
          <dd>
            {% if build.package %}
              <a href="{{ copr_url('coprs_ns.copr_package', copr, package_name=build.package.name) }}">
                {{ build.package.name }}
              </a>
            {% else %}
                 -
            {% endif %}
          </dd>
          <dt> Version:</dt>
          <dd>
            {% if build.pkg_version %}
                {{ build.pkg_version}}
            {% else %}
                 -
            {% endif %}
          </dd>
          {{ describe_source(build.source_type_text, build.source_json_dict) }}
        </dl>
      </div>
    </div>


    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">Results</h3>
      </div>
      <div class="panel-body">
        <dl class="dl-horizontal">
        {{ describe_failure(build) }}
        <dt>Source state:</dt>
        <dd>
          {{ build_state_text(build.source_state) }}
          {% if g.user and config.LOG_DETECTIVE_BUTTON and build.source_state == "failed" %}
            {% set link_shown = namespace(value=False) %}
              {% for url in build.get_source_log_urls %}
                {% if not link_shown.value %}
                  &nbsp&nbsp {{ log_detective_ai_link(url, build.id, "srpm-builds", build.submitted_on) }}
                  {% set link_shown.value = true %}
                {% endif %}
              {% endfor %}
          {% endif %}
        </dd>
        <dt>Source build logs:</dt>
        <dd>
        {% if build.resubmitted_from_id and build.source_is_uploaded %}
          Build resubmitted from build
          {% if build.resubmitted_from %}
            <a href="{{ copr_url('coprs_ns.copr_build', build.copr, build_id=build.resubmitted_from.id) }}">#{{ build.resubmitted_from.id }}</a>
          {% else %}
            #{{ build.resubmitted_from_id }}
          {% endif %}
        {% else %}
          {% for url in build.get_source_log_urls %}
            <a href="{{ url }}">{{ url | basename | fix_import_log_name }}</a> {{ "," if not loop.last }}
          {% else %}
            Source build has not started yet
          {% endfor %}
        {% endif %}
        </dd>

        <dt> Built Packages:</dt>
        {% if build.built_packages %}
          {% for pkg in build.built_packages.split("\n"): %}
            <dd>{{ pkg }}</dd>
          {% endfor %}
        {% else %}
          <dd> - </dd>
        {% endif %}

        {% if not build.build_chroots %}
        <dt>RPM builds:</dt>
        <dd>waiting for SRPM (package name is not known for this build, yet)</dd>
        {% endif %}
        </dl>

        {% if build.build_chroots %}
        {% set has_failed_chroots = namespace(value=false) %}
        {% for chroot in build.build_chroots %}
          {% if chroot.state == "failed" %}
            {% set has_failed_chroots.value = true %}
          {% endif %}
        {% endfor %}
        <table class="table table-striped table-bordered">
          <thead>
            <tr>
              <th>Chroot Name</th>
              <th>Dist Git Source</th>
              <th>Build Time</th>
              <th>Logs</th>
              <th>State</th>
              {% if g.user and config.LOG_DETECTIVE_BUTTON and has_failed_chroots.value and build.source_state == "succeeded" %}
              <th>Log Detective AI</th>
              {% endif %}
            </tr>
          </thead>
          <tbody>
          {% for chroot in build.build_chroots|sort(attribute='name') %}
            <tr>
              <td>
                {% if chroot.result_dir %}
                <a href="{{ chroot.result_dir_url }}">
                  <span class="pficon pficon-folder-open"></span> {{ chroot.name }}
                </a>
                {% else %}
                {{ chroot.name }}
                {% endif %}
              </td>
              <td>
                {% if chroot.git_hash %}
                  {% if g.user and chroot.dist_git_url %}
                    <a href="{{chroot.dist_git_url}}">
                      {{ chroot.git_hash[:7] }}
                    </a>
                  {% else %}
                    {{ chroot.git_hash[:7] }}
                  {% endif %}
                {% else %}
                  -
                {% endif %}
              </td>
              <td>
                {{ chroot.started_on|time_ago(chroot.ended_on) }}
              </td>
              <td>
                {% for log in chroot.rpm_live_logs %}
                <a href="{{ log }}"> {{ log | basename }} </a>
                {{ "," if not loop.last }}
                {% else %}
                RPM build has not started yet
                {% endfor %}
              </td>
              <td>
                {{ build_state_text(chroot.state, chroot.status_reason, chroot.rpm_live_log_url) }}
              </td>
              {% if g.user and config.LOG_DETECTIVE_BUTTON and has_failed_chroots.value %}
              <td>
                {% if chroot.state == "failed" %}
                  {{ log_detective_ai_link(chroot.rpm_live_log_url, build.id, chroot.name, build.submitted_on) }}
                {% else %}
                  -
                {% endif %}
              </td>
              {% endif %}
            </tr>
          {% endfor %}
          </tbody>
        </table>
        {% endif %}
      </div>
    </div>

  </div>

</div>

{% endif %}
{% endblock %}
