2.2 KiB · text History b868907
defmodule GitGud.Workflows.MergePushTest do
  @moduledoc """
  A merge moves the target branch without the post-receive hook, so the
  forge plays the push itself: the merge commit gets its `push` run (and
  webhook, caches, schedules) as if someone had pushed it.
  """
  use GitGud.DataCase, async: false
  use Oban.Testing, repo: GitGud.Repo

  alias GitGud.PullRequests
  alias GitGud.Repositories.Workers.CommitBackfill
  alias GitGud.Workflows.WorkflowRun

  import GitGud.ForgeFixtures
  import GitGud.WorkflowGitHelpers

  @moduletag :capture_log

  @workflow """
  name: ci
  on:
    push:
      branches: [main]
  jobs:
    build:
      runs-on: self-hosted
      steps:
        - run: echo hello
  """

  setup do
    {user, repo} = repository_fixture()
    main = commit!(repo, %{"README" => "hi\n"}, [])
    git!(repo.disk_path, ["update-ref", "refs/heads/main", main])

    feature =
      commit!(repo, %{"README" => "hi\n", ".forgejo/workflows/ci.yml" => @workflow}, [main])

    git!(repo.disk_path, ["update-ref", "refs/heads/feature", feature])

    {:ok, pr} = PullRequests.create_pull_request(repo, user, pr_attrs("feature", "main"))
    {:ok, repo: repo, user: user, pr: pr}
  end

  defp push_runs(repo) do
    from(r in WorkflowRun,
      where: r.repository_id == ^repo.id and r.trigger == "push",
      order_by: r.id
    )
    |> Repo.all()
  end

  test "the merge commit gets a push run, by whoever merged", ctx do
    {:ok, merged} = PullRequests.merge!(ctx.pr, ctx.user)

    assert_enqueued(
      worker: CommitBackfill,
      args: %{
        ref: "refs/heads/main",
        new_sha_hex: GitGud.Git.to_hex(merged.merge_commit_sha),
        pusher_id: ctx.user.id
      }
    )

    assert push_runs(ctx.repo) == []
    Oban.drain_queue(queue: :git_cache)

    assert [run] = push_runs(ctx.repo)
    assert run.head_sha == merged.merge_commit_sha
    assert run.head_ref == "refs/heads/main"
    assert run.triggered_by_id == ctx.user.id
  end

  test "a merge message with [skip ci] starts no push run", ctx do
    {:ok, _merged} = PullRequests.merge!(ctx.pr, ctx.user, message: "Merge it [skip ci]")
    Oban.drain_queue(queue: :git_cache)

    assert push_runs(ctx.repo) == []
  end
end