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

open-turo / action-setup-tools / 13360358966

17 Feb 2025 12:31AM UTC coverage: 74.865%. Remained the same
13360358966

push

github

open-turo-bot
chore(deps): update pre-commit hook alessandrojcm/commitlint-pre-commit-hook to v9.21.0

199 of 285 branches covered (69.82%)

Branch coverage included in aggregate %.

495 of 642 relevant lines covered (77.1%)

173.53 hits per line

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

75.64
/java.js
1
import fs from "fs"
2
import path from "path"
3
import process from "process"
4

5
import core from "@actions/core"
6

7
import SdkmanTool from "./sdkmantool.js"
8

9
export default class Java extends SdkmanTool {
10
    static tool = "java"
3✔
11
    static toolVersion = "java -version"
3✔
12

13
    constructor() {
14
        super(Java.tool)
33✔
15
    }
16

17
    // desiredVersion : The identifier for the specific desired version of java as
18
    // known to sdkman such as "11.0.2-open" for version 11.0.2 from java.net.
19
    // assumes sdkman is already installed on the self-hosted runner, is a failure
20
    // condition otherwise.
21
    async setup(desiredVersion) {
22
        const [checkVersion, isVersionOverridden] =
23
            this.getJavaVersion(desiredVersion)
9✔
24
        if (!(await this.haveVersion(checkVersion))) {
9!
25
            return checkVersion
9✔
26
        }
27

28
        // Make sure that sdkman is installed
29
        await this.findInstaller()
×
30

31
        // This doesn't fail hard, but it probably should
32
        this.checkSdkmanSettings(
×
33
            path.join(`${await this.findRoot()}`, "etc/config"),
34
        )
35

36
        // Set downstream environment variable for future steps in this Job
37
        if (isVersionOverridden) {
×
38
            core.exportVariable("JAVA_VERSION", checkVersion)
×
39
        }
40

41
        // If sdkman is requested to install the same version of java more than once,
42
        // all subsequent attempts will be a no-op and sdkman will report a message of the
43
        // form "java 11.0.2-open is already installed". sdk install does not pick up the
44
        // environment variable JAVA_VERSION unlike tfenv, so we specify it here as an
45
        // argument explicitly, if it's set
46
        await this.subprocessShell(`sdk install java ${checkVersion}`).catch(
×
47
            this.logAndExit(`failed to install: ${checkVersion}`),
48
        )
49

50
        // Set the "current" default Java version to the desired version
51
        await this.subprocessShell(`sdk default java ${checkVersion}`).catch(
×
52
            this.logAndExit(`failed to set default: ${checkVersion}`),
53
        )
54

55
        // export JAVA_HOME to force using the correct version of java
56
        const javaHome = `${process.env[this.envVar]}/candidates/java/current`
×
57
        core.exportVariable("JAVA_HOME", javaHome)
×
58

59
        // Augment path so that the current version of java according to sdkman will be
60
        // the version found.
61
        core.addPath(`${javaHome}/bin`)
×
62

63
        // Remove the trailing -blah from the Java version string
64
        const expectedVersion = checkVersion.replace(/[-_][^-_]+$/, "")
×
65

66
        // Sanity check that the java command works and its reported version matches what we have
67
        // requested to be in place.
68
        await this.validateVersion(expectedVersion)
×
69

70
        // If we got this far, we have successfully configured java.
71
        core.setOutput(Java.tool, checkVersion)
×
72
        this.info("java success!")
×
73
        return checkVersion
×
74
    }
75

76
    // determines the desired version of java that is being requested. if the desired version
77
    // presented to the action is present, that version is honored rather than the version
78
    // presented in the .sdkmanrc file that can be optionally present in the checked out repo itself.
79
    // Second value returned indicates whether or not the version returned has overridden
80
    // the version from the .sdkmanrc file.
81
    getJavaVersion(actionDesiredVersion) {
82
        // Check if we have any version passed in to the action (can be null/empty string)
83
        if (actionDesiredVersion) return [actionDesiredVersion, true]
19✔
84

85
        const readJavaVersion = this.parseSdkmanrc()
15✔
86
        if (readJavaVersion) {
15✔
87
            this.debug(`Found java version ${readJavaVersion} in .sdkmanrc`)
2✔
88
            return [readJavaVersion, false]
2✔
89
        }
90
        // No version has been specified
91
        return [null, null]
13✔
92
    }
93

94
    parseSdkmanrc(filename) {
95
        filename = filename || ".sdkmanrc"
21✔
96
        filename = path.resolve(path.join(process.cwd(), filename))
21✔
97
        // No file? We're done
98
        if (!fs.existsSync(filename)) {
21✔
99
            this.debug(`No .sdkmanrc file found: ${filename}`)
13✔
100
            return
13✔
101
        }
102

103
        // Read our file and split it linewise
104
        let data = fs.readFileSync(filename, { encoding: "utf8", flag: "r" })
8✔
105
        if (!data) return
8✔
106
        data = data.split("\n")
6✔
107

108
        // Iterate over each line and match against the regex
109
        const find = new RegExp("^([^#=]+)=([^# ]+)$")
6✔
110
        let found = {}
6✔
111
        for (let line of data) {
6✔
112
            const match = find.exec(line)
20✔
113
            if (!match) continue
20✔
114
            found[match[1]] = match[2]
10✔
115
        }
116
        this.debug(`Found .sdkmanrc entries ${JSON.stringify(found)}`)
6✔
117
        return found["java"]
6✔
118
    }
119

120
    // versionParser specially handles version string extraction
121
    // because we have to map strings like 1.8.0_282 to 8.0.282 for the actual
122
    // SemVer comparison
123
    versionParser(text) {
124
        // Default case for 11.x or 17.x it should match and we're ok
125
        let versions = super.versionParser(text)
4✔
126
        this.debug(`versionParser: ${versions}`)
4✔
127
        if (!versions.length) return versions
4!
128

129
        // Fast check for 1.x versions that don't parse right
130
        const v = /^\d+\.\d+\.\d+/ // Check against X.Y.Z
4✔
131
        const v1x = /^1\.\d+\.\d+/ // Check against 1.Y.Z
4✔
132
        if (v.test(versions[0]) && !v1x.test(versions[0])) return versions
4✔
133

134
        // This parsing is to match the version string for 1.8.0_282 (or
135
        // similar) which is what the java binary puts out, however `sdkman`
136
        // uses the updated naming of `8.0.282` which is what we want to check
137
        // against, so we're going to hard parse against X.Y.Z_W to rewrite it
138
        // to Y.Z.W
139
        const parser = /1\.([0-9]+\.[0-9]+_[0-9]+)/
2✔
140
        const matched = parser.exec(text)
2✔
141
        this.debug(`versionParser: matched 1.x version: ${matched}`)
2✔
142
        if (!matched) return versions
2!
143
        return [matched[1].replace("_", ".")]
2✔
144
    }
145
}
146

147
// Register the subclass in our tool list
148
Java.register()
3✔
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