Skip to content

Fix bug #3797: Ensure PR is still open before commenting#3804

Open
rtyley wants to merge 2 commits intomainfrom
ensure-pr-state-is-still-open-before-commenting
Open

Fix bug #3797: Ensure PR is still open before commenting#3804
rtyley wants to merge 2 commits intomainfrom
ensure-pr-state-is-still-open-before-commenting

Conversation

@rtyley
Copy link
Contributor

@rtyley rtyley commented Jan 28, 2026

Fixes this issue:

This PR is made of two commits:

  1. Add ForgeApiAlg.getPullRequest() (implementing this for the 6 Forge types, GitHub, GitLab, BitBucket, etc) - this new capability, of getting current information for the state of an existing PR, is required for this fix.
  2. Introduce PullRequestService, encapsulating PullRequestRepository & ForgeApiAlg

...when reviewing them, it's probably helpful to review each commit separately.

PullRequestService: combining calls to PullRequestRepository & ForgeApiAlg

Although the key fix here was to additionally hit the Forge API to see if the PRs returned by pullRequestRepository.getObsoleteOpenPullRequests() actually are still open, this leads to the creation of a PullRequestService that provides a combined interface to calls of the Forge API & the flat-file Repository.

It turns out there are several places within NurtureAlg where complementary pairs of calls to the Forge API and the flat-file Repository were occurring, and it simplifies NurtureAlg to make sure that PullRequestService takes over all of them:

  • createPullRequest replaces forgeApiAlg.createPullRequest & pullRequestRepository.createOrUpdate
  • closePullRequest replaces pullRequestRepository.changeState & forgeApiAlg.closePullRequest
  • listPullRequestsForUpdate replaces forgeApiAlg.listPullRequests & pullRequestRepository.createOrUpdate

Additionally, these two PullRequestService methods enhance existing PullRequestRepository methods with forgeApiAlg.getPullRequest where it previously should have been used, but wasn't:

  • getObsoleteOpenPullRequests enhances pullRequestRepository.getObsoleteOpenPullRequests
  • getRetractedOpenPullRequests enhances pullRequestRepository.getRetractedPullRequests

Testing

Testing ForgeApiAlg.getPullRequest()

Scala Steward supports six different Forge types (GitHub, GitLab, Gitea, etc) and so adding even one method to the ForgeApiAlg involves multiple implementations. I've included tests for all 6 implementations, but do not have user accounts on many of the forge types, and so have included best-guess stub test data that's based on what we already had:

Reusing existing test data without modification

  • GitLab
  • BitbucketServer

Reusing existing test data, single item extracted from the 'list PRs' case

  • GitHub
  • Bitbucket
  • AzureRepos
  • Gitea

Testing PullRequestService: requires new MockForgeApiAlg

In order to test the substantial new PullRequestService class, it was necessary to mock its two encapsulated components; PullRequestRepository (already supported in MockContext) and ForgeApiAlg - which did not have any existing test implementation. Mocking it requires a minimal in-memory implementation of the Forge API (eg, like the GitHub or BitBucket API) - something that can remember a PR has been created, what its state is, what the next PR number should be, etc.

This has been done with MockForgeApiAlg, implementing all the methods required for testing PullRequestRepository, and leaving other methods unimplemented with ???. The state for all this (MockForgeState) has been added as a field to the existing org.scalasteward.core.mock.MockState class, so it can used with existing MockEff testing.

Coverage

Coverage of the project as a whole slightly increases this this PR, but the codecov also points out that of the lines touched in the diff, 8 lines (all in NurtureAlg) are currently not covered by tests at all:

Having reviewed the fairly straightforward changes to NurtureAlg, and given the effort already expended getting testing working on PullRequestService, I feel that it's acceptable to allow the diff coverage to be lower than the target of 90%.

Next steps

After this has bedded in, we can re-enable the commenting temporarily disabled by:

cc @danicheg

@rtyley rtyley force-pushed the ensure-pr-state-is-still-open-before-commenting branch from 6d611ae to 6049847 Compare January 28, 2026 12:58
@codecov
Copy link

codecov bot commented Jan 28, 2026

Codecov Report

❌ Patch coverage is 87.69231% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.51%. Comparing base (e6ce04e) to head (7b428aa).

Files with missing lines Patch % Lines
...ala/org/scalasteward/core/nurture/NurtureAlg.scala 0.00% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3804      +/-   ##
==========================================
+ Coverage   90.03%   90.51%   +0.48%     
==========================================
  Files         174      176       +2     
  Lines        5058     5084      +26     
  Branches      464      445      -19     
==========================================
+ Hits         4554     4602      +48     
+ Misses        504      482      -22     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@rtyley rtyley force-pushed the ensure-pr-state-is-still-open-before-commenting branch 7 times, most recently from 2b83abc to 0792ddf Compare January 31, 2026 10:54
@rtyley rtyley changed the title WIP: Ensure PR is still open before commenting Fix bug #3797: Ensure PR is still open before commenting Jan 31, 2026
@rtyley rtyley force-pushed the ensure-pr-state-is-still-open-before-commenting branch 2 times, most recently from b0c90b0 to 9fe2ffc Compare January 31, 2026 11:24
@rtyley rtyley force-pushed the ensure-pr-state-is-still-open-before-commenting branch 8 times, most recently from b5323a6 to 668875b Compare March 3, 2026 18:53
@rtyley rtyley force-pushed the ensure-pr-state-is-still-open-before-commenting branch from 668875b to c1bcc6e Compare March 4, 2026 12:03
Fixing bug #3797 (Scala Steward commenting unnecessarily & noisily on PRs that
have already been closed) needs a way for Scala Steward to _know_ whether a
PR has been closed by external action (for example, simply a user _merging_ the
PR, which is what we _want_ the users to do!).

This is needed within `NurtureAlg.closeObsoletePullRequests()`, and typically
there could be _several_ PRs where we need to get latest status - and so a bulk
query (where we specify several PR ids) would probably be ideal. For the GitHub
_REST_ API, at least, that's not available, and so we're settling with adding a
single-item-fetching `ForgeApiAlg.getPullRequest()` method which we will call
several times.

Scala Steward supports SIX different Forge types (GitHub, GitLab, Gitea, etc) and
so adding even one method to the `ForgeApiAlg` involves multiple implementations.
I've included tests for all 6 implementations, but do not have accounts on many
of the forge types, and so have included best-guess stub test data that's based on
what we already had:

### Reusing existing test data without modification

* `GitLab`
* `BitbucketServer`

### Reusing existing test data, single item extracted from the 'list PRs' case

* `GitHub`
* `Bitbucket`
* `AzureRepos`
* `Gitea`
@rtyley rtyley force-pushed the ensure-pr-state-is-still-open-before-commenting branch 2 times, most recently from ffe0808 to a0a73d9 Compare March 4, 2026 12:37
Comment on lines -124 to 113
update = pullRequestRepository
update = pullRequestService
.getObsoleteOpenPullRequests(data.repo, _)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the key line that actually fixes issue #3797 - instead of just checking our file-based pullRequestRepository to query which of the pull-requests Scala Steward has opened are now obsolete, we now call our new pullRequestService - and that service additionally hits the Forge API to ensure that the PRs we get from the pullRequestRepository remain currently open.

This change introduces `PullRequestService`, encapsulating `PullRequestRepository` &
`ForgeApiAlg`
@rtyley rtyley force-pushed the ensure-pr-state-is-still-open-before-commenting branch from a0a73d9 to 7b428aa Compare March 4, 2026 14:41
@rtyley rtyley marked this pull request as ready for review March 4, 2026 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant