@@ -5,22 +5,38 @@ List project submodules and get the commits they point to with python-gitlab.
55
66The [ Gitlab REST API V4] ( https://docs.gitlab.com/ee/api/api_resources.html )
77doesn't implement anything for submodule inspection yet. The only thing we can
8- currently do is [ updating a submodule to a new commit id] ( https://docs.gitlab.com/ee/api/repository_submodules.html ) ,
8+ currently do is [ updating a submodule to a new commit id] (
9+ https://docs.gitlab.com/ee/api/repository_submodules.html ),
910but how can we trigger such an update if we don't know the current commit id
1011of our submodule?
1112
1213If you're using ` python-gitlab ` and you're distributing shared code among
1314your projects with submodules, you've probably run into this issue already.
1415
1516This package provides minimal utils to list the submodules present in a
16- Gitlab project, and more importantly to get the commits they're pointing to
17- (when the submodules are Gitlab projects themselves, otherwise we cannot
18- access the project via their URLs using ` python-gitlab ` only).
17+ Gitlab project, and more importantly to get the commits they're pointing to.
1918
2019Internally, it reads and parses the ` .gitmodules ` file at the root of the
2120Project. To get the commit id of a submodule, it finds the last commit that
2221updated the submodule and parses its diff.
2322
23+ ---
24+ ** About the future of this package**
25+
26+ I don't plan to make PRs to ` python-gitlab ` for now.
27+
28+ In my opinion this problem should ideally be fixed in the Gitlab REST API,
29+ and then ` python-gitlab ` could wrap around the new endpoints.
30+
31+ So I see this package as a temporary solution until the API gets extended
32+ with more submodule functionalities.
33+
34+ [ @darkdragon-001 ] ( https://github.com/darkdragon-001 ) created an issue on
35+ GitLab about the lack of support for submodules, feel free to support it with
36+ a thumb up: https://gitlab.com/gitlab-org/gitlab/-/issues/352836
37+
38+ ---
39+
2440## Requirements
2541- Python >= __ 3.7__ (required by ` python-gitlab ` since version ` 3.0.0 ` )
2642
@@ -100,14 +116,13 @@ Output:
100116### ` iterate_subprojects(...) `
101117What you'll probably use most of the time.<br />
102118- Yields [ ` Subproject ` ] ( #class-subproject ) objects that describe the submodules.
103- - Ignores submodules that are not hosted on Gitlab. If you want to list all
104- modules present in the ` .gitmodules ` file but without mapping them to
105- ` gitlab.v4.objects.Project ` objects, use [ ` list_submodules(...) ` ] ( #list_submodules ) instead.
106119``` python
107120iterate_subprojects(
108121 project: Project,
109122 gl: Union[Gitlab, ProjectManager],
110123 ref: Optional[str ] = None ,
124+ only_gitlab_subprojects: bool = False ,
125+ self_managed_gitlab_host: Optional[str ] = None ,
111126 get_latest_commit_possible_if_not_found: bool = False ,
112127 get_latest_commit_possible_ref: Optional[str ] = None
113128) -> Generator[Subproject, None , None ]
@@ -118,13 +133,20 @@ Parameters:
118133 ` projects: gitlab.v4.objects.ProjectManager ` attribute
119134- ` ref ` : (optional) a ref to a branch, commit, tag etc. Defaults to the
120135 HEAD of the project default branch.
121- - ` get_latest_commit_possible_if_not_found ` : in some rare cases, there
122- won't be any ` Subproject commit ... ` info in the diff of the last commit
123- that updated the submodules. Set this option to ` True ` if you want to get
124- instead the most recent commit in the subproject that is anterior to the
136+ - ` only_gitlab_subprojects ` : (optional) if set to ` True ` , will ignore the
137+ submodules not hosted on GitLab. If set to ` False ` (default), it will yield
138+ [ ` Subproject ` ] ( #class-subproject ) objects with ` self.project = None `
139+ for submodules not hosted on GitLab.
140+ - ` self_managed_gitlab_host ` : (optional) if some submodules are hosted on a
141+ self-managed GitLab instance, you should pass its url here otherwise it
142+ may be impossible to know from the URL that it's a GitLab project.
143+ - ` get_latest_commit_possible_if_not_found ` : (optional) in some rare cases,
144+ there won't be any ` Subproject commit ... ` info in the diff of the last
145+ commit that updated the submodules. Set this option to ` True ` if you want to
146+ get instead the most recent commit in the subproject that is anterior to the
125147 commit that updated the submodules of the project. If your goal is to
126148 check that your submodules are up-to-date, you might want to use this.
127- - ` get_latest_commit_possible_ref ` : in case you set
149+ - ` get_latest_commit_possible_ref ` : (optional) in case you set
128150 ` get_latest_commit_possible_if_not_found ` to ` True ` , you can specify a ref for the
129151 subproject (for instance your submodule could point to a different branch
130152 than the main one). By default, the main branch of the subproject will be
@@ -140,10 +162,13 @@ returns a `list` of [`Subproject`](#class-subproject) objects.
140162Basic objects that contain the info about a Gitlab subproject.
141163
142164Attributes:
143- - ` project: gitlab.v4.objects.Project ` : the Gitlab project that the submodule links to
165+ - ` project: Optional[gitlab.v4.objects.Project] ` : the Gitlab project that the
166+ submodule links to (can be ` None ` if the submodule is not hosted on GitLab)
144167- ` submodule: ` [ ` Submodule ` ] ( #class-submodule ) : a basic object that contains
145168 the info found in the ` .gitmodules ` file (name, path, url).
146- - ` commit: gitlab.v4.objects.ProjectCommit ` : the commit that the submodule points to
169+ - ` commit: Union[gitlab.v4.objects.ProjectCommit, Commit] ` : the commit that
170+ the submodule points to (if the submodule is not hosted on GitLab, it will
171+ be a dummy ` Commit ` object with a single attribute ` id ` )
147172- ` commit_is_exact: bool ` : ` True ` most of the time, ` False ` only if the commit
148173 had to be guessed via the ` get_latest_commit_possible_if_not_found ` option
149174
@@ -195,6 +220,7 @@ hosted on Gitlab.
195220submodule_to_subproject(
196221 gitmodules_submodule: Submodule,
197222 gl: Union[Gitlab, ProjectManager],
223+ self_managed_gitlab_host: Optional[str ] = None ,
198224 get_latest_commit_possible_if_not_found: bool = False ,
199225 get_latest_commit_possible_ref: Optional[str ] = None
200226) -> Subproject
0 commit comments