{"action":"created","comment":{"url":"https://api.github.com/repos/sourcegraph/sourcegraph/pulls/comments/234356781","pull_request_review_id":175992375,"id":234356781,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIzNDM1Njc4MQ==","diff_hunk":"@@ -0,0 +1,323 @@\n+package main\n+\n+import (\n+\t\"encoding/json\"\n+\t\"fmt\"\n+\t\"io\"\n+\t\"io/ioutil\"\n+\t\"log\"\n+\t\"math/rand\"\n+\t\"net\"\n+\t\"net/http\"\n+\t\"net/url\"\n+\t\"os\"\n+\t\"os/exec\"\n+\t\"path/filepath\"\n+\t\"strconv\"\n+\t\"strings\"\n+\t\"time\"\n+\n+\t\"gopkg.in/inconshreveable/log15.v2\"\n+\n+\t\"github.com/pkg/errors\"\n+\t\"github.com/sourcegraph/sourcegraph/pkg/env\"\n+)\n+\n+var (\n+\trepositoriesRoot = env.Get(\"REPOSITORIES_ROOT\", \"./repositories\", \"Root path to store repository data\")\n+\trepositoryTTL    = mustParseDuration(env.Get(\"REPOSITORY_TTL\", \"30m\", \"How long to keep repositories, as a duration string\"))\n+\n+\tlistenerPort = env.Get(\"LISTENER_PORT\", \"4014\", \"The network port to listen on\")\n+\n+\tmaxRequestSize = 64 * 1024 // Repo creation requests must be at most 64KB.\n+)\n+\n+const repositoryRequestPrefix = \"/repository/\"\n+\n+func mustParseDuration(durationString string) time.Duration {\n+\tduration, err := time.ParseDuration(durationString)\n+\tif err != nil {\n+\t\tpanic(fmt.Sprintf(\"Unrecognized duration string '%v'\", durationString))\n+\t}\n+\treturn duration\n+}\n+\n+// A repository is represented by its directory name under repositoriesRoot.\n+type repository string\n+\n+// filePathForReponsitory returns the location of this repository on disk.\n+func (r repository) filePath() string {\n+\treturn filepath.Join(repositoriesRoot, string(r))\n+}\n+\n+// urlPath returns the URL path to use when cloning this repository over http.\n+func (r repository) urlPath() string {\n+\treturn repositoryRequestPrefix + string(r)\n+}\n+\n+// goodUntil checks the repository's directory on disk and returns the time\n+// it will expire, or an error if the repository couldn't be read.\n+func (r repository) goodUntil() (time.Time, error) {\n+\tpath := r.filePath()\n+\tinfo, err := os.Lstat(path)\n+\tif err != nil {\n+\t\treturn time.Time{}, err\n+\t}\n+\treturn info.ModTime().Add(repositoryTTL), nil\n+}\n+\n+func createRandomKey() string {\n+\tkeyIndex := rand.Int31()\n+\treturn strconv.FormatInt(int64(keyIndex), 16)\n+}\n+\n+// createNewRepository creates a new repository with the given name and data.\n+// Returns a repository string if successful, otherwise returns error.\n+func createNewRepository(name string, data map[string]string) (repository, error) {\n+\t// Create a temporary directory to initialize with the repository data.\n+\ttempRoot, err := ioutil.TempDir(\"\", \"web-repo-proxy\")\n+\tif err != nil {\n+\t\treturn \"\", err\n+\t}\n+\tdefer func() {\n+\t\t// Make sure we remove the temporary directory, in case there was an\n+\t\t// error before it was moved to its final location  (otherwise this is a\n+\t\t// no-op).\n+\t\tos.RemoveAll(tempRoot)\n+\t}()\n+\n+\t// Write out the given data to the file system.\n+\tfor filename, content := range data {\n+\t\tfilePath := filepath.Join(tempRoot, filename)\n+\t\tif err = ioutil.WriteFile(filePath, []byte(content), 0644); err != nil {\n+\t\t\treturn \"\", err\n+\t\t}\n+\t}\n+\n+\t// Set up the git repository.\n+\tgitCommands := [][]string{\n+\t\t[]string{\"init\"},\n+\t\t[]string{\"add\", \".\"},\n+\t\t[]string{\"commit\", \"-m\", \"Initial commit\"},\n+\t\t// update-server-info is needed to serve repositories over static http.\n+\t\t[]string{\"update-server-info\"},\n+\t}\n+\tfor _, command := range gitCommands {\n+\t\tcmd := exec.Command(\"git\", command...)\n+\t\tcmd.Dir = tempRoot\n+\t\tif err = cmd.Run(); err != nil {\n+\t\t\treturn \"\", err\n+\t\t}\n+\t}\n+\n+\t// Choose the real repository path and move it there.\n+\tfor i := 0; i < 3; i++ {\n+\t\t// Retry up to 3 times to avoid ephemeral errors and name collisions.\n+\t\trepository := repository(name + \"-\" + createRandomKey())\n+\t\trepositoryRoot := repository.filePath()\n+\t\tif err = os.Rename(tempRoot, repositoryRoot); err == nil {\n+\t\t\treturn repository, nil\n+\t\t}\n+\t}\n+\t// If we still failed after 3 tries, return the most recent error.\n+\treturn \"\", err\n+}\n+\n+// Check whether any repositories have expired, and delete them if so.\n+func deleteExpiredRepositories() {\n+\tcurTime := time.Now()","path":"cmd/web-repo-proxy/main.go","position":128,"original_position":128,"commit_id":"03dd9140b1f26e7aac34bd0e6c52eaefec274b7d","original_commit_id":"03dd9140b1f26e7aac34bd0e6c52eaefec274b7d","user":{"login":"ggilmore","id":9022011,"node_id":"MDQ6VXNlcjkwMjIwMTE=","avatar_url":"https://avatars0.githubusercontent.com/u/9022011?v=4","gravatar_id":"","url":"https://api.github.com/users/ggilmore","html_url":"https://github.com/ggilmore","followers_url":"https://api.github.com/users/ggilmore/followers","following_url":"https://api.github.com/users/ggilmore/following{/other_user}","gists_url":"https://api.github.com/users/ggilmore/gists{/gist_id}","starred_url":"https://api.github.com/users/ggilmore/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ggilmore/subscriptions","organizations_url":"https://api.github.com/users/ggilmore/orgs","repos_url":"https://api.github.com/users/ggilmore/repos","events_url":"https://api.github.com/users/ggilmore/events{/privacy}","received_events_url":"https://api.github.com/users/ggilmore/received_events","type":"User","site_admin":false},"body":"For `curTime`, I suggest:\r\n\r\n1) Move `curTime`'s declaration to right above the `for` loop so that it can be closer to where it's actually used. See: https://github.com/sourcegraph/sourcegraph/blob/master/doc/dev/style.md#group-code-blocks-logically\r\n\r\n2) Change `curTime`'s name to just `now`. e.g. `now := time.Now()`. See https://github.com/sourcegraph/sourcegraph/blob/master/doc/dev/style.md#use-pronouncable-names\r\n\r\n","created_at":"2018-11-16T21:43:42Z","updated_at":"2018-11-16T21:43:42Z","html_url":"https://github.com/sourcegraph/sourcegraph/pull/862#discussion_r234356781","pull_request_url":"https://api.github.com/repos/sourcegraph/sourcegraph/pulls/862","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/sourcegraph/sourcegraph/pulls/comments/234356781"},"html":{"href":"https://github.com/sourcegraph/sourcegraph/pull/862#discussion_r234356781"},"pull_request":{"href":"https://api.github.com/repos/sourcegraph/sourcegraph/pulls/862"}}},"pull_request":{"url":"https://api.github.com/repos/sourcegraph/sourcegraph/pulls/862","id":228869179,"node_id":"MDExOlB1bGxSZXF1ZXN0MjI4ODY5MTc5","html_url":"https://github.com/sourcegraph/sourcegraph/pull/862","diff_url":"https://github.com/sourcegraph/sourcegraph/pull/862.diff","patch_url":"https://github.com/sourcegraph/sourcegraph/pull/862.patch","issue_url":"https://api.github.com/repos/sourcegraph/sourcegraph/issues/862","number":862,"state":"open","locked":false,"title":"Create a service that hosts ephemeral git repositories","user":{"login":"faec","id":16033661,"node_id":"MDQ6VXNlcjE2MDMzNjYx","avatar_url":"https://avatars1.githubusercontent.com/u/16033661?v=4","gravatar_id":"","url":"https://api.github.com/users/faec","html_url":"https://github.com/faec","followers_url":"https://api.github.com/users/faec/followers","following_url":"https://api.github.com/users/faec/following{/other_user}","gists_url":"https://api.github.com/users/faec/gists{/gist_id}","starred_url":"https://api.github.com/users/faec/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/faec/subscriptions","organizations_url":"https://api.github.com/users/faec/orgs","repos_url":"https://api.github.com/users/faec/repos","events_url":"https://api.github.com/users/faec/events{/privacy}","received_events_url":"https://api.github.com/users/faec/received_events","type":"User","site_admin":false},"body":"This is a server that hosts ephemeral git repositories, intended for applying code intelligence to web pages and similar short-term sources.\r\n\r\nHelps https://github.com/sourcegraph/sourcegraph/issues/423\r\n\r\nServer parameters and testing procedure are documented in `README.md`.","created_at":"2018-11-06T23:24:38Z","updated_at":"2018-11-16T21:43:42Z","closed_at":null,"merged_at":null,"merge_commit_sha":"541e5151c130d6bb0685a9aa93aa5236d465f3d0","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/sourcegraph/sourcegraph/pulls/862/commits","review_comments_url":"https://api.github.com/repos/sourcegraph/sourcegraph/pulls/862/comments","review_comment_url":"https://api.github.com/repos/sourcegraph/sourcegraph/pulls/comments{/number}","comments_url":"https://api.github.com/repos/sourcegraph/sourcegraph/issues/862/comments","statuses_url":"https://api.github.com/repos/sourcegraph/sourcegraph/statuses/03dd9140b1f26e7aac34bd0e6c52eaefec274b7d","head":{"label":"faec:webproxy","ref":"webproxy","sha":"03dd9140b1f26e7aac34bd0e6c52eaefec274b7d","user":{"login":"faec","id":16033661,"node_id":"MDQ6VXNlcjE2MDMzNjYx","avatar_url":"https://avatars1.githubusercontent.com/u/16033661?v=4","gravatar_id":"","url":"https://api.github.com/users/faec","html_url":"https://github.com/faec","followers_url":"https://api.github.com/users/faec/followers","following_url":"https://api.github.com/users/faec/following{/other_user}","gists_url":"https://api.github.com/users/faec/gists{/gist_id}","starred_url":"https://api.github.com/users/faec/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/faec/subscriptions","organizations_url":"https://api.github.com/users/faec/orgs","repos_url":"https://api.github.com/users/faec/repos","events_url":"https://api.github.com/users/faec/events{/privacy}","received_events_url":"https://api.github.com/users/faec/received_events","type":"User","site_admin":false},"repo":{"id":156460394,"node_id":"MDEwOlJlcG9zaXRvcnkxNTY0NjAzOTQ=","name":"sourcegraph","full_name":"faec/sourcegraph","private":false,"owner":{"login":"faec","id":16033661,"node_id":"MDQ6VXNlcjE2MDMzNjYx","avatar_url":"https://avatars1.githubusercontent.com/u/16033661?v=4","gravatar_id":"","url":"https://api.github.com/users/faec","html_url":"https://github.com/faec","followers_url":"https://api.github.com/users/faec/followers","following_url":"https://api.github.com/users/faec/following{/other_user}","gists_url":"https://api.github.com/users/faec/gists{/gist_id}","starred_url":"https://api.github.com/users/faec/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/faec/subscriptions","organizations_url":"https://api.github.com/users/faec/orgs","repos_url":"https://api.github.com/users/faec/repos","events_url":"https://api.github.com/users/faec/events{/privacy}","received_events_url":"https://api.github.com/users/faec/received_events","type":"User","site_admin":false},"html_url":"https://github.com/faec/sourcegraph","description":"Code search and intelligence, self-hosted and scalable","fork":true,"url":"https://api.github.com/repos/faec/sourcegraph","forks_url":"https://api.github.com/repos/faec/sourcegraph/forks","keys_url":"https://api.github.com/repos/faec/sourcegraph/keys{/key_id}","collaborators_url":"https://api.github.com/repos/faec/sourcegraph/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/faec/sourcegraph/teams","hooks_url":"https://api.github.com/repos/faec/sourcegraph/hooks","issue_events_url":"https://api.github.com/repos/faec/sourcegraph/issues/events{/number}","events_url":"https://api.github.com/repos/faec/sourcegraph/events","assignees_url":"https://api.github.com/repos/faec/sourcegraph/assignees{/user}","branches_url":"https://api.github.com/repos/faec/sourcegraph/branches{/branch}","tags_url":"https://api.github.com/repos/faec/sourcegraph/tags","blobs_url":"https://api.github.com/repos/faec/sourcegraph/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/faec/sourcegraph/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/faec/sourcegraph/git/refs{/sha}","trees_url":"https://api.github.com/repos/faec/sourcegraph/git/trees{/sha}","statuses_url":"https://api.github.com/repos/faec/sourcegraph/statuses/{sha}","languages_url":"https://api.github.com/repos/faec/sourcegraph/languages","stargazers_url":"https://api.github.com/repos/faec/sourcegraph/stargazers","contributors_url":"https://api.github.com/repos/faec/sourcegraph/contributors","subscribers_url":"https://api.github.com/repos/faec/sourcegraph/subscribers","subscription_url":"https://api.github.com/repos/faec/sourcegraph/subscription","commits_url":"https://api.github.com/repos/faec/sourcegraph/commits{/sha}","git_commits_url":"https://api.github.com/repos/faec/sourcegraph/git/commits{/sha}","comments_url":"https://api.github.com/repos/faec/sourcegraph/comments{/number}","issue_comment_url":"https://api.github.com/repos/faec/sourcegraph/issues/comments{/number}","contents_url":"https://api.github.com/repos/faec/sourcegraph/contents/{+path}","compare_url":"https://api.github.com/repos/faec/sourcegraph/compare/{base}...{head}","merges_url":"https://api.github.com/repos/faec/sourcegraph/merges","archive_url":"https://api.github.com/repos/faec/sourcegraph/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/faec/sourcegraph/downloads","issues_url":"https://api.github.com/repos/faec/sourcegraph/issues{/number}","pulls_url":"https://api.github.com/repos/faec/sourcegraph/pulls{/number}","milestones_url":"https://api.github.com/repos/faec/sourcegraph/milestones{/number}","notifications_url":"https://api.github.com/repos/faec/sourcegraph/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/faec/sourcegraph/labels{/name}","releases_url":"https://api.github.com/repos/faec/sourcegraph/releases{/id}","deployments_url":"https://api.github.com/repos/faec/sourcegraph/deployments","created_at":"2018-11-06T23:04:49Z","updated_at":"2018-11-06T23:04:55Z","pushed_at":"2018-11-16T15:33:49Z","git_url":"git://github.com/faec/sourcegraph.git","ssh_url":"git@github.com:faec/sourcegraph.git","clone_url":"https://github.com/faec/sourcegraph.git","svn_url":"https://github.com/faec/sourcegraph","homepage":"https://sourcegraph.com","size":37684,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"other","name":"Other","spdx_id":"NOASSERTION","url":null,"node_id":"MDc6TGljZW5zZTA="},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"sourcegraph:master","ref":"master","sha":"32dd12756fc1c4a87e223344a5eb2f82a3ad4225","user":{"login":"sourcegraph","id":3979584,"node_id":"MDEyOk9yZ2FuaXphdGlvbjM5Nzk1ODQ=","avatar_url":"https://avatars0.githubusercontent.com/u/3979584?v=4","gravatar_id":"","url":"https://api.github.com/users/sourcegraph","html_url":"https://github.com/sourcegraph","followers_url":"https://api.github.com/users/sourcegraph/followers","following_url":"https://api.github.com/users/sourcegraph/following{/other_user}","gists_url":"https://api.github.com/users/sourcegraph/gists{/gist_id}","starred_url":"https://api.github.com/users/sourcegraph/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sourcegraph/subscriptions","organizations_url":"https://api.github.com/users/sourcegraph/orgs","repos_url":"https://api.github.com/users/sourcegraph/repos","events_url":"https://api.github.com/users/sourcegraph/events{/privacy}","received_events_url":"https://api.github.com/users/sourcegraph/received_events","type":"Organization","site_admin":false},"repo":{"id":41288708,"node_id":"MDEwOlJlcG9zaXRvcnk0MTI4ODcwOA==","name":"sourcegraph","full_name":"sourcegraph/sourcegraph","private":false,"owner":{"login":"sourcegraph","id":3979584,"node_id":"MDEyOk9yZ2FuaXphdGlvbjM5Nzk1ODQ=","avatar_url":"https://avatars0.githubusercontent.com/u/3979584?v=4","gravatar_id":"","url":"https://api.github.com/users/sourcegraph","html_url":"https://github.com/sourcegraph","followers_url":"https://api.github.com/users/sourcegraph/followers","following_url":"https://api.github.com/users/sourcegraph/following{/other_user}","gists_url":"https://api.github.com/users/sourcegraph/gists{/gist_id}","starred_url":"https://api.github.com/users/sourcegraph/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sourcegraph/subscriptions","organizations_url":"https://api.github.com/users/sourcegraph/orgs","repos_url":"https://api.github.com/users/sourcegraph/repos","events_url":"https://api.github.com/users/sourcegraph/events{/privacy}","received_events_url":"https://api.github.com/users/sourcegraph/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/sourcegraph/sourcegraph","description":"Code search and intelligence, self-hosted and scalable","fork":false,"url":"https://api.github.com/repos/sourcegraph/sourcegraph","forks_url":"https://api.github.com/repos/sourcegraph/sourcegraph/forks","keys_url":"https://api.github.com/repos/sourcegraph/sourcegraph/keys{/key_id}","collaborators_url":"https://api.github.com/repos/sourcegraph/sourcegraph/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/sourcegraph/sourcegraph/teams","hooks_url":"https://api.github.com/repos/sourcegraph/sourcegraph/hooks","issue_events_url":"https://api.github.com/repos/sourcegraph/sourcegraph/issues/events{/number}","events_url":"https://api.github.com/repos/sourcegraph/sourcegraph/events","assignees_url":"https://api.github.com/repos/sourcegraph/sourcegraph/assignees{/user}","branches_url":"https://api.github.com/repos/sourcegraph/sourcegraph/branches{/branch}","tags_url":"https://api.github.com/repos/sourcegraph/sourcegraph/tags","blobs_url":"https://api.github.com/repos/sourcegraph/sourcegraph/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/sourcegraph/sourcegraph/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/sourcegraph/sourcegraph/git/refs{/sha}","trees_url":"https://api.github.com/repos/sourcegraph/sourcegraph/git/trees{/sha}","statuses_url":"https://api.github.com/repos/sourcegraph/sourcegraph/statuses/{sha}","languages_url":"https://api.github.com/repos/sourcegraph/sourcegraph/languages","stargazers_url":"https://api.github.com/repos/sourcegraph/sourcegraph/stargazers","contributors_url":"https://api.github.com/repos/sourcegraph/sourcegraph/contributors","subscribers_url":"https://api.github.com/repos/sourcegraph/sourcegraph/subscribers","subscription_url":"https://api.github.com/repos/sourcegraph/sourcegraph/subscription","commits_url":"https://api.github.com/repos/sourcegraph/sourcegraph/commits{/sha}","git_commits_url":"https://api.github.com/repos/sourcegraph/sourcegraph/git/commits{/sha}","comments_url":"https://api.github.com/repos/sourcegraph/sourcegraph/comments{/number}","issue_comment_url":"https://api.github.com/repos/sourcegraph/sourcegraph/issues/comments{/number}","contents_url":"https://api.github.com/repos/sourcegraph/sourcegraph/contents/{+path}","compare_url":"https://api.github.com/repos/sourcegraph/sourcegraph/compare/{base}...{head}","merges_url":"https://api.github.com/repos/sourcegraph/sourcegraph/merges","archive_url":"https://api.github.com/repos/sourcegraph/sourcegraph/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/sourcegraph/sourcegraph/downloads","issues_url":"https://api.github.com/repos/sourcegraph/sourcegraph/issues{/number}","pulls_url":"https://api.github.com/repos/sourcegraph/sourcegraph/pulls{/number}","milestones_url":"https://api.github.com/repos/sourcegraph/sourcegraph/milestones{/number}","notifications_url":"https://api.github.com/repos/sourcegraph/sourcegraph/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/sourcegraph/sourcegraph/labels{/name}","releases_url":"https://api.github.com/repos/sourcegraph/sourcegraph/releases{/id}","deployments_url":"https://api.github.com/repos/sourcegraph/sourcegraph/deployments","created_at":"2015-08-24T07:27:28Z","updated_at":"2018-11-16T17:54:42Z","pushed_at":"2018-11-16T20:58:15Z","git_url":"git://github.com/sourcegraph/sourcegraph.git","ssh_url":"git@github.com:sourcegraph/sourcegraph.git","clone_url":"https://github.com/sourcegraph/sourcegraph.git","svn_url":"https://github.com/sourcegraph/sourcegraph","homepage":"https://sourcegraph.com","size":43924,"stargazers_count":264,"watchers_count":264,"language":"Go","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":20,"mirror_url":null,"archived":false,"open_issues_count":368,"license":{"key":"other","name":"Other","spdx_id":"NOASSERTION","url":null,"node_id":"MDc6TGljZW5zZTA="},"forks":20,"open_issues":368,"watchers":264,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/sourcegraph/sourcegraph/pulls/862"},"html":{"href":"https://github.com/sourcegraph/sourcegraph/pull/862"},"issue":{"href":"https://api.github.com/repos/sourcegraph/sourcegraph/issues/862"},"comments":{"href":"https://api.github.com/repos/sourcegraph/sourcegraph/issues/862/comments"},"review_comments":{"href":"https://api.github.com/repos/sourcegraph/sourcegraph/pulls/862/comments"},"review_comment":{"href":"https://api.github.com/repos/sourcegraph/sourcegraph/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/sourcegraph/sourcegraph/pulls/862/commits"},"statuses":{"href":"https://api.github.com/repos/sourcegraph/sourcegraph/statuses/03dd9140b1f26e7aac34bd0e6c52eaefec274b7d"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"ggilmore"}}