Skip to content

Commit 15c2e20

Browse files
[ci-visibility] Attempt to use repository root to find CODEOWNERS file (DataDog#4021)
1 parent 5359bfc commit 15c2e20

File tree

4 files changed

+58
-13
lines changed

4 files changed

+58
-13
lines changed

packages/datadog-plugin-cypress/src/plugin.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ const {
4545
GIT_REPOSITORY_URL,
4646
GIT_COMMIT_SHA,
4747
GIT_BRANCH,
48-
CI_PROVIDER_NAME
48+
CI_PROVIDER_NAME,
49+
CI_WORKSPACE_PATH
4950
} = require('../../dd-trace/src/plugins/util/tags')
5051
const {
5152
OS_VERSION,
@@ -186,7 +187,8 @@ module.exports = (on, config) => {
186187
[RUNTIME_NAME]: runtimeName,
187188
[RUNTIME_VERSION]: runtimeVersion,
188189
[GIT_BRANCH]: branch,
189-
[CI_PROVIDER_NAME]: ciProviderName
190+
[CI_PROVIDER_NAME]: ciProviderName,
191+
[CI_WORKSPACE_PATH]: repositoryRoot
190192
} = testEnvironmentMetadata
191193

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

208-
const codeOwnersEntries = getCodeOwnersFileEntries()
210+
const codeOwnersEntries = getCodeOwnersFileEntries(repositoryRoot)
209211

210212
let activeSpan = null
211213
let testSessionSpan = null

packages/dd-trace/src/plugins/ci_plugin.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const {
2727
TELEMETRY_EVENT_CREATED,
2828
TELEMETRY_ITR_SKIPPED
2929
} = require('../ci-visibility/telemetry')
30-
const { CI_PROVIDER_NAME, GIT_REPOSITORY_URL, GIT_COMMIT_SHA, GIT_BRANCH } = require('./util/tags')
30+
const { CI_PROVIDER_NAME, GIT_REPOSITORY_URL, GIT_COMMIT_SHA, GIT_BRANCH, CI_WORKSPACE_PATH } = require('./util/tags')
3131
const { OS_VERSION, OS_PLATFORM, OS_ARCHITECTURE, RUNTIME_NAME, RUNTIME_VERSION } = require('./util/env')
3232

3333
module.exports = class CiPlugin extends Plugin {
@@ -140,7 +140,6 @@ module.exports = class CiPlugin extends Plugin {
140140
configure (config) {
141141
super.configure(config)
142142
this.testEnvironmentMetadata = getTestEnvironmentMetadata(this.constructor.id, this.config)
143-
this.codeOwnersEntries = getCodeOwnersFileEntries()
144143

145144
const {
146145
[GIT_REPOSITORY_URL]: repositoryUrl,
@@ -151,9 +150,12 @@ module.exports = class CiPlugin extends Plugin {
151150
[RUNTIME_NAME]: runtimeName,
152151
[RUNTIME_VERSION]: runtimeVersion,
153152
[GIT_BRANCH]: branch,
154-
[CI_PROVIDER_NAME]: ciProviderName
153+
[CI_PROVIDER_NAME]: ciProviderName,
154+
[CI_WORKSPACE_PATH]: repositoryRoot
155155
} = this.testEnvironmentMetadata
156156

157+
this.codeOwnersEntries = getCodeOwnersFileEntries(repositoryRoot)
158+
157159
this.isUnsupportedCIProvider = !ciProviderName
158160

159161
this.testConfiguration = {

packages/dd-trace/src/plugins/util/test.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,16 +281,36 @@ const POSSIBLE_CODEOWNERS_LOCATIONS = [
281281
'.gitlab/CODEOWNERS'
282282
]
283283

284-
function getCodeOwnersFileEntries (rootDir = process.cwd()) {
285-
let codeOwnersContent
286-
287-
POSSIBLE_CODEOWNERS_LOCATIONS.forEach(location => {
284+
function readCodeOwners (rootDir) {
285+
for (const location of POSSIBLE_CODEOWNERS_LOCATIONS) {
288286
try {
289-
codeOwnersContent = fs.readFileSync(`${rootDir}/${location}`).toString()
287+
return fs.readFileSync(path.join(rootDir, location)).toString()
290288
} catch (e) {
291289
// retry with next path
292290
}
293-
})
291+
}
292+
return ''
293+
}
294+
295+
function getCodeOwnersFileEntries (rootDir) {
296+
let codeOwnersContent
297+
let usedRootDir = rootDir
298+
let isTriedCwd = false
299+
300+
const processCwd = process.cwd()
301+
302+
if (!usedRootDir || usedRootDir === processCwd) {
303+
usedRootDir = processCwd
304+
isTriedCwd = true
305+
}
306+
307+
codeOwnersContent = readCodeOwners(usedRootDir)
308+
309+
// If we haven't found CODEOWNERS in the provided root dir, we try with process.cwd()
310+
if (!codeOwnersContent && !isTriedCwd) {
311+
codeOwnersContent = readCodeOwners(processCwd)
312+
}
313+
294314
if (!codeOwnersContent) {
295315
return null
296316
}

packages/dd-trace/test/plugins/util/test.spec.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,30 @@ describe('getCodeOwnersFileEntries', () => {
7979
})
8080
it('returns null if CODEOWNERS can not be found', () => {
8181
const rootDir = path.join(__dirname, '__not_found__')
82+
// We have to change the working directory,
83+
// otherwise it will find the CODEOWNERS file in the root of dd-trace-js
84+
const oldCwd = process.cwd()
85+
process.chdir(path.join(__dirname))
8286
const codeOwnersFileEntries = getCodeOwnersFileEntries(rootDir)
83-
8487
expect(codeOwnersFileEntries).to.equal(null)
88+
process.chdir(oldCwd)
89+
})
90+
it('tries both input rootDir and process.cwd()', () => {
91+
const rootDir = path.join(__dirname, '__not_found__')
92+
const oldCwd = process.cwd()
93+
94+
process.chdir(path.join(__dirname, '__test__'))
95+
const codeOwnersFileEntries = getCodeOwnersFileEntries(rootDir)
96+
97+
expect(codeOwnersFileEntries[0]).to.eql({
98+
pattern: 'packages/dd-trace/test/plugins/util/test.spec.js',
99+
owners: ['@datadog-ci-app']
100+
})
101+
expect(codeOwnersFileEntries[1]).to.eql({
102+
pattern: 'packages/dd-trace/test/plugins/util/*',
103+
owners: ['@datadog-dd-trace-js']
104+
})
105+
process.chdir(oldCwd)
85106
})
86107
})
87108

0 commit comments

Comments
 (0)