| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 735efff commit 0d41da3
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -95,6 +95,7 @@ Get a single MR:: | |||
| 95 | 95 | mr = project.mergerequests.get(mr_iid) | |
| 96 | 96 | ||
| 97 | 97 | Get MR reviewer details:: | |
| 98 | + | ||
| 98 | 99 | mr = project.mergerequests.get(mr_iid) | |
| 99 | 100 | reviewers = mr.reviewer_details.list() | |
| 100 | 101 | ||
@@ -105,6 +106,13 @@ Create a MR:: | |||
| 105 | 106 | 'title': 'merge cool feature', | |
| 106 | 107 | 'labels': ['label1', 'label2']}) | |
| 107 | 108 | ||
| 109 | + # Use a project MR description template | ||
| 110 | + mr_description_template = project.merge_request_templates.get("Default") | ||
| 111 | + mr = project.mergerequests.create({'source_branch': 'cool_feature', | ||
| 112 | + 'target_branch': 'main', | ||
| 113 | + 'title': 'merge cool feature', | ||
| 114 | + 'description': mr_description_template.content}) | ||
| 115 | + | ||
| 108 | 116 | Update a MR:: | |
| 109 | 117 | ||
| 110 | 118 | mr.description = 'New description' | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -99,7 +99,7 @@ Reference | |||
| 99 | 99 | + :class:`gitlab.v4.objects.DockerfileManager` | |
| 100 | 100 | + :attr:`gitlab.Gitlab.gitlabciymls` | |
| 101 | 101 | ||
| 102 | - * GitLab API: Not documented. | ||
| 102 | + * GitLab API: https://docs.gitlab.com/ce/api/templates/dockerfiles.html | ||
| 103 | 103 | ||
| 104 | 104 | Examples | |
| 105 | 105 | -------- | |
@@ -112,3 +112,73 @@ Get a Dockerfile template:: | |||
| 112 | 112 | ||
| 113 | 113 | dockerfile = gl.dockerfiles.get('Python') | |
| 114 | 114 | print(dockerfile.content) | |
| 115 | + | ||
| 116 | + Project templates | ||
| 117 | + ========================= | ||
| 118 | + | ||
| 119 | + These templates are project-specific versions of the templates above, as | ||
| 120 | + well as issue and merge request templates. | ||
| 121 | + | ||
| 122 | + Reference | ||
| 123 | + --------- | ||
| 124 | + | ||
| 125 | + * v4 API: | ||
| 126 | + | ||
| 127 | + + :class:`gitlab.v4.objects.ProjectLicenseTemplate` | ||
| 128 | + + :class:`gitlab.v4.objects.ProjectLicenseTemplateManager` | ||
| 129 | + + :attr:`gitlab.v4.objects.Project.license_templates` | ||
| 130 | + + :class:`gitlab.v4.objects.ProjectGitignoreTemplate` | ||
| 131 | + + :class:`gitlab.v4.objects.ProjectGitignoreTemplateManager` | ||
| 132 | + + :attr:`gitlab.v4.objects.Project.gitignore_templates` | ||
| 133 | + + :class:`gitlab.v4.objects.ProjectGitlabciymlTemplate` | ||
| 134 | + + :class:`gitlab.v4.objects.ProjectGitlabciymlTemplateManager` | ||
| 135 | + + :attr:`gitlab.v4.objects.Project.gitlabciyml_templates` | ||
| 136 | + + :class:`gitlab.v4.objects.ProjectDockerfileTemplate` | ||
| 137 | + + :class:`gitlab.v4.objects.ProjectDockerfileTemplateManager` | ||
| 138 | + + :attr:`gitlab.v4.objects.Project.dockerfile_templates` | ||
| 139 | + + :class:`gitlab.v4.objects.ProjectIssueTemplate` | ||
| 140 | + + :class:`gitlab.v4.objects.ProjectIssueTemplateManager` | ||
| 141 | + + :attr:`gitlab.v4.objects.Project.issue_templates` | ||
| 142 | + + :class:`gitlab.v4.objects.ProjectMergeRequestTemplate` | ||
| 143 | + + :class:`gitlab.v4.objects.ProjectMergeRequestTemplateManager` | ||
| 144 | + + :attr:`gitlab.v4.objects.Project.merge_request_templates` | ||
| 145 | + | ||
| 146 | + * GitLab API: https://docs.gitlab.com/ce/api/project_templates.html | ||
| 147 | + | ||
| 148 | + Examples | ||
| 149 | + -------- | ||
| 150 | + | ||
| 151 | + List known project templates:: | ||
| 152 | + | ||
| 153 | + license_templates = project.license_templates.list() | ||
| 154 | + gitignore_templates = project.gitignore_templates.list() | ||
| 155 | + gitlabciyml_templates = project.gitlabciyml_templates.list() | ||
| 156 | + dockerfile_templates = project.dockerfile_templates.list() | ||
| 157 | + issue_templates = project.issue_templates.list() | ||
| 158 | + merge_request_templates = project.merge_request_templates.list() | ||
| 159 | + | ||
| 160 | + Get project templates:: | ||
| 161 | + | ||
| 162 | + license_template = project.license_templates.get('apache-2.0') | ||
| 163 | + gitignore_template = project.gitignore_templates.get('Python') | ||
| 164 | + gitlabciyml_template = project.gitlabciyml_templates.get('Pelican') | ||
| 165 | + dockerfile_template = project.dockerfile_templates.get('Python') | ||
| 166 | + issue_template = project.issue_templates.get('Default') | ||
| 167 | + merge_request_template = project.merge_request_templates.get('Default') | ||
| 168 | + | ||
| 169 | + print(license_template.content) | ||
| 170 | + print(gitignore_template.content) | ||
| 171 | + print(gitlabciyml_template.content) | ||
| 172 | + print(dockerfile_template.content) | ||
| 173 | + print(issue_template.content) | ||
| 174 | + print(merge_request_template.content) | ||
| 175 | + | ||
| 176 | + Create an issue or merge request using a description template:: | ||
| 177 | + | ||
| 178 | + issue = project.issues.create({'title': 'I have a bug', | ||
| 179 | + 'description': issue_template.content}) | ||
| 180 | + mr = project.mergerequests.create({'source_branch': 'cool_feature', | ||
| 181 | + 'target_branch': 'main', | ||
| 182 | + 'title': 'merge cool feature', | ||
| 183 | + 'description': merge_request_template.content}) | ||
| 184 | + | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -100,6 +100,14 @@ | |||
| 100 | 100 | ProjectIssuesStatisticsManager, | |
| 101 | 101 | ) | |
| 102 | 102 | from .tags import ProjectProtectedTagManager, ProjectTagManager # noqa: F401 | |
| 103 | + from .templates import ( # noqa: F401 | ||
| 104 | + ProjectDockerfileTemplateManager, | ||
| 105 | + ProjectGitignoreTemplateManager, | ||
| 106 | + ProjectGitlabciymlTemplateManager, | ||
| 107 | + ProjectIssueTemplateManager, | ||
| 108 | + ProjectLicenseTemplateManager, | ||
| 109 | + ProjectMergeRequestTemplateManager, | ||
| 110 | + ) | ||
| 103 | 111 | from .triggers import ProjectTriggerManager # noqa: F401 | |
| 104 | 112 | from .users import ProjectUserManager # noqa: F401 | |
| 105 | 113 | from .variables import ProjectVariableManager # noqa: F401 | |
@@ -189,27 +197,33 @@ class Project( | |||
| 189 | 197 | customattributes: ProjectCustomAttributeManager | |
| 190 | 198 | deployments: ProjectDeploymentManager | |
| 191 | 199 | deploytokens: ProjectDeployTokenManager | |
| 200 | + dockerfile_templates: ProjectDockerfileTemplateManager | ||
| 192 | 201 | environments: ProjectEnvironmentManager | |
| 193 | 202 | events: ProjectEventManager | |
| 194 | 203 | exports: ProjectExportManager | |
| 195 | 204 | files: ProjectFileManager | |
| 196 | 205 | forks: "ProjectForkManager" | |
| 197 | 206 | generic_packages: GenericPackageManager | |
| 207 | + gitignore_templates: ProjectGitignoreTemplateManager | ||
| 208 | + gitlabciyml_templates: ProjectGitlabciymlTemplateManager | ||
| 198 | 209 | groups: ProjectGroupManager | |
| 199 | 210 | hooks: ProjectHookManager | |
| 200 | 211 | imports: ProjectImportManager | |
| 201 | 212 | integrations: ProjectIntegrationManager | |
| 202 | 213 | invitations: ProjectInvitationManager | |
| 203 | 214 | issues: ProjectIssueManager | |
| 215 | + issue_templates: ProjectIssueTemplateManager | ||
| 204 | 216 | issues_statistics: ProjectIssuesStatisticsManager | |
| 205 | 217 | iterations: ProjectIterationManager | |
| 206 | 218 | jobs: ProjectJobManager | |
| 207 | 219 | job_token_scope: ProjectJobTokenScopeManager | |
| 208 | 220 | keys: ProjectKeyManager | |
| 209 | 221 | labels: ProjectLabelManager | |
| 222 | + license_templates: ProjectLicenseTemplateManager | ||
| 210 | 223 | members: ProjectMemberManager | |
| 211 | 224 | members_all: ProjectMemberAllManager | |
| 212 | 225 | mergerequests: ProjectMergeRequestManager | |
| 226 | + merge_request_templates: ProjectMergeRequestTemplateManager | ||
| 213 | 227 | merge_trains: ProjectMergeTrainManager | |
| 214 | 228 | milestones: ProjectMilestoneManager | |
| 215 | 229 | notes: ProjectNoteManager | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,6 +12,18 @@ | |||
| 12 | 12 | "GitlabciymlManager", | |
| 13 | 13 | "License", | |
| 14 | 14 | "LicenseManager", | |
| 15 | + "ProjectDockerfileTemplate", | ||
| 16 | + "ProjectDockerfileTemplateManager", | ||
| 17 | + "ProjectGitignoreTemplate", | ||
| 18 | + "ProjectGitignoreTemplateManager", | ||
| 19 | + "ProjectGitlabciymlTemplate", | ||
| 20 | + "ProjectGitlabciymlTemplateManager", | ||
| 21 | + "ProjectIssueTemplate", | ||
| 22 | + "ProjectIssueTemplateManager", | ||
| 23 | + "ProjectLicenseTemplate", | ||
| 24 | + "ProjectLicenseTemplateManager", | ||
| 25 | + "ProjectMergeRequestTemplate", | ||
| 26 | + "ProjectMergeRequestTemplateManager", | ||
| 15 | 27 | ] | |
| 16 | 28 | ||
| 17 | 29 | ||
@@ -65,3 +77,95 @@ class LicenseManager(RetrieveMixin, RESTManager): | |||
| 65 | 77 | ||
| 66 | 78 | def get(self, id: Union[str, int], lazy: bool = False, **kwargs: Any) -> License: | |
| 67 | 79 | return cast(License, super().get(id=id, lazy=lazy, **kwargs)) | |
| 80 | + | ||
| 81 | + | ||
| 82 | + class ProjectDockerfileTemplate(RESTObject): | ||
| 83 | + _id_attr = "name" | ||
| 84 | + | ||
| 85 | + | ||
| 86 | + class ProjectDockerfileTemplateManager(RetrieveMixin, RESTManager): | ||
| 87 | + _path = "/projects/{project_id}/templates/dockerfiles" | ||
| 88 | + _obj_cls = ProjectDockerfileTemplate | ||
| 89 | + _from_parent_attrs = {"project_id": "id"} | ||
| 90 | + | ||
| 91 | + def get( | ||
| 92 | + self, id: Union[str, int], lazy: bool = False, **kwargs: Any | ||
| 93 | + ) -> ProjectDockerfileTemplate: | ||
| 94 | + return cast(ProjectDockerfileTemplate, super().get(id=id, lazy=lazy, **kwargs)) | ||
| 95 | + | ||
| 96 | + | ||
| 97 | + class ProjectGitignoreTemplate(RESTObject): | ||
| 98 | + _id_attr = "name" | ||
| 99 | + | ||
| 100 | + | ||
| 101 | + class ProjectGitignoreTemplateManager(RetrieveMixin, RESTManager): | ||
| 102 | + _path = "/projects/{project_id}/templates/gitignores" | ||
| 103 | + _obj_cls = ProjectGitignoreTemplate | ||
| 104 | + _from_parent_attrs = {"project_id": "id"} | ||
| 105 | + | ||
| 106 | + def get( | ||
| 107 | + self, id: Union[str, int], lazy: bool = False, **kwargs: Any | ||
| 108 | + ) -> ProjectGitignoreTemplate: | ||
| 109 | + return cast(ProjectGitignoreTemplate, super().get(id=id, lazy=lazy, **kwargs)) | ||
| 110 | + | ||
| 111 | + | ||
| 112 | + class ProjectGitlabciymlTemplate(RESTObject): | ||
| 113 | + _id_attr = "name" | ||
| 114 | + | ||
| 115 | + | ||
| 116 | + class ProjectGitlabciymlTemplateManager(RetrieveMixin, RESTManager): | ||
| 117 | + _path = "/projects/{project_id}/templates/gitlab_ci_ymls" | ||
| 118 | + _obj_cls = ProjectGitlabciymlTemplate | ||
| 119 | + _from_parent_attrs = {"project_id": "id"} | ||
| 120 | + | ||
| 121 | + def get( | ||
| 122 | + self, id: Union[str, int], lazy: bool = False, **kwargs: Any | ||
| 123 | + ) -> ProjectGitlabciymlTemplate: | ||
| 124 | + return cast(ProjectGitlabciymlTemplate, super().get(id=id, lazy=lazy, **kwargs)) | ||
| 125 | + | ||
| 126 | + | ||
| 127 | + class ProjectLicenseTemplate(RESTObject): | ||
| 128 | + _id_attr = "key" | ||
| 129 | + | ||
| 130 | + | ||
| 131 | + class ProjectLicenseTemplateManager(RetrieveMixin, RESTManager): | ||
| 132 | + _path = "/projects/{project_id}/templates/licenses" | ||
| 133 | + _obj_cls = ProjectLicenseTemplate | ||
| 134 | + _from_parent_attrs = {"project_id": "id"} | ||
| 135 | + | ||
| 136 | + def get( | ||
| 137 | + self, id: Union[str, int], lazy: bool = False, **kwargs: Any | ||
| 138 | + ) -> ProjectLicenseTemplate: | ||
| 139 | + return cast(ProjectLicenseTemplate, super().get(id=id, lazy=lazy, **kwargs)) | ||
| 140 | + | ||
| 141 | + | ||
| 142 | + class ProjectIssueTemplate(RESTObject): | ||
| 143 | + _id_attr = "name" | ||
| 144 | + | ||
| 145 | + | ||
| 146 | + class ProjectIssueTemplateManager(RetrieveMixin, RESTManager): | ||
| 147 | + _path = "/projects/{project_id}/templates/issues" | ||
| 148 | + _obj_cls = ProjectIssueTemplate | ||
| 149 | + _from_parent_attrs = {"project_id": "id"} | ||
| 150 | + | ||
| 151 | + def get( | ||
| 152 | + self, id: Union[str, int], lazy: bool = False, **kwargs: Any | ||
| 153 | + ) -> ProjectIssueTemplate: | ||
| 154 | + return cast(ProjectIssueTemplate, super().get(id=id, lazy=lazy, **kwargs)) | ||
| 155 | + | ||
| 156 | + | ||
| 157 | + class ProjectMergeRequestTemplate(RESTObject): | ||
| 158 | + _id_attr = "name" | ||
| 159 | + | ||
| 160 | + | ||
| 161 | + class ProjectMergeRequestTemplateManager(RetrieveMixin, RESTManager): | ||
| 162 | + _path = "/projects/{project_id}/templates/merge_requests" | ||
| 163 | + _obj_cls = ProjectMergeRequestTemplate | ||
| 164 | + _from_parent_attrs = {"project_id": "id"} | ||
| 165 | + | ||
| 166 | + def get( | ||
| 167 | + self, id: Union[str, int], lazy: bool = False, **kwargs: Any | ||
| 168 | + ) -> ProjectMergeRequestTemplate: | ||
| 169 | + return cast( | ||
| 170 | + ProjectMergeRequestTemplate, super().get(id=id, lazy=lazy, **kwargs) | ||
| 171 | + ) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,106 @@ | |||
| 1 | + """ | ||
| 2 | + Gitlab API: | ||
| 3 | + https://docs.gitlab.com/ce/api/templates/dockerfiles.html | ||
| 4 | + https://docs.gitlab.com/ce/api/templates/gitignores.html | ||
| 5 | + https://docs.gitlab.com/ce/api/templates/gitlab_ci_ymls.html | ||
| 6 | + https://docs.gitlab.com/ce/api/templates/licenses.html | ||
| 7 | + https://docs.gitlab.com/ce/api/project_templates.html | ||
| 8 | + """ | ||
| 9 | + | ||
| 10 | + import pytest | ||
| 11 | + import responses | ||
| 12 | + | ||
| 13 | + from gitlab.v4.objects import ( | ||
| 14 | + Dockerfile, | ||
| 15 | + Gitignore, | ||
| 16 | + Gitlabciyml, | ||
| 17 | + License, | ||
| 18 | + ProjectDockerfileTemplate, | ||
| 19 | + ProjectGitignoreTemplate, | ||
| 20 | + ProjectGitlabciymlTemplate, | ||
| 21 | + ProjectIssueTemplate, | ||
| 22 | + ProjectLicenseTemplate, | ||
| 23 | + ProjectMergeRequestTemplate, | ||
| 24 | + ) | ||
| 25 | + | ||
| 26 | + | ||
| 27 | + @pytest.mark.parametrize( | ||
| 28 | + "tmpl, tmpl_mgr, tmpl_path", | ||
| 29 | + [ | ||
| 30 | + (Dockerfile, "dockerfiles", "dockerfiles"), | ||
| 31 | + (Gitignore, "gitignores", "gitignores"), | ||
| 32 | + (Gitlabciyml, "gitlabciymls", "gitlab_ci_ymls"), | ||
| 33 | + (License, "licenses", "licenses"), | ||
| 34 | + ], | ||
| 35 | + ids=[ | ||
| 36 | + "dockerfile", | ||
| 37 | + "gitignore", | ||
| 38 | + "gitlabciyml", | ||
| 39 | + "license", | ||
| 40 | + ], | ||
| 41 | + ) | ||
| 42 | + def test_get_template(gl, tmpl, tmpl_mgr, tmpl_path): | ||
| 43 | + tmpl_id = "sample" | ||
| 44 | + tmpl_content = {"name": tmpl_id, "content": "Sample template content"} | ||
| 45 | + | ||
| 46 | + # License templates have 'key' as the id attribute, so ensure | ||
| 47 | + # this is included in the response content | ||
| 48 | + if tmpl == License: | ||
| 49 | + tmpl_id = "smpl" | ||
| 50 | + tmpl_content.update({"key": tmpl_id}) | ||
| 51 | + | ||
| 52 | + path = f"templates/{tmpl_path}/{tmpl_id}" | ||
| 53 | + with responses.RequestsMock() as rsps: | ||
| 54 | + rsps.add( | ||
| 55 | + method=responses.GET, | ||
| 56 | + url=f"http://localhost/api/v4/{path}", | ||
| 57 | + json=tmpl_content, | ||
| 58 | + ) | ||
| 59 | + | ||
| 60 | + template = getattr(gl, tmpl_mgr).get(tmpl_id) | ||
| 61 | + | ||
| 62 | + assert isinstance(template, tmpl) | ||
| 63 | + assert getattr(template, template._id_attr) == tmpl_id | ||
| 64 | + | ||
| 65 | + | ||
| 66 | + @pytest.mark.parametrize( | ||
| 67 | + "tmpl, tmpl_mgr, tmpl_path", | ||
| 68 | + [ | ||
| 69 | + (ProjectDockerfileTemplate, "dockerfile_templates", "dockerfiles"), | ||
| 70 | + (ProjectGitignoreTemplate, "gitignore_templates", "gitignores"), | ||
| 71 | + (ProjectGitlabciymlTemplate, "gitlabciyml_templates", "gitlab_ci_ymls"), | ||
| 72 | + (ProjectLicenseTemplate, "license_templates", "licenses"), | ||
| 73 | + (ProjectIssueTemplate, "issue_templates", "issues"), | ||
| 74 | + (ProjectMergeRequestTemplate, "merge_request_templates", "merge_requests"), | ||
| 75 | + ], | ||
| 76 | + ids=[ | ||
| 77 | + "dockerfile", | ||
| 78 | + "gitignore", | ||
| 79 | + "gitlabciyml", | ||
| 80 | + "license", | ||
| 81 | + "issue", | ||
| 82 | + "mergerequest", | ||
| 83 | + ], | ||
| 84 | + ) | ||
| 85 | + def test_get_project_template(project, tmpl, tmpl_mgr, tmpl_path): | ||
| 86 | + tmpl_id = "sample" | ||
| 87 | + tmpl_content = {"name": tmpl_id, "content": "Sample template content"} | ||
| 88 | + | ||
| 89 | + # ProjectLicenseTemplate templates have 'key' as the id attribute, so ensure | ||
| 90 | + # this is included in the response content | ||
| 91 | + if tmpl == ProjectLicenseTemplate: | ||
| 92 | + tmpl_id = "smpl" | ||
| 93 | + tmpl_content.update({"key": tmpl_id}) | ||
| 94 | + | ||
| 95 | + path = f"projects/{project.id}/templates/{tmpl_path}/{tmpl_id}" | ||
| 96 | + with responses.RequestsMock() as rsps: | ||
| 97 | + rsps.add( | ||
| 98 | + method=responses.GET, | ||
| 99 | + url=f"http://localhost/api/v4/{path}", | ||
| 100 | + json=tmpl_content, | ||
| 101 | + ) | ||
| 102 | + | ||
| 103 | + template = getattr(project, tmpl_mgr).get(tmpl_id) | ||
| 104 | + | ||
| 105 | + assert isinstance(template, tmpl) | ||
| 106 | + assert getattr(template, template._id_attr) == tmpl_id | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments