Repository: xanzy/go-gitlab
Pull request #1303 (2021-12-02 16:19:40)

feat: add markdown endpoint support
I noticed that the markdown (https://docs.gitlab.com/ee/api/markdown.html) endpoint is not implemented. So I've added it, I wasn't really sure on the naming of the functions since it's not a get route and technically it retrieves HTML. However, I decided to follow the name in the url/docs of gitlab with just `Markdown`. If you like an other name please let me know, I'd be happy to change it :)

doriandekoning: @svanharmelen  Thanks for the review, I've changed it according to the suggestions.

Patch:
diff --git a/README.md b/README.md
index 568fffc..acbf747 100644
--- a/README.md
+++ b/README.md
@@ -52,6 +52,7 @@ to add new and/or missing endpoints. Currently the following services are suppor
 - [x] Keys
 - [x] Labels
 - [x] License
+- [x] Markdown
 - [x] Merge Request Approvals
 - [x] Merge Requests
 - [x] Namespaces
diff --git a/gitlab.go b/gitlab.go
index ddfee94..d36c467 100644
--- a/gitlab.go
+++ b/gitlab.go
@@ -147,6 +147,7 @@ type Client struct {
 	License               *LicenseService
 	LicenseTemplates      *LicenseTemplatesService
 	ManagedLicenses       *ManagedLicensesService
+	Markdown              *MarkdownService
 	MergeRequestApprovals *MergeRequestApprovalsService
 	MergeRequests         *MergeRequestsService
 	Milestones            *MilestonesService
@@ -338,6 +339,7 @@ func newClient(options ...ClientOptionFunc) (*Client, error) {
 	c.License = &LicenseService{client: c}
 	c.LicenseTemplates = &LicenseTemplatesService{client: c}
 	c.ManagedLicenses = &ManagedLicensesService{client: c}
+	c.Markdown = &MarkdownService{client: c}
 	c.MergeRequestApprovals = &MergeRequestApprovalsService{client: c}
 	c.MergeRequests = &MergeRequestsService{client: c, timeStats: timeStats}
 	c.Milestones = &MilestonesService{client: c}
diff --git a/markdown.go b/markdown.go
new file mode 100644
index 0000000..8c20749
--- /dev/null
+++ b/markdown.go
@@ -0,0 +1,47 @@
+package gitlab
+
+import "net/http"
+
+// MarkdownService handles communication with the markdown related methods of
+// the GitLab API.
+//
+// Gitlab API docs: https://docs.gitlab.com/ee/api/markdown.html
+type MarkdownService struct {
+	client *Client
+}
+
+// Markdown represents a markdown document.
+//
+// Gitlab API docs: https://docs.gitlab.com/ee/api/markdown.html
+type Markdown struct {
+	HTML string `json:"html"`
+}
+
+// RenderOptions represents the available Render() options.
+//
+// Gitlab API docs:
+// https://docs.gitlab.com/ee/api/markdown.html#render-an-arbitrary-markdown-document
+type RenderOptions struct {
+	Text                    *string `url:"text,omitempty" json:"text,omitempty"`
+	GitlabFlavouredMarkdown *bool   `url:"gfm,omitempty" json:"gfm,omitempty"`
+	Project                 *string `url:"project,omitempty" json:"project,omitempty"`
+}
+
+// Render an arbitrary markdown document.
+//
+// Gitlab API docs:
+// https://docs.gitlab.com/ee/api/markdown.html#render-an-arbitrary-markdown-document
+func (s *MarkdownService) Render(opt *RenderOptions, options ...RequestOptionFunc) (*Markdown, *Response, error) {
+	req, err := s.client.NewRequest(http.MethodPost, "markdown", opt, options)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	md := new(Markdown)
+	response, err := s.client.Do(req, md)
+	if err != nil {
+		return nil, response, err
+	}
+
+	return md, response, nil
+}


Test patch:
diff --git a/markdown_test.go b/markdown_test.go
new file mode 100644
index 0000000..fef9475
--- /dev/null
+++ b/markdown_test.go
@@ -0,0 +1,41 @@
+package gitlab
+
+import (
+	"encoding/json"
+	"net/http"
+	"testing"
+)
+
+const markdownHTMLResponse = "<h1>Testing</h1>"
+
+func TestRender(t *testing.T) {
+	mux, server, client := setup(t)
+	defer teardown(server)
+
+	mux.HandleFunc("/api/v4/markdown", func(writer http.ResponseWriter, request *http.Request) {
+		testMethod(t, request, http.MethodPost)
+		writer.WriteHeader(http.StatusOK)
+		markdown := Markdown{HTML: markdownHTMLResponse}
+		resp, _ := json.Marshal(markdown)
+		_, _ = writer.Write(resp)
+	})
+
+	opt := &RenderOptions{
+		Text:                    String("# Testing"),
+		GitlabFlavouredMarkdown: Bool(true),
+		Project:                 String("some/sub/group/project"),
+	}
+	markdown, resp, err := client.Markdown.Render(opt)
+	if err != nil {
+		t.Fatalf("Render returned error: %v", err)
+	}
+
+	if resp.StatusCode != http.StatusOK {
+		t.Fatalf("Render returned status, expected %q but got %q", http.StatusOK, resp.Status)
+	}
+
+	if markdown.HTML != markdownHTMLResponse {
+		t.Fatalf("Render returned wrong response, expected %q but got %q",
+			markdownHTMLResponse, markdown.HTML)
+	}
+}