From b70aeea4af51de92af17d034f2a139ae1e7ba12d Mon Sep 17 00:00:00 2001 From: AkashSrivastava Date: Wed, 21 Nov 2018 16:12:09 +0530 Subject: [PATCH 1/2] Added wikis API --- lib/gitlab/client.rb | 1 + lib/gitlab/client/wikis.rb | 79 ++++++++++++++++++++++++++++++ spec/fixtures/wiki.json | 7 +++ spec/fixtures/wiki_attachment.json | 9 ++++ spec/fixtures/wikis.json | 19 +++++++ spec/gitlab/client/wikis_spec.rb | 79 ++++++++++++++++++++++++++++++ 6 files changed, 194 insertions(+) create mode 100644 lib/gitlab/client/wikis.rb create mode 100644 spec/fixtures/wiki.json create mode 100644 spec/fixtures/wiki_attachment.json create mode 100644 spec/fixtures/wikis.json create mode 100644 spec/gitlab/client/wikis_spec.rb diff --git a/lib/gitlab/client.rb b/lib/gitlab/client.rb index 71755ce07..45b473c65 100644 --- a/lib/gitlab/client.rb +++ b/lib/gitlab/client.rb @@ -43,6 +43,7 @@ class Client < API include Todos include Users include Versions + include Wikis # Text representation of the client, masking private token. # diff --git a/lib/gitlab/client/wikis.rb b/lib/gitlab/client/wikis.rb new file mode 100644 index 000000000..25fe23e5e --- /dev/null +++ b/lib/gitlab/client/wikis.rb @@ -0,0 +1,79 @@ +# frozen_string_literal: true + +class Gitlab::Client + # Defines methods related to wikis. + # @see https://docs.gitlab.com/ce/api/wikis.html + module Wikis + # Get all wiki pages for a given project. + # + # @example + # Gitlab.wikis(3) + # Gitlab.wikis(3, {with_content: 'Some wiki content'}) + # + # @param [Integer, String] project The ID or name of a project. + # @param [Hash] options A customizable set of options. + # @option options [String] :with_content(optional) Include pages content + # @return [Array] + def wikis(project, options = {}) + get("/projects/#{url_encode project}/wikis", query: options) + end + + # Get a wiki page for a given project. + # + # @example + # Gitlab.wiki(3, 'home') + # + # @param [Integer, String] project The ID or name of a project. + # @param [String] slug The slug (a unique string) of the wiki page + # @return [Gitlab::ObjectifiedHash] + def wiki(project, slug) + get("/projects/#{url_encode project}/wikis/#{slug}") + end + + # Creates a new wiki page for the given repository with the given title, slug, and content. + # + # @example + # Gitlab.create_wiki(3, 'Some Content', 'Some Title') + # Gitlab.create_wiki(3, 'Some Content', 'Some Title', { format: 'rdoc' }) + # + # @param [Integer, String] project The ID or name of a project. + # @param [String] content The content of the wiki page. + # @param [String] title The title of the wiki page. + # @param [Hash] options A customizable set of options. + # @option options [String] format (optional) The format of the wiki page. Available formats are: markdown (default), rdoc, and asciidoc. + # @return [Gitlab::ObjectifiedHash] Information about created wiki page. + def create_wiki(project, content, title, options = {}) + body = { content: content, title: title }.merge(options) + post("/projects/#{url_encode project}/wikis", body: body) + end + + # Updates an existing wiki page. At least one parameter is required to update the wiki page. + # + # @example + # Gitlab.update_wiki(6, 'home', { title: 'New title' }) + # Gitlab.update_wiki(6, 'home', { title: 'New title', message: 'New Message', format: 'rdoc' }) + # + # @param [Integer, String] project The ID or name of a project. + # @param [String] slug The slug (a unique string) of the wiki page. + # @param [Hash] options A customizable set of options. + # @option options [String] content The content of the wiki page. + # @option options [String] title The title of the wiki page. + # @option options [String] format (optional) The format of the wiki page. Available formats are: markdown (default), rdoc, and asciidoc. + # @return [Gitlab::ObjectifiedHash] Information about updated wiki page. + def update_wiki(project, slug, options = {}) + put("/projects/#{url_encode project}/wikis/#{slug}", body: options) + end + + # Deletes a wiki page with a given slug. + # + # @example + # Gitlab.delete_wiki(42, 'foo') + # + # @param [Integer, String] project The ID or name of a project. + # @param [String] slug The slug (a unique string) of the wiki page. + # @return [Gitlab::ObjectifiedHash] An empty objectified hash + def delete_wiki(project, slug) + delete("/projects/#{url_encode project}/wikis/#{slug}") + end + end +end diff --git a/spec/fixtures/wiki.json b/spec/fixtures/wiki.json new file mode 100644 index 000000000..a893abf93 --- /dev/null +++ b/spec/fixtures/wiki.json @@ -0,0 +1,7 @@ +{ + "content" : "home page", + "format" : "markdown", + "slug" : "home", + "title" : "home" +} + diff --git a/spec/fixtures/wiki_attachment.json b/spec/fixtures/wiki_attachment.json new file mode 100644 index 000000000..e984e73d7 --- /dev/null +++ b/spec/fixtures/wiki_attachment.json @@ -0,0 +1,9 @@ +{ + "file_name" : "dk.png", + "file_path" : "uploads/6a061c4cf9f1c28cb22c384b4b8d4e3c/dk.png", + "branch" : "master", + "link" : { + "url" : "uploads/6a061c4cf9f1c28cb22c384b4b8d4e3c/dk.png", + "markdown" : "![dk](uploads/6a061c4cf9f1c28cb22c384b4b8d4e3c/dk.png)" + } +} diff --git a/spec/fixtures/wikis.json b/spec/fixtures/wikis.json new file mode 100644 index 000000000..e2bba6c25 --- /dev/null +++ b/spec/fixtures/wikis.json @@ -0,0 +1,19 @@ +[ + { + "content" : "Here is an instruction how to deploy this project.", + "format" : "markdown", + "slug" : "deploy", + "title" : "deploy" + }, + { + "content" : "Our development process is described here.", + "format" : "markdown", + "slug" : "development", + "title" : "development" + },{ + "content" : "* [Deploy](deploy)\n* [Development](development)", + "format" : "markdown", + "slug" : "home", + "title" : "home" + } +] diff --git a/spec/gitlab/client/wikis_spec.rb b/spec/gitlab/client/wikis_spec.rb new file mode 100644 index 000000000..b9c9325d3 --- /dev/null +++ b/spec/gitlab/client/wikis_spec.rb @@ -0,0 +1,79 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::Client do + describe '.wikis' do + before do + stub_get('/projects/1/wikis', 'wikis') + @wikis = Gitlab.wikis(1) + end + + it 'gets the correct resource' do + expect(a_get('/projects/1/wikis')).to have_been_made + end + + it "returns a response of a project's wikis" do + expect(@wikis).to be_a Gitlab::PaginatedResponse + end + end + + describe '.wiki' do + before do + stub_get('/projects/1/wikis/home', 'wiki') + @wiki = Gitlab.wiki(1, 'home') + end + + it 'gets the correct resource' do + expect(a_get('/projects/1/wikis/home')).to have_been_made + end + + it 'returns correct information about the wiki' do + expect(@wiki.slug).to eq 'home' + end + end + + describe '.create_wiki' do + before do + stub_post('/projects/1/wikis', 'wiki') + @wiki = Gitlab.create_wiki(1, 'home page', 'home') + end + + it 'gets the correct resource' do + expect(a_post('/projects/1/wikis') + .with(body: { content: 'home page', title: 'home' })).to have_been_made + end + + it 'returns correct information about the created wiki' do + expect(@wiki.content).to eq 'home page' + expect(@wiki.title).to eq 'home' + end + end + + describe '.update_wiki' do + before do + stub_put('/projects/1/wikis/home', 'wiki') + @wiki = Gitlab.update_wiki(1, 'home', format: 'markdown') + end + + it 'gets the correct resource' do + expect(a_put('/projects/1/wikis/home') + .with(body: { format: 'markdown' })).to have_been_made + end + + it 'returns correct information about the updated wiki' do + expect(@wiki.format).to eq 'markdown' + end + end + + describe '.delete_wiki' do + before do + stub_delete('/projects/1/wikis/home', 'empty') + @wiki = Gitlab.delete_wiki(1, 'home') + end + + it 'gets the correct resource' do + expect(a_delete('/projects/1/wikis/home')).to have_been_made + end + end +end From d79a625602cac033ce2ac344b99cb838f24089e8 Mon Sep 17 00:00:00 2001 From: AkashSrivastava Date: Wed, 21 Nov 2018 16:42:53 +0530 Subject: [PATCH 2/2] Argument order change in wikis creation --- lib/gitlab/client/wikis.rb | 2 +- spec/gitlab/client/wikis_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/gitlab/client/wikis.rb b/lib/gitlab/client/wikis.rb index 25fe23e5e..b288e3e0b 100644 --- a/lib/gitlab/client/wikis.rb +++ b/lib/gitlab/client/wikis.rb @@ -42,7 +42,7 @@ def wiki(project, slug) # @param [Hash] options A customizable set of options. # @option options [String] format (optional) The format of the wiki page. Available formats are: markdown (default), rdoc, and asciidoc. # @return [Gitlab::ObjectifiedHash] Information about created wiki page. - def create_wiki(project, content, title, options = {}) + def create_wiki(project, title, content, options = {}) body = { content: content, title: title }.merge(options) post("/projects/#{url_encode project}/wikis", body: body) end diff --git a/spec/gitlab/client/wikis_spec.rb b/spec/gitlab/client/wikis_spec.rb index b9c9325d3..e8e793515 100644 --- a/spec/gitlab/client/wikis_spec.rb +++ b/spec/gitlab/client/wikis_spec.rb @@ -36,7 +36,7 @@ describe '.create_wiki' do before do stub_post('/projects/1/wikis', 'wiki') - @wiki = Gitlab.create_wiki(1, 'home page', 'home') + @wiki = Gitlab.create_wiki(1, 'home', 'home page') end it 'gets the correct resource' do