From ac77c93f2bb2e0096e8ab058d0f18f8200c27ca0 Mon Sep 17 00:00:00 2001 From: Glenn Lewis Date: Fri, 12 Feb 2016 13:37:43 -0800 Subject: [PATCH] go-github: add support for issue locking / unlocking Fixes #279. Change-Id: I55ffdadfe5970d664ac94048e8bc2b8f5d935903 --- github/github.go | 3 +++ github/issues.go | 32 ++++++++++++++++++++++++++++++++ github/issues_test.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/github/github.go b/github/github.go index f9f27e2..640aec7 100644 --- a/github/github.go +++ b/github/github.go @@ -56,6 +56,9 @@ const ( // https://developer.github.com/changes/2015-11-11-protected-branches-api/ mediaTypeProtectedBranchesPreview = "application/vnd.github.loki-preview+json" + + // https://developer.github.com/changes/2016-02-11-issue-locking-api/ + mediaTypeIssueLockingPreview = "application/vnd.github.the-key-preview+json" ) // A Client manages communication with the GitHub API. diff --git a/github/issues.go b/github/issues.go index a14bd52..58a3a69 100644 --- a/github/issues.go +++ b/github/issues.go @@ -260,3 +260,35 @@ func (s *IssuesService) Edit(owner string, repo string, number int, issue *Issue return i, resp, err } + +// Lock an issue's conversation. +// +// GitHub API docs: https://developer.github.com/v3/issues/#lock-an-issue +func (s *IssuesService) Lock(owner string, repo string, number int) (*Response, error) { + u := fmt.Sprintf("repos/%v/%v/issues/%d/lock", owner, repo, number) + req, err := s.client.NewRequest("PUT", u, nil) + if err != nil { + return nil, err + } + + // TODO: remove custom Accept header when this API fully launches. + req.Header.Set("Accept", mediaTypeIssueLockingPreview) + + return s.client.Do(req, nil) +} + +// Unlock an issue's conversation. +// +// GitHub API docs: https://developer.github.com/v3/issues/#unlock-an-issue +func (s *IssuesService) Unlock(owner string, repo string, number int) (*Response, error) { + u := fmt.Sprintf("repos/%v/%v/issues/%d/lock", owner, repo, number) + req, err := s.client.NewRequest("DELETE", u, nil) + if err != nil { + return nil, err + } + + // TODO: remove custom Accept header when this API fully launches. + req.Header.Set("Accept", mediaTypeIssueLockingPreview) + + return s.client.Do(req, nil) +} diff --git a/github/issues_test.go b/github/issues_test.go index f69efd3..46183f4 100644 --- a/github/issues_test.go +++ b/github/issues_test.go @@ -240,3 +240,35 @@ func TestIssuesService_Edit_invalidOwner(t *testing.T) { _, _, err := client.Issues.Edit("%", "r", 1, nil) testURLParseError(t, err) } + +func TestIssuesService_Lock(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/issues/1/lock", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + testHeader(t, r, "Accept", mediaTypeIssueLockingPreview) + + w.WriteHeader(http.StatusNoContent) + }) + + if _, err := client.Issues.Lock("o", "r", 1); err != nil { + t.Errorf("Issues.Lock returned error: %v", err) + } +} + +func TestIssuesService_Unlock(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/issues/1/lock", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + testHeader(t, r, "Accept", mediaTypeIssueLockingPreview) + + w.WriteHeader(http.StatusNoContent) + }) + + if _, err := client.Issues.Unlock("o", "r", 1); err != nil { + t.Errorf("Issues.Unlock returned error: %v", err) + } +}