File: test_cli_repository.py

package info (click to toggle)
python-gitlab 1%3A4.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,048 kB
  • sloc: python: 24,168; makefile: 171; ruby: 27; javascript: 3
file content (151 lines) | stat: -rw-r--r-- 3,791 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import json
import time


def test_project_create_file(gitlab_cli, project):
    file_path = "README"
    branch = "main"
    content = "CONTENT"
    commit_message = "Initial commit"

    cmd = [
        "project-file",
        "create",
        "--project-id",
        project.id,
        "--file-path",
        file_path,
        "--branch",
        branch,
        "--content",
        content,
        "--commit-message",
        commit_message,
    ]
    ret = gitlab_cli(cmd)

    assert ret.success


def test_list_all_commits(gitlab_cli, project):
    data = {
        "branch": "new-branch",
        "start_branch": "main",
        "commit_message": "chore: test commit on new branch",
        "actions": [
            {
                "action": "create",
                "file_path": "test-cli-repo.md",
                "content": "new content",
            }
        ],
    }
    commit = project.commits.create(data)

    cmd = ["project-commit", "list", "--project-id", project.id, "--get-all"]
    ret = gitlab_cli(cmd)
    assert commit.id not in ret.stdout

    # Listing commits on other branches requires `all` parameter passed to the API
    cmd = [
        "project-commit",
        "list",
        "--project-id",
        project.id,
        "--get-all",
        "--all",
        "true",
    ]
    ret_all = gitlab_cli(cmd)
    assert commit.id in ret_all.stdout
    assert len(ret_all.stdout) > len(ret.stdout)


def test_list_merge_request_commits(gitlab_cli, merge_request, project):
    cmd = [
        "project-merge-request",
        "commits",
        "--project-id",
        project.id,
        "--iid",
        merge_request.iid,
    ]

    ret = gitlab_cli(cmd)
    assert ret.success
    assert ret.stdout


def test_commit_merge_requests(gitlab_cli, project, merge_request):
    """This tests the `project-commit merge-requests` command and also tests
    that we can print the result using the `json` formatter"""

    # Pause to let GL catch up (happens on hosted too, sometimes takes a while for server to be ready to merge)
    time.sleep(30)

    merge_result = merge_request.merge(should_remove_source_branch=True)
    # Pause to let GL catch up (happens on hosted too, sometimes takes a while for server to be ready to merge)
    time.sleep(5)

    # Wait until it is merged
    mr = None
    mr_iid = merge_request.iid
    for _ in range(60):
        mr = project.mergerequests.get(mr_iid)
        if mr.merged_at is not None:
            break
        time.sleep(0.5)

    assert mr is not None
    assert mr.merged_at is not None
    # Pause to let GL catch up (happens on hosted too, sometimes takes a while for server to be ready to merge)
    time.sleep(5)

    commit_sha = merge_result["sha"]
    cmd = [
        "-o",
        "json",
        "project-commit",
        "merge-requests",
        "--project-id",
        project.id,
        "--id",
        commit_sha,
    ]
    ret = gitlab_cli(cmd)
    assert ret.success

    json_list = json.loads(ret.stdout)
    assert isinstance(json_list, list)
    assert len(json_list) == 1
    mr_dict = json_list[0]
    assert mr_dict["id"] == mr.id
    assert mr_dict["iid"] == mr.iid


def test_revert_commit(gitlab_cli, project):
    commit = project.commits.list()[0]

    cmd = [
        "project-commit",
        "revert",
        "--project-id",
        project.id,
        "--id",
        commit.id,
        "--branch",
        "main",
    ]
    ret = gitlab_cli(cmd)

    assert ret.success


def test_get_commit_signature_not_found(gitlab_cli, project):
    commit = project.commits.list()[0]

    cmd = ["project-commit", "signature", "--project-id", project.id, "--id", commit.id]
    ret = gitlab_cli(cmd)

    assert not ret.success
    assert "404 Signature Not Found" in ret.stderr