diff --git a/packages/server/lib/api.coffee b/packages/server/lib/api.coffee index 9687358d8f8f..7026dd2baaff 100644 --- a/packages/server/lib/api.coffee +++ b/packages/server/lib/api.coffee @@ -47,8 +47,9 @@ rp = request.defaults (params = {}, callback) -> method = params.method.toLowerCase() + # use %j argument to ensure deep nested properties are serialized debug( - "request to url: %s with params: %o", + "request to url: %s with params: %j", "#{params.method} #{params.url}", _.pick(params, "body", "headers") ) diff --git a/packages/server/lib/modes/record.coffee b/packages/server/lib/modes/record.coffee index b47add3c6573..d4117987c05e 100644 --- a/packages/server/lib/modes/record.coffee +++ b/packages/server/lib/modes/record.coffee @@ -219,6 +219,18 @@ updateInstance = (options = {}) -> else null +getCommitFromGitOrCi = (git) -> + la(check.object(git), 'expected git information object', git) + ciProvider.commitDefaults({ + sha: git.sha + branch: git.branch + authorName: git.author + authorEmail: git.email + message: git.message + remoteOrigin: git.remote + defaultBranch: null + }) + createRun = (options = {}) -> _.defaults(options, { group: null, @@ -251,6 +263,10 @@ createRun = (options = {}) -> specs = _.map(specs, getSpecRelativePath) + commit = getCommitFromGitOrCi(git) + debug("commit information from Git or from environment variables") + debug(commit) + makeRequest = -> api.createRun({ specs @@ -265,15 +281,7 @@ createRun = (options = {}) -> params: ciProvider.ciParams() provider: ciProvider.provider() } - commit: ciProvider.commitDefaults({ - sha: git.sha - branch: git.branch - authorName: git.author - authorEmail: git.email - message: git.message - remoteOrigin: git.remote - defaultBranch: null - }) + commit }) api.retryWithBackoff(makeRequest, { onBeforeRetry }) @@ -415,6 +423,9 @@ createRunAndRecordSpecs = (options = {}) -> commitInfo.commitInfo(projectRoot) .then (git) -> + debug("found the following git information") + debug(git) + platform = { osCpus: sys.osCpus osName: sys.osName @@ -545,4 +556,5 @@ module.exports = { createRunAndRecordSpecs + getCommitFromGitOrCi } diff --git a/packages/server/lib/util/ci_provider.coffee b/packages/server/lib/util/ci_provider.coffee index c6b59b68bd71..b6e48a7b426a 100644 --- a/packages/server/lib/util/ci_provider.coffee +++ b/packages/server/lib/util/ci_provider.coffee @@ -1,6 +1,7 @@ _ = require("lodash") la = require("lazy-ass") check = require("check-more-types") +debug = require("debug")("cypress:server") join = (char, pieces...) -> _.chain(pieces).compact().join(char).value() @@ -15,7 +16,7 @@ isCodeship = -> process.env.CI_NAME and process.env.CI_NAME is "codeship" isGitlab = -> - process.env.GITLAB_CI or (process.env.CI_SERVER_NAME and process.env.CI_SERVER_NAME is "GitLab CI") + process.env.GITLAB_CI or (process.env.CI_SERVER_NAME and /^GitLab/.test(process.env.CI_SERVER_NAME)) isJenkins = -> process.env.JENKINS_URL or @@ -41,8 +42,8 @@ CI_PROVIDERS = { "snap": "SNAP_CI" "teamcity": "TEAMCITY_VERSION" "teamfoundation": "TF_BUILD" - "travis": "TRAVIS" - "wercker": isWercker + "travis": "TRAVIS" + "wercker": isWercker } _detectProviderName = -> @@ -114,10 +115,16 @@ _providerCiParams = -> "DRONE_BUILD_NUMBER" "DRONE_PULL_REQUEST" ]) + # see https://docs.gitlab.com/ee/ci/variables/ gitlab: extract([ + # pipeline is common among all jobs + "CI_PIPELINE_ID", + "CI_PIPELINE_URL", + # individual jobs + "CI_BUILD_ID" # build id and job id are aliases "CI_JOB_ID" "CI_JOB_URL" - "CI_BUILD_ID" + # other information "GITLAB_HOST" "CI_PROJECT_ID" "CI_PROJECT_URL" @@ -302,6 +309,8 @@ commitParams = -> commitDefaults = (existingInfo) -> commitParamsObj = commitParams() or {} + debug("commit params object") + debug(commitParamsObj) ## based on the existingInfo properties ## merge in the commitParams if null or undefined diff --git a/packages/server/package.json b/packages/server/package.json index ce1d5d6f48d7..1eed841a88da 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -81,7 +81,7 @@ }, "dependencies": { "@cypress/browserify-preprocessor": "1.1.0", - "@cypress/commit-info": "^1.2.2", + "@cypress/commit-info": "2.0.0", "@cypress/icons": "0.5.4", "@cypress/mocha-teamcity-reporter": "^1.0.0", "@ffmpeg-installer/ffmpeg": "1.0.15", diff --git a/packages/server/test/unit/ci_provider_spec.coffee b/packages/server/test/unit/ci_provider_spec.coffee index e975901d3529..15030a5f3dca 100644 --- a/packages/server/test/unit/ci_provider_spec.coffee +++ b/packages/server/test/unit/ci_provider_spec.coffee @@ -243,9 +243,14 @@ describe "lib/util/ci_provider", -> it "gitlab", -> process.env.GITLAB_CI = true + # Gitlab has job id and build id as synonyms + process.env.CI_BUILD_ID = "ciJobId" process.env.CI_JOB_ID = "ciJobId" process.env.CI_JOB_URL = "ciJobUrl" - process.env.CI_BUILD_ID = "ciBuildId" + + process.env.CI_PIPELINE_ID = "ciPipelineId" + process.env.CI_PIPELINE_URL = "ciPipelineUrl" + process.env.GITLAB_HOST = "gitlabHost" process.env.CI_PROJECT_ID = "ciProjectId" process.env.CI_PROJECT_URL = "ciProjectUrl" @@ -262,7 +267,9 @@ describe "lib/util/ci_provider", -> expectsCiParams({ ciJobId: "ciJobId" ciJobUrl: "ciJobUrl" - ciBuildId: "ciBuildId" + ciBuildId: "ciJobId" + ciPipelineId: "ciPipelineId" + ciPipelineUrl: "ciPipelineUrl" gitlabHost: "gitlabHost" ciProjectId: "ciProjectId" ciProjectUrl: "ciProjectUrl" @@ -283,6 +290,12 @@ describe "lib/util/ci_provider", -> expectsName("gitlab") + resetEnv() + + process.env.CI_SERVER_NAME = "GitLab" + + expectsName("gitlab") + it "jenkins", -> process.env.JENKINS_URL = true diff --git a/packages/server/test/unit/modes/record_spec.coffee b/packages/server/test/unit/modes/record_spec.coffee index c738c5d4c0d6..f17fd1035f05 100644 --- a/packages/server/test/unit/modes/record_spec.coffee +++ b/packages/server/test/unit/modes/record_spec.coffee @@ -2,6 +2,7 @@ require("../../spec_helper") _ = require("lodash") os = require("os") +debug = require("debug")("test") commitInfo = require("@cypress/commit-info") api = require("#{root}../lib/api") errors = require("#{root}../lib/errors") @@ -19,43 +20,69 @@ initialEnv = _.clone(process.env) describe "lib/modes/record", -> ## QUESTION: why are these tests here when ## this is a module... ? - context "commitInfo.getBranch", -> + context "getCommitFromGitOrCi", -> + gitCommit = { + branch: null + } + beforeEach -> delete process.env.CIRCLE_BRANCH delete process.env.TRAVIS_BRANCH delete process.env.BUILDKITE_BRANCH delete process.env.CI_BRANCH + delete process.env.CIRCLECI + delete process.env.TRAVIS + delete process.env.BUILDKITE + delete process.env.CI_NAME + delete process.env.APPVEYOR + delete process.env.APPVEYOR_REPO_BRANCH afterEach -> process.env = initialEnv it "gets branch from process.env.CIRCLE_BRANCH", -> + process.env.CIRCLECI = "1" process.env.CIRCLE_BRANCH = "bem/circle" process.env.TRAVIS_BRANCH = "bem/travis" process.env.CI_BRANCH = "bem/ci" - commitInfo.getBranch().then (ret) -> - expect(ret).to.eq("bem/circle") + commit = recordMode.getCommitFromGitOrCi(gitCommit) + debug(commit) + expect(commit.branch).to.eq("bem/circle") it "gets branch from process.env.TRAVIS_BRANCH", -> + process.env.TRAVIS = "1" process.env.TRAVIS_BRANCH = "bem/travis" process.env.CI_BRANCH = "bem/ci" - commitInfo.getBranch().then (ret) -> - expect(ret).to.eq("bem/travis") + commit = recordMode.getCommitFromGitOrCi(gitCommit) + debug(commit) + expect(commit.branch).to.eq("bem/travis") it "gets branch from process.env.BUILDKITE_BRANCH", -> - process.env.BUILDKITE_BRANCH = "bem/buildkite" - process.env.CI_BRANCH = "bem/ci" + process.env.BUILDKITE = "1" + process.env.BUILDKITE_BRANCH = "bem/buildkite" + process.env.CI_BRANCH = "bem/ci" - commitInfo.getBranch().then (ret) -> - expect(ret).to.eq("bem/buildkite") + commit = recordMode.getCommitFromGitOrCi(gitCommit) + debug(commit) + expect(commit.branch).to.eq("bem/buildkite") - it "gets branch from process.env.CI_BRANCH", -> + it "gets branch from process.env.CI_BRANCH for codeship", -> + process.env.CI_NAME = "codeship" process.env.CI_BRANCH = "bem/ci" - commitInfo.getBranch().then (ret) -> - expect(ret).to.eq("bem/ci") + commit = recordMode.getCommitFromGitOrCi(gitCommit) + debug(commit) + expect(commit.branch).to.eq("bem/ci") + + it "gets branch from process.env.APPVEYOR_REPO_BRANCH for AppVeyor", -> + process.env.APPVEYOR = "1" + process.env.APPVEYOR_REPO_BRANCH = "bem/app" + + commit = recordMode.getCommitFromGitOrCi(gitCommit) + debug(commit) + expect(commit.branch).to.eq("bem/app") it "gets branch from git" # this is tested inside @cypress/commit-info