Commit 6c1f03fe authored by Jan Provaznik's avatar Jan Provaznik

Merge branch '33880-add-api-to-disable-pages' into 'master'

Add API endpoint to unpublish GitLab Pages

Closes #33880

See merge request gitlab-org/gitlab!19781
parents 15616dbf c66b841a
...@@ -15,8 +15,7 @@ class Projects::PagesController < Projects::ApplicationController ...@@ -15,8 +15,7 @@ class Projects::PagesController < Projects::ApplicationController
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
def destroy def destroy
project.remove_pages ::Pages::DeleteService.new(@project, current_user).execute
project.pages_domains.destroy_all # rubocop: disable DestroyAll
respond_to do |format| respond_to do |format|
format.html do format.html do
......
# frozen_string_literal: true
module Pages
class DeleteService < BaseService
def execute
project.remove_pages
project.pages_domains.destroy_all # rubocop: disable DestroyAll
end
end
end
---
title: Add API endpoint to unpublish GitLab Pages
merge_request: 19781
author:
type: added
# Pages API
Endpoints for managing [GitLab Pages](https://about.gitlab.com/product/pages/).
The GitLab Pages feature must be enabled to use these endpoints. Find out more about [administering](../administration/pages/index.md) and [using](../user/project/pages/index.md) the feature.
## Unpublish pages
Remove pages. The user must have admin priviledges.
```text
DELETE /projects/:id/pages
```
| Attribute | Type | Required | Description |
| --------- | -------------- | -------- | ---------------------------------------- |
| `id` | integer/string | yes | The ID or [URL-encoded path of the project](README.md#namespaced-path-encoding) owned by the authenticated user |
```bash
curl --request 'DELETE' --header "PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/projects/2/pages
```
...@@ -137,6 +137,7 @@ module API ...@@ -137,6 +137,7 @@ module API
mount ::API::Discussions mount ::API::Discussions
mount ::API::ResourceLabelEvents mount ::API::ResourceLabelEvents
mount ::API::NotificationSettings mount ::API::NotificationSettings
mount ::API::Pages
mount ::API::PagesDomains mount ::API::PagesDomains
mount ::API::Pipelines mount ::API::Pipelines
mount ::API::PipelineSchedules mount ::API::PipelineSchedules
......
# frozen_string_literal: true
module API
class Pages < Grape::API
before do
require_pages_config_enabled!
authenticated_with_full_private_access!
end
params do
requires :id, type: String, desc: 'The ID of a project'
end
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
desc 'Unpublish pages' do
detail 'This feature was introduced in GitLab 12.6'
end
delete ':id/pages' do
authorize! :remove_pages, user_project
status 204
::Pages::DeleteService.new(user_project, current_user).execute
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe API::Pages do
let_it_be(:project) { create(:project, path: 'my.project', pages_https_only: false) }
let_it_be(:admin) { create(:admin) }
let_it_be(:user) { create(:user) }
before do
project.add_maintainer(user)
project.mark_pages_as_deployed
end
describe 'DELETE /projects/:id/pages' do
context 'when Pages is disabled' do
before do
allow(Gitlab.config.pages).to receive(:enabled).and_return(false)
end
it_behaves_like '404 response' do
let(:request) { delete api("/projects/#{project.id}/pages", admin)}
end
end
context 'when Pages is enabled' do
before do
allow(Gitlab.config.pages).to receive(:enabled).and_return(true)
end
context 'when Pages are deployed' do
it 'returns 204' do
delete api("/projects/#{project.id}/pages", admin)
expect(response).to have_gitlab_http_status(204)
end
it 'removes the pages' do
expect_any_instance_of(Gitlab::PagesTransfer).to receive(:rename_project).and_return true
expect(PagesWorker).to receive(:perform_in).with(5.minutes, :remove, project.namespace.full_path, anything)
delete api("/projects/#{project.id}/pages", admin )
expect(project.reload.pages_metadatum.deployed?).to be(false)
end
end
context 'when pages are not deployed' do
before do
project.mark_pages_as_not_deployed
end
it 'returns 204' do
delete api("/projects/#{project.id}/pages", admin)
expect(response).to have_gitlab_http_status(204)
end
end
context 'when there is no project' do
it 'returns 404' do
id = -1
delete api("/projects/#{id}/pages", admin)
expect(response).to have_gitlab_http_status(404)
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Pages::DeleteService do
let_it_be(:project) { create(:project, path: "my.project")}
let_it_be(:admin) { create(:admin) }
let_it_be(:domain) { create(:pages_domain, project: project) }
let_it_be(:service) { described_class.new(project, admin)}
it 'deletes published pages' do
expect_any_instance_of(Gitlab::PagesTransfer).to receive(:rename_project).and_return true
expect(PagesWorker).to receive(:perform_in).with(5.minutes, :remove, project.namespace.full_path, anything)
service.execute
expect(project.reload.pages_metadatum.deployed?).to be(false)
end
it 'deletes all domains' do
expect(project.pages_domains.count).to be 1
service.execute
expect(project.reload.pages_domains.count).to be 0
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment