2

In my organization, we are using a free version of GitLab - I wanted to know If there is a way to export GitLab Merge requests + comments in a CSV file

Looking for help!!

Chandler
  • 23
  • 1
  • 6

2 Answers2

2

Since gitlab 13.6 you can export your merge-requests from the gui to a csv file.

In the left side bar of your project you need to select merge-requests and there you have the "Export as CSV" button which will send a cvs file containing all merge-requests to your configured email address.

enter image description here

For lower gitlab versions you can take a look at the merge-requests api and try something like this

GET /merge_requests?scope=all
danielnelz
  • 3,794
  • 4
  • 25
  • 35
0

The previous answers work for single projects, but I wanted to export all of my MRs across my org's GitLab instance.

I asked ChatGPT-4 to do this for me, and I used it successfully, it worked perfectly the first time. I hope this answer is acceptable because I fully tested it; I also had to augment it to use an environment variable with my token, vs its original suggestion of hardcoding it.

It's in python:

import requests
import csv
import json
import os

# Set your GitLab instance URL, Personal Access Token, and username
gitlab_url = "https://gitlab.myorg.com"
token = os.environ["MY_GITLAB_TOKEN"]
username = "your username"

# Set the API endpoint to get MRs
api_url = f"{gitlab_url}/api/v4/merge_requests?scope=all&state=all&author_username={username}&per_page=100"

# Add your Personal Access Token to the request headers
headers = {"Private-Token": token}

# Send the API request and get the response
response = requests.get(api_url, headers=headers)
merge_requests = response.json()

# Write the Merge Requests to a CSV file
with open("merge_requests.csv", mode="w", newline="", encoding="utf-8") as csvfile:
    fieldnames = ["id", "title", "state", "created_at", "updated_at", "web_url"]
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    for mr in merge_requests:
        writer.writerow({k: mr[k] for k in fieldnames})

This creates a file called merge_requests.csv in the directory you run the script in. I didn't try, but you could most likely play with the fieldnames to get exactly the data you want.

Max Cascone
  • 648
  • 9
  • 25
  • Using ChatGPT to post answers is forbidden. See https://meta.stackoverflow.com/questions/421831/temporary-policy-chatgpt-is-banned – Eric Aya Mar 31 '23 at 08:52
  • 1
    I know. I know! I figure, honesty’s the best policy. I’m a long-time user. I have been using gpt to help with my professional work recently, and it’s been a massive accelerator. In this case, I used it, checked its accuracy, changed it a little, tested it, used it in my work, and shared it here. I could have opted to keep its involvement secret, but again, honestly prevails. – Max Cascone Mar 31 '23 at 12:22
  • You said `I fully tested it` which led me to post this comment instead of flagging for moderator. I believe you. However, I think the ban on GPT answers doesn't make the distinction and will probably apply if someone else flags your answer. My comment was just to let you know about the ban in case you didn't. – Eric Aya Apr 01 '23 at 12:35