Home > Development Tools > git > body text

How to get the file list modified by MR in gitlab

PHPz
Release: 2023-03-31 10:35:53
Original
1976 people have browsed it

Code review in GitLab is a very important link, among which Merge Request (MR) is a very important way. When the code editor submits an MR on GitLab, you can discuss and change requests on the MR page.

When discussing MR, understanding the list of files modified in MR can help better understand and evaluate the changes made by developers. This article will introduce you how to get the file list modified by MR in GitLab.

Method 1: Obtain through the command line

First, you need to clone the repo locally.

Next, we need to install GitLab’s API. You can use pip to install in the following way:

pip install python-gitlab
Copy after login

After the installation is completed, we need to obtain the repo's ID and private token. You can obtain the private token by visiting the repo home page --> Click the three dots in the upper right corner --> Visit "Settings" --> Visit "Access Token".

After obtaining the private token, you need to set the environment variables required by the GitLab API:

export GITLAB_PRIVATE_TOKEN=<your_access_token>
export GITLAB_URL=https://<your_gitlab_server>/api/v4
Copy after login

Now, we can use the following command to obtain the modified files in MR:

gl = gitlab.Gitlab(os.environ['GITLAB_URL'], private_token=os.environ['GITLAB_PRIVATE_TOKEN'])
project = gl.projects.get(<project_id>)
merge = project.merge_requests.get(<merge_request_id>)
changes = merge.changes()
files = changes.get('changes')
for file in files:
    print(file['new_file']['path'])
Copy after login

You can replace <project_id> and <merge_request_id> in the code snippet with actual numbers.

Method 2: Obtain through GitLab API

First, you need to obtain the repo's ID and private token. Please refer to Method 1 for this process.

Below, we will introduce you how to use GitLab API to obtain modified files in MR.

We can send the following request (please replace <your_gitlab_server>, <project_id>, <merge_request_id> and other information):

GET https://<your_gitlab_server>/api/v4/projects/<project_id>/merge_requests/<merge_request_id>/changes
Copy after login

The response will look like this:

{
  "changes": [
    {
      "old_path": "Old file path",
      "new_path": "New file path"
    },
    ...
  ]
}
Copy after login

We can use the following Python code in the code to parse the response and get the file list:

import os
import requests

url = f"https://<your_gitlab_server>/api/v4/projects/<project_id>/merge_requests/<merge_request_id>/changes"
response = requests.get(url, headers={"PRIVATE-TOKEN": "<your_access_token>"})
changes = response.json()["changes"]

for change in changes:
    print(change["new_path"])
Copy after login

You can base it on your needs Modify the code for outputting file lists.

Summary

In this article, we introduced you how to obtain the file list modified by MR through the command line and GitLab API. Depending on your needs, you can choose any method. No matter which method you choose, it will help you better understand the changes in the code review.

The above is the detailed content of How to get the file list modified by MR in gitlab. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!