• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

levibostian / new-deployment-tool / 15442294228

04 Jun 2025 12:28PM UTC coverage: 73.591% (+0.4%) from 73.169%
15442294228

Pull #55

github

levibostian
test: write unit tests for get-latest-release step that doesn't use real github api
Pull Request #55: all behavior is a script

129 of 145 branches covered (88.97%)

Branch coverage included in aggregate %.

82 of 100 new or added lines in 10 files covered. (82.0%)

1 existing line in 1 file now uncovered.

785 of 1097 relevant lines covered (71.56%)

11.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

27.59
/lib/github-actions.ts
1
import { DetermineNextReleaseStepConfig } from "./steps/determine-next-release.ts"
2
import * as log from "./log.ts"
1✔
3
import * as githubActions from "@actions/core"
1✔
4
import { AnyStepName } from "./steps/types/any-step.ts"
5

6
export interface GitHubActions {
7
  getNameOfCurrentBranch(): string
8
  getDetermineNextReleaseStepConfig(): DetermineNextReleaseStepConfig | undefined
9
  getSimulatedMergeType(): "merge" | "rebase" | "squash"
10
  getEventThatTriggeredThisRun(): "push" | "pull_request" | unknown
11
  isRunningInPullRequest(): Promise<{ baseBranch: string; targetBranch: string; prTitle: string; prDescription: string } | undefined>
12
  getCommandForStep({ stepName }: { stepName: AnyStepName }): string | undefined
13
  setOutput({ key, value }: { key: string; value: string }): void
14
}
15

16
export class GitHubActionsImpl implements GitHubActions {
1✔
17
  getNameOfCurrentBranch(): string {
×
18
    const githubRef = Deno.env.get("GITHUB_REF")!
×
19
    log.debug(`GITHUB_REF: ${githubRef}`)
×
20

21
    // if the ref starts with "refs/pull/", then it's a pull request.
22
    // the tool is only compatible with branch names that start with "refs/heads/".
23
    // We need a different way to get the branch name.
24
    if (githubRef.startsWith("refs/pull/")) {
×
25
      const githubHeadRef = Deno.env.get("GITHUB_HEAD_REF")!
×
26
      log.debug(`GITHUB_HEAD_REF: ${githubHeadRef}`)
×
27
      return githubHeadRef
×
28
    }
×
29

30
    return githubRef.replace("refs/heads/", "")
×
31
  }
×
32

33
  getDetermineNextReleaseStepConfig(): DetermineNextReleaseStepConfig | undefined {
1✔
34
    const githubActionInputKey = "analyze_commits_config"
5✔
35

36
    const determineNextReleaseStepConfig = this.getInput(githubActionInputKey)
5✔
37
    if (!determineNextReleaseStepConfig) {
5✔
38
      return undefined
6✔
39
    }
6✔
40

41
    try {
8✔
42
      // Because every property in the config is optional, if JSON.parse results in an object that is not a DetermineNextReleaseStepConfig, it's ok.
43
      return JSON.parse(determineNextReleaseStepConfig)
8✔
44
    } catch (error) {
5✔
45
      log.error(`When trying to parse the GitHub Actions input value for ${githubActionInputKey}, I encountered an error: ${error}`)
7✔
46
      log.error(`The value I tried to parse was: ${determineNextReleaseStepConfig}`)
7✔
47

48
      throw new Error()
7✔
49
    }
7✔
50
  }
5✔
51

52
  getSimulatedMergeType(): "merge" | "rebase" | "squash" {
×
53
    const githubActionInputKey = "simulated_merge_type"
×
54

55
    const simulateMergeType = this.getInput(githubActionInputKey)
×
56
    if (!simulateMergeType) {
×
57
      return "merge"
×
58
    }
×
59

60
    if (simulateMergeType !== "merge" && simulateMergeType !== "rebase" && simulateMergeType !== "squash") {
×
61
      log.error(
×
62
        `The value for the GitHub Actions input ${githubActionInputKey} is invalid. The value must be either "merge", "rebase", or "squash". The value provided was: ${simulateMergeType}`,
×
63
      )
64

65
      throw new Error()
×
66
    }
×
67

68
    return simulateMergeType
×
69
  }
×
70

71
  getEventThatTriggeredThisRun(): "push" | "pull_request" | string {
×
72
    const eventName = Deno.env.get("GITHUB_EVENT_NAME")
×
73

74
    switch (eventName) {
×
75
      case "push":
×
76
        return "push"
×
77
      case "pull_request":
×
78
        return "pull_request"
×
79
      default:
×
80
        return eventName || "unknown"
×
81
    }
×
82
  }
×
83

84
  async isRunningInPullRequest(): Promise<{ baseBranch: string; targetBranch: string; prTitle: string; prDescription: string } | undefined> {
×
85
    const githubEventName = Deno.env.get("GITHUB_EVENT_NAME")
×
86
    if (githubEventName !== "pull_request") {
×
87
      return undefined
×
88
    }
×
89

90
    // object reference: https://docs.github.com/en/webhooks/webhook-events-and-payloads?actionType=opened#pull_request
91
    const pullRequestContext = await this.getFullRunContext()! // we can force since we know we are in a pull request event
×
92

93
    const eventData = pullRequestContext.pull_request
×
94

95
    return {
×
96
      baseBranch: eventData.head.ref,
×
97
      targetBranch: eventData.base.ref,
×
98
      prTitle: eventData.title,
×
99
      prDescription: eventData.body || "", // github body can be null, we want a string.
×
100
    }
×
101
  }
×
102

103
  setOutput({ key, value }: { key: string; value: string }): void {
×
104
    githubActions.setOutput(key, value)
×
105
  }
×
106

NEW
107
  getCommandForStep({ stepName }: { stepName: string }): string | undefined {
×
NEW
108
    const command = this.getInput(stepName)
×
NEW
109
    if (!command) return undefined
×
NEW
110
    return command
×
NEW
111
  }
×
112

113
  private async getFullRunContext(): Promise<any | undefined> {
×
114
    const eventPath = Deno.env.get("GITHUB_EVENT_PATH")
×
115
    if (eventPath) {
×
116
      const fileContents = new TextDecoder("utf-8").decode(Deno.readFileSync(eventPath))
×
117
      return JSON.parse(fileContents)
×
118
    }
×
119
  }
×
120

121
  private getInput(key: string): string {
1✔
122
    return githubActions.getInput(key)
5✔
123
  }
5✔
124
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc