context.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const fs_1 = require("fs");
  4. const os_1 = require("os");
  5. class Context {
  6. /**
  7. * Hydrate the context from the environment
  8. */
  9. constructor() {
  10. this.payload = {};
  11. if (process.env.GITHUB_EVENT_PATH) {
  12. if (fs_1.existsSync(process.env.GITHUB_EVENT_PATH)) {
  13. this.payload = JSON.parse(fs_1.readFileSync(process.env.GITHUB_EVENT_PATH, { encoding: 'utf8' }));
  14. }
  15. else {
  16. const path = process.env.GITHUB_EVENT_PATH;
  17. process.stdout.write(`GITHUB_EVENT_PATH ${path} does not exist${os_1.EOL}`);
  18. }
  19. }
  20. this.eventName = process.env.GITHUB_EVENT_NAME;
  21. this.sha = process.env.GITHUB_SHA;
  22. this.ref = process.env.GITHUB_REF;
  23. this.workflow = process.env.GITHUB_WORKFLOW;
  24. this.action = process.env.GITHUB_ACTION;
  25. this.actor = process.env.GITHUB_ACTOR;
  26. }
  27. get issue() {
  28. const payload = this.payload;
  29. return Object.assign(Object.assign({}, this.repo), { number: (payload.issue || payload.pull_request || payload).number });
  30. }
  31. get repo() {
  32. if (process.env.GITHUB_REPOSITORY) {
  33. const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/');
  34. return { owner, repo };
  35. }
  36. if (this.payload.repository) {
  37. return {
  38. owner: this.payload.repository.owner.login,
  39. repo: this.payload.repository.name
  40. };
  41. }
  42. throw new Error("context.repo requires a GITHUB_REPOSITORY environment variable like 'owner/repo'");
  43. }
  44. }
  45. exports.Context = Context;
  46. //# sourceMappingURL=context.js.map