{% from 'macros.common.j2' import capitalize_first_letter_only %}


{#
  MACRO: format a post-paragraph link reference in RST
#}{%  macro format_link_reference(link, label)
%}{{    ".. _%s: %s" | format(label, link)
}}{%  endmacro
%}


{# MACRO: generate a heading underline that matches the exact length of the header #}
{%    macro generate_heading_underline(header, underline_char)
%}{%    set header_underline = []
%}{%    for _ in header
%}{%      set __ = header_underline.append(underline_char)
%}{%    endfor
%}{#    # Print out the header underline
#}{{    header_underline | join
}}{%  endmacro
%}


{#
  MACRO: formats a commit message for a non-inline RST link for a commit hash and/or PR/MR
#}{%  macro commit_msg_links(commit)
%}{%    if commit.error is undefined
%}{#
   #      # Initialize variables
#}{%      set closes_statement = ""
%}{%      set link_references = []
%}{%      set summary_line = capitalize_first_letter_only(
            commit.descriptions[0] | safe
          )
%}{#
#}{%      if commit.linked_issues | length > 0
%}{%        set closes_statement = ", closes `%s`_" | format(
              commit.linked_issues | join("`_, `")
            )
%}{%      endif
%}{#
#}{%      if commit.linked_merge_request != ""
%}{#        # Add PR references with a link to the PR
#}{%        set _ = link_references.append("`PR%s`_" | format(commit.linked_merge_request))
%}{%      endif
%}{#
          # DEFAULT: Always include the commit hash as a link
#}{%      set _ = link_references.append("`%s`_" | format(commit.short_hash))
%}{#
#}{%      set formatted_links = ""
%}{%      if link_references | length > 0
%}{%        set formatted_links = " (%s)" | format(link_references | join(", "))
%}{%      endif
%}{#
          # Return the modified summary_line
#}{{      summary_line ~ closes_statement ~ formatted_links
}}{%    endif
%}{%  endmacro
%}


{#
  MACRO: format commit summary line
#}{%  macro format_commit_summary_line(commit)
%}{#    # Check for Parsing Error
#}{%    if commit.error is undefined
%}{#
   #      # Add any message links to the commit summary line
#}{%      set summary_line = commit_msg_links(commit)
%}{#
#}{%      if commit.scope
%}{%        set summary_line = "**%s**: %s" | format(commit.scope, summary_line)
%}{%      endif
%}{#
   #      # Return the modified summary_line
#}{{      summary_line
}}{#
#}{%    else
%}{#      # Return the first line of the commit if there was a Parsing Error
#}{{      (commit.commit.message | string).split("\n", maxsplit=1)[0]
}}{%    endif
%}{%  endmacro
%}


{#
  MACRO: Extract issue references from a parsed commit object
  - Stores the issue urls in the namespace object
#}{%  macro extract_issue_link_references(ns, commit)
%}{%    set issue_urls = []
%}{#
#}{%    if commit.linked_issues is defined and commit.linked_issues | length > 0
%}{%      for issue_num in commit.linked_issues
%}{#        # Create an issue reference url
#}{%        set _ = issue_urls.append(
              format_link_reference(
                issue_num | issue_url,
                issue_num,
              )
            )
%}{%      endfor
%}{%    endif
%}{#
   #    # Store the issue urls in the namespace object
#}{%    set ns.urls = issue_urls
%}{%  endmacro
%}


{#
  MACRO: Create & return an non-inline RST link from a commit message
  - Returns empty string if no PR/MR identifier is found
#}{%  macro extract_pr_link_reference(commit)
%}{%    if commit.error is undefined
%}{%      set summary_line = commit.descriptions[0]
%}{#
#}{%      if commit.linked_merge_request != ""
%}{#        # Create a PR/MR reference url
#}{{        format_link_reference(
              commit.linked_merge_request | pull_request_url,
              "PR" ~ commit.linked_merge_request,
            )
}}{%      endif
%}{%    endif
%}{%  endmacro
%}
