Skip to content

GitHub import should rewrite #xxx links for MRs to !xxx format

Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.

Summary

GitHub import should rewrite #xxx links for MRs to !xxx format

Steps to reproduce

  1. Import a project from GitHub to GitLab, using the built-in GitHub importer. The project should contain one or more PR or issue comments containing a reference to a Pull Request (#xxx).
  2. The project will be imported, but the reference in the comment will remain as #xxx. Because GitLab uses different formats to refer to issues (#xxx) and Merge Requests (!xxx), the reference(s) will essentially be broken after the import.

Example Project

N/A

What is the current bug behavior?

See above. The references are retained as-is (#xxx) instead of being rewritten to !xxx format.

What is the expected correct behavior?

References to Merge Requests (only) should be rewritten to !xxx format, and references to issues should remain as #xxx.

Possible fixes

The challenge with this issue is to distinguish between which references should be retained as #xxx and which ones should be rewritten to !xxx format. GitHub uses the #xxx notion for both of these, while they differ in GitLab so logic is needed on our place to deal with this distinction.

For what it's worth, the following Rails console snippet seems to work fine on my (self-hosted GitLab Community) instance. It will surely need polishing and adjustments to be used in the GitHub importer proper, but feel free to use it as inspiration for a full fix. (I tried getting started with a GitLab Community Fork branch fixing this in the importer, but it's too complex for me given my current bandwidth.)

Update 2024-11-19: I fixed similar Rails scripts for issue and MR descriptions too. All of those are available in a dedicated repo in my GitLab instance here. The other scripts are also available further down in this issue: #478851 (comment 2218190231).

# Loops over all issue and MR notes and fixes #xxx GitHub ambiguous PR/issue
# references where the xxx number refers to an existing GitLab MR.
#
notes = Note.all

notes.each { |note|
  modified = false

  issue_or_mr_references = note.note.scan(/#\d+/)

  issue_or_mr_references.each { |iid|
    iid.delete!('#')

    merge_request = MergeRequest.find_by(iid: iid)

    if merge_request
      note.note.sub!("\##{iid}", "!#{iid}")
      modified = true
    end
  }

  if modified
    puts "Note with ID #{note.id} updated"
    note.save!
  end
}
Edited by 🤖 GitLab Bot 🤖