@@ -138,9 +138,20 @@ function versionToNumber(version) {
138138 * @returns {string | undefined }
139139 */
140140function getTargetBranch ( ) {
141- // https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables-devops-services
142- const adoTargetBranchName = process . env [ "SYSTEM_PULLREQUEST_TARGETBRANCH" ] ;
143- return adoTargetBranchName ?. replace ( / ^ r e f s \/ h e a d s \/ / , "" ) ;
141+ // Azure Pipelines
142+ if ( process . env [ "TF_BUILD" ] === "True" ) {
143+ // https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables-devops-services
144+ const targetBranch = process . env [ "SYSTEM_PULLREQUEST_TARGETBRANCH" ] ;
145+ return targetBranch ?. replace ( / ^ r e f s \/ h e a d s \/ / , "" ) ;
146+ }
147+
148+ // GitHub Actions
149+ if ( process . env [ "GITHUB_ACTIONS" ] === "true" ) {
150+ // https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
151+ return process . env [ "GITHUB_BASE_REF" ] ;
152+ }
153+
154+ return undefined ;
144155}
145156
146157/**
@@ -150,15 +161,32 @@ function getTargetBranch() {
150161 * @returns {string }
151162 */
152163function getCurrentBranch ( options ) {
153- const adoTargetBranchName = getTargetBranch ( ) ;
154- if ( adoTargetBranchName ) {
155- return adoTargetBranchName ;
164+ const targetBranch = getTargetBranch ( ) ;
165+ if ( targetBranch ) {
166+ return targetBranch ;
156167 }
157168
158- // https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables-devops-services
159- const adoSourceBranchName = process . env [ "BUILD_SOURCEBRANCHNAME" ] ;
160- if ( adoSourceBranchName ) {
161- return adoSourceBranchName . replace ( / ^ r e f s \/ h e a d s \/ / , "" ) ;
169+ // Azure DevOps Pipelines
170+ if ( process . env [ "TF_BUILD" ] === "True" ) {
171+ // https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables-devops-services
172+ const sourceBranch = process . env [ "BUILD_SOURCEBRANCHNAME" ] ;
173+ if ( sourceBranch ) {
174+ return sourceBranch . replace ( / ^ r e f s \/ h e a d s \/ / , "" ) ;
175+ }
176+ }
177+
178+ // GitHub Actions
179+ if ( process . env [ "GITHUB_ACTIONS" ] === "true" ) {
180+ // https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
181+ const headRef = process . env [ "GITHUB_HEAD_REF" ] ;
182+ if ( headRef ) {
183+ return headRef ; // For pull requests
184+ }
185+
186+ const ref = process . env [ "GITHUB_REF" ] ;
187+ if ( ref ) {
188+ return ref . replace ( / ^ r e f s \/ h e a d s \/ / , "" ) ; // For push events
189+ }
162190 }
163191
164192 const { "mock-branch" : mockBranch } = options ;
0 commit comments