Skip to content

Commit

Permalink
Fix org rules file (#22)
Browse files Browse the repository at this point in the history
Adding debug capabilities, and fixing some odd issues that popped up once I started using it in more places.
  • Loading branch information
manchicken authored Mar 16, 2023
1 parent 875844c commit 7fd4c44
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 36 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog],
and this project adheres to [Semantic Versioning].

## [1.1.2] - 2023-03-16

### Fixed

- Org rule file was not being read correctly. This has been fixed.
- The org name comparison shouldn't have been case-sensitive. This has been fixed.

### Changed

- The `currentOrg()` function is no longer `async` as it doesn't need to be.

## [1.1.1] - 2023-03-15

### Added
Expand Down
8 changes: 4 additions & 4 deletions __tests__/lib/org-rules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ describe('org-rules.js Org Rules the basics', () => {
process.env.GITHUB_TOKEN = GH_TOKEN
})

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

// This test is only run locally, not in GH Actions CI
testLocallyOnly('#currentOrg() - no value', async () => {
testLocallyOnly('#currentOrg() - no value', () => {
const old_gh_repo = process.env.GITHUB_REPOSITORY
core.setFailed = jest.fn()
delete process.env.GITHUB_REPOSITORY
const result = await currentOrg()
const result = currentOrg()
expect(core.setFailed).toHaveBeenCalledWith(
'This GitHub Actions environment does not have a valid context.',
)
Expand Down
67 changes: 55 additions & 12 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,18 @@ const ghHandle = async (token = undefined) =>
* @function
* @async
*/
const currentOrg = async (gh = undefined) =>
Promise.resolve()
.then((ghub) => github.context.repo?.owner)
.catch((err) => {
console.warn(err)
return core.setFailed(
'This GitHub Actions environment does not have a valid context.',
)
})
const currentOrg = (gh = undefined) => {
try {
return github.context.repo?.owner
} catch (err) {
core.error(`Unable to determine current organization or owner: ${err}`)
core.setFailed(
'This GitHub Actions environment does not have a valid context.',
)
}

return
}

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

const orgName = await currentOrg(octokit)
const orgName = currentOrg(octokit)
const defaultPayload = { org: orgName || 'UNKNOWN', rules: [] }

if (!orgName) {
Expand All @@ -307,14 +310,33 @@ const fetchRemoteRules = async (
repo: '.github',
path: rulesFileName,
})
.then((res) => (res?.data ? res : { data: undefined }))
.then((res) => {
core.debug(`Result of fetching remote rules: ${JSON.stringify(res)}`)
return res?.data ? res : { data: undefined }
})
.catch((err) => {
core.debug(`Caught exception fetching remote rules: ${err}`)
return Promise.resolve({ data: undefined })
})
if (!data) {
core.debug(
`The Org Rules File "${rulesFileName}" in the «${orgName}/.github» repository appears to contain no content.`,
)
return defaultPayload
}

// Debugging
if (!!process.env['RUNNER_DEBUG']) {
core.debug(
`Org Rules File "${rulesFileName}" contents: ${JSON.stringify(
data,
undefined,
2,
)}`,
)
}

// Start parsing the rules file.
const orgRulesFileContents = YAML.parse(
decodeURIComponent(
Buffer.from(data.content, data.encoding ?? 'base64').toString(),
Expand All @@ -327,7 +349,10 @@ const fetchRemoteRules = async (
)
}

if (orgRulesFileContents?.org !== orgName) {
if (
orgRulesFileContents?.org?.toLocaleLowerCase() !==
orgName?.toLocaleLowerCase()
) {
return core.warning(
`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.`,
)
Expand Down Expand Up @@ -733,11 +758,29 @@ const fetchAndApplyOrgRules = (serviceDescription) =>
core.getInput('org-rules-file') || DEFAULT_RULES_NAME,
),
)
.then((remoteOrgRules) => {
if (!remoteOrgRules) {
core.warning(`No rules found for the organization "${currentOrg()}".`)
} else {
core.debug(
`Rules found for the organization "${currentOrg()}": ${JSON.stringify(
remoteOrgRules,
undefined,
2,
)}`,
)
}
return remoteOrgRules
})
.then((remoteOrgRules) =>
!!remoteOrgRules
? applyOrgRules(serviceDescription, remoteOrgRules)
: true,
)
.catch((err) => {
core.warning('Failing with error: ' + err)
return Promise.reject(err)
})

module.exports = {
fetchAndApplyOrgRules,
Expand Down
67 changes: 55 additions & 12 deletions lib/org-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,18 @@ const ghHandle = async (token = undefined) =>
* @function
* @async
*/
const currentOrg = async (gh = undefined) =>
Promise.resolve()
.then((ghub) => github.context.repo?.owner)
.catch((err) => {
console.warn(err)
return core.setFailed(
'This GitHub Actions environment does not have a valid context.',
)
})
const currentOrg = (gh = undefined) => {
try {
return github.context.repo?.owner
} catch (err) {
core.error(`Unable to determine current organization or owner: ${err}`)
core.setFailed(
'This GitHub Actions environment does not have a valid context.',
)
}

return
}

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

const orgName = await currentOrg(octokit)
const orgName = currentOrg(octokit)
const defaultPayload = { org: orgName || 'UNKNOWN', rules: [] }

if (!orgName) {
Expand All @@ -85,14 +88,33 @@ const fetchRemoteRules = async (
repo: '.github',
path: rulesFileName,
})
.then((res) => (res?.data ? res : { data: undefined }))
.then((res) => {
core.debug(`Result of fetching remote rules: ${JSON.stringify(res)}`)
return res?.data ? res : { data: undefined }
})
.catch((err) => {
core.debug(`Caught exception fetching remote rules: ${err}`)
return Promise.resolve({ data: undefined })
})
if (!data) {
core.debug(
`The Org Rules File "${rulesFileName}" in the «${orgName}/.github» repository appears to contain no content.`,
)
return defaultPayload
}

// Debugging
if (!!process.env['RUNNER_DEBUG']) {
core.debug(
`Org Rules File "${rulesFileName}" contents: ${JSON.stringify(
data,
undefined,
2,
)}`,
)
}

// Start parsing the rules file.
const orgRulesFileContents = YAML.parse(
decodeURIComponent(
Buffer.from(data.content, data.encoding ?? 'base64').toString(),
Expand All @@ -105,7 +127,10 @@ const fetchRemoteRules = async (
)
}

if (orgRulesFileContents?.org !== orgName) {
if (
orgRulesFileContents?.org?.toLocaleLowerCase() !==
orgName?.toLocaleLowerCase()
) {
return core.warning(
`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.`,
)
Expand Down Expand Up @@ -511,11 +536,29 @@ const fetchAndApplyOrgRules = (serviceDescription) =>
core.getInput('org-rules-file') || DEFAULT_RULES_NAME,
),
)
.then((remoteOrgRules) => {
if (!remoteOrgRules) {
core.warning(`No rules found for the organization "${currentOrg()}".`)
} else {
core.debug(
`Rules found for the organization "${currentOrg()}": ${JSON.stringify(
remoteOrgRules,
undefined,
2,
)}`,
)
}
return remoteOrgRules
})
.then((remoteOrgRules) =>
!!remoteOrgRules
? applyOrgRules(serviceDescription, remoteOrgRules)
: true,
)
.catch((err) => {
core.warning('Failing with error: ' + err)
return Promise.reject(err)
})

module.exports = {
fetchAndApplyOrgRules,
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@arcxp/datadog-service-catalog-metadata-provider",
"version": "1.1.1",
"version": "1.1.2",
"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.",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -32,7 +32,7 @@
"yaml": "^2.2.1"
},
"devDependencies": {
"@types/jest": "^29.4.3",
"@types/jest": "^29.5.0",
"@vercel/ncc": "^0.36.1",
"ajv": "^8.12.0",
"jest": "^29.5.0",
Expand Down

0 comments on commit 7fd4c44

Please sign in to comment.