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 blob link without line hash #320

Merged
merged 2 commits into from
Jun 5, 2020
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
23 changes: 20 additions & 3 deletions gitlab/blob_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type blobFetcher struct {
}

func (f *blobFetcher) fetchPath(path string, client *gitlab.Client, isDebugLogging bool) (*Page, error) {
re := regexp.MustCompile(reProjectName + "/blob/([^/]+)/(.+)#L([0-9-]+)$")
re := regexp.MustCompile(reProjectName + "/blob/([^/]+)/(.+)$")
matched := re.FindStringSubmatch(path)

if matched == nil {
Expand All @@ -26,6 +26,13 @@ func (f *blobFetcher) fetchPath(path string, client *gitlab.Client, isDebugLoggi
sha1 := matched[2]
fileName := matched[3]

lineRe := regexp.MustCompile("(.+)#L([0-9-]+)$")
lineMatched := lineRe.FindStringSubmatch(fileName)

if lineMatched != nil {
fileName = lineMatched[1]
}

var eg errgroup.Group

selectedFile := ""
Expand All @@ -45,7 +52,12 @@ func (f *blobFetcher) fetchPath(path string, client *gitlab.Client, isDebugLoggi
fmt.Printf("[DEBUG] blobFetcher (%s): fileBody=%s\n", duration, fileBody)
}

lineHash := matched[4]
if lineMatched == nil {
selectedFile = fileBody
return nil
}

lineHash := lineMatched[2]
lines := strings.Split(lineHash, "-")

switch len(lines) {
Expand Down Expand Up @@ -87,8 +99,13 @@ func (f *blobFetcher) fetchPath(path string, client *gitlab.Client, isDebugLoggi
return nil, err
}

title := fileName
if lineRange != "" {
title = fmt.Sprintf("%s:%s", title, lineRange)
}

page := &Page{
Title: fmt.Sprintf("%s:%s", fileName, lineRange),
Title: title,
Description: fmt.Sprintf("```\n%s\n```", selectedFile),
AuthorName: "",
AuthorAvatarURL: "",
Expand Down
18 changes: 18 additions & 0 deletions gitlab/url_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,24 @@ func TestGitlabUrlParser_FetchURL(t *testing.T) {
Color: "",
},
},
{
name: "Blob URL (without line hash)",
args: args{
url: "http://example.com/diaspora/diaspora-project-site/blob/master/gitlabci-templates/continuous_bundle_update.yml",
},
want: &Page{
Title: "gitlabci-templates/continuous_bundle_update.yml",
Description: "```\ncontinuous_bundle_update:\n image: ruby\n\n variables:\n CACHE_VERSION: \"v1\"\n GIT_EMAIL: \"[email protected]\"\n GIT_USER: \"GitLab CI\"\n LABELS: \"bundle update\"\n OPTIONS: \"\"\n\n cache:\n key: \"$CACHE_VERSION-$CI_BUILD_NAME\"\n paths:\n - vendor/bundle/\n\n script:\n - bundle install --path vendor/bundle --clean\n - gem install --no-doc gitlabci-bundle-update-mr\n - gitlabci-bundle-update-mr --user=\"$GIT_USER\" --email=\"$GIT_EMAIL\" --labels=\"$LABELS\" $OPTIONS\n\n only:\n - schedules\n\n```",
AuthorName: "",
AuthorAvatarURL: "",
AvatarURL: "http://example.com/uploads/project/avatar/3/uploads/avatar.png",
CanTruncateDescription: false,
FooterTitle: "diaspora/diaspora-project-site",
FooterURL: "http://example.com/diaspora/diaspora-project-site",
FooterTime: nil,
Color: "",
},
},
{
name: "Job URL",
args: args{
Expand Down