forked from actions-rs/toolchain
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
958 additions
and
385 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import * as github from '@actions/github'; | ||
|
||
interface Output { | ||
title: string; | ||
summary: string; | ||
text: string; | ||
} | ||
|
||
/** | ||
* Thin wrapper around the GitHub Checks API | ||
*/ | ||
export class CheckReporter { | ||
private readonly client: github.GitHub; | ||
private readonly checkName: string; | ||
private checkId: undefined | number; | ||
|
||
constructor(client: github.GitHub, checkName: string) { | ||
this.client = client; | ||
this.checkName = checkName; | ||
this.checkId = undefined; | ||
} | ||
|
||
/** | ||
* Starts a new Check and returns check ID. | ||
*/ | ||
public async startCheck( | ||
status?: 'queued' | 'in_progress' | 'completed', | ||
): Promise<number> { | ||
const { owner, repo } = github.context.repo; | ||
|
||
const response = await this.client.checks.create({ | ||
owner: owner, | ||
repo: repo, | ||
name: this.checkName, | ||
head_sha: github.context.sha, // eslint-disable-line | ||
status: status ? status : 'in_progress', | ||
}); | ||
// TODO: Check for errors | ||
|
||
this.checkId = response.data.id; | ||
return this.checkId; | ||
} | ||
|
||
// TODO: | ||
// public async sendAnnotations(annotations: Array<octokit.ChecksCreateParamsOutputAnnotations>): Promise<void> { | ||
// } | ||
|
||
/** | ||
* It is up to caller to call the `startCheck` first! | ||
*/ | ||
public async finishCheck( | ||
conclusion: | ||
| 'cancelled' | ||
| 'success' | ||
| 'failure' | ||
| 'neutral' | ||
| 'timed_out' | ||
| 'action_required', | ||
output: Output, | ||
): Promise<void> { | ||
const { owner, repo } = github.context.repo; | ||
|
||
// TODO: Check for errors | ||
await this.client.checks.update({ | ||
owner: owner, | ||
repo: repo, | ||
name: this.checkName, | ||
check_run_id: this.checkId!, // eslint-disable-line | ||
status: 'completed', | ||
conclusion: conclusion, | ||
completed_at: new Date().toISOString(), // eslint-disable-line | ||
output: output, | ||
}); | ||
|
||
return; | ||
} | ||
|
||
public async cancelCheck(): Promise<void> { | ||
const { owner, repo } = github.context.repo; | ||
|
||
// TODO: Check for errors | ||
await this.client.checks.update({ | ||
owner: owner, | ||
repo: repo, | ||
name: this.checkName, | ||
check_run_id: this.checkId!, // eslint-disable-line | ||
status: 'completed', | ||
conclusion: 'cancelled', | ||
completed_at: new Date().toISOString(), // eslint-disable-line | ||
output: { | ||
title: this.checkName, | ||
summary: 'Unhandled error', | ||
text: | ||
'Check was cancelled due to unhandled error. Check the Action logs for details.', | ||
}, | ||
}); | ||
|
||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import * as path from 'path'; | ||
import * as process from 'process'; | ||
|
||
import * as io from '@actions/io'; | ||
import * as core from '@actions/core'; | ||
import * as exec from '@actions/exec'; | ||
import * as tc from '@actions/tool-cache'; | ||
|
||
export interface ToolchainOptions { | ||
default?: boolean; | ||
override?: boolean; | ||
profile?: 'minimal' | 'default' | 'full'; | ||
} | ||
|
||
export class RustUp { | ||
private readonly path: string; | ||
|
||
private constructor(exePath: string) { | ||
this.path = exePath; | ||
} | ||
|
||
public static async getOrInstall(): Promise<RustUp> { | ||
try { | ||
return await RustUp.get(); | ||
} catch (error) { | ||
core.debug( | ||
`Unable to find "rustup" executable, installing it now. Reason: ${error}`, | ||
); | ||
return await RustUp.install(); | ||
} | ||
} | ||
|
||
// Will throw an error if `rustup` is not installed. | ||
public static async get(): Promise<RustUp> { | ||
const exePath = await io.which('rustup', true); | ||
|
||
return new RustUp(exePath); | ||
} | ||
|
||
public static async install(): Promise<RustUp> { | ||
const args = ['--default-toolchain', 'none']; | ||
|
||
switch (process.platform) { | ||
case 'darwin': | ||
case 'linux': { | ||
// eslint-disable-line prettier/prettier | ||
const rustupSh = await tc.downloadTool('https://sh.rustup.rs'); | ||
await exec.exec(rustupSh, args); | ||
break; | ||
} | ||
|
||
case 'win32': { | ||
const rustupExe = await tc.downloadTool('http://win.rustup.rs'); | ||
await exec.exec(rustupExe, args); | ||
break; | ||
} | ||
|
||
default: | ||
throw new Error( | ||
`Unknown platform ${process.platform}, can't install rustup`, | ||
); | ||
} | ||
|
||
// `$HOME` should always be declared, so it is more to get the linters happy | ||
core.addPath(path.join(process.env.HOME!, '.cargo', 'bin')); // eslint-disable-line @typescript-eslint/no-non-null-assertion | ||
|
||
// Assuming it is in the $PATH already | ||
return new RustUp('rustup'); | ||
} | ||
|
||
public async installToolchain( | ||
name: string, | ||
options?: ToolchainOptions, | ||
): Promise<number> { | ||
await this.call(['toolchain', 'install', name]); | ||
|
||
if (options && options.default) { | ||
await this.call(['default', name]); | ||
} | ||
|
||
if (options && options.override) { | ||
await this.call(['override', 'set', name]); | ||
} | ||
|
||
// TODO: Support profiles | ||
|
||
// TODO: Is there smth like Rust' `return Ok(())`? | ||
return 0; | ||
} | ||
|
||
public async addTarget( | ||
name: string, | ||
forToolchain?: string, | ||
): Promise<number> { | ||
const args = ['target', 'add']; | ||
if (forToolchain) { | ||
args.push('--toolchain'); | ||
args.push(forToolchain); | ||
} | ||
args.push(name); | ||
|
||
return await this.call(args); | ||
} | ||
|
||
public async activeToolchain(): Promise<string> { | ||
let stdout = ''; | ||
await this.call(['show', 'active-toolchain'], { | ||
listeners: { | ||
stdout: (buffer: Buffer) => { | ||
stdout = buffer.toString().trim(); | ||
}, | ||
}, | ||
}); | ||
|
||
if (stdout) { | ||
return stdout.split(' ', 2)[0]; | ||
} else { | ||
throw new Error('Unable to determine active toolchain'); | ||
} | ||
} | ||
|
||
// rustup which `program` | ||
public async which(program: string): Promise<string> { | ||
let stdout = ''; | ||
await this.call(['which', program], { | ||
listeners: { | ||
stdout: (buffer: Buffer) => { | ||
stdout = buffer.toString().trim(); | ||
}, | ||
}, | ||
}); | ||
|
||
if (stdout) { | ||
return stdout; | ||
} else { | ||
throw new Error(`Unable to find the ${program}`); | ||
} | ||
} | ||
|
||
public async call(args: string[], options?: {}): Promise<number> { | ||
return await exec.exec(this.path, args, options); | ||
} | ||
} |
Oops, something went wrong.