Skip to content

Commit bd05a6c

Browse files
committed
Initial commit
0 parents  commit bd05a6c

File tree

7 files changed

+233
-0
lines changed

7 files changed

+233
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
venv
2+
.idea
3+
main.py

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# GitLab Issue Creator
2+
3+
GitLab Issue Creator is a Python package that lets you create issues on a GitLab project. It is a simple wrapper around [Python GitLab](https://python-gitlab.readthedocs.io/en/stable/) with features limited only to the creation of issues. I built it to allow me to create GitLab issues for members of my teams based on the outputs of automated tasks.
4+
5+
## Installation
6+
To install GitLab Issue Creator enter the following command:
7+
8+
```python
9+
pip3 install git+https://github.com/practical-data-science/gitlab-issue-creator.git
10+
```
11+
12+
### Authentication
13+
To authenticate with GitLab you need to create a personal access token. You can do this by going to your profile page and clicking on the [Personal Access Tokens](https://gitlab.com/-/profile/personal_access_tokens) tab.
14+
15+
Import the `gitlab_issue_creator` module as `gic` and use the `authenticate()` function to authenticate with GitLab.
16+
17+
```python
18+
import gitlab_issue_creator as gic
19+
20+
gl = gic.authenticate('YOUR-PERSONAL-ACCESS-TOKEN')
21+
```
22+
23+
### Usage
24+
To create an issue you need to call the `create_issue()` function. This takes the following parameters: gl (the GitLab object), project_id (the ID of the project you want to create the issue in), title (the title of the issue), description (the description of the issue), labels (a list of labels to apply to the issue), the milestone_id (the ID of the milestone), and assignee_id (the ID of the user to assign the issue to).
25+
26+
```python
27+
issue = gic.create_issue(gl,
28+
project_id=987654321,
29+
title='Housekeeping / Fix URLs returning 404s',
30+
description='Please fix the below URLs returning 404s:',
31+
assignee_id=54321,
32+
milestone_id=123456,
33+
labels=['Content'])
34+
```
35+
36+
You can't attach a file to the main issue body in GitLab, so instead we'll create a note and attach the file of data to that. To do this we'll call the `upload_file_to_issue_note()` function. This takes the following parameters: gl (the GitLab object), project_id (the ID of the project you want to create the issue in), issue (the issue object you just created), filename (the name of the file), and filepath (the path and filename of the file).
37+
38+
```python
39+
gic.upload_file_to_issue_note(gl,
40+
project_id=987654321,
41+
issue=issue,
42+
filepath='404s.csv',
43+
filename='/data/404s.csv')
44+
45+
```
46+
47+
### Helpers
48+
In order to create an issue you'll require various IDs that aren't exposed in the GitLab interface, such as the `project_id`, `assignee_id`, and `milestone_id`.
49+
50+
#### Get the project ID
51+
The `get_owned_projects()` function returns a list of projects that the user owns. You'll find the names and IDs of projects in here.
52+
53+
```python
54+
owned_projects = gic.get_owned_projects(gl)
55+
```
56+
57+
#### Get the milestone ID
58+
The `get_project_milestones()` function returns a list of milestones for a project. You'll find the names and IDs of milestones in here.
59+
60+
```python
61+
milestones = gic.get_project_milestones(gl, 123456789)
62+
```
63+
64+
#### Get the user ID of a username
65+
The `get_user_by_username()` function returns the ID of a GitLab username.
66+
67+
```python
68+
user = gic.get_user_by_username(gl, 'your-username-here')
69+
```
70+

gitlab_issue_creator/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .issues import *

gitlab_issue_creator/issues.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
"""
2+
GitLab Issue Creator
3+
"""
4+
5+
import gitlab
6+
7+
8+
def authenticate(personal_access_token):
9+
"""Authenticates to GitLab.
10+
11+
Args:
12+
personal_access_token (str): The PERSONAL_ACCESS_TOKEN URL.
13+
"""
14+
15+
gl = gitlab.Gitlab(private_token=personal_access_token)
16+
gl.auth()
17+
18+
return gl
19+
20+
21+
def get_owned_projects(gl):
22+
"""Gets the owned projects.
23+
24+
Args:
25+
gl (gitlab.Gitlab): The GitLab object.
26+
"""
27+
28+
owned_projects = gl.projects.list(owned=True, archived=0)
29+
return owned_projects
30+
31+
32+
def get_project_by_id(gl, project_id):
33+
"""Gets the project by ID.
34+
35+
Args:
36+
gl (gitlab.Gitlab): The GitLab object.
37+
project_id (int): The project ID.
38+
"""
39+
40+
project = gl.projects.get(project_id)
41+
return project
42+
43+
44+
def get_project_milestones(gl, project_id):
45+
"""Gets the project milestones.
46+
47+
Args:
48+
gl (gitlab.Gitlab): The GitLab object.
49+
project_id (int): The project ID.
50+
"""
51+
52+
project = gl.projects.get(project_id)
53+
milestones = project.milestones.list(state='active')
54+
return milestones
55+
56+
57+
def get_user_by_username(gl, username):
58+
"""Gets the user by username.
59+
60+
Args:
61+
gl (gitlab.Gitlab): The GitLab object.
62+
username (str): The username.
63+
"""
64+
65+
user = gl.users.list(search=username)
66+
return user
67+
68+
69+
def create_issue(gl, project_id, title, description, assignee_id, milestone_id, labels: list):
70+
"""
71+
Create an issue.
72+
73+
Args:
74+
gl (gitlab.Gitlab): The GitLab object.
75+
project_id (int): The project ID.
76+
title (str): The issue title.
77+
description (str): The issue description.
78+
assignee_id (int): The assignee ID.
79+
milestone_id (int): The milestone ID.
80+
labels (list): The labels.
81+
"""
82+
83+
project = gl.projects.get(project_id)
84+
issue = project.issues.create({'title': title,
85+
'description': description,
86+
'assignee_id': assignee_id,
87+
'milestone_id': milestone_id,
88+
'labels': labels})
89+
return issue
90+
91+
92+
def add_note_to_issue(gl, project_id, issue, note):
93+
"""
94+
Add a note to an issue.
95+
96+
Args:
97+
gl (gitlab.Gitlab): The GitLab object.
98+
project_id (int): The project ID.
99+
issue (int): The issue object.
100+
note (str): The note.
101+
"""
102+
103+
project = gl.projects.get(project_id)
104+
issue.notes.create({'body': note})
105+
return issue
106+
107+
108+
def upload_file_to_issue_note(gl, project_id, issue, filepath, filename):
109+
"""
110+
Upload a file to an issue note.
111+
112+
Args:
113+
gl (gitlab.Gitlab): The GitLab object.
114+
project_id (int): The project ID.
115+
issue (int): The issue object.
116+
filepath (str): The file path.
117+
filename (str): The file name.
118+
"""
119+
120+
project = gl.projects.get(project_id)
121+
uploaded_file = project.upload(filename, filepath=filepath)
122+
issue.notes.create({
123+
"body": "See the [attached file]({})".format(uploaded_file["url"])
124+
})
125+
126+
return issue
127+

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python-gitlab

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[metadata]
2+
description-file = README.md

setup.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from setuptools import setup
2+
3+
from os import path
4+
this_directory = path.abspath(path.dirname(__file__))
5+
with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f:
6+
long_description = f.read()
7+
8+
setup(
9+
name='GitLab Issue Creator',
10+
packages=['gitlab_issue_creator'],
11+
version='0.001',
12+
license='MIT',
13+
description='GitLab Issue Creator is a Python package for creating GitLab issues.',
14+
long_description=long_description,
15+
long_description_content_type='text/markdown',
16+
author='Matt Clarke',
17+
author_email='[email protected]',
18+
url='https://github.com/practicaldatascience/gitlab-issue-creator',
19+
download_url='https://github.com/practicaldatascience/gitlab-issue-creator/archive/master.zip',
20+
keywords=['python', 'gitlab'],
21+
classifiers=[
22+
'Development Status :: 3 - Alpha',
23+
'Intended Audience :: Developers',
24+
'Topic :: Software Development :: Libraries :: Python Modules',
25+
'License :: OSI Approved :: MIT License',
26+
'Programming Language :: Python :: 3.8',
27+
],
28+
install_requires=['python-gitlab']
29+
)

0 commit comments

Comments
 (0)