workflow-context-helper.ts 891 B

12345678910111213141516171819202122232425262728293031
  1. import * as core from '@actions/core'
  2. import * as fs from 'fs'
  3. /**
  4. * Gets the organization ID of the running workflow or undefined if the value cannot be loaded from the GITHUB_EVENT_PATH
  5. */
  6. export async function getOrganizationId(): Promise<number | undefined> {
  7. try {
  8. const eventPath = process.env.GITHUB_EVENT_PATH
  9. if (!eventPath) {
  10. core.debug(`GITHUB_EVENT_PATH is not defined`)
  11. return
  12. }
  13. const content = await fs.promises.readFile(eventPath, {encoding: 'utf8'})
  14. const event = JSON.parse(content)
  15. const id = event?.repository?.owner?.id
  16. if (typeof id !== 'number') {
  17. core.debug('Repository owner ID not found within GITHUB event info')
  18. return
  19. }
  20. return id as number
  21. } catch (err) {
  22. core.debug(
  23. `Unable to load organization ID from GITHUB_EVENT_PATH: ${
  24. (err as any).message || err
  25. }`
  26. )
  27. }
  28. }