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 group URL #68

Merged
merged 3 commits into from
Jun 30, 2019
Merged
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
17 changes: 17 additions & 0 deletions gitlab/testdata/group.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"id": 9970,
"web_url": "https://gitlab.com/groups/gitlab-org",
"name": "GitLab.org",
"path": "gitlab-org",
"description": "Open source software to collaborate on code",
"visibility": "public",
"lfs_enabled": true,
"avatar_url": "https://assets.gitlab-static.net/uploads/-/system/group/avatar/9970/logo-extra-whitespace.png",
"request_access_enabled": false,
"full_name": "GitLab.org",
"full_path": "gitlab-org",
"parent_id": null,
"ldap_cn": null,
"ldap_access": null,
"file_template_project_id": null
}
1 change: 1 addition & 0 deletions gitlab/testdata/users_by_group_name.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
2 changes: 1 addition & 1 deletion gitlab/url_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (p *URLParser) FetchURL(url string) (*Page, error) {
&pipelineFetcher{},
&blobFetcher{},
&projectFetcher{},
&userFetcher{baseURL: p.baseURL},
&userOrGroupFetcher{baseURL: p.baseURL},
}

for _, fetcher := range fetchers {
Expand Down
46 changes: 46 additions & 0 deletions gitlab/url_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ func TestGitlabUrlParser_FetchURL(t *testing.T) {
"http://example.com/api/v4/users?username=john_smith",
httpmock.NewStringResponder(200, testutil.ReadTestData("testdata/users.json")),
)
httpmock.RegisterResponder(
"GET",
"http://example.com/api/v4/users?username=gitlab-org",
httpmock.NewStringResponder(200, testutil.ReadTestData("testdata/users_by_group_name.json")),
)
httpmock.RegisterResponder(
"GET",
"http://example.com/api/v4/groups/gitlab-org?with_projects=false",
httpmock.NewStringResponder(200, testutil.ReadTestData("testdata/group.json")),
)
httpmock.RegisterResponder(
"GET",
"http://example.com/api/v4/projects/diaspora%2Fdiaspora-project-site/repository/files/gitlabci-templates%2Fcontinuous_bundle_update.yml/raw?ref=master",
Expand Down Expand Up @@ -339,6 +349,42 @@ func TestGitlabUrlParser_FetchURL(t *testing.T) {
Color: "",
},
},
{
name: "Group URL",
args: args{
url: "http://example.com/gitlab-org",
},
want: &Page{
Title: "GitLab.org · GitLab",
Description: "Open source software to collaborate on code",
AuthorName: "",
AuthorAvatarURL: "",
AvatarURL: "https://assets.gitlab-static.net/uploads/-/system/group/avatar/9970/logo-extra-whitespace.png",
CanTruncateDescription: true,
FooterTitle: "@gitlab-org",
FooterURL: "https://gitlab.com/groups/gitlab-org",
FooterTime: nil,
Color: "",
},
},
{
name: "Group URL (ends with slash)",
args: args{
url: "http://example.com/gitlab-org/",
},
want: &Page{
Title: "GitLab.org · GitLab",
Description: "Open source software to collaborate on code",
AuthorName: "",
AuthorAvatarURL: "",
AvatarURL: "https://assets.gitlab-static.net/uploads/-/system/group/avatar/9970/logo-extra-whitespace.png",
CanTruncateDescription: true,
FooterTitle: "@gitlab-org",
FooterURL: "https://gitlab.com/groups/gitlab-org",
FooterTime: nil,
Color: "",
},
},
{
name: "Blob URL (single line)",
args: args{
Expand Down
55 changes: 0 additions & 55 deletions gitlab/user_fetcher.go

This file was deleted.

111 changes: 111 additions & 0 deletions gitlab/user_or_group_fetcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package gitlab

import (
"fmt"
"github.com/xanzy/go-gitlab"
"net/http"
"regexp"
"strings"
"time"
)

type userOrGroupFetcher struct {
baseURL string
}

func (f *userOrGroupFetcher) fetchPath(path string, client *gitlab.Client, isDebugLogging bool) (*Page, error) {
re := regexp.MustCompile("^([^/]+)/?$")
matched := re.FindStringSubmatch(path)

if matched == nil {
return nil, nil
}

name := matched[1]
userPage, err := f.fetchUserPath(name, client, isDebugLogging)

if err != nil {
return nil, err
}

if userPage != nil {
return userPage, nil
}

groupPage, err := f.fetchGroupPath(name, client, isDebugLogging)

if err != nil {
return nil, err
}

if groupPage != nil {
return groupPage, nil
}

return nil, fmt.Errorf("%s is not found", name)
}

func (f *userOrGroupFetcher) fetchUserPath(name string, client *gitlab.Client, isDebugLogging bool) (*Page, error) {
start := time.Now()
users, _, err := client.Users.ListUsers(&gitlab.ListUsersOptions{Username: &name})

if err != nil {
return nil, err
}

if isDebugLogging {
duration := time.Now().Sub(start)
fmt.Printf("[DEBUG] fetchUserPath (%s): users=%+v\n", duration, users)
}

if len(users) < 1 {
return nil, nil
}

user := users[0]

page := &Page{
Title: strings.Join([]string{user.Name, "GitLab"}, titleSeparator),
Description: user.Name,
AuthorName: user.Name,
AuthorAvatarURL: user.AvatarURL,
AvatarURL: user.AvatarURL,
CanTruncateDescription: true,
FooterTitle: fmt.Sprintf("@%s", user.Username),
FooterURL: fmt.Sprintf("%s/%s", f.baseURL, user.Username),
FooterTime: user.CreatedAt,
}

return page, nil
}

func (f *userOrGroupFetcher) fetchGroupPath(name string, client *gitlab.Client, isDebugLogging bool) (*Page, error) {
start := time.Now()
group, _, err := client.Groups.GetGroup(name, func(req *http.Request) error {
req.URL.RawQuery = "with_projects=false"
return nil
})

if err != nil {
return nil, err
}

if isDebugLogging {
duration := time.Now().Sub(start)
fmt.Printf("[DEBUG] fetchGroupPath (%s): group=%+v\n", duration, group)
}

page := &Page{
Title: strings.Join([]string{group.Name, "GitLab"}, titleSeparator),
Description: group.Description,
AuthorName: "",
AuthorAvatarURL: "",
AvatarURL: group.AvatarURL,
CanTruncateDescription: true,
FooterTitle: fmt.Sprintf("@%s", group.Path),
FooterURL: group.WebURL,
FooterTime: nil,
}

return page, nil
}