internal static void ReloadEnvironmentData() { // ********** // Setup variables // ********** Provider = null; PipelineId = null; PipelineName = null; PipelineNumber = null; PipelineUrl = null; JobUrl = null; JobName = null; StageName = null; WorkspacePath = null; Repository = null; Commit = null; Branch = null; Tag = null; AuthorName = null; AuthorEmail = null; AuthorDate = null; CommitterName = null; CommitterEmail = null; CommitterDate = null; Message = null; SourceRoot = null; GitInfo gitInfo = GitInfo.GetCurrent(); if (EnvironmentHelpers.GetEnvironmentVariable("TRAVIS") != null) { SetupTravisEnvironment(); } else if (EnvironmentHelpers.GetEnvironmentVariable("CIRCLECI") != null) { SetupCircleCiEnvironment(); } else if (EnvironmentHelpers.GetEnvironmentVariable("JENKINS_URL") != null) { SetupJenkinsEnvironment(); } else if (EnvironmentHelpers.GetEnvironmentVariable("GITLAB_CI") != null) { SetupGitlabEnvironment(); } else if (EnvironmentHelpers.GetEnvironmentVariable("APPVEYOR") != null) { SetupAppveyorEnvironment(); } else if (EnvironmentHelpers.GetEnvironmentVariable("TF_BUILD") != null) { SetupAzurePipelinesEnvironment(); } else if (EnvironmentHelpers.GetEnvironmentVariable("BITBUCKET_COMMIT") != null) { SetupBitbucketEnvironment(); } else if (EnvironmentHelpers.GetEnvironmentVariable("GITHUB_SHA") != null) { SetupGithubActionsEnvironment(); } else if (EnvironmentHelpers.GetEnvironmentVariable("TEAMCITY_VERSION") != null) { SetupTeamcityEnvironment(); } else if (EnvironmentHelpers.GetEnvironmentVariable("BUILDKITE") != null) { SetupBuildkiteEnvironment(); } else if (EnvironmentHelpers.GetEnvironmentVariable("BITRISE_BUILD_SLUG") != null) { SetupBitriseEnvironment(); } else { Branch = gitInfo.Branch; Commit = gitInfo.Commit; Repository = gitInfo.Repository; SourceRoot = gitInfo.SourceRoot; WorkspacePath = gitInfo.SourceRoot; } // ********** // Add Author, Committer and Message from git // ********** // Merge commits have a different commit hash from the one reported by the CI. if (gitInfo.Commit == Commit) { AuthorName = gitInfo.AuthorName; AuthorEmail = gitInfo.AuthorEmail; AuthorDate = gitInfo.AuthorDate; CommitterName = gitInfo.CommitterName; CommitterEmail = gitInfo.CommitterEmail; CommitterDate = gitInfo.CommitterDate; Message = gitInfo.Message; } // ********** // Remove sensitive info from repository url // ********** if (Uri.TryCreate(Repository, UriKind.Absolute, out Uri repository)) { if (!string.IsNullOrEmpty(repository.UserInfo)) { Repository = repository.GetComponents(UriComponents.Fragment | UriComponents.Query | UriComponents.Path | UriComponents.Port | UriComponents.Host | UriComponents.Scheme, UriFormat.SafeUnescaped); } } // ********** // Expand ~ in Paths // ********** SourceRoot = ExpandPath(SourceRoot); WorkspacePath = ExpandPath(WorkspacePath); // ********** // Clean Refs // ********** CleanBranchAndTag(); }
private static GitInfo GetFrom(DirectoryInfo gitDirectory) { if (gitDirectory == null) { return(new GitInfo()); } GitInfo gitInfo = new GitInfo(); try { gitInfo.SourceRoot = gitDirectory.Parent?.FullName; // Get Git commit string headPath = Path.Combine(gitDirectory.FullName, "HEAD"); if (File.Exists(headPath)) { string head = File.ReadAllText(headPath).Trim(); // Symbolic Reference if (head.StartsWith("ref:")) { gitInfo.Branch = head.Substring(4).Trim(); string refPath = Path.Combine(gitDirectory.FullName, gitInfo.Branch); string infoRefPath = Path.Combine(gitDirectory.FullName, "info", "refs"); if (File.Exists(refPath)) { // Get the commit from the .git/{refPath} file. gitInfo.Commit = File.ReadAllText(refPath).Trim(); } else if (File.Exists(infoRefPath)) { // Get the commit from the .git/info/refs file. string[] lines = File.ReadAllLines(infoRefPath); foreach (string line in lines) { string[] hashRef = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); if (hashRef[1] == gitInfo.Branch) { gitInfo.Commit = hashRef[0]; } } } } else { // Hash reference gitInfo.Commit = head; } } // Process Git Config string configPath = Path.Combine(gitDirectory.FullName, "config"); List <ConfigItem> lstConfigs = GetConfigItems(configPath); if (lstConfigs != null && lstConfigs.Count > 0) { var remote = "origin"; var branchItem = lstConfigs.Find(i => i.Type == "branch" && i.Merge == gitInfo.Branch); if (branchItem != null) { gitInfo.Branch = branchItem.Name; remote = branchItem.Remote; } var remoteItem = lstConfigs.Find(i => i.Type == "remote" && i.Name == remote); if (remoteItem != null) { gitInfo.Repository = remoteItem.Url; } } // Get author and committer data if (!string.IsNullOrEmpty(gitInfo.Commit)) { string folder = gitInfo.Commit.Substring(0, 2); string file = gitInfo.Commit.Substring(2); string objectFilePath = Path.Combine(gitDirectory.FullName, "objects", folder, file); if (File.Exists(objectFilePath)) { // Load and parse object file if (GitCommitObject.TryGetFromObjectFile(objectFilePath, out var commitObject)) { gitInfo.AuthorDate = commitObject.AuthorDate; gitInfo.AuthorEmail = commitObject.AuthorEmail; gitInfo.AuthorName = commitObject.AuthorName; gitInfo.CommitterDate = commitObject.CommitterDate; gitInfo.CommitterEmail = commitObject.CommitterEmail; gitInfo.CommitterName = commitObject.CommitterName; gitInfo.Message = commitObject.Message; gitInfo.PgpSignature = commitObject.PgpSignature; } } else { // Search git object file from the pack files string packFolder = Path.Combine(gitDirectory.FullName, "objects", "pack"); string[] files = Directory.GetFiles(packFolder, "*.idx", SearchOption.TopDirectoryOnly); foreach (string idxFile in files) { if (GitPackageOffset.TryGetPackageOffset(idxFile, gitInfo.Commit, out var packageOffset)) { if (GitCommitObject.TryGetFromPackageOffset(packageOffset, out var commitObject)) { gitInfo.AuthorDate = commitObject.AuthorDate; gitInfo.AuthorEmail = commitObject.AuthorEmail; gitInfo.AuthorName = commitObject.AuthorName; gitInfo.CommitterDate = commitObject.CommitterDate; gitInfo.CommitterEmail = commitObject.CommitterEmail; gitInfo.CommitterName = commitObject.CommitterName; gitInfo.Message = commitObject.Message; gitInfo.PgpSignature = commitObject.PgpSignature; break; } } } } } } catch (Exception ex) { Log.Error(ex, "Error loading git information from directory"); } return(gitInfo); }
internal void ReloadEnvironmentData() { // ********** // Setup variables // ********** Provider = null; PipelineId = null; PipelineName = null; PipelineNumber = null; PipelineUrl = null; JobUrl = null; JobName = null; StageName = null; WorkspacePath = null; Repository = null; Commit = null; Branch = null; Tag = null; AuthorName = null; AuthorEmail = null; AuthorDate = null; CommitterName = null; CommitterEmail = null; CommitterDate = null; Message = null; SourceRoot = null; GitInfo gitInfo = GitInfo.GetCurrent(); if (EnvironmentHelpers.GetEnvironmentVariable("TRAVIS") != null) { SetupTravisEnvironment(); } else if (EnvironmentHelpers.GetEnvironmentVariable("CIRCLECI") != null) { SetupCircleCiEnvironment(); } else if (EnvironmentHelpers.GetEnvironmentVariable("JENKINS_URL") != null) { SetupJenkinsEnvironment(); } else if (EnvironmentHelpers.GetEnvironmentVariable("GITLAB_CI") != null) { SetupGitlabEnvironment(); } else if (EnvironmentHelpers.GetEnvironmentVariable("APPVEYOR") != null) { SetupAppveyorEnvironment(); } else if (EnvironmentHelpers.GetEnvironmentVariable("TF_BUILD") != null) { SetupAzurePipelinesEnvironment(); } else if (EnvironmentHelpers.GetEnvironmentVariable("BITBUCKET_COMMIT") != null) { SetupBitbucketEnvironment(); } else if (EnvironmentHelpers.GetEnvironmentVariable("GITHUB_SHA") != null) { SetupGithubActionsEnvironment(); } else if (EnvironmentHelpers.GetEnvironmentVariable("TEAMCITY_VERSION") != null) { SetupTeamcityEnvironment(); } else if (EnvironmentHelpers.GetEnvironmentVariable("BUILDKITE") != null) { SetupBuildkiteEnvironment(); } else if (EnvironmentHelpers.GetEnvironmentVariable("BITRISE_BUILD_SLUG") != null) { SetupBitriseEnvironment(); } else { Branch = gitInfo.Branch; Commit = gitInfo.Commit; Repository = gitInfo.Repository; SourceRoot = gitInfo.SourceRoot; WorkspacePath = gitInfo.SourceRoot; } // ********** // Add Author, Committer and Message from git // ********** // Merge commits have a different commit hash from the one reported by the CI. if (gitInfo.Commit == Commit) { if (string.IsNullOrEmpty(AuthorName)) { AuthorName = gitInfo.AuthorName; } if (string.IsNullOrEmpty(AuthorEmail)) { AuthorEmail = gitInfo.AuthorEmail; } if (AuthorDate is null) { AuthorDate = gitInfo.AuthorDate; } if (string.IsNullOrEmpty(CommitterName)) { CommitterName = gitInfo.CommitterName; } if (string.IsNullOrEmpty(CommitterEmail)) { CommitterEmail = gitInfo.CommitterEmail; } if (CommitterDate is null) { CommitterDate = gitInfo.CommitterDate; } if (string.IsNullOrEmpty(Message)) { Message = gitInfo.Message; } } // ********** // Remove sensitive info from repository url // ********** if (Uri.TryCreate(Repository, UriKind.Absolute, out Uri repository)) { if (!string.IsNullOrEmpty(repository.UserInfo)) { Repository = repository.GetComponents(UriComponents.Fragment | UriComponents.Query | UriComponents.Path | UriComponents.Port | UriComponents.Host | UriComponents.Scheme, UriFormat.SafeUnescaped); } } // ********** // Expand ~ in Paths // ********** SourceRoot = ExpandPath(SourceRoot); WorkspacePath = ExpandPath(WorkspacePath); // ********** // Custom environment variables. // ********** Branch = GetEnvironmentVariableIfIsNotEmpty("SIGNALFX_GIT_BRANCH", Branch); Tag = GetEnvironmentVariableIfIsNotEmpty("SIGNALFX_GIT_TAG", Tag); Repository = GetEnvironmentVariableIfIsNotEmpty("SIGNALFX_GIT_REPOSITORY_URL", Repository); Commit = GetEnvironmentVariableIfIsNotEmpty("SIGNALFX_GIT_COMMIT_SHA", Commit); Message = GetEnvironmentVariableIfIsNotEmpty("SIGNALFX_GIT_COMMIT_MESSAGE", Message); AuthorName = GetEnvironmentVariableIfIsNotEmpty("SIGNALFX_GIT_COMMIT_AUTHOR_NAME", AuthorName); AuthorEmail = GetEnvironmentVariableIfIsNotEmpty("SIGNALFX_GIT_COMMIT_AUTHOR_EMAIL", AuthorEmail); AuthorDate = GetDateTimeOffsetEnvironmentVariableIfIsNotEmpty("SIGNALFX_GIT_COMMIT_AUTHOR_DATE", AuthorDate); CommitterName = GetEnvironmentVariableIfIsNotEmpty("SIGNALFX_GIT_COMMIT_COMMITTER_NAME", CommitterName); CommitterEmail = GetEnvironmentVariableIfIsNotEmpty("SIGNALFX_GIT_COMMIT_COMMITTER_EMAIL", CommitterEmail); CommitterDate = GetDateTimeOffsetEnvironmentVariableIfIsNotEmpty("SIGNALFX_GIT_COMMIT_COMMITTER_DATE", CommitterDate); // ********** // Clean Refs // ********** CleanBranchAndTag(); // ********** // Try load CodeOwners // ********** if (!string.IsNullOrEmpty(SourceRoot)) { foreach (var codeOwnersPath in GetCodeOwnersPaths(SourceRoot)) { Log.Debug("Looking for CODEOWNERS file in: {path}", codeOwnersPath); if (File.Exists(codeOwnersPath)) { Log.Debug("CODEOWNERS file found: {path}", codeOwnersPath); CodeOwners = new CodeOwners(codeOwnersPath); break; } } }