Skip to content

Commit

Permalink
collect GitLab pipeline id and url for #2343 (#2345)
Browse files Browse the repository at this point in the history
* collect GitLab pipeline id and url for #2343

* collect gitlab CI_COMMIT_REF_NAME for branch or tag name

* no need to send gitlab commit ref name

* print all API request objects without [object Object] shortcuts

* fix GitLab CI detection

* use explicit git and ci commit branch information

* update record test

* add appveyor ci branch name test
  • Loading branch information
bahmutov authored and brian-mann committed Aug 16, 2018
1 parent 3c0a8ab commit 8f0a31a
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 29 deletions.
3 changes: 2 additions & 1 deletion packages/server/lib/api.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
Expand Down
30 changes: 21 additions & 9 deletions packages/server/lib/modes/record.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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 })
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -545,4 +556,5 @@ module.exports = {

createRunAndRecordSpecs

getCommitFromGitOrCi
}
17 changes: 13 additions & 4 deletions packages/server/lib/util/ci_provider.coffee
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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
Expand All @@ -41,8 +42,8 @@ CI_PROVIDERS = {
"snap": "SNAP_CI"
"teamcity": "TEAMCITY_VERSION"
"teamfoundation": "TF_BUILD"
"travis": "TRAVIS"
"wercker": isWercker
"travis": "TRAVIS"
"wercker": isWercker
}

_detectProviderName = ->
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 15 additions & 2 deletions packages/server/test/unit/ci_provider_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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

Expand Down
51 changes: 39 additions & 12 deletions packages/server/test/unit/modes/record_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down

0 comments on commit 8f0a31a

Please sign in to comment.