File: snippets.rst

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 (78 lines) | stat: -rw-r--r-- 1,837 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
########
Snippets
########

Reference
=========

* v4 API:

  + :class:`gitlab.v4.objects.Snippet`
  + :class:`gitlab.v4.objects.SnipptManager`
  + :attr:`gitlab.Gitlab.snippets`

* GitLab API: https://docs.gitlab.com/ce/api/snippets.html

Examples
========

List snippets owned by the current user::

    snippets = gl.snippets.list()

List the public snippets::

    public_snippets = gl.snippets.list_public()

List all snippets::

    all_snippets = gl.snippets.list_all()

.. warning::

   Only users with the Administrator or Auditor access levels can see all snippets
   (both personal and project). See the upstream API documentation for more details.

Get a snippet::

    snippet = gl.snippets.get(snippet_id)
    # get the content
    content = snippet.content()

.. warning::

   Blobs are entirely stored in memory unless you use the streaming feature.
   See :ref:`the artifacts example <streaming_example>`.


Create a snippet::

    snippet = gl.snippets.create({'title': 'snippet1',
                                  'files': [{
                                      'file_path': 'foo.py',
                                      'content': 'import gitlab'
                                   }],
                                })

Update the snippet attributes::

    snippet.visibility_level = gitlab.const.Visibility.PUBLIC
    snippet.save()

To update a snippet code you need to create a ``ProjectSnippet`` object::

    snippet = gl.snippets.get(snippet_id)
    project = gl.projects.get(snippet.projec_id, lazy=True)
    editable_snippet = project.snippets.get(snippet.id)
    editable_snippet.code = new_snippet_content
    editable_snippet.save()

Delete a snippet::

    gl.snippets.delete(snippet_id)
    # or
    snippet.delete()

Get user agent detail (admin only)::

    detail = snippet.user_agent_detail()