-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from sue445/feature/snippet
Support snippet
- Loading branch information
Showing
12 changed files
with
455 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package gitlab | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestPage_FormatFooter(t *testing.T) { | ||
type fields struct { | ||
FooterTitle string | ||
FooterURL string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
want string | ||
}{ | ||
{ | ||
name: "title and url", | ||
fields: fields{ | ||
FooterTitle: "GitHub", | ||
FooterURL: "https://github.com/", | ||
}, | ||
want: "<https://github.com/|GitHub>", | ||
}, | ||
{ | ||
name: "title only", | ||
fields: fields{ | ||
FooterTitle: "GitHub", | ||
FooterURL: "", | ||
}, | ||
want: "GitHub", | ||
}, | ||
{ | ||
name: "url only", | ||
fields: fields{ | ||
FooterTitle: "", | ||
FooterURL: "https://github.com/", | ||
}, | ||
want: "https://github.com/", | ||
}, | ||
{ | ||
name: "nothing", | ||
fields: fields{ | ||
FooterTitle: "", | ||
FooterURL: "", | ||
}, | ||
want: "", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
p := &Page{ | ||
FooterTitle: tt.fields.FooterTitle, | ||
FooterURL: tt.fields.FooterURL, | ||
} | ||
if got := p.FormatFooter(); got != tt.want { | ||
t.Errorf("Page.formatFooter() = %v, want %+v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package gitlab | ||
|
||
import ( | ||
"fmt" | ||
"github.com/xanzy/go-gitlab" | ||
"golang.org/x/sync/errgroup" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type projectSnippetFetcher struct { | ||
} | ||
|
||
func (f *projectSnippetFetcher) fetchPath(path string, client *gitlab.Client, isDebugLogging bool) (*Page, error) { | ||
re := regexp.MustCompile("^([^/]+)/([^/]+)/snippets/(\\d+)") | ||
matched := re.FindStringSubmatch(path) | ||
|
||
if matched == nil { | ||
return nil, nil | ||
} | ||
|
||
projectName := matched[1] + "/" + matched[2] | ||
|
||
var eg errgroup.Group | ||
|
||
var snippet *gitlab.Snippet | ||
authorName := "" | ||
authorAvatarURL := "" | ||
var footerTime *time.Time | ||
var note *gitlab.Note | ||
|
||
snippetID, _ := strconv.Atoi(matched[3]) | ||
|
||
eg.Go(func() error { | ||
var err error | ||
start := time.Now() | ||
snippet, _, err = client.ProjectSnippets.GetSnippet(projectName, snippetID) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if isDebugLogging { | ||
duration := time.Now().Sub(start) | ||
fmt.Printf("[DEBUG] projectSnippetFetcher (%s): snippet=%+v\n", duration, snippet) | ||
} | ||
|
||
authorName = snippet.Author.Name | ||
footerTime = snippet.CreatedAt | ||
|
||
re2 := regexp.MustCompile("#note_(\\d+)$") | ||
matched2 := re2.FindStringSubmatch(path) | ||
|
||
if matched2 != nil { | ||
noteID, _ := strconv.Atoi(matched2[1]) | ||
start := time.Now() | ||
note, _, err = client.Notes.GetSnippetNote(projectName, snippetID, noteID) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if isDebugLogging { | ||
duration := time.Now().Sub(start) | ||
fmt.Printf("[DEBUG] projectSnippetFetcher (%s): note=%+v\n", duration, note) | ||
} | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
content := "" | ||
|
||
eg.Go(func() error { | ||
var err error | ||
start := time.Now() | ||
rawFile, _, err := client.ProjectSnippets.SnippetContent(projectName, snippetID) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
content = strings.TrimSpace(string(rawFile)) | ||
|
||
if isDebugLogging { | ||
duration := time.Now().Sub(start) | ||
fmt.Printf("[DEBUG] projectSnippetFetcher (%s): content=%+v\n", duration, content) | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
var project *gitlab.Project | ||
eg.Go(func() error { | ||
var err error | ||
start := time.Now() | ||
project, _, err = client.Projects.GetProject(projectName, nil) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if isDebugLogging { | ||
duration := time.Now().Sub(start) | ||
fmt.Printf("[DEBUG] projectSnippetFetcher (%s): project=%+v\n", duration, project) | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
if err := eg.Wait(); err != nil { | ||
return nil, err | ||
} | ||
|
||
description := fmt.Sprintf("```\n%s\n```", content) | ||
canTruncateDescription := false | ||
|
||
if note != nil { | ||
description = note.Body | ||
authorName = note.Author.Name | ||
authorAvatarURL = note.Author.AvatarURL | ||
footerTime = note.CreatedAt | ||
canTruncateDescription = true | ||
} | ||
|
||
page := &Page{ | ||
Title: snippet.FileName, | ||
Description: description, | ||
AuthorName: authorName, | ||
AuthorAvatarURL: authorAvatarURL, | ||
AvatarURL: project.AvatarURL, | ||
CanTruncateDescription: canTruncateDescription, | ||
FooterTitle: project.PathWithNamespace, | ||
FooterURL: project.WebURL, | ||
FooterTime: footerTime, | ||
} | ||
|
||
return page, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package gitlab | ||
|
||
import ( | ||
"fmt" | ||
"github.com/xanzy/go-gitlab" | ||
"golang.org/x/sync/errgroup" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type snippetFetcher struct { | ||
} | ||
|
||
func (f *snippetFetcher) fetchPath(path string, client *gitlab.Client, isDebugLogging bool) (*Page, error) { | ||
re := regexp.MustCompile("^snippets/(\\d+)") | ||
matched := re.FindStringSubmatch(path) | ||
|
||
if matched == nil { | ||
return nil, nil | ||
} | ||
|
||
var eg errgroup.Group | ||
|
||
var snippet *gitlab.Snippet | ||
authorName := "" | ||
var footerTime *time.Time | ||
|
||
snippetID, _ := strconv.Atoi(matched[1]) | ||
|
||
eg.Go(func() error { | ||
var err error | ||
start := time.Now() | ||
snippet, _, err = client.Snippets.GetSnippet(snippetID) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if isDebugLogging { | ||
duration := time.Now().Sub(start) | ||
fmt.Printf("[DEBUG] snippetFetcher (%s): snippet=%+v\n", duration, snippet) | ||
} | ||
|
||
authorName = snippet.Author.Name | ||
footerTime = snippet.CreatedAt | ||
|
||
return nil | ||
}) | ||
|
||
content := "" | ||
|
||
eg.Go(func() error { | ||
var err error | ||
start := time.Now() | ||
rawFile, _, err := client.Snippets.SnippetContent(snippetID) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
content = strings.TrimSpace(string(rawFile)) | ||
|
||
if isDebugLogging { | ||
duration := time.Now().Sub(start) | ||
fmt.Printf("[DEBUG] snippetFetcher (%s): content=%+v\n", duration, content) | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
if err := eg.Wait(); err != nil { | ||
return nil, err | ||
} | ||
|
||
page := &Page{ | ||
Title: snippet.FileName, | ||
Description: fmt.Sprintf("```\n%s\n```", content), | ||
AuthorName: authorName, | ||
AuthorAvatarURL: "", | ||
AvatarURL: "", | ||
CanTruncateDescription: false, | ||
FooterTitle: "", | ||
FooterURL: "", | ||
FooterTime: footerTime, | ||
} | ||
|
||
return page, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"id": 1, | ||
"title": "test", | ||
"file_name": "add.rb", | ||
"description": "Ruby test snippet", | ||
"author": { | ||
"id": 1, | ||
"username": "john_smith", | ||
"email": "[email protected]", | ||
"name": "John Smith", | ||
"state": "active", | ||
"created_at": "2012-05-23T08:00:58Z" | ||
}, | ||
"updated_at": "2012-06-28T10:52:04Z", | ||
"created_at": "2012-06-28T10:52:04Z", | ||
"web_url": "http://example.com/example/example/snippets/1" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"id": 400, | ||
"body": "comment", | ||
"attachment": null, | ||
"author": { | ||
"id": 1, | ||
"username": "pipin", | ||
"email": "[email protected]", | ||
"name": "Pip", | ||
"state": "active", | ||
"avatar_url": "http://localhost:3000/uploads/user/avatar/1/pipin.jpeg", | ||
"created_at": "2013-09-30T13:46:01Z" | ||
}, | ||
"created_at": "2013-10-02T09:22:45Z", | ||
"updated_at": "2013-10-02T10:22:45Z", | ||
"system": true, | ||
"noteable_id": 377, | ||
"noteable_type": "Snippet", | ||
"noteable_iid": 377, | ||
"resolvable": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"id": 1, | ||
"title": "test", | ||
"file_name": "add.rb", | ||
"description": "Ruby test snippet", | ||
"visibility": "private", | ||
"author": { | ||
"id": 1, | ||
"username": "john_smith", | ||
"email": "[email protected]", | ||
"name": "John Smith", | ||
"state": "active", | ||
"created_at": "2012-05-23T08:00:58Z" | ||
}, | ||
"expires_at": null, | ||
"updated_at": "2012-06-28T10:52:04Z", | ||
"created_at": "2012-06-28T10:52:04Z", | ||
"web_url": "http://example.com/snippets/1", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
puts 'Hello' |
Oops, something went wrong.