Skip to content

Add best-effort strategy to find the valid Git commit in Jenkins Pipelines traces.#214

Merged
hithwen merged 3 commits into
masterfrom
drodriguezhdez/fix_short_sha
Jun 14, 2021
Merged

Add best-effort strategy to find the valid Git commit in Jenkins Pipelines traces.#214
hithwen merged 3 commits into
masterfrom
drodriguezhdez/fix_short_sha

Conversation

@drodriguezhdez

Copy link
Copy Markdown
Collaborator

Requirements for Contributing to this repository

  • Fill out the template below. Any pull request that does not include enough information to be reviewed in a timely manner may be closed at the maintainers' discretion.
  • The pull request must only fix one issue at the time.
  • The pull request must update the test suite to demonstrate the changed functionality.
  • After you create the pull request, all status checks must be pass before a maintainer reviews your contribution. For more details, please see CONTRIBUTING.

What does this PR do?

This PR adds some mechanisms to find the valid Git commit to use in Jenkins Pipelines traces.

During the process of building the pipeline traces, the Git information is only available when an actual step is executed (via environment variables), so we're updating the global information for Git during this process.

There could be scenarios where the step modifies the GIT_COMMIT environment variable, (e.g. to set a short version of it). In this case, if during other steps we found a valid SHA1 commit (40 char length), we want to use that commit in all spans of the traces.

This approach is "best effort" because if the user overrides the GIT_COMMIT environment variable at top level of the pipeline, we cannot find a valid commit. In this case, we set the value that we obtained, showing a warning in the Jenkins log file.

Description of the Change

Alternate Designs

Possible Drawbacks

Verification Process

Additional Notes

Release Notes

Review checklist (to be filled by reviewers)

  • Feature or bug fix MUST have appropriate tests (unit, integration, etc...)
  • PR title must be written as a CHANGELOG entry (see why)
  • Files changes must correspond to the primary purpose of the PR as described in the title (small unrelated changes should have their own PR)
  • PR must have one changelog/ label attached. If applicable it should have the backward-incompatible label attached.
  • PR should not have do-not-merge/ label attached.
  • If Applicable, issue must have kind/ and severity/ labels attached at least.

@drodriguezhdez drodriguezhdez added the changelog/Added Added features results into a minor version bump label Jun 11, 2021
AdrianLC
AdrianLC previously approved these changes Jun 11, 2021
return false;
}

return SHA1_PATTERN.matcher(gitCommit).matches();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit but maybe this could be faster if the regex is any non-alphanumeric because you're already checking length before and it could stop at the first non allowed character

// If we could not detect a valid commit, that means that the GIT_COMMIT environment variable
// was overridden by the user at top level, so we set the content what we have (despite it's not valid).
// We will show a logger.warning at the end of the pipeline.
final String gitCommit = (isValidCommit(envVars.get("GIT_COMMIT"))) ? envVars.get("GIT_COMMIT") : buildData.getGitCommit("");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit I would copy envVars.get("GIT_COMMIT") to a temporary var to make this simpler

AdrianLC
AdrianLC previously approved these changes Jun 14, 2021
Comment on lines 149 to 150
buildSpan.setTag(CITags.GIT_COMMIT__SHA, gitCommit); //Maintain retrocompatibility
buildSpan.setTag(CITags.GIT_COMMIT_SHA, gitCommit);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this still happen if gitCommit is not valid?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say yes because we need git information to correlate traces for pipeline/stages/jobs spans (atm). We're showing a warning in the logs, though.

If the commit is not "valid", that means the user modified it explicitly... so, we may want to show what the user set (most probably a short version of the commit).

@hithwen
hithwen merged commit d91aeb2 into master Jun 14, 2021
@hithwen
hithwen deleted the drodriguezhdez/fix_short_sha branch June 14, 2021 11:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog/Added Added features results into a minor version bump

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants