Skip to content
10 changes: 8 additions & 2 deletions __snapshots__/commit-info-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ exports['commit-info no environment variables has certain api 1'] = [
"getRemoteOrigin",
"getSubject",
"getTimestamp",
"getBody"
"getBody",
"resolvePullRequestCi",
"PROVIDER_GITHUB_ACTIONS",
"PROVIDER_AZURE_PIPELINES"
]

exports['commit-info no environment variables returns information 1'] = {
Expand Down Expand Up @@ -41,7 +44,10 @@ exports['commit-info combination with environment variables has certain api 1']
"getRemoteOrigin",
"getSubject",
"getTimestamp",
"getBody"
"getBody",
"resolvePullRequestCi",
"PROVIDER_GITHUB_ACTIONS",
"PROVIDER_AZURE_PIPELINES"
]

exports['commit-info combination with environment variables returns information 1'] = {
Expand Down
40 changes: 40 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Azure Pipelines — Azure Repos (Git in Azure DevOps)
#
# First-time setup:
# 1. Pipelines → New pipeline → Azure Repos Git → select this repo
# 2. Existing Azure Pipelines YAML file → branch → /azure-pipelines.yml
#
# Push builds: only for branches listed under trigger (main/master here).
# PR builds: create a Pull Request targeting main/master; BUILD_REASON=PullRequest.
#

trigger:
branches:
include:
- main
- master

pr:
branches:
include:
- main
- master

pool:
vmImage: ubuntu-latest

steps:
- task: NodeTool@0
displayName: Use Node.js 18
inputs:
versionSpec: 18.x

- script: npm ci
displayName: npm ci

- script: npm test
displayName: Unit tests

# Optional: SYSTEM_ACCESSTOKEN: $(System.AccessToken) enriches prTitle via REST when allowed.
- script: npm run ado-pr-verify
displayName: Verify Azure PR → commit-info adoEventData
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";",
"test": "npm run unit",
"unit": "mocha src/*-spec.js",
"ado-pr-verify": "node scripts/verify-ado-pr-info.js",
"gha-e2e": "mocha src/utils-e2e.js",
"semantic-release": "semantic-release pre && npm publish --access public && semantic-release post"
},
Expand Down
44 changes: 44 additions & 0 deletions scripts/verify-ado-pr-info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict'

/**
* Run in Azure Pipelines on PR builds to ensure commit-info reads
* SYSTEM_PULLREQUEST_* variables into adoEventData.
*
* Non-PR runs (push to main, manual) skip with a log line.
*/

const la = require('lazy-ass')
const { commitInfo } = require('../src')

const reason = process.env.BUILD_REASON || ''

if (reason !== 'PullRequest') {
console.log(
'ado-pr-verify: skip (BUILD_REASON=%s; only PullRequest runs assertions)',
reason || '(empty)'
)
process.exit(0)
}

commitInfo(process.cwd(), { pullRequestProvider: 'azure-pipelines' })
.then(info => {
const ado = info.adoEventData
console.log('ado-pr-verify: adoEventData =', JSON.stringify(ado, null, 2))
la(
ado,
'expected adoEventData on Azure PR build; is SYSTEM_PULLREQUEST_PULLREQUESTID set?'
)
la(ado.pullRequestNumber, 'expected pullRequestNumber from env')
la(
ado.pullRequestId,
'expected pullRequestId (PR URL); SYSTEM_TEAMFOUNDATIONCOLLECTIONURI / TEAMPROJECT / BUILD_REPOSITORY_NAME missing?'
)
la(
ado.pullRequestId === ado.htmlUrl,
'pullRequestId must equal htmlUrl (canonical PR URL)'
)
})
.catch(err => {
console.error(err)
process.exit(1)
})
60 changes: 46 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,27 @@ const {
getTimestamp,
getRemoteOrigin
} = require('./git-api')
const { getBranch, getCommitInfoFromEnvironment } = require('./utils')
const {
getBranch,
getCommitInfoFromEnvironment,
getGhaEventData
} = require('./utils')
resolvePullRequestCi,
enrichAzurePullRequestCi,
PROVIDER_GITHUB_ACTIONS,
PROVIDER_AZURE_PIPELINES,
withoutProvider
} = require('./pull-request-ci')
const Promise = require('bluebird')
const { mergeWith, or } = require('ramda')

function commitInfo (folder) {
function commitInfo (folder, options = {}) {
folder = folder || process.cwd()
const { pullRequestProvider = 'auto' } = options
debug('commit-info in folder', folder)

const pullRequestCi = resolvePullRequestCi({
env: process.env,
provider: pullRequestProvider
})

return Promise.props({
branch: getBranch(folder),
message: getMessage(folder),
Expand All @@ -31,15 +40,35 @@ function commitInfo (folder) {
sha: getSha(folder),
timestamp: getTimestamp(folder),
remote: getRemoteOrigin(folder),
ghaEventData: getGhaEventData(
process.env.GITHUB_EVENT_PATH,
process.env.GITHUB_ACTIONS
)
pullRequestCi
}).then(info => {
const envVariables = getCommitInfoFromEnvironment()
debug('git commit: %o', info)
debug('env commit: %o', envVariables)
return mergeWith(or, envVariables, info)
const finish = prCi => {
const next = {
...info,
pullRequestCi: prCi,
ghaEventData:
prCi && prCi.provider === PROVIDER_GITHUB_ACTIONS
? withoutProvider(prCi)
: undefined,
adoEventData:
prCi && prCi.provider === PROVIDER_AZURE_PIPELINES
? withoutProvider(prCi)
: undefined
}
const envVariables = getCommitInfoFromEnvironment()
debug('git commit: %o', next)
debug('env commit: %o', envVariables)
return mergeWith(or, envVariables, next)
}
if (
info.pullRequestCi &&
info.pullRequestCi.provider === PROVIDER_AZURE_PIPELINES
) {
return enrichAzurePullRequestCi(info.pullRequestCi, process.env).then(
finish
)
}
return finish(info.pullRequestCi)
})
}

Expand All @@ -53,5 +82,8 @@ module.exports = {
getRemoteOrigin,
getSubject,
getTimestamp,
getBody
getBody,
resolvePullRequestCi,
PROVIDER_GITHUB_ACTIONS,
PROVIDER_AZURE_PIPELINES
}
Loading
Loading