[Automation] Operate GitLab with Python to facilitate inquiry management

The incident is happening on the spot!

I'm developing software at a company, but at the development site, I get a lot of inquiries from sales people, saying, "The problem isn't happening at the evaluation site! It's happening at the site!"

The survey and answers to this inquiry will further exhaust the development members who are already exhausted.

It is unavoidable to receive inquiries from the field, but the problem is that the process from inquiry to answer is analog **.

  1. ** Inquiry email ** comes from the secretariat at the site
  2. The development window receives the email and ** fills in the inquiry management table **
  3. Request an email from the appropriate development member **

However, it is troublesome because it is basically all exchanged by e-mail, and the current state is difficult to understand for each inquiry **, which is the worst situation.

toiawase_before.png

Let's automate the process

I tried to improve this worst process as follows.

  1. ** Inquiry email ** comes from the secretariat at the site
  2. Use Microsoft's ** Power Automate ** to ** automatically add to the query management table in Excel Online **
  3. Use Python to search the query management table, and if a new query is added, ** automatically register an issue in GitLab **
  4. Development members will automatically receive a survey request email from GitLab

toiawase_after.png

This makes management a lot easier.

In this article

** 3. Use Python to search the inquiry management table, and if a new inquiry is added, automatically register an issue in GitLab **

In connection with, I will introduce how to ** manage Gitlab issues with Python **.

Working with GitLab in Python

You can operate GitLab with Python by using a package called ** python-gitlab **.

Installation

First, let's install python-gitlab with the pip install command.

pip install python-gitlab

Connect to GitLab

This is to connect to GitLab from Python, but GitLab is user-managed and inaccessible to anyone. Therefore, I will issue my own ** access token ** and use it to access GitLab in Python.

Issuing GitLab access tokens

First, let's issue your own "access token" from the GitLab settings screen.

1. Log in to GitLab and select "Settings" from the avatar image on the upper right.

gitlab_token.png

2. Select "Access Token" from the menu on the left.

gitlab_token2.png

3. Issue a token to access GitLab.

--Name: Give it an appropriate name for easy understanding. --Expiration date: You do not have to enter anything. In that case, the token will expire indefinitely. --Scope: Select api.

Press the button labeled "Create personal access token" to issue a token.

gitlab_token3.png

4. The character string of the issued access token is displayed.

Let's ** save ** the character string of this token in Notepad etc. If you leave this page, you will never be able to confirm it.

gitlab_token4.png

python-Create gitlab config file

In python-gitlab, set the necessary information in the config file and use that information to access GitLab. The access token issued above is also set in this config file.

Write the following content with a text editor and save it as ".python-gitlab.cfg" in the user folder (C: \ Users \ XXXX (user name)).

:.python-gitlab.cfg


[global]
default = gitlab
ssl_verify = true
timeout = 5

[gitlab]
url =GitLab URL
private_token =Personal access token

Thank you for your support. Now you are ready to go. Let's finally access GitLab with Python.

GitLab operation

Although python-gitlab can also manage users and milestones, this article will explain the operation of issues (also called "issues" or "tickets").

See also the official documentation (https://python-gitlab.readthedocs.io/en/stable/gl_objects/issues.html) if needed.

Access the project

Use the config file from earlier to access GitLab.

Python



import gitlab

gl = gitlab.Gitlab.from_config()

Then, use gl.projects.get () to specify the GitLab project ID and access the project. Log in to GitLab to check your project ID.

Python



project_id = 1234567 #Project ID
project = gl.projects.get(project_id)

You now have an object for that project.

Now that you're ready, it's a little scary to register an issue suddenly. So first, let's see if we can refer to the issue.

Refer to issue

I have registered such an issue in GitLab. Let's get these issues in Python.

gitlab_issue.png

Use project.issues.list () to get the issues registered in the project in a list. After that, use the for statement to retrieve the issue objects one by one.

Python



issues = project.issues.list()

for issue in issues:
    print("-------------------")
    print("【title】", issue.title)
    print("【Description】", issue.description)
    print("【Status】", issue.state)
    print("【Assignee】", issue.assignee["name"])
    print("【Due date】", issue.due_date)
    print("【Labels】", issue.labels)

Please refer to here for the attributes of issues.

The main attributes are listed below.

issue Attribute meaning
id ID
title title
description Description
state State (open/Close)
assignee Assignee
due_date Deadline
labels label

Execution result


-------------------
[Title] Visit Seki-sensei's shop!
[Description] It's hard!
Next time, Mr. Seki wants to talk with Majolica about the store!
I have to do something about it!
[Condition] closed
【Assignee】 Doremi
【Due date】 2020-09-13
【Labels】 []
-------------------
[Title] Soaring purchase price
[Description] This month, the purchase price has more than doubled compared to last month.

I feel like Dela is crazy, but ... There is a story that the prices of the magic society are soaring recently.
Let me consider measures from next month onward with a view to changing suppliers.
[Condition] opened
【Assignee】 Doremi
【Due date】 2020-09-18
【Labels】 ['management']
-------------------
[Title] Suspicious person
[Description] Recently, I heard a rumor that a suspicious person who seems to be an old man is wandering around MAHO-do.
Would you like to take some measures?
[Condition] opened
【Assignee】 Doremi
【Due date】 2020-09-25
【Labels】 []

You were able to get it successfully.

Register an issue

Then, it is issue registration of the main subject. Use project.issues.create () to register an issue.

Python



#Ticket registration
new_issue = project.issues.create({"title":"Autumn trip to Tohoku(Business trip sales)",
                                    "description":"This year as well, I will go on a business trip sale called Autumn Travel.",
                                    "due_date":"2020-10-20"})

print(new_issue.id)

Execution result


71385002

gitlab_issue_create.png

You have successfully registered!

Let the machine do what the machine can do

Machines are better than humans in watching incoming emails, writing to management tables, and managing status. As long as the program is correct, the machine is faster and more accurate and won't complain even if you work 24 hours a day.

I think we are very happy to live in an era where we can work with such a convenient machine. Let's use machines (programs) to free yourself and your friends from boring work!

Recommended Posts

[Automation] Operate GitLab with Python to facilitate inquiry management
Try to operate Facebook with Python
Operate Kinesis with Python
Operate Blender with Python
Operate Excel with Python (1)
Operate Excel with Python (2)
Try to operate DB with Python and visualize with d3
How to operate Discord API with Python (bot registration)
Operate Excel with Python openpyxl
Connect to BigQuery with Python
Operate TwitterBot with Lambda, Python
Connect to Wikipedia with Python
Post to slack with Python 3
[Note] Operate MongoDB with Python
[GUI with Python] PyQt5-Layout management-
[Python] [SQLite3] Operate SQLite with Python (Basic)
Password management with python: keyring
Introduction to her made with Python ~ Tinder automation project ~ Episode 6
Introduction to her made with Python ~ Tinder automation project ~ Episode 5
Operate Jupyter with REST API to extract and save Python code
Python: How to use async with
Operate a receipt printer with python
Link to get started with python
[Python] Write to csv file with Python
[Automation with python! ] Part 1: Setting file
Create folders from '01' to '12' with python
Output to csv file with Python
[Automation] Send Outlook email with Python
[Python] A memo to operate ROM created by GBDK with PyBoy
Convert list to DataFrame with python
MP3 to WAV conversion with Python
To do tail recursion with Python2
How to get started with Python
What to do with PYTHON release?
Unable to install Python with pyenv
How to use FTP with Python
Easily post to twitter with Python 3
[Automation with python! ] Part 2: File operation
I want to debug with Python
Try to reproduce color film with Python
Try logging in to qiita with Python
Change Python 64bit environment to 32bit environment with Anaconda
Test Python non-functional programs with GitLab CI
English speech recognition with python [speech to text]
HTML email with image to send with python
Memo to ask for KPI with python
[Automation] Manipulate mouse and keyboard with Python
Python to remember only with hello, worlds
Output color characters to pretty with python
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
Output Python log to console with GAE
Convert Excel data to JSON with python
Convert Hiragana to Romaji with Python (Beta)
Fractal to make and play with Python
I wanted to solve ABC160 with Python
Connect to MySQL with Python within Docker
Single pixel camera to experience with Python
[Python] Introduction to CNN with Pytorch MNIST
Convert FX 1-minute data to 5-minute data with Python
I want to analyze logs with Python
How to do portmanteau test with python