     project = batch.project
     token = GitHub.get_installation_token!(project.installation.installation_xref)
     GitHub.copy_branch!(token, project.repo_xref, project.staging_branch, project.master_branch)
-    patches = Repo.all Patch.all_for_batch(batch.id)
+    patches = Repo.all(Patch.all_for_batch(batch.id))
+    |> Enum.sort_by(&(&1.pr_xref))
+    |> Enum.dedup_by(&(&1.pr_xref))
     body = Enum.reduce(succeeded, "# Build succeeded", &gen_status_link/2)
     Enum.each(patches, &GitHub.post_comment!(token, project.repo_xref, &1.pr_xref, body))
   end

    token = GitHub.get_installation_token!(project.installation.installation_xref)
    stmp = "#{project.staging_branch}.tmp"
    patches = Repo.all(Patch.all_for_batch(batch.id))
    base = GitHub.copy_branch!(token, project.repo_xref, project.master_branch, stmp)
    do_merge_patch = fn patch, _branch ->
      GitHub.merge_branch!(token, project.repo_xref, patch.commit, stmp, "tmp")

  defp fail_batch(batch, erred) do
    project = batch.project
    token = GitHub.get_installation_token!(project.installation.installation_xref)
    patches = Repo.all(Patch.all_for_batch(batch.id))
    |> Enum.sort_by(&(&1.pr_xref))
    |> Enum.dedup_by(&(&1.pr_xref))
    body = Enum.reduce(erred, "# Build failed", &gen_status_link/2)
    body = case patches do
      [_patch] -> body
      [] -> raise("Empty patches make no sense")
      _ -> "#{body}\n\n*Retrying...*"
    end
    Enum.each(patches, &GitHub.post_comment!(token, project.repo_xref, &1.pr_xref, body))
    count = Enum.count(patches)
    if count > 1 do
      {patches_lo, patches_hi} = Enum.split(patches, div(count, 2))
      make_batch(patches_lo, project.id)
      make_batch(patches_hi, project.id)
    end
  end

  defp make_batch(patches, project_id) do
