fs2gitlab.py 2.2 KB
Newer Older
Paul Spooren's avatar
Paul Spooren committed
1
2
3
4
5
import gitlab
import peewee as pw
import time


6
# load local gitlab token
Paul Spooren's avatar
Paul Spooren committed
7
8
9
with open("token_com", "r") as token_file:
    token = token_file.readline()[:-1]

10
# connect to gitlab
Paul Spooren's avatar
Paul Spooren committed
11
12
13
14
gl = gitlab.Gitlab("https://gitlab.com", private_token=token)
gl.auth()
project = gl.projects.get(14341737)

15
# connect to local running mariadb with flyspray bugs
Paul Spooren's avatar
Paul Spooren committed
16
17
18
19
db = pw.MySQLDatabase("bugs", host="172.17.0.2", port=3306, user="root", passwd="bugs")
db.connect()


20
21
22
23
24
25
# get a list of all tasks
with open("select_tasks.sql", "r") as tasks_file:
    cursor = db.execute_sql(tasks_file.read())
    tasks = list(cursor.fetchall())

for task in tasks:
Paul Spooren's avatar
Paul Spooren committed
26
    try:
27
        # try getting task, if this fails the task does not yet exists...
Paul Spooren's avatar
Paul Spooren committed
28
29
        issue = project.issues.get(task[0])
        print("-", end="", flush=True)
30
31
32
    except Exception as e:
        # ... so create it here
        print(e)
Paul Spooren's avatar
Paul Spooren committed
33
34
35
36
37
38
39
40
41
42
43
44
        labels = []
        if task[3] != "Feature Request":
            labels.append("type:" + task[3])

        issue_gl = {
            "id": "openwrt/openwrt",
            "iid": task[0],
            "created_at": task[1],
            "title": task[7],
            "labels": labels,
            "description": task[8].replace("@", "@ "),
        }
45
        issue = project.issues.create(issue_gl)
Paul Spooren's avatar
Paul Spooren committed
46
47

        print("+", end="", flush=True)
48
49
        # gitlab.com has a rate limit of 10 add actions per minute
        time.sleep(7)
Paul Spooren's avatar
Paul Spooren committed
50

51
52
    # close closed tasks
    if task[4] and issue.state != "closed":
Paul Spooren's avatar
Paul Spooren committed
53
54
        issue.state_event = "close"
        print("x", end="", flush=True)
55
56
57
58
59
60
        issue.save()

# now add comment
for task in tasks:
    # get the issue
    issue = project.issues.get(task[0])
Paul Spooren's avatar
Paul Spooren committed
61

62
    # get comments of the issue
Paul Spooren's avatar
Paul Spooren committed
63
    with open("select_comments.sql", "r") as comments_file:
64
        cursor = db.execute_sql(comments_file.read(), (str(task[0]),))
Paul Spooren's avatar
Paul Spooren committed
65
66
        comments = cursor.fetchall()

67
68
    # iterate over only new comment, as comments don't have unique id
    # if a comment would be edited, it is not covered here
Paul Spooren's avatar
Paul Spooren committed
69
    for comment in comments[len(issue.notes.list()) :]:
70
71
72
73
74
75
        issue.notes.create(
            {"created_at": comment[0], "body": comment[1].replace("@", "@ ")}
        )
        print("c", end="", flush=True)
        # same rate limit as above
        time.sleep(7)