2023-01-30 00:08:45 +01:00
|
|
|
import fs from 'fs';
|
|
|
|
|
import os from 'os';
|
|
|
|
|
import path from 'path';
|
|
|
|
|
import * as tmp from 'tmp';
|
|
|
|
|
import * as github from '@actions/github';
|
|
|
|
|
|
2023-01-30 02:31:09 +01:00
|
|
|
import {GitHub} from './github';
|
2023-01-30 00:08:45 +01:00
|
|
|
|
|
|
|
|
export class Context {
|
|
|
|
|
public gitRef: string;
|
|
|
|
|
public buildGitContext: string;
|
|
|
|
|
public provenanceBuilderID: string;
|
|
|
|
|
|
|
|
|
|
private readonly _tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-actions-toolkit-')).split(path.sep).join(path.posix.sep);
|
|
|
|
|
|
2023-01-30 02:31:09 +01:00
|
|
|
constructor() {
|
2023-01-30 00:08:45 +01:00
|
|
|
this.gitRef = github.context.ref;
|
|
|
|
|
if (github.context.sha && this.gitRef && !this.gitRef.startsWith('refs/')) {
|
|
|
|
|
this.gitRef = `refs/heads/${github.context.ref}`;
|
|
|
|
|
}
|
|
|
|
|
if (github.context.sha && !this.gitRef.startsWith(`refs/pull/`)) {
|
|
|
|
|
this.gitRef = github.context.sha;
|
|
|
|
|
}
|
2023-01-30 02:31:09 +01:00
|
|
|
this.buildGitContext = `${GitHub.serverURL}/${github.context.repo.owner}/${github.context.repo.repo}.git#${this.gitRef}`;
|
|
|
|
|
this.provenanceBuilderID = `${GitHub.serverURL}/${github.context.repo.owner}/${github.context.repo.repo}/actions/runs/${github.context.runId}`;
|
2023-01-30 00:08:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public tmpDir(): string {
|
|
|
|
|
return this._tmpDir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public tmpName(options?: tmp.TmpNameOptions): string {
|
|
|
|
|
return tmp.tmpNameSync(options);
|
|
|
|
|
}
|
|
|
|
|
}
|