Skip to content

Commit

Permalink
Lighthanded touches
Browse files Browse the repository at this point in the history
  • Loading branch information
lucperkins committed Mar 19, 2024
1 parent 3d5762a commit b7aa682
Showing 1 changed file with 37 additions and 35 deletions.
72 changes: 37 additions & 35 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type DiagnosticEvent = {

export class IdsToolbox {
private identity: correlation.AnonymizedCorrelationHashes;
private options: ConfidentActionOptions;
private actionOptions: ConfidentActionOptions;
private archOs: string;
private nixSystem: string;
private architectureFetchSuffix: string;
Expand All @@ -73,8 +73,8 @@ export class IdsToolbox {
private events: DiagnosticEvent[];
private client: Got;

constructor(options: ActionOptions) {
this.options = makeOptionsConfident(options);
constructor(actionOptions: ActionOptions) {
this.actionOptions = makeOptionsConfident(actionOptions);
this.facts = {};
this.events = [];
this.client = got.extend({
Expand All @@ -93,7 +93,7 @@ export class IdsToolbox {
},
});

this.identity = correlation.identify(this.options.name);
this.identity = correlation.identify(this.actionOptions.name);
this.archOs = platform.getArchOs();
this.nixSystem = platform.getNixPlatform(this.archOs);

Expand All @@ -108,26 +108,28 @@ export class IdsToolbox {
this.facts.execution_phase = this.executionPhase;
}

if (options.fetchStyle === "gh-env-style") {
if (this.actionOptions.fetchStyle === "gh-env-style") {
this.architectureFetchSuffix = this.archOs;
} else if (options.fetchStyle === "nix-style") {
} else if (this.actionOptions.fetchStyle === "nix-style") {
this.architectureFetchSuffix = this.nixSystem;
} else if (options.fetchStyle === "universal") {
} else if (this.actionOptions.fetchStyle === "universal") {
this.architectureFetchSuffix = "universal";
} else {
throw new Error(`fetchStyle ${options.fetchStyle} is not a valid style`);
throw new Error(
`fetchStyle ${this.actionOptions.fetchStyle} is not a valid style`,
);
}

this.sourceParameters = constructSourceParameters(
options.legacySourcePrefix,
this.actionOptions.legacySourcePrefix,
);

this.recordEvent(`start_${this.executionPhase}`);
}

recordEvent(event_name: string, context: Record<string, unknown> = {}): void {
recordEvent(eventName: string, context: Record<string, unknown> = {}): void {
this.events.push({
event_name: `${this.options.eventPrefix}${event_name}`,
event_name: `${this.actionOptions.eventPrefix}${eventName}`,
context,
correlation: this.identity,
facts: this.facts,
Expand Down Expand Up @@ -201,7 +203,7 @@ export class IdsToolbox {
}

const fetchUrl = new URL(IDS_HOST);
fetchUrl.pathname += this.options.idsProjectName;
fetchUrl.pathname += this.actionOptions.idsProjectName;

if (p.tag) {
fetchUrl.pathname += `/tag/${p.tag}`;
Expand All @@ -222,7 +224,7 @@ export class IdsToolbox {

private cacheKey(version: string): string {
const cleanedVersion = version.replace(/[^a-zA-Z0-9-+.]/g, "");
return `determinatesystem-${this.options.name}-${this.architectureFetchSuffix}-${cleanedVersion}`;
return `determinatesystem-${this.actionOptions.name}-${this.architectureFetchSuffix}-${cleanedVersion}`;
}

private async getCachedVersion(version: string): Promise<undefined | string> {
Expand All @@ -239,15 +241,15 @@ export class IdsToolbox {

if (
await actionsCache.restoreCache(
[this.options.name],
[this.actionOptions.name],
this.cacheKey(version),
[],
undefined,
true,
)
) {
this.recordEvent("artifact_cache_hit");
return `${tempDir}/${this.options.name}`;
return `${tempDir}/${this.actionOptions.name}`;
}

this.recordEvent("artifact_cache_miss");
Expand All @@ -269,14 +271,14 @@ export class IdsToolbox {
const tempDir = this.getTemporaryName();
await mkdir(tempDir);
process.chdir(tempDir);
await copyFile(toolPath, `${tempDir}/${this.options.name}`);
await copyFile(toolPath, `${tempDir}/${this.actionOptions.name}`);

// extremely evil shit right here:
process.env.GITHUB_WORKSPACE_BACKUP = process.env.GITHUB_WORKSPACE;
delete process.env.GITHUB_WORKSPACE;

await actionsCache.saveCache(
[this.options.name],
[this.actionOptions.name],
this.cacheKey(version),
undefined,
true,
Expand All @@ -290,7 +292,7 @@ export class IdsToolbox {
}

private async submitEvents(): Promise<void> {
if (!this.options.diagnosticsUrl) {
if (!this.actionOptions.diagnosticsUrl) {
actionsCore.debug(
"Diagnostics are disabled. Not sending the following events:",
);
Expand All @@ -299,7 +301,7 @@ export class IdsToolbox {
}

try {
await this.client.post(this.options.diagnosticsUrl, {
await this.client.post(this.actionOptions.diagnosticsUrl, {
json: {
type: "eventlog",
sent_at: new Date(),
Expand All @@ -314,26 +316,26 @@ export class IdsToolbox {

private getTemporaryName(): string {
const _tmpdir = process.env["RUNNER_TEMP"] || tmpdir();
return path.join(_tmpdir, `${this.options.name}-${uuidV4()}`);
return path.join(_tmpdir, `${this.actionOptions.name}-${uuidV4()}`);
}
}

function makeOptionsConfident(options: ActionOptions): ConfidentActionOptions {
const finalOpts: ConfidentActionOptions = {
name: options.name,
idsProjectName: options.idsProjectName || options.name,
eventPrefix: options.eventPrefix || `action:${options.name}:`,
fetchStyle: options.fetchStyle,
legacySourcePrefix: options.legacySourcePrefix,
diagnosticsUrl: undefined,
function makeOptionsConfident(
actionOptions: ActionOptions,
): ConfidentActionOptions {
const idsProjectName = actionOptions.idsProjectName ?? actionOptions.name;

return {
name: actionOptions.name,
idsProjectName,
eventPrefix: actionOptions.eventPrefix || `action:${actionOptions.name}:`,
fetchStyle: actionOptions.fetchStyle,
legacySourcePrefix: actionOptions.legacySourcePrefix,
diagnosticsUrl: determineDiagnosticsUrl(
idsProjectName,
actionOptions.diagnosticsUrl,
),
};

finalOpts.diagnosticsUrl = determineDiagnosticsUrl(
finalOpts.idsProjectName,
options.diagnosticsUrl,
);

return finalOpts;
}

function determineDiagnosticsUrl(
Expand Down

0 comments on commit b7aa682

Please sign in to comment.