async function isPullRequestClosedWithAssociatedCommit()

in ng-dev/release/publish/pull-request-state.ts [42:77]


async function isPullRequestClosedWithAssociatedCommit(api: GitClient, id: number) {
  const events = await api.github.paginate(api.github.issues.listEvents, {
    ...api.remoteParams,
    issue_number: id,
  });
  // Iterate through the events of the pull request in reverse. We want to find the most
  // recent events and check if the PR has been closed with a commit associated with it.
  // If the PR has been closed through a commit, we assume that the PR has been merged
  // using the autosquash merge strategy. For more details. See the `AutosquashMergeStrategy`.
  for (let i = events.length - 1; i >= 0; i--) {
    const {event, commit_id} = events[i];
    // If we come across a "reopened" event, we abort looking for referenced commits. Any
    // commits that closed the PR before, are no longer relevant and did not close the PR.
    if (event === 'reopened') {
      return false;
    }
    // If a `closed` event is captured with a commit assigned, then we assume that
    // this PR has been merged properly.
    if (event === 'closed' && commit_id) {
      return true;
    }
    // If the PR has been referenced by a commit, check if the commit closes this pull
    // request. Note that this is needed besides checking `closed` as PRs could be merged
    // into any non-default branch where the `Closes <..>` keyword does not work and the PR
    // is simply closed without an associated `commit_id`. For more details see:
    // https://docs.github.com/en/enterprise/2.16/user/github/managing-your-work-on-github/closing-issues-using-keywords#:~:text=non-default.
    if (
      event === 'referenced' &&
      commit_id &&
      (await isCommitClosingPullRequest(api, commit_id, id))
    ) {
      return true;
    }
  }
  return false;
}