1.4 KiB · text History 53f3a6c
defmodule Mix.Tasks.Gitgud.BackfillLicenses do
  @moduledoc """
  Re-detect every repository's licence from the licence file at the tip
  of its default branch.

      mix gitgud.backfill_licenses

  A push refreshes a repository's licence, so this is only needed once,
  for repositories that predate detection — and for any that nobody is
  pushing to.

  Starts the Repo alone rather than the whole application, so it can be
  run against a live instance.

  The walk itself is `GitGud.Repositories.backfill_licenses/1`, shared
  with `GitGud.Release.backfill_licenses/0` — a release ships no Mix,
  and two copies of this would drift.
  """
  @shortdoc "Detect each repository's licence from its licence file"

  use Mix.Task

  alias GitGud.Repositories

  @impl true
  def run(_args) do
    Mix.Task.run("app.config")
    {:ok, _} = Application.ensure_all_started(:ecto_sql)
    {:ok, _} = Application.ensure_all_started(:postgrex)

    started? =
      case GitGud.Repo.start_link() do
        {:ok, _pid} -> true
        {:error, {:already_started, _pid}} -> false
      end

    {seen, detected} =
      Repositories.backfill_licenses(
        on_repo: fn %{repository: repo, license: license} ->
          Mix.shell().info("#{repo.name}: #{license || "(none detected)"}")
        end
      )

    Mix.shell().info("\n#{seen} repositories, #{detected} with a licence.")

    if started?, do: GitGud.Repo.stop()
  end
end