Skip to main content

lib/api/pulls/pulls.ex

# Generated by `mix github.gen` from docs/github-api/api.github.com.json.
# Do not edit by hand; re-run the task instead.

defmodule Noizu.Github.Api.Pulls do
  @moduledoc """
  GitHub `pulls` API.
  """
  import Noizu.Github

  @doc """
  Check if a pull request has been merged

  @see https://docs.github.com/rest/pulls/pulls#check-if-a-pull-request-has-been-merged
  """
  def check_if_merged(pull_number, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)
    url = github_base() <> "/repos/#{owner}/#{repo}/pulls/#{pull_number}/merge"
    body = %{}
    api_call(:get, url, body, Noizu.Github.Raw, options)
  end

  @doc """
  Create a pull request

  @see https://docs.github.com/rest/pulls/pulls#create-a-pull-request
  """
  def create(body, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)
    url = github_base() <> "/repos/#{owner}/#{repo}/pulls"
    body = body
    api_call(:post, url, body, Noizu.Github.PullRequest, options)
  end

  @doc """
  Create a reply for a review comment

  @see https://docs.github.com/rest/pulls/comments#create-a-reply-for-a-review-comment
  """
  def create_reply_for_review_comment(pull_number, comment_id, body, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)

    url =
      github_base() <>
        "/repos/#{owner}/#{repo}/pulls/#{pull_number}/comments/#{comment_id}/replies"

    body = body
    api_call(:post, url, body, Noizu.Github.PullRequestReviewComment, options)
  end

  @doc """
  Create a review for a pull request

  @see https://docs.github.com/rest/pulls/reviews#create-a-review-for-a-pull-request
  """
  def create_review(pull_number, body, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)
    url = github_base() <> "/repos/#{owner}/#{repo}/pulls/#{pull_number}/reviews"
    body = body
    api_call(:post, url, body, Noizu.Github.PullRequestReview, options)
  end

  @doc """
  Create a review comment for a pull request

  @see https://docs.github.com/rest/pulls/comments#create-a-review-comment-for-a-pull-request
  """
  def create_review_comment(pull_number, body, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)
    url = github_base() <> "/repos/#{owner}/#{repo}/pulls/#{pull_number}/comments"
    body = body
    api_call(:post, url, body, Noizu.Github.PullRequestReviewComment, options)
  end

  @doc """
  Delete a pending review for a pull request

  @see https://docs.github.com/rest/pulls/reviews#delete-a-pending-review-for-a-pull-request
  """
  def delete_pending_review(pull_number, review_id, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)
    url = github_base() <> "/repos/#{owner}/#{repo}/pulls/#{pull_number}/reviews/#{review_id}"
    body = %{}
    api_call(:delete, url, body, Noizu.Github.PullRequestReview, options)
  end

  @doc """
  Delete a review comment for a pull request

  @see https://docs.github.com/rest/pulls/comments#delete-a-review-comment-for-a-pull-request
  """
  def delete_review_comment(comment_id, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)
    url = github_base() <> "/repos/#{owner}/#{repo}/pulls/comments/#{comment_id}"
    body = %{}
    api_call(:delete, url, body, Noizu.Github.Raw, options)
  end

  @doc """
  Dismiss a review for a pull request

  @see https://docs.github.com/rest/pulls/reviews#dismiss-a-review-for-a-pull-request
  """
  def dismiss_review(pull_number, review_id, body, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)

    url =
      github_base() <>
        "/repos/#{owner}/#{repo}/pulls/#{pull_number}/reviews/#{review_id}/dismissals"

    body = body
    api_call(:put, url, body, Noizu.Github.PullRequestReview, options)
  end

  @doc """
  Get a pull request

  @see https://docs.github.com/rest/pulls/pulls#get-a-pull-request
  """
  def get(pull_number, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)
    url = github_base() <> "/repos/#{owner}/#{repo}/pulls/#{pull_number}"
    body = %{}
    api_call(:get, url, body, Noizu.Github.PullRequest, options)
  end

  @doc """
  Get a review for a pull request

  @see https://docs.github.com/rest/pulls/reviews#get-a-review-for-a-pull-request
  """
  def get_review(pull_number, review_id, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)
    url = github_base() <> "/repos/#{owner}/#{repo}/pulls/#{pull_number}/reviews/#{review_id}"
    body = %{}
    api_call(:get, url, body, Noizu.Github.PullRequestReview, options)
  end

  @doc """
  Get a review comment for a pull request

  @see https://docs.github.com/rest/pulls/comments#get-a-review-comment-for-a-pull-request
  """
  def get_review_comment(comment_id, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)
    url = github_base() <> "/repos/#{owner}/#{repo}/pulls/comments/#{comment_id}"
    body = %{}
    api_call(:get, url, body, Noizu.Github.PullRequestReviewComment, options)
  end

  @doc """
  List pull requests

  @see https://docs.github.com/rest/pulls/pulls#list-pull-requests
  """
  def list(options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)

    url =
      (
        query =
          [
            get_field(:state, options, nil),
            get_field(:head, options, nil),
            get_field(:base, options, nil),
            get_field(:sort, options, nil),
            get_field(:direction, options, nil),
            get_field(:per_page, options, nil),
            get_field(:page, options, nil)
          ]
          |> Enum.filter(& &1)

        qs = if query == [], do: "", else: "?" <> Enum.join(query, "&")
        github_base() <> "/repos/#{owner}/#{repo}/pulls" <> qs
      )

    body = %{}
    api_call(:get, url, body, Noizu.Github.Collection.PullRequestSimple, options)
  end

  @doc """
  List comments for a pull request review

  @see https://docs.github.com/rest/pulls/reviews#list-comments-for-a-pull-request-review
  """
  def list_comments_for_review(pull_number, review_id, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)

    url =
      (
        query =
          [
            get_field(:per_page, options, nil),
            get_field(:page, options, nil)
          ]
          |> Enum.filter(& &1)

        qs = if query == [], do: "", else: "?" <> Enum.join(query, "&")

        github_base() <>
          "/repos/#{owner}/#{repo}/pulls/#{pull_number}/reviews/#{review_id}/comments" <> qs
      )

    body = %{}
    api_call(:get, url, body, Noizu.Github.Collection.ReviewComment, options)
  end

  @doc """
  List commits on a pull request

  @see https://docs.github.com/rest/pulls/pulls#list-commits-on-a-pull-request
  """
  def list_commits(pull_number, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)

    url =
      (
        query =
          [
            get_field(:per_page, options, nil),
            get_field(:page, options, nil)
          ]
          |> Enum.filter(& &1)

        qs = if query == [], do: "", else: "?" <> Enum.join(query, "&")
        github_base() <> "/repos/#{owner}/#{repo}/pulls/#{pull_number}/commits" <> qs
      )

    body = %{}
    api_call(:get, url, body, Noizu.Github.Collection.Commit, options)
  end

  @doc """
  List pull requests files

  @see https://docs.github.com/rest/pulls/pulls#list-pull-requests-files
  """
  def list_files(pull_number, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)

    url =
      (
        query =
          [
            get_field(:per_page, options, nil),
            get_field(:page, options, nil)
          ]
          |> Enum.filter(& &1)

        qs = if query == [], do: "", else: "?" <> Enum.join(query, "&")
        github_base() <> "/repos/#{owner}/#{repo}/pulls/#{pull_number}/files" <> qs
      )

    body = %{}
    api_call(:get, url, body, Noizu.Github.Collection.DiffEntry, options)
  end

  @doc """
  Get all requested reviewers for a pull request

  @see https://docs.github.com/rest/pulls/review-requests#get-all-requested-reviewers-for-a-pull-request
  """
  def list_requested_reviewers(pull_number, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)
    url = github_base() <> "/repos/#{owner}/#{repo}/pulls/#{pull_number}/requested_reviewers"
    body = %{}
    api_call(:get, url, body, Noizu.Github.PullRequestReviewRequest, options)
  end

  @doc """
  List review comments on a pull request

  @see https://docs.github.com/rest/pulls/comments#list-review-comments-on-a-pull-request
  """
  def list_review_comments(pull_number, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)

    url =
      (
        query =
          [
            get_field(:sort, options, nil),
            get_field(:direction, options, nil),
            get_field(:since, options, nil),
            get_field(:per_page, options, nil),
            get_field(:page, options, nil)
          ]
          |> Enum.filter(& &1)

        qs = if query == [], do: "", else: "?" <> Enum.join(query, "&")
        github_base() <> "/repos/#{owner}/#{repo}/pulls/#{pull_number}/comments" <> qs
      )

    body = %{}
    api_call(:get, url, body, Noizu.Github.Collection.PullRequestReviewComment, options)
  end

  @doc """
  List review comments in a repository

  @see https://docs.github.com/rest/pulls/comments#list-review-comments-in-a-repository
  """
  def list_review_comments_for_repo(options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)

    url =
      (
        query =
          [
            get_field(:sort, options, nil),
            get_field(:direction, options, nil),
            get_field(:since, options, nil),
            get_field(:per_page, options, nil),
            get_field(:page, options, nil)
          ]
          |> Enum.filter(& &1)

        qs = if query == [], do: "", else: "?" <> Enum.join(query, "&")
        github_base() <> "/repos/#{owner}/#{repo}/pulls/comments" <> qs
      )

    body = %{}
    api_call(:get, url, body, Noizu.Github.Collection.PullRequestReviewComment, options)
  end

  @doc """
  List reviews for a pull request

  @see https://docs.github.com/rest/pulls/reviews#list-reviews-for-a-pull-request
  """
  def list_reviews(pull_number, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)

    url =
      (
        query =
          [
            get_field(:per_page, options, nil),
            get_field(:page, options, nil)
          ]
          |> Enum.filter(& &1)

        qs = if query == [], do: "", else: "?" <> Enum.join(query, "&")
        github_base() <> "/repos/#{owner}/#{repo}/pulls/#{pull_number}/reviews" <> qs
      )

    body = %{}
    api_call(:get, url, body, Noizu.Github.Collection.PullRequestReview, options)
  end

  @doc """
  Merge a pull request

  @see https://docs.github.com/rest/pulls/pulls#merge-a-pull-request
  """
  def merge(pull_number, body, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)
    url = github_base() <> "/repos/#{owner}/#{repo}/pulls/#{pull_number}/merge"
    body = body
    api_call(:put, url, body, Noizu.Github.PullRequestMergeResult, options)
  end

  @doc """
  Remove requested reviewers from a pull request

  @see https://docs.github.com/rest/pulls/review-requests#remove-requested-reviewers-from-a-pull-request
  """
  def remove_requested_reviewers(pull_number, body, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)
    url = github_base() <> "/repos/#{owner}/#{repo}/pulls/#{pull_number}/requested_reviewers"
    body = body
    api_call(:delete, url, body, Noizu.Github.PullRequestSimple, options)
  end

  @doc """
  Request reviewers for a pull request

  @see https://docs.github.com/rest/pulls/review-requests#request-reviewers-for-a-pull-request
  """
  def request_reviewers(pull_number, body, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)
    url = github_base() <> "/repos/#{owner}/#{repo}/pulls/#{pull_number}/requested_reviewers"
    body = body
    api_call(:post, url, body, Noizu.Github.PullRequestSimple, options)
  end

  @doc """
  Submit a review for a pull request

  @see https://docs.github.com/rest/pulls/reviews#submit-a-review-for-a-pull-request
  """
  def submit_review(pull_number, review_id, body, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)

    url =
      github_base() <> "/repos/#{owner}/#{repo}/pulls/#{pull_number}/reviews/#{review_id}/events"

    body = body
    api_call(:post, url, body, Noizu.Github.PullRequestReview, options)
  end

  @doc """
  Update a pull request

  @see https://docs.github.com/rest/pulls/pulls#update-a-pull-request
  """
  def update(pull_number, body, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)
    url = github_base() <> "/repos/#{owner}/#{repo}/pulls/#{pull_number}"
    body = body
    api_call(:patch, url, body, Noizu.Github.PullRequest, options)
  end

  @doc """
  Update a pull request branch

  @see https://docs.github.com/rest/pulls/pulls#update-a-pull-request-branch
  """
  def update_branch(pull_number, body, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)
    url = github_base() <> "/repos/#{owner}/#{repo}/pulls/#{pull_number}/update-branch"
    body = body
    api_call(:put, url, body, Noizu.Github.Raw, options)
  end

  @doc """
  Update a review for a pull request

  @see https://docs.github.com/rest/pulls/reviews#update-a-review-for-a-pull-request
  """
  def update_review(pull_number, review_id, body, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)
    url = github_base() <> "/repos/#{owner}/#{repo}/pulls/#{pull_number}/reviews/#{review_id}"
    body = body
    api_call(:put, url, body, Noizu.Github.PullRequestReview, options)
  end

  @doc """
  Update a review comment for a pull request

  @see https://docs.github.com/rest/pulls/comments#update-a-review-comment-for-a-pull-request
  """
  def update_review_comment(comment_id, body, options \\ nil) do
    owner = repo_owner(options)
    repo = repo_name(options)
    url = github_base() <> "/repos/#{owner}/#{repo}/pulls/comments/#{comment_id}"
    body = body
    api_call(:patch, url, body, Noizu.Github.PullRequestReviewComment, options)
  end
end