Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions nx-set-shas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,29 @@ async function commitExists(
});

// Check the commit exists on the expected main branch (it will not in the case of a rebased main branch)
const commits = await octokit.request('GET /repos/{owner}/{repo}/commits', {
owner,
repo,
sha: branchName,
per_page: 100,
});
let maxPages = 20; // This could be made a input param to allow larger/longer searches
let commitFound = false;
yield octokit.paginate("GET /repos/{owner}/{repo}/commits", {
owner,
repo,
sha: branchName,
per_page: 100,
}, (response, done) => {
if (response.data.some((commit: { sha: string }) => commit.sha === commitSha)) {
commitFound = true;
done(); // Stop pagination if commit is found
}

return commits.data.some(
(commit: { sha: string }) => commit.sha === commitSha,
// Decrement maxPages and stop if limit reached
if (maxPages <= 1) { // Use <= 1 because it's decremented after checking
done();
}
maxPages--;
return response;
}
);

return commitFound;
Comment on lines +260 to +281
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work outside of a generator. Can you modify it to use async iterrator instead?

    let maxPages = 20; // TODO: This could be made an input param to allow larger/longer searches
    for await (const response of octokit.paginate.iterator(
      'GET /repos/{owner}/{repo}/commits',
      {
        owner,
        repo,
        sha: branchName,
        per_page: 100,
      },
    )) {
      if (
        response.data.some(
          (commit: { sha: string }) => commit.sha === commitSha,
        )
      ) {
        return true;
      }
      maxPages--;
      if (maxPages <= 1) {
        break;
      }
    }

    return false;

} catch {
return false;
}
Expand Down