Skip to content
This repository has been archived by the owner on Jun 20, 2022. It is now read-only.

Commit

Permalink
Merge pull request #95 from dannysperling/gitignore_templates
Browse files Browse the repository at this point in the history
Add gitignore endpoint for templates
  • Loading branch information
dannysperling committed Mar 15, 2015
2 parents b5b53c7 + 27fc22c commit de32d95
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 3 deletions.
6 changes: 3 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@
- [ ] https://developer.github.com/v3/git/trees/#create-a-tree
- [ ] https://developer.github.com/v3/git/trees/#get-a-tree
- [ ] https://developer.github.com/v3/git/trees/#get-a-tree-recursively
- [ ] https://developer.github.com/v3/gitignore/
- [ ] https://developer.github.com/v3/gitignore/#get-a-single-template
- [ ] https://developer.github.com/v3/gitignore/#listing-available-templates
- [x] https://developer.github.com/v3/gitignore/
- [x] https://developer.github.com/v3/gitignore/#get-a-single-template
- [x] https://developer.github.com/v3/gitignore/#listing-available-templates
- [ ] https://developer.github.com/v3/issues/
- [ ] https://developer.github.com/v3/issues/#create-an-issue
- [ ] https://developer.github.com/v3/issues/#edit-an-issue
Expand Down
4 changes: 4 additions & 0 deletions fixtures/git_ignore_c_template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "C",
"source": "# Object files\n*.o\n\n# Libraries\n*.lib\n*.a\n\n# Shared objects (inc. Windows DLLs)\n*.dll\n*.so\n*.so.*\n*.dylib\n\n# Executables\n*.exe\n*.out\n*.app\n"
}
9 changes: 9 additions & 0 deletions fixtures/git_ignore_templates.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
"Actionscript",
"Android",
"AppceleratorTitanium",
"Autotools",
"Bancha",
"C",
"C++"
]
47 changes: 47 additions & 0 deletions octokit/git_ignore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package octokit

// GitIgnoreURL is an address for accessing various templates to apply
// to a repository upon creation.
var GitIgnoreURL = Hyperlink("/gitignore/templates{/name}")

// GitIgnore creates a GitIgnoreService to access gitignore templates
func (c *Client) GitIgnore() *GitIgnoreService {
return &GitIgnoreService{client: c}
}

// A service to return gitignore templates
type GitIgnoreService struct {
client *Client
}

// All gets a list all the available templates
func (s *GitIgnoreService) All(uri *Hyperlink) (templates []string, result *Result) {
if uri == nil {
uri = &GitIgnoreURL
}
url, err := uri.Expand(nil)
if err != nil {
return make([]string, 0), &Result{Err: err}
}
result = s.client.get(url, &templates)
return
}

// One gets a specific gitignore template based on the passed url
func (s *GitIgnoreService) One(uri *Hyperlink, params M) (template GitIgnoreTemplate, result *Result) {
if uri == nil {
uri = &GitIgnoreURL
}
url, err := uri.Expand(params)
if err != nil {
return GitIgnoreTemplate{}, &Result{Err: err}
}
result = s.client.get(url, &template)
return
}

//GitIgnoreTemplate is a representation of a given template returned by the service
type GitIgnoreTemplate struct {
Name string `json:"name,omitempty"`
Source string `json:"source,omitempty"`
}
32 changes: 32 additions & 0 deletions octokit/git_ignore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package octokit

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGitIgnoreService_All(t *testing.T) {
setup()
defer tearDown()

stubGet(t, "/gitignore/templates", "git_ignore_templates", nil)

templates, result := client.GitIgnore().All(nil)
assert.False(t, result.HasError())
assert.Equal(t, "AppceleratorTitanium", templates[2])
assert.Equal(t, "Autotools", templates[3])
assert.Len(t, templates, 7)
}

func TestGitIgnoreService_One(t *testing.T) {
setup()
defer tearDown()

stubGet(t, "/gitignore/templates/C", "git_ignore_c_template", nil)

template, result := client.GitIgnore().One(&GitIgnoreURL, M{"name": "C"})
assert.False(t, result.HasError())
assert.Equal(t, "C", template.Name)
assert.Equal(t, "# Object files\n*.o\n\n# Libraries\n*.lib\n*.a\n\n# Shared objects (inc. Windows DLLs)\n*.dll\n*.so\n*.so.*\n*.dylib\n\n# Executables\n*.exe\n*.out\n*.app\n", template.Source)
}

0 comments on commit de32d95

Please sign in to comment.