Merge pull request #263 from actions/users/aiyan/allow-all-events

Allow all events to access cache
This commit is contained in:
Aiqiao Yan 2020-05-15 14:13:16 -04:00 committed by GitHub
commit 16a133d9a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 32 additions and 63 deletions

View file

@ -4,7 +4,7 @@ import { promises as fs } from "fs";
import * as os from "os"; import * as os from "os";
import * as path from "path"; import * as path from "path";
import { Events, Outputs, State } from "../src/constants"; import { Events, Outputs, RefKey, State } from "../src/constants";
import { ArtifactCacheEntry } from "../src/contracts"; import { ArtifactCacheEntry } from "../src/contracts";
import * as actionUtils from "../src/utils/actionUtils"; import * as actionUtils from "../src/utils/actionUtils";
@ -19,6 +19,7 @@ function getTempDir(): string {
afterEach(() => { afterEach(() => {
delete process.env[Events.Key]; delete process.env[Events.Key];
delete process.env[RefKey];
}); });
afterAll(async () => { afterAll(async () => {
@ -185,7 +186,7 @@ test("logWarning logs a message with a warning prefix", () => {
expect(infoMock).toHaveBeenCalledWith(`[warning]${message}`); expect(infoMock).toHaveBeenCalledWith(`[warning]${message}`);
}); });
test("isValidEvent returns false for unknown event", () => { test("isValidEvent returns false for event that does not have a branch or tag", () => {
const event = "foo"; const event = "foo";
process.env[Events.Key] = event; process.env[Events.Key] = event;
@ -325,18 +326,10 @@ test("resolvePaths exclusion pattern returns not found", async () => {
} }
}); });
test("isValidEvent returns true for push event", () => { test("isValidEvent returns true for event that has a ref", () => {
const event = Events.Push; const event = Events.Push;
process.env[Events.Key] = event; process.env[Events.Key] = event;
process.env[RefKey] = "ref/heads/feature";
const isValidEvent = actionUtils.isValidEvent();
expect(isValidEvent).toBe(true);
});
test("isValidEvent returns true for pull request event", () => {
const event = Events.PullRequest;
process.env[Events.Key] = event;
const isValidEvent = actionUtils.isValidEvent(); const isValidEvent = actionUtils.isValidEvent();

View file

@ -6,7 +6,8 @@ import {
CacheFilename, CacheFilename,
CompressionMethod, CompressionMethod,
Events, Events,
Inputs Inputs,
RefKey
} from "../src/constants"; } from "../src/constants";
import { ArtifactCacheEntry } from "../src/contracts"; import { ArtifactCacheEntry } from "../src/contracts";
import run from "../src/restore"; import run from "../src/restore";
@ -31,11 +32,6 @@ beforeAll(() => {
return actualUtils.isValidEvent(); return actualUtils.isValidEvent();
}); });
jest.spyOn(actionUtils, "getSupportedEvents").mockImplementation(() => {
const actualUtils = jest.requireActual("../src/utils/actionUtils");
return actualUtils.getSupportedEvents();
});
jest.spyOn(actionUtils, "getCacheFileName").mockImplementation(cm => { jest.spyOn(actionUtils, "getCacheFileName").mockImplementation(cm => {
const actualUtils = jest.requireActual("../src/utils/actionUtils"); const actualUtils = jest.requireActual("../src/utils/actionUtils");
return actualUtils.getCacheFileName(cm); return actualUtils.getCacheFileName(cm);
@ -44,11 +40,13 @@ beforeAll(() => {
beforeEach(() => { beforeEach(() => {
process.env[Events.Key] = Events.Push; process.env[Events.Key] = Events.Push;
process.env[RefKey] = "refs/heads/feature-branch";
}); });
afterEach(() => { afterEach(() => {
testUtils.clearInputs(); testUtils.clearInputs();
delete process.env[Events.Key]; delete process.env[Events.Key];
delete process.env[RefKey];
}); });
test("restore with invalid event outputs warning", async () => { test("restore with invalid event outputs warning", async () => {
@ -56,9 +54,10 @@ test("restore with invalid event outputs warning", async () => {
const failedMock = jest.spyOn(core, "setFailed"); const failedMock = jest.spyOn(core, "setFailed");
const invalidEvent = "commit_comment"; const invalidEvent = "commit_comment";
process.env[Events.Key] = invalidEvent; process.env[Events.Key] = invalidEvent;
delete process.env[RefKey];
await run(); await run();
expect(logWarningMock).toHaveBeenCalledWith( expect(logWarningMock).toHaveBeenCalledWith(
`Event Validation Error: The event type ${invalidEvent} is not supported. Only push, pull_request events are supported at this time.` `Event Validation Error: The event type ${invalidEvent} is not supported because it's not tied to a branch or tag ref.`
); );
expect(failedMock).toHaveBeenCalledTimes(0); expect(failedMock).toHaveBeenCalledTimes(0);
}); });

View file

@ -6,7 +6,8 @@ import {
CacheFilename, CacheFilename,
CompressionMethod, CompressionMethod,
Events, Events,
Inputs Inputs,
RefKey
} from "../src/constants"; } from "../src/constants";
import { ArtifactCacheEntry } from "../src/contracts"; import { ArtifactCacheEntry } from "../src/contracts";
import run from "../src/save"; import run from "../src/save";
@ -41,11 +42,6 @@ beforeAll(() => {
return actualUtils.isValidEvent(); return actualUtils.isValidEvent();
}); });
jest.spyOn(actionUtils, "getSupportedEvents").mockImplementation(() => {
const actualUtils = jest.requireActual("../src/utils/actionUtils");
return actualUtils.getSupportedEvents();
});
jest.spyOn(actionUtils, "resolvePaths").mockImplementation( jest.spyOn(actionUtils, "resolvePaths").mockImplementation(
async filePaths => { async filePaths => {
return filePaths.map(x => path.resolve(x)); return filePaths.map(x => path.resolve(x));
@ -64,11 +60,13 @@ beforeAll(() => {
beforeEach(() => { beforeEach(() => {
process.env[Events.Key] = Events.Push; process.env[Events.Key] = Events.Push;
process.env[RefKey] = "refs/heads/feature-branch";
}); });
afterEach(() => { afterEach(() => {
testUtils.clearInputs(); testUtils.clearInputs();
delete process.env[Events.Key]; delete process.env[Events.Key];
delete process.env[RefKey];
}); });
test("save with invalid event outputs warning", async () => { test("save with invalid event outputs warning", async () => {
@ -76,9 +74,10 @@ test("save with invalid event outputs warning", async () => {
const failedMock = jest.spyOn(core, "setFailed"); const failedMock = jest.spyOn(core, "setFailed");
const invalidEvent = "commit_comment"; const invalidEvent = "commit_comment";
process.env[Events.Key] = invalidEvent; process.env[Events.Key] = invalidEvent;
delete process.env[RefKey];
await run(); await run();
expect(logWarningMock).toHaveBeenCalledWith( expect(logWarningMock).toHaveBeenCalledWith(
`Event Validation Error: The event type ${invalidEvent} is not supported. Only push, pull_request events are supported at this time.` `Event Validation Error: The event type ${invalidEvent} is not supported because it's not tied to a branch or tag ref.`
); );
expect(failedMock).toHaveBeenCalledTimes(0); expect(failedMock).toHaveBeenCalledTimes(0);
}); });

15
dist/restore/index.js vendored
View file

@ -3345,16 +3345,10 @@ function resolvePaths(patterns) {
}); });
} }
exports.resolvePaths = resolvePaths; exports.resolvePaths = resolvePaths;
function getSupportedEvents() { // Cache token authorized for all events that are tied to a ref
return [constants_1.Events.Push, constants_1.Events.PullRequest];
}
exports.getSupportedEvents = getSupportedEvents;
// Currently the cache token is only authorized for push and pull_request events
// All other events will fail when reading and saving the cache
// See GitHub Context https://help.github.com/actions/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions#github-context // See GitHub Context https://help.github.com/actions/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions#github-context
function isValidEvent() { function isValidEvent() {
const githubEvent = process.env[constants_1.Events.Key] || ""; return constants_1.RefKey in process.env && Boolean(process.env[constants_1.RefKey]);
return getSupportedEvents().includes(githubEvent);
} }
exports.isValidEvent = isValidEvent; exports.isValidEvent = isValidEvent;
function unlinkFile(path) { function unlinkFile(path) {
@ -4613,6 +4607,7 @@ var CompressionMethod;
// over the socket during this period, the socket is destroyed and the download // over the socket during this period, the socket is destroyed and the download
// is aborted. // is aborted.
exports.SocketTimeout = 5000; exports.SocketTimeout = 5000;
exports.RefKey = "GITHUB_REF";
/***/ }), /***/ }),
@ -4706,9 +4701,7 @@ function run() {
try { try {
// Validate inputs, this can cause task failure // Validate inputs, this can cause task failure
if (!utils.isValidEvent()) { if (!utils.isValidEvent()) {
utils.logWarning(`Event Validation Error: The event type ${process.env[constants_1.Events.Key]} is not supported. Only ${utils utils.logWarning(`Event Validation Error: The event type ${process.env[constants_1.Events.Key]} is not supported because it's not tied to a branch or tag ref.`);
.getSupportedEvents()
.join(", ")} events are supported at this time.`);
return; return;
} }
const primaryKey = core.getInput(constants_1.Inputs.Key, { required: true }); const primaryKey = core.getInput(constants_1.Inputs.Key, { required: true });

15
dist/save/index.js vendored
View file

@ -3345,16 +3345,10 @@ function resolvePaths(patterns) {
}); });
} }
exports.resolvePaths = resolvePaths; exports.resolvePaths = resolvePaths;
function getSupportedEvents() { // Cache token authorized for all events that are tied to a ref
return [constants_1.Events.Push, constants_1.Events.PullRequest];
}
exports.getSupportedEvents = getSupportedEvents;
// Currently the cache token is only authorized for push and pull_request events
// All other events will fail when reading and saving the cache
// See GitHub Context https://help.github.com/actions/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions#github-context // See GitHub Context https://help.github.com/actions/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions#github-context
function isValidEvent() { function isValidEvent() {
const githubEvent = process.env[constants_1.Events.Key] || ""; return constants_1.RefKey in process.env && Boolean(process.env[constants_1.RefKey]);
return getSupportedEvents().includes(githubEvent);
} }
exports.isValidEvent = isValidEvent; exports.isValidEvent = isValidEvent;
function unlinkFile(path) { function unlinkFile(path) {
@ -4604,9 +4598,7 @@ function run() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
try { try {
if (!utils.isValidEvent()) { if (!utils.isValidEvent()) {
utils.logWarning(`Event Validation Error: The event type ${process.env[constants_1.Events.Key]} is not supported. Only ${utils utils.logWarning(`Event Validation Error: The event type ${process.env[constants_1.Events.Key]} is not supported because it's not tied to a branch or tag ref.`);
.getSupportedEvents()
.join(", ")} events are supported at this time.`);
return; return;
} }
const state = utils.getCacheState(); const state = utils.getCacheState();
@ -4702,6 +4694,7 @@ var CompressionMethod;
// over the socket during this period, the socket is destroyed and the download // over the socket during this period, the socket is destroyed and the download
// is aborted. // is aborted.
exports.SocketTimeout = 5000; exports.SocketTimeout = 5000;
exports.RefKey = "GITHUB_REF";
/***/ }), /***/ }),

View file

@ -33,3 +33,5 @@ export enum CompressionMethod {
// over the socket during this period, the socket is destroyed and the download // over the socket during this period, the socket is destroyed and the download
// is aborted. // is aborted.
export const SocketTimeout = 5000; export const SocketTimeout = 5000;
export const RefKey = "GITHUB_REF";

View file

@ -13,9 +13,7 @@ async function run(): Promise<void> {
utils.logWarning( utils.logWarning(
`Event Validation Error: The event type ${ `Event Validation Error: The event type ${
process.env[Events.Key] process.env[Events.Key]
} is not supported. Only ${utils } is not supported because it's not tied to a branch or tag ref.`
.getSupportedEvents()
.join(", ")} events are supported at this time.`
); );
return; return;
} }

View file

@ -12,9 +12,7 @@ async function run(): Promise<void> {
utils.logWarning( utils.logWarning(
`Event Validation Error: The event type ${ `Event Validation Error: The event type ${
process.env[Events.Key] process.env[Events.Key]
} is not supported. Only ${utils } is not supported because it's not tied to a branch or tag ref.`
.getSupportedEvents()
.join(", ")} events are supported at this time.`
); );
return; return;
} }

View file

@ -10,8 +10,8 @@ import * as uuidV4 from "uuid/v4";
import { import {
CacheFilename, CacheFilename,
CompressionMethod, CompressionMethod,
Events,
Outputs, Outputs,
RefKey,
State State
} from "../constants"; } from "../constants";
import { ArtifactCacheEntry } from "../contracts"; import { ArtifactCacheEntry } from "../contracts";
@ -108,16 +108,10 @@ export async function resolvePaths(patterns: string[]): Promise<string[]> {
return paths; return paths;
} }
export function getSupportedEvents(): string[] { // Cache token authorized for all events that are tied to a ref
return [Events.Push, Events.PullRequest];
}
// Currently the cache token is only authorized for push and pull_request events
// All other events will fail when reading and saving the cache
// See GitHub Context https://help.github.com/actions/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions#github-context // See GitHub Context https://help.github.com/actions/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions#github-context
export function isValidEvent(): boolean { export function isValidEvent(): boolean {
const githubEvent = process.env[Events.Key] || ""; return RefKey in process.env && Boolean(process.env[RefKey]);
return getSupportedEvents().includes(githubEvent);
} }
export function unlinkFile(path: fs.PathLike): Promise<void> { export function unlinkFile(path: fs.PathLike): Promise<void> {