Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bh is pull request #117

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

FISHMANPET
Copy link
Contributor

fixes #79
This reads environment variables in the following CIs to set a IsPullRequest variable if the current build has been triggered by a PR: AppVeyor, Gitlab CI, Azure Pipelines, Bamboo, GoCD, Travis CI, and Github Actions. I couldn't figure out a way to determine it in Jenkins or Teamcity but if there is a reliable way that someone is aware of it's easy to see in the code where to set it.

I thought about figuring out way to determine this from Git directly but I'm not sure it's possible or even reasonable. GIt doesn't have a notion of a pull request directly it seems, in Github a pull request creates a branch something like refs/remotes/pull/2/merge but other systems may do something different so I'm not sure checking that the current branch matches a certain pattern is going to be reliable

If the build environment says the build was triggered by a PR, it says IsPullRequest to be True otherwise it won't create the variable, so you can then check for the existence of the environment variable (or $BHIsPullRequest in the case of Set-BuildVariable.ps1)

I also thought about setting some more details about the PR, like maybe the PR number, the source branch, the destination branch, etc, but I'd need help from people with experience in other CI systems to do that. I know where to find the values in my CI, Azure Pipelines, but less so in the others.

Finally, I'd added CommitHash a while ago but realized I'd never added it to Set-BuildVariable.ps1 so in addition to adding the logic for IsPullRequest in there I also added CommitHash to that file.

@@ -107,6 +108,11 @@ function Get-BuildEnvironment {
PSModuleManifest = ${Build.ManifestPath}
ModulePath = ${Build.ModulePath}
}
if (${Build.Vars}.IsPullRequest) {
#This will ensure IsPullRequest is inserted into the ordered table after the BuildNumber property
$index = $($BuildHelpersVariables.keys).IndexOf("BuildNumber")

Choose a reason for hiding this comment

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

It's late so may be missing it, but any reason not to add IsPullRequest to the ordered hash definition above rather than this extra logic?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My intention here (and answer to the rest of your questions sort of flow from this) is that since an Environment Variable can't be a boolean, just some text, that IsPullRequest would only exist if the run was a pull request, and so you could check just for existence of the environment variable. Here, I wanted to ensure that it would appear after BuildNumber in the ordered hash, but I couldn't just add it to the hash definition because it wouldn't always be defined. This addition here will add it into the ordered hash after BuildNumber, but only do so if there actually is a value

'GitHub Actions' {if ($ENV:GITHUB_EVENT_NAME -eq "pull_request") {"True"}; break}
#'Jenkins' {if ($Environment.CHANGE_ID) {"True"}} ???? is this correct?
#'Teamcity' {if ???????) {"True"}} who even knows
}

Choose a reason for hiding this comment

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

Two questions!

  • Can we add a default case that sets this to $null
  • Can we add else statements to indicate $false when a build system is identified, but no pull request evidence is found (i.e. not in a PR, unlike default case of not detecting being in a PR, which is $null)
  • Can we switch to boolean $true/$false rather than strings?

Sorry to be a pain, and sorry for such a long delay!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Doing anything differently with the variable here (setting it to null or false) would be a change of my intended functionality. Additionally, since these are going to be written to the environment as Environment variables, we'd lose the boolean-ness and they'd just end up as flat strings either way.

The intention was that the environment variable would only exist if it was a pull request, though I can see the benefit to knowing if you're definitively not in a PR build vs not being in a build environment where it can be detected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok looked more closely at the code. The usual entry point is going to be Set-BuildEnvironment which takes the values from Get-BuildEnvironment which is what calls this (Get-BuildVariable).

In the end it runs this line:

$Output = New-Item -Path Env:\ -Name $prefixedVar -Value $BuildHelpersVariables[$VarName] -Force:$Force

If the value is $true that will set the Environment Variable to be 'True', if it's $false it will set it as 'False', but if it's $null this will throw an error.

@RamblingCookieMonster
Copy link
Owner

Hiii! Awesome idea! Sorry for the delay, and for the quantity of feedback!

BuildNumber = ${Build.Vars}.BuildNumber
ProjectName = ${Build.ProjectName}
PSModuleManifest = ${Build.ManifestPath}
ModulePath = $(Split-Path -Path ${Build.ManifestPath} -Parent)
}
if (${Build.Vars}.IsPullRequest) {
$BuildHelpersVariables.IsPullRequest = [bool]${Build.Vars}.IsPullRequest
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here at least we're setting PowerShell variables so it can be a boolean. Here it might make sense to set $false or $null, but then it's a slightly different behavior then when environment variables are used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add BHIsPullRequest
2 participants