From 4bc176fc464b1016960d624bb40f4aed8798e33a Mon Sep 17 00:00:00 2001 From: suusan2go Date: Fri, 5 Jun 2020 21:33:11 +0900 Subject: [PATCH 1/2] support blob preview without line hash --- gitlab/blob_fetcher.go | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/gitlab/blob_fetcher.go b/gitlab/blob_fetcher.go index a07286bb..f44087b1 100644 --- a/gitlab/blob_fetcher.go +++ b/gitlab/blob_fetcher.go @@ -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 { @@ -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 := "" @@ -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) { @@ -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: "", From cb4ffd07a0d8e92bb3991b03bb34d82f7b52183d Mon Sep 17 00:00:00 2001 From: suusan2go Date: Fri, 5 Jun 2020 23:27:24 +0900 Subject: [PATCH 2/2] add test case for blob url without line hash --- gitlab/url_parser_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/gitlab/url_parser_test.go b/gitlab/url_parser_test.go index dc2a1ace..81e0e99e 100644 --- a/gitlab/url_parser_test.go +++ b/gitlab/url_parser_test.go @@ -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: \"gitlabci@example.com\"\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{