Skip to content

Commit

Permalink
[ci-visibility] Attempt to use repository root to find CODEOWNERS file (
Browse files Browse the repository at this point in the history
  • Loading branch information
juan-fernandez authored and tlhunter committed Feb 12, 2024
1 parent 00164ca commit 7a8fd53
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 13 deletions.
8 changes: 5 additions & 3 deletions packages/datadog-plugin-cypress/src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ const {
GIT_REPOSITORY_URL,
GIT_COMMIT_SHA,
GIT_BRANCH,
CI_PROVIDER_NAME
CI_PROVIDER_NAME,
CI_WORKSPACE_PATH
} = require('../../dd-trace/src/plugins/util/tags')
const {
OS_VERSION,
Expand Down Expand Up @@ -186,7 +187,8 @@ module.exports = (on, config) => {
[RUNTIME_NAME]: runtimeName,
[RUNTIME_VERSION]: runtimeVersion,
[GIT_BRANCH]: branch,
[CI_PROVIDER_NAME]: ciProviderName
[CI_PROVIDER_NAME]: ciProviderName,
[CI_WORKSPACE_PATH]: repositoryRoot
} = testEnvironmentMetadata

const isUnsupportedCIProvider = !ciProviderName
Expand All @@ -205,7 +207,7 @@ module.exports = (on, config) => {
testLevel: 'test'
}

const codeOwnersEntries = getCodeOwnersFileEntries()
const codeOwnersEntries = getCodeOwnersFileEntries(repositoryRoot)

let activeSpan = null
let testSessionSpan = null
Expand Down
8 changes: 5 additions & 3 deletions packages/dd-trace/src/plugins/ci_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const {
TELEMETRY_EVENT_CREATED,
TELEMETRY_ITR_SKIPPED
} = require('../ci-visibility/telemetry')
const { CI_PROVIDER_NAME, GIT_REPOSITORY_URL, GIT_COMMIT_SHA, GIT_BRANCH } = require('./util/tags')
const { CI_PROVIDER_NAME, GIT_REPOSITORY_URL, GIT_COMMIT_SHA, GIT_BRANCH, CI_WORKSPACE_PATH } = require('./util/tags')
const { OS_VERSION, OS_PLATFORM, OS_ARCHITECTURE, RUNTIME_NAME, RUNTIME_VERSION } = require('./util/env')

module.exports = class CiPlugin extends Plugin {
Expand Down Expand Up @@ -140,7 +140,6 @@ module.exports = class CiPlugin extends Plugin {
configure (config) {
super.configure(config)
this.testEnvironmentMetadata = getTestEnvironmentMetadata(this.constructor.id, this.config)
this.codeOwnersEntries = getCodeOwnersFileEntries()

const {
[GIT_REPOSITORY_URL]: repositoryUrl,
Expand All @@ -151,9 +150,12 @@ module.exports = class CiPlugin extends Plugin {
[RUNTIME_NAME]: runtimeName,
[RUNTIME_VERSION]: runtimeVersion,
[GIT_BRANCH]: branch,
[CI_PROVIDER_NAME]: ciProviderName
[CI_PROVIDER_NAME]: ciProviderName,
[CI_WORKSPACE_PATH]: repositoryRoot
} = this.testEnvironmentMetadata

this.codeOwnersEntries = getCodeOwnersFileEntries(repositoryRoot)

this.isUnsupportedCIProvider = !ciProviderName

this.testConfiguration = {
Expand Down
32 changes: 26 additions & 6 deletions packages/dd-trace/src/plugins/util/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,16 +281,36 @@ const POSSIBLE_CODEOWNERS_LOCATIONS = [
'.gitlab/CODEOWNERS'
]

function getCodeOwnersFileEntries (rootDir = process.cwd()) {
let codeOwnersContent

POSSIBLE_CODEOWNERS_LOCATIONS.forEach(location => {
function readCodeOwners (rootDir) {
for (const location of POSSIBLE_CODEOWNERS_LOCATIONS) {
try {
codeOwnersContent = fs.readFileSync(`${rootDir}/${location}`).toString()
return fs.readFileSync(path.join(rootDir, location)).toString()
} catch (e) {
// retry with next path
}
})
}
return ''
}

function getCodeOwnersFileEntries (rootDir) {
let codeOwnersContent
let usedRootDir = rootDir
let isTriedCwd = false

const processCwd = process.cwd()

if (!usedRootDir || usedRootDir === processCwd) {
usedRootDir = processCwd
isTriedCwd = true
}

codeOwnersContent = readCodeOwners(usedRootDir)

// If we haven't found CODEOWNERS in the provided root dir, we try with process.cwd()
if (!codeOwnersContent && !isTriedCwd) {
codeOwnersContent = readCodeOwners(processCwd)
}

if (!codeOwnersContent) {
return null
}
Expand Down
23 changes: 22 additions & 1 deletion packages/dd-trace/test/plugins/util/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,30 @@ describe('getCodeOwnersFileEntries', () => {
})
it('returns null if CODEOWNERS can not be found', () => {
const rootDir = path.join(__dirname, '__not_found__')
// We have to change the working directory,
// otherwise it will find the CODEOWNERS file in the root of dd-trace-js
const oldCwd = process.cwd()
process.chdir(path.join(__dirname))
const codeOwnersFileEntries = getCodeOwnersFileEntries(rootDir)

expect(codeOwnersFileEntries).to.equal(null)
process.chdir(oldCwd)
})
it('tries both input rootDir and process.cwd()', () => {
const rootDir = path.join(__dirname, '__not_found__')
const oldCwd = process.cwd()

process.chdir(path.join(__dirname, '__test__'))
const codeOwnersFileEntries = getCodeOwnersFileEntries(rootDir)

expect(codeOwnersFileEntries[0]).to.eql({
pattern: 'packages/dd-trace/test/plugins/util/test.spec.js',
owners: ['@datadog-ci-app']
})
expect(codeOwnersFileEntries[1]).to.eql({
pattern: 'packages/dd-trace/test/plugins/util/*',
owners: ['@datadog-dd-trace-js']
})
process.chdir(oldCwd)
})
})

Expand Down

0 comments on commit 7a8fd53

Please sign in to comment.