Skip to content

Commit 7fd4c44

Browse files
authored
Fix org rules file (#22)
Adding debug capabilities, and fixing some odd issues that popped up once I started using it in more places.
1 parent 875844c commit 7fd4c44

File tree

6 files changed

+133
-36
lines changed

6 files changed

+133
-36
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog],
66
and this project adheres to [Semantic Versioning].
77

8+
## [1.1.2] - 2023-03-16
9+
10+
### Fixed
11+
12+
- Org rule file was not being read correctly. This has been fixed.
13+
- The org name comparison shouldn't have been case-sensitive. This has been fixed.
14+
15+
### Changed
16+
17+
- The `currentOrg()` function is no longer `async` as it doesn't need to be.
18+
819
## [1.1.1] - 2023-03-15
920

1021
### Added

__tests__/lib/org-rules.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ describe('org-rules.js Org Rules the basics', () => {
5959
process.env.GITHUB_TOKEN = GH_TOKEN
6060
})
6161

62-
test('#currentOrg() - got value', async () => {
63-
expect(currentOrg()).resolves.toBe('arcxp')
62+
test('#currentOrg() - got value', () => {
63+
expect(currentOrg()).toBe('arcxp')
6464
})
6565

6666
// This test is only run locally, not in GH Actions CI
67-
testLocallyOnly('#currentOrg() - no value', async () => {
67+
testLocallyOnly('#currentOrg() - no value', () => {
6868
const old_gh_repo = process.env.GITHUB_REPOSITORY
6969
core.setFailed = jest.fn()
7070
delete process.env.GITHUB_REPOSITORY
71-
const result = await currentOrg()
71+
const result = currentOrg()
7272
expect(core.setFailed).toHaveBeenCalledWith(
7373
'This GitHub Actions environment does not have a valid context.',
7474
)

dist/index.js

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -263,15 +263,18 @@ const ghHandle = async (token = undefined) =>
263263
* @function
264264
* @async
265265
*/
266-
const currentOrg = async (gh = undefined) =>
267-
Promise.resolve()
268-
.then((ghub) => github.context.repo?.owner)
269-
.catch((err) => {
270-
console.warn(err)
271-
return core.setFailed(
272-
'This GitHub Actions environment does not have a valid context.',
273-
)
274-
})
266+
const currentOrg = (gh = undefined) => {
267+
try {
268+
return github.context.repo?.owner
269+
} catch (err) {
270+
core.error(`Unable to determine current organization or owner: ${err}`)
271+
core.setFailed(
272+
'This GitHub Actions environment does not have a valid context.',
273+
)
274+
}
275+
276+
return
277+
}
275278

276279
/**
277280
* Fetch the contents of the Org Rules File from the org's `.github` repository.
@@ -293,7 +296,7 @@ const fetchRemoteRules = async (
293296
return
294297
}
295298

296-
const orgName = await currentOrg(octokit)
299+
const orgName = currentOrg(octokit)
297300
const defaultPayload = { org: orgName || 'UNKNOWN', rules: [] }
298301

299302
if (!orgName) {
@@ -307,14 +310,33 @@ const fetchRemoteRules = async (
307310
repo: '.github',
308311
path: rulesFileName,
309312
})
310-
.then((res) => (res?.data ? res : { data: undefined }))
313+
.then((res) => {
314+
core.debug(`Result of fetching remote rules: ${JSON.stringify(res)}`)
315+
return res?.data ? res : { data: undefined }
316+
})
311317
.catch((err) => {
318+
core.debug(`Caught exception fetching remote rules: ${err}`)
312319
return Promise.resolve({ data: undefined })
313320
})
314321
if (!data) {
322+
core.debug(
323+
`The Org Rules File "${rulesFileName}" in the «${orgName}/.github» repository appears to contain no content.`,
324+
)
315325
return defaultPayload
316326
}
317327

328+
// Debugging
329+
if (!!process.env['RUNNER_DEBUG']) {
330+
core.debug(
331+
`Org Rules File "${rulesFileName}" contents: ${JSON.stringify(
332+
data,
333+
undefined,
334+
2,
335+
)}`,
336+
)
337+
}
338+
339+
// Start parsing the rules file.
318340
const orgRulesFileContents = YAML.parse(
319341
decodeURIComponent(
320342
Buffer.from(data.content, data.encoding ?? 'base64').toString(),
@@ -327,7 +349,10 @@ const fetchRemoteRules = async (
327349
)
328350
}
329351

330-
if (orgRulesFileContents?.org !== orgName) {
352+
if (
353+
orgRulesFileContents?.org?.toLocaleLowerCase() !==
354+
orgName?.toLocaleLowerCase()
355+
) {
331356
return core.warning(
332357
`Org ${orgName} does not match the org in the Org Rules File. This isn't fatal, but it might be an indication that you're using the wrong Org Rules File.`,
333358
)
@@ -733,11 +758,29 @@ const fetchAndApplyOrgRules = (serviceDescription) =>
733758
core.getInput('org-rules-file') || DEFAULT_RULES_NAME,
734759
),
735760
)
761+
.then((remoteOrgRules) => {
762+
if (!remoteOrgRules) {
763+
core.warning(`No rules found for the organization "${currentOrg()}".`)
764+
} else {
765+
core.debug(
766+
`Rules found for the organization "${currentOrg()}": ${JSON.stringify(
767+
remoteOrgRules,
768+
undefined,
769+
2,
770+
)}`,
771+
)
772+
}
773+
return remoteOrgRules
774+
})
736775
.then((remoteOrgRules) =>
737776
!!remoteOrgRules
738777
? applyOrgRules(serviceDescription, remoteOrgRules)
739778
: true,
740779
)
780+
.catch((err) => {
781+
core.warning('Failing with error: ' + err)
782+
return Promise.reject(err)
783+
})
741784

742785
module.exports = {
743786
fetchAndApplyOrgRules,

lib/org-rules.js

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,18 @@ const ghHandle = async (token = undefined) =>
4141
* @function
4242
* @async
4343
*/
44-
const currentOrg = async (gh = undefined) =>
45-
Promise.resolve()
46-
.then((ghub) => github.context.repo?.owner)
47-
.catch((err) => {
48-
console.warn(err)
49-
return core.setFailed(
50-
'This GitHub Actions environment does not have a valid context.',
51-
)
52-
})
44+
const currentOrg = (gh = undefined) => {
45+
try {
46+
return github.context.repo?.owner
47+
} catch (err) {
48+
core.error(`Unable to determine current organization or owner: ${err}`)
49+
core.setFailed(
50+
'This GitHub Actions environment does not have a valid context.',
51+
)
52+
}
53+
54+
return
55+
}
5356

5457
/**
5558
* Fetch the contents of the Org Rules File from the org's `.github` repository.
@@ -71,7 +74,7 @@ const fetchRemoteRules = async (
7174
return
7275
}
7376

74-
const orgName = await currentOrg(octokit)
77+
const orgName = currentOrg(octokit)
7578
const defaultPayload = { org: orgName || 'UNKNOWN', rules: [] }
7679

7780
if (!orgName) {
@@ -85,14 +88,33 @@ const fetchRemoteRules = async (
8588
repo: '.github',
8689
path: rulesFileName,
8790
})
88-
.then((res) => (res?.data ? res : { data: undefined }))
91+
.then((res) => {
92+
core.debug(`Result of fetching remote rules: ${JSON.stringify(res)}`)
93+
return res?.data ? res : { data: undefined }
94+
})
8995
.catch((err) => {
96+
core.debug(`Caught exception fetching remote rules: ${err}`)
9097
return Promise.resolve({ data: undefined })
9198
})
9299
if (!data) {
100+
core.debug(
101+
`The Org Rules File "${rulesFileName}" in the «${orgName}/.github» repository appears to contain no content.`,
102+
)
93103
return defaultPayload
94104
}
95105

106+
// Debugging
107+
if (!!process.env['RUNNER_DEBUG']) {
108+
core.debug(
109+
`Org Rules File "${rulesFileName}" contents: ${JSON.stringify(
110+
data,
111+
undefined,
112+
2,
113+
)}`,
114+
)
115+
}
116+
117+
// Start parsing the rules file.
96118
const orgRulesFileContents = YAML.parse(
97119
decodeURIComponent(
98120
Buffer.from(data.content, data.encoding ?? 'base64').toString(),
@@ -105,7 +127,10 @@ const fetchRemoteRules = async (
105127
)
106128
}
107129

108-
if (orgRulesFileContents?.org !== orgName) {
130+
if (
131+
orgRulesFileContents?.org?.toLocaleLowerCase() !==
132+
orgName?.toLocaleLowerCase()
133+
) {
109134
return core.warning(
110135
`Org ${orgName} does not match the org in the Org Rules File. This isn't fatal, but it might be an indication that you're using the wrong Org Rules File.`,
111136
)
@@ -511,11 +536,29 @@ const fetchAndApplyOrgRules = (serviceDescription) =>
511536
core.getInput('org-rules-file') || DEFAULT_RULES_NAME,
512537
),
513538
)
539+
.then((remoteOrgRules) => {
540+
if (!remoteOrgRules) {
541+
core.warning(`No rules found for the organization "${currentOrg()}".`)
542+
} else {
543+
core.debug(
544+
`Rules found for the organization "${currentOrg()}": ${JSON.stringify(
545+
remoteOrgRules,
546+
undefined,
547+
2,
548+
)}`,
549+
)
550+
}
551+
return remoteOrgRules
552+
})
514553
.then((remoteOrgRules) =>
515554
!!remoteOrgRules
516555
? applyOrgRules(serviceDescription, remoteOrgRules)
517556
: true,
518557
)
558+
.catch((err) => {
559+
core.warning('Failing with error: ' + err)
560+
return Promise.reject(err)
561+
})
519562

520563
module.exports = {
521564
fetchAndApplyOrgRules,

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@arcxp/datadog-service-catalog-metadata-provider",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "This is a package which provides GitHub Actions with a workflow for providing the DataDog Service Catalog Provider with information that will register your service in the Service Catalog.",
55
"main": "index.js",
66
"scripts": {
@@ -32,7 +32,7 @@
3232
"yaml": "^2.2.1"
3333
},
3434
"devDependencies": {
35-
"@types/jest": "^29.4.3",
35+
"@types/jest": "^29.5.0",
3636
"@vercel/ncc": "^0.36.1",
3737
"ajv": "^8.12.0",
3838
"jest": "^29.5.0",

0 commit comments

Comments
 (0)