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

Support snippet URL #71

Merged
merged 4 commits into from
Jul 8, 2019
Merged
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -33,6 +33,10 @@
* e.g. `${GITLAB_BASE_URL}/:namespace/:reponame/pipelines/:id`
* Blob URL
* e.g. `${GITLAB_BASE_URL}/:namespace/:reponame/blob/:sha1/:filename`
* Project snippet URL
* e.g. `${GITLAB_BASE_URL}/:namespace/:reponame/snippets/:id`
* Snippet URL
* e.g. `${GITLAB_BASE_URL}/snippets/:id`

## Running standalone
Download latest binary from https://github.com/sue445/gitpanda/releases
20 changes: 19 additions & 1 deletion gitlab/page.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package gitlab

import "time"
import (
"fmt"
"time"
)

// Page represents info of GitLab page
type Page struct {
@@ -15,3 +18,18 @@ type Page struct {
FooterTime *time.Time
Color string
}

// FormatFooter returns formatted footer for slack
func (p *Page) FormatFooter() string {
if p.FooterURL != "" {
if p.FooterTitle != "" {
return fmt.Sprintf("<%s|%s>", p.FooterURL, p.FooterTitle)
}
return p.FooterURL
}

if p.FooterTitle != "" {
return p.FooterTitle
}
return ""
}
61 changes: 61 additions & 0 deletions gitlab/page_test.go
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)
}
})
}
}
141 changes: 141 additions & 0 deletions gitlab/project_snippet_fetcher.go
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
}
90 changes: 90 additions & 0 deletions gitlab/snippet_fetcher.go
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
}
17 changes: 17 additions & 0 deletions gitlab/testdata/project_snippet.json
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"
}
21 changes: 21 additions & 0 deletions gitlab/testdata/project_snippet_note.json
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
}
19 changes: 19 additions & 0 deletions gitlab/testdata/snippet.json
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",
}
1 change: 1 addition & 0 deletions gitlab/testdata/snippet_code.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
puts 'Hello'
Loading