| 1 |
# |
| 2 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
# you may not use this file except in compliance with the License. |
| 4 |
# You may obtain a copy of the License at |
| 5 |
# |
| 6 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
# |
| 8 |
# Unless required by applicable law or agreed to in writing, software |
| 9 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
# See the License for the specific language governing permissions and |
| 12 |
# limitations under the License. |
| 13 |
|
| 14 |
""" |
| 15 |
gitlab_lfs_mirror - plugin for accessing files stored in git-lfs on GitLab |
| 16 |
========================================================================== |
| 17 |
|
| 18 |
.. note:: |
| 19 |
|
| 20 |
The ``gitlab_lfs_mirror`` plugin is available *Since 2.4.0* |
| 21 |
|
| 22 |
If you store files in a git-lfs repository on gitlab.com, you can access |
| 23 |
them using a URL like |
| 24 |
|
| 25 |
https://gitlab.com/path/to/repo/-/raw/master/path/to/file |
| 26 |
|
| 27 |
which you can then use as a mirror for buildstream. However this only works for |
| 28 |
public repositories. For internal (on self-hosted GitLab instances) and private |
| 29 |
repositories, the above doesn't work since buildstream cannot authenticate with |
| 30 |
the GitLab web UI. |
| 31 |
|
| 32 |
This plugin solves this by going through the GitLab REST API. You need an |
| 33 |
access token that access the Repository Files API (i.e. with read_api or |
| 34 |
read_repository scope). As of this writing, the GitLab CI/CD job token doesn't |
| 35 |
allow access to this API endpoint, so you need a dedicated access token. |
| 36 |
|
| 37 |
**Usage:** |
| 38 |
|
| 39 |
.. code:: yaml |
| 40 |
|
| 41 |
- name: my-mirror |
| 42 |
kind: gitlab_lfs_mirror |
| 43 |
config: |
| 44 |
url: https://gitlab.example.com/ |
| 45 |
project: mirrors/{alias} |
| 46 |
ref: main # optional, defaults to master |
| 47 |
aliases: |
| 48 |
- my-alias |
| 49 |
- another-alias |
| 50 |
""" |
| 51 |
|
| 52 |
from posixpath import join |
| 53 |
from urllib.parse import quote |
| 54 |
|
| 55 |
from buildstream import SourceMirror |
| 56 |
|
| 57 |
|
| 58 |
class GitlabLFSMirror(SourceMirror): |
| 59 |
BST_MIN_VERSION = "2.2" |
| 60 |
|
| 61 |
def configure(self, node): |
| 62 |
node.validate_keys(["aliases", "url", "project", "ref"]) |
| 63 |
self.set_supported_aliases(node.get_str_list("aliases")) |
| 64 |
|
| 65 |
self.url = node.get_str("url") |
| 66 |
self.project = node.get_str("project") |
| 67 |
self.ref = node.get_str("ref", "master") |
| 68 |
|
| 69 |
def translate_url(self, *, alias, alias_url, source_url, extra_data): |
| 70 |
project_id = quote(self.project.format(alias=alias), safe="") |
| 71 |
filename = quote(source_url, safe="") |
| 72 |
|
| 73 |
translated_url = join( |
| 74 |
self.url, |
| 75 |
"api/v4/projects", |
| 76 |
project_id, |
| 77 |
"repository/files", |
| 78 |
filename, |
| 79 |
f"raw?ref={self.ref}&lfs=true", |
| 80 |
) |
| 81 |
|
| 82 |
if extra_data is not None: |
| 83 |
extra_data["http-auth"] = "bearer" |
| 84 |
|
| 85 |
return translated_url |
| 86 |
|
| 87 |
|
| 88 |
def setup(): |
| 89 |
return GitlabLFSMirror |