Repository: internetarchive/openlibrary

Issue:
Librarian Merge Queue: Create merge request on work merge for super librarians
<!-- What problem are we solving? What does the experience look like today? What are the symptoms? -->
### Evidence / Screenshot (if possible)
Today, merge requests are created for all patrons whenever the ILE "Merge" button is pressed.  This is fine for most patrons, but causes problems when a super librarian clicks "Merge" button, determines that the merge is unnecessary, and doesn't reject the request by pressing the red "Reject Merge" button in the merge UI.  This is resulting in a number of requests that have a "Pending" state, but should be closed.

### Relevant url?
<!-- `https://openlibrary.org/...` -->

### Steps to Reproduce
<!-- What steps caused you to find the bug? -->
1. Go to ...
2. Do ...

<!-- What actually happened after these steps? What did you expect to happen? -->
* Actual:
* Expected:

### Details

- **Logged in (Y/N)?**Y
- **Browser type/version?**Any
- **Operating system?**Any
- **Environment (prod/dev/local)?** prod
<!-- If not sure, put prod -->

### Proposal & Constraints
<!-- What is the proposed solution / implementation? Is there a precedent of this approach succeeding elsewhere? -->
For super librarians only, postpone merge request creation until the merge is submitted via the merge UI.  When a super librarian makes a merge, create an MR with status "Merged" and submitter and reviewer set to the super librarian's username.

### Related files
<!-- Files related to this issue; this is super useful for new contributors who might want to help! If you're not sure, leave this blank; a maintainer will add them. -->

### Stakeholders
<!-- @ tag stakeholders of this bug -->



Discussion:

Resolving patch:
diff --git a/openlibrary/core/edits.py b/openlibrary/core/edits.py
--- a/openlibrary/core/edits.py
+++ b/openlibrary/core/edits.py
@@ -122,7 +122,12 @@ def where_clause(cls, mode, **kwargs):
 
     @classmethod
     def submit_work_merge_request(
-        cls, work_ids: list[str], submitter: str, comment: str = None
+        cls,
+        work_ids: list[str],
+        submitter: str,
+        comment: str = None,
+        reviewer: str = None,
+        status: int = STATUS['PENDING'],
     ):
         """
         Creates new work merge requests with the given work olids.
@@ -132,7 +137,13 @@ def submit_work_merge_request(
 
         url = f"/works/merge?records={','.join(work_ids)}"
         if not cls.exists(url):
-            return cls.submit_request(url, submitter=submitter, comment=comment)
+            return cls.submit_request(
+                url,
+                submitter=submitter,
+                comment=comment,
+                reviewer=reviewer,
+                status=status,
+            )
 
     @classmethod
     def submit_author_merge_request(cls, author_ids, submitter, comment=None):
diff --git a/openlibrary/plugins/upstream/code.py b/openlibrary/plugins/upstream/code.py
--- a/openlibrary/plugins/upstream/code.py
+++ b/openlibrary/plugins/upstream/code.py
@@ -109,9 +109,7 @@ def GET(self):
         )
         if not has_access:
             raise web.HTTPError('403 Forbidden')
-        if not i.mrid:
-            username = user['key'].split('/')[-1]
-            i.mrid = create_request(i.records, username)
+
         return render_template('merge/works', mrid=i.mrid)
 
 
diff --git a/openlibrary/plugins/upstream/edits.py b/openlibrary/plugins/upstream/edits.py
--- a/openlibrary/plugins/upstream/edits.py
+++ b/openlibrary/plugins/upstream/edits.py
@@ -30,7 +30,7 @@ def response(status='ok', **kwargs):
             work_ids="",  # Comma-separated OLIDs (OL1W,OL2W,OL3W,...,OL111W)
             rtype="merge-works",
             mrid=None,
-            action=None,  # create, approve, decline, comment, unassign
+            action=None,  # create, approve, decline, comment, unassign, create-merged
             comment=None,
         )
         user = accounts.get_current_user()
@@ -84,6 +84,16 @@ def response(status='ok', **kwargs):
                 return delegate.RawText(
                     json.dumps(resp), content_type="application/json"
                 )
+            elif i.action == 'create-merged':
+                result = CommunityEditsQueue.submit_work_merge_request(
+                    i.work_ids.split(','),
+                    submitter=username,
+                    reviewer=username,
+                    status=CommunityEditsQueue.STATUS['MERGED'],
+                )
+                return delegate.RawText(
+                    json.dumps(response(id=result)), content_type='application/json'
+                )
 
     def GET(self):
         i = web.input(page=1, limit=25, mode="open", submitter=None, reviewer=None)


Test patch: