-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ci): add github util to get previous commit sha
- Loading branch information
1 parent
a2cff8d
commit 1a4b9d6
Showing
10 changed files
with
305 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from .commit_range import collect_commit_range_from_ci_env | ||
from .previous_commit import get_previous_commit_from_ci_env | ||
from .supported_ci import SupportedCI | ||
|
||
|
||
__all__ = [ | ||
"SupportedCI", | ||
"collect_commit_range_from_ci_env", | ||
"get_previous_commit_from_ci_env", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import os | ||
from typing import Optional, Tuple | ||
|
||
import click | ||
|
||
from ggshield.core.errors import UnexpectedError | ||
from ggshield.core.git_shell import get_list_commit_SHA | ||
from ggshield.core.utils import EMPTY_SHA | ||
|
||
from .supported_ci import SupportedCI | ||
|
||
|
||
def get_previous_commit_from_ci_env( | ||
verbose: bool, | ||
) -> Tuple[str, SupportedCI]: | ||
""" | ||
Returns the previous HEAD sha of the targeted branch. | ||
Returns EMPTY_SHA if there was no commit before. | ||
""" | ||
if os.getenv("GITLAB_CI"): | ||
ci_mode = SupportedCI.GITLAB | ||
commit_sha = gitlav_previous_commit_sha(verbose) | ||
|
||
elif os.getenv("GITHUB_ACTIONS"): | ||
ci_mode = SupportedCI.GITHUB | ||
commit_sha = github_previous_commit_sha(verbose) | ||
|
||
elif os.getenv("TRAVIS"): | ||
ci_mode = SupportedCI.TRAVIS | ||
raise NotImplementedError("Not implemented for Travis.") | ||
|
||
elif os.getenv("JENKINS_HOME") or os.getenv("JENKINS_URL"): | ||
ci_mode = SupportedCI.JENKINS | ||
raise NotImplementedError("Not implemented for Jenkins.") | ||
|
||
elif os.getenv("CIRCLECI"): | ||
ci_mode = SupportedCI.CIRCLECI | ||
raise NotImplementedError("Not implemented for CircleCI.") | ||
|
||
elif os.getenv("BITBUCKET_COMMIT"): | ||
ci_mode = SupportedCI.BITBUCKET | ||
raise NotImplementedError("Not implemented for BitBucket.") | ||
|
||
elif os.getenv("DRONE"): | ||
ci_mode = SupportedCI.DRONE | ||
raise NotImplementedError("Not implemented for Drone.") | ||
|
||
elif os.getenv("BUILD_BUILDID"): | ||
ci_mode = SupportedCI.AZURE | ||
raise NotImplementedError("Not implemented for Azure.") | ||
|
||
else: | ||
raise UnexpectedError( | ||
f"Current CI is not detected or supported." | ||
f" Supported CIs: {', '.join([ci.value for ci in SupportedCI])}." | ||
) | ||
return (commit_sha, ci_mode) | ||
|
||
|
||
def github_previous_commit_sha(verbose: bool) -> str: | ||
push_before_sha = github_push_previous_commit_sha() | ||
pull_req_base_sha = github_pull_request_previous_commit_sha() | ||
|
||
if verbose: | ||
click.echo( | ||
f"github_push_before_sha: {push_before_sha}\n" | ||
f"github_pull_base_sha: {pull_req_base_sha}\n", | ||
err=True, | ||
) | ||
|
||
# This one has to be checked before the push one | ||
# because this one is only populated in case of PR | ||
# whereas push_before_sha can be populated in PR in case of | ||
# push force event in a PR | ||
if pull_req_base_sha: | ||
return pull_req_base_sha | ||
|
||
if push_before_sha: | ||
return push_before_sha | ||
|
||
raise UnexpectedError( | ||
"Unable to get previous commit. Please submit an issue with the following info:\n" | ||
" Repository URL: <Fill if public>\n" | ||
f"github_push_before_sha: {push_before_sha}\n" | ||
f"github_pull_base_sha: {pull_req_base_sha}\n" | ||
) | ||
|
||
|
||
def github_push_previous_commit_sha() -> Optional[str]: | ||
return os.getenv("GITHUB_PUSH_BEFORE_SHA") | ||
|
||
|
||
def github_pull_request_previous_commit_sha() -> Optional[str]: | ||
targeted_branch = os.getenv("GITHUB_BASE_REF") | ||
|
||
# Not in a pull request workflow | ||
if targeted_branch is None: | ||
return None | ||
|
||
# The branch is not directly available in CI env | ||
# We need to get commits through remotes | ||
last_target_commit = get_list_commit_SHA( | ||
f"remotes/origin/{targeted_branch}", max_count=1 | ||
) | ||
|
||
# Unable to find a commit on this branch | ||
# Consider it empty | ||
if len(last_target_commit) == 0: | ||
return None | ||
|
||
return last_target_commit[0] | ||
|
||
|
||
def gitlav_previous_commit_sha(verbose: bool) -> str: | ||
push_before_sha = gitlab_push_previous_commit_sha() | ||
merge_req_base_sha = gitlab_merge_request_previous_commit_sha() | ||
|
||
if verbose: | ||
click.echo( | ||
f"gitlab_push_before_sha: {push_before_sha}\n" | ||
f"gitlab_merge_base_sha: {merge_req_base_sha}\n", | ||
err=True, | ||
) | ||
|
||
# Always EMPTY_SHA in MR pipeline according with | ||
# https://docs.gitlab.com/ee/ci/variables/predefined_variables.html | ||
if push_before_sha and push_before_sha is not EMPTY_SHA: | ||
return push_before_sha | ||
|
||
if merge_req_base_sha: | ||
return merge_req_base_sha | ||
|
||
raise UnexpectedError( | ||
"Unable to get previous commit. Please submit an issue with the following info:\n" | ||
" Repository URL: <Fill if public>\n" | ||
f"gitlab_push_before_sha: {push_before_sha}\n" | ||
f"gitlab_pull_base_sha: {merge_req_base_sha}\n" | ||
) | ||
|
||
|
||
def gitlab_push_previous_commit_sha() -> Optional[str]: | ||
return os.getenv("CI_COMMIT_BEFORE_SHA") | ||
|
||
|
||
def gitlab_merge_request_previous_commit_sha() -> Optional[str]: | ||
targeted_branch = os.getenv("CI_MERGE_REQUEST_TARGET_BRANCH_NAME") | ||
|
||
# Not in a pull request workflow | ||
if targeted_branch is None: | ||
return None | ||
|
||
# The branch is not directly available in CI env | ||
# We need to get commits through remotes | ||
last_target_commit = get_list_commit_SHA( | ||
f"remotes/origin/{targeted_branch}", max_count=1 | ||
) | ||
|
||
# Unable to find a commit on this branch | ||
# Consider it empty | ||
if len(last_target_commit) == 0: | ||
return None | ||
|
||
return last_target_commit[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from enum import Enum | ||
|
||
|
||
class SupportedCI(Enum): | ||
GITLAB = "GITLAB" | ||
TRAVIS = "TRAVIS" | ||
CIRCLECI = "CIRCLECI" | ||
JENKINS = "JENKINS HOME" | ||
GITHUB = "GITHUB ACTIONS" | ||
BITBUCKET = "BITBUCKET PIPELINES" | ||
DRONE = "DRONE" | ||
AZURE = "AZURE PIPELINES" |
Oops, something went wrong.