Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go-github: add support for issue locking / unlocking #280

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
32 changes: 32 additions & 0 deletions github/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,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)
}
28 changes: 28 additions & 0 deletions github/issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,31 @@ 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) {
w.WriteHeader(http.StatusNoContent)
testMethod(t, r, "PUT")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to better simulate the actual GitHub API, go ahead and have this return a 204 status:

w.WriteHeader(http.StatusNoContent)

same with Unlock test below

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. PTAL.

})

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) {
w.WriteHeader(http.StatusNoContent)
testMethod(t, r, "DELETE")
})

if _, err := client.Issues.Unlock("o", "r", 1); err != nil {
t.Errorf("Issues.Unlock returned error: %v", err)
}
}