Created
June 3, 2021 21:00
-
-
Save amadeubarbosa/9a93088c1ea0f522d1db5a268b046dda to your computer and use it in GitHub Desktop.
Recreating merge-request in another repository
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// WHY? | |
// https://forum.gitlab.com/t/how-to-move-merge-request-between-projects/52914 | |
// OLD PROJECT: | |
let r = await fetch("https://gitlab.xxx.com/api/v4/projects/269/merge_requests?labels=Moving") | |
let mr = await r.json() | |
console.log(mr) | |
// ADD NEW PROJECTS AS ADDITIONAL REMOTES: | |
// git remote add mongodb https://gitlab.xxx.com/xxx/xxx-data/xxx-storage/xxx-mongodb.git | |
// git remote add postgresql https://xxx.intelie.com/xxx/xxx-data/xxx-storage/xxx-postgresql.git | |
// git remote add sql https://gitlab.xxx.com/xxx/xxx-data/xxx-storage/xxx-sql.git | |
let projects = { | |
"sql": 1254, | |
"mongodb": 1252, | |
"postgresql": 1253 | |
} | |
// MANUAL STEP: a human should classify the MR and the destination projects | |
let destination = { | |
"sql": [1062, 865], | |
"mongodb": [1036, 748, 632], | |
"postgresql": [1030, 1003, 988] | |
} | |
// RECREATING THE BRANCHES IN THE EACH NEW REMOTES ACCORDILYING | |
Object.keys(destination) | |
.flatMap(project => | |
destination[project].map(iid => { | |
let branch = mr.find(item => item.iid === iid).source_branch | |
return "git push " + project + " 'refs/remotes/origin/" + branch + ":refs/heads/" + branch + "'" | |
}) | |
) | |
let newMergeRequests = {} | |
mr.forEach(async original => { | |
// LISTING ALL NOTES | |
// GET /projects/:id/merge_requests/:merge_request_iid/notes?per_page=100 | |
let response = await fetch("https://gitlab.xxx.com/api/v4/projects/" + original.project_id + "/merge_requests/" + original.iid + "/notes") | |
let allNotes = await response.json() | |
let userNotes = allNotes.filter(note => note.system === false) | |
// TODO: HOW TO CREATE A DIFF NOTE | |
// if note.type === 'DiffNote' .. | |
// POST https://gitlab.intelie.com/amadeu.barbosa/live/notes?target_id=9029&target_type=merge_request | |
// { | |
// _discussion_id: "", | |
// line_type: "old", | |
// merge_request_diff_head_sha: "9999c8d5ac3cbb3620592df14c84cb8cbabafb21", | |
// note: { | |
// commit_id: null, | |
// line_code: "587d266bb27a4dc3022bbed44dfa19849df3044c_145_145", | |
// note: "aaaaaaaaaa", | |
// noteable_id: 9029, | |
// noteable_type: "MergeRequest", | |
// position: "{\"base_sha\":\"6cab25c38045891fbf4b0ad84744a6252b478063\",\"start_sha\":\"6cab25c38045891fbf4b0ad84744a6252b478063\",\"head_sha\":\"9999c8d5ac3cbb3620592df14c84cb8cbabafb21\",\"old_path\":\".gitlab-ci.yml\",\"new_path\":\".gitlab-ci.yml\",\"position_type\":\"text\",\"old_line\":null,\"new_line\":145,\"line_range\":{\"start\":{\"line_code\":\"587d266bb27a4dc3022bbed44dfa19849df3044c_145_145\",\"type\":\"new\",\"old_line\":null,\"new_line\":145},\"end\":{\"line_code\":\"587d266bb27a4dc3022bbed44dfa19849df3044c_145_145\",\"type\":\"new\",\"old_line\":null,\"new_line\":145}}}", | |
// type: "DiffNote" | |
// }, | |
// note_project_id: "", | |
// return_discussion: true, | |
// target_id: 9029, | |
// target_type: "merge_request", | |
// view: "inline" | |
// } | |
// TODO: HOW TO REPLY IN A THREAD | |
// https://gitlab.intelie.com/amadeu.barbosa/live/notes?target_id=9029&target_type=merge_request | |
// { | |
// in_reply_to_discussion_id: "3269e5ce4185ccd21dff552d26522cdc6ae535bf", | |
// note: { | |
// note: "bbbbbb" | |
// }, | |
// target_type: "merge_request" | |
// } | |
let targetProject = Object.keys(destination) | |
.filter(newProjName => destination[newProjName].includes(original.iid)) | |
if (targetProject.length > 1) { | |
console.error("Failure a merge-request should not be related more than once", targetProject, original.iid) | |
} else { | |
let targetProjName = targetProject[0] | |
newMergeRequests[targetProjName] = newMergeRequests[targetProjName] || [] | |
newMergeRequests[targetProjName].push({ | |
payload: { | |
title: original.title, | |
description: original.description, | |
labels: original.labels, | |
assignee_id: original.assignee?.id, | |
reviewer_ids: original.reviewers?.map(rev => rev.id), | |
squash: false, | |
source_branch: original.source_branch, | |
target_branch: original.target_branch, | |
remove_source_branch: true | |
}, | |
notes: userNotes, | |
original_iid: original.iid, | |
original_project_id: original.project_id, | |
original_author: original.author | |
}) | |
} | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment