diff --git a/codecov/__init__.py b/codecov/__init__.py index c036f425..00354d11 100644 --- a/codecov/__init__.py +++ b/codecov/__init__.py @@ -28,13 +28,14 @@ from urllib import urlencode quote = None -if sys.platform == 'win32': # pragma: no cover +if sys.platform == "win32": # pragma: no cover try: # https://github.com/python/cpython/blob/3.7/Lib/subprocess.py#L174-L175 from subprocess import list2cmdline def quote(arg): return list2cmdline([arg]) + except ImportError: pass @@ -758,17 +759,21 @@ def main(*argv, **kwargs): # Gitlab CI # --------- elif os.getenv("CI_SERVER_NAME", "").startswith("GitLab"): - # http://doc.gitlab.com/ci/examples/README.html#environmental-variables - # https://gitlab.com/gitlab-org/gitlab-ci-runner/blob/master/lib/build.rb#L96 + # https://docs.gitlab.com/ee/ci/variables/predefined_variables.html + # https://gitlab.com/gitlab-org/gitlab-ci-runner/blob/master/lib/build.rb query.update( dict( service="gitlab", - branch=os.getenv("CI_BUILD_REF_NAME"), - build=os.getenv("CI_BUILD_ID"), - commit=os.getenv("CI_BUILD_REF"), + branch=os.getenv( + "CI_COMMIT_REF_NAME", os.getenv("CI_BUILD_REF_NAME") + ), + build=os.getenv("CI_JOB_ID", os.getenv("CI_BUILD_ID")), + commit=os.getenv("CI_COMMIT_SHA", os.getenv("CI_BUILD_REF")), ) ) - if sys.platform == "win32" or os.getenv("CI_PROJECT_DIR", "").startswith("/"): + if sys.platform == "win32" or os.getenv("CI_PROJECT_DIR", "").startswith( + "/" + ): root = os.getenv("CI_PROJECT_DIR") else: root = os.getenv("HOME") + "/" + os.getenv("CI_PROJECT_DIR", "") diff --git a/tests/test.py b/tests/test.py index 279d21ec..5e305eda 100644 --- a/tests/test.py +++ b/tests/test.py @@ -98,6 +98,10 @@ def setUp(self): "CI_PROJECT_DIR", "CI_BUILD_REF", "CI_SERVER_NAME", + "CI_COMMIT_REF_NAME", + "CI_JOB_ID", + "CI_REPOSITORY_URL", + "CI_COMMIT_SHA", "ghprbActualCommit", "ghprbSourceBranch", "ghprbPullId", @@ -791,7 +795,7 @@ def test_ci_magnum(self): @unittest.skipUnless( os.getenv("CI_SERVER_NAME", "").startswith("GitLab"), "Skip GitLab CI test" ) - def test_ci_gitlab(self): + def test_ci_gitlab_pre9(self): self.set_env( CI_BUILD_REF_NAME="master", CI_BUILD_ID="1399372237", @@ -812,6 +816,30 @@ def test_ci_gitlab(self): self.assertEqual(res["query"]["slug"], "owner/repo") self.assertEqual(res["codecov"].token, "token") + @unittest.skipUnless( + os.getenv("CI_SERVER_NAME", "").startswith("GitLab"), "Skip GitLab CI test" + ) + def test_ci_gitlab(self): + self.set_env( + CI_COMMIT_REF_NAME="master", + CI_JOB_ID="1399372237", + CI_REPOSITORY_URL="https://gitlab.com/owner/repo.git", + CI_SERVER_NAME="GitLab CI", + CI_COMMIT_SHA="d653b934ed59c1a785cc1cc79d08c9aaa4eba73b", + HOME="/", + CI_PROJECT_DIR=os.getcwd().strip("/"), + CODECOV_TOKEN="token", + ) + self.fake_report() + res = self.run_cli() + self.assertEqual(res["query"]["service"], "gitlab") + self.assertEqual( + res["query"]["commit"], "d653b934ed59c1a785cc1cc79d08c9aaa4eba73b" + ) + self.assertEqual(res["query"]["build"], "1399372237") + self.assertEqual(res["query"]["slug"], "owner/repo") + self.assertEqual(res["codecov"].token, "token") + @unittest.skip("Skip CI None") def test_ci_none(self): self.set_env(CODECOV_TOKEN="token")