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

Capture repositoryId from heroku #25910

Merged
merged 2 commits into from
Jul 21, 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
52 changes: 48 additions & 4 deletions packages/gatsby-telemetry/src/repository-id.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { createHash } from "crypto"
import path from "path"
import { basename } from "path"
import { createHash } from "crypto"
import { execSync } from "child_process"
import gitUp from "git-up"
import { readdirSync, readFileSync } from "fs"

import gitUp from "git-up"
import { getCIName } from "gatsby-core-utils"
// there are no types for git-up, so we create our own
// based on https://github.com/IonicaBizau/git-up/blob/60e6a4ff93d50360bbb80953bfab2f82d3418900/lib/index.js#L8-L28
const typedGitUp = gitUp as (
Expand Down Expand Up @@ -31,7 +34,8 @@ export const getRepoMetadata = (url: string): IRepositoryData | null => {
const res: IRepositoryData = { provider: hash(provider) }

const userAndRepo = pathname.split(`/`)
if (userAndRepo.length == 3) {

if (userAndRepo.length >= 3) {
res.owner = hash(userAndRepo[1])
res.name = hash(userAndRepo[2].replace(`.git`, ``))
}
Expand Down Expand Up @@ -83,8 +87,48 @@ const getRepositoryFromNetlifyEnv = (): IRepositoryId | null => {
return null
}

function getRepositoryFromHerokuEnv(): IRepositoryId | null {
if (getCIName() !== `Heroku`) {
return null
}

// Parse repository metadata from /proc/*/environ. This is a naive glob approach to only
// look pids in procfs.
const proc = readdirSync(`/proc`).filter(dir => Number.isFinite(Number(dir)))
jamo marked this conversation as resolved.
Show resolved Hide resolved
const len = proc.length
for (let i = 0; i < len; i++) {
const dir = proc[i]
try {
// Piggyback on internal datastructures for control processes to see the git repo info
const environData = readFileSync(path.join(`/proc`, dir, `environ`))
?.toString(`utf8`)
?.split(/\0/)
?.find(e => e.indexOf(`RECEIVE_DATA`) >= 0)
?.replace(`RECEIVE_DATA=`, ``)

if (!environData) {
continue
}
const data = JSON.parse(environData)
const url = data?.push_metadata?.source_url
if (url) {
return {
repositoryId: `heroku:${hash(url)}`,
repositoryData: getRepoMetadata(url),
}
}
} catch (e) {
// ignore
}
}
return null
}

export const getRepositoryId = (): IRepositoryId => {
const gitRepo = getGitRemoteWithGit() || getRepositoryFromNetlifyEnv()
const gitRepo =
getGitRemoteWithGit() ||
getRepositoryFromNetlifyEnv() ||
getRepositoryFromHerokuEnv()
if (gitRepo) {
return gitRepo
} else {
Expand Down
1 change: 0 additions & 1 deletion packages/gatsby-telemetry/src/telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import uuidV4 from "uuid/v4"
import { isCI, getCIName } from "gatsby-core-utils"

import {
getRepositoryId as _getRepositoryId,
IRepositoryId,
Expand Down