-
Notifications
You must be signed in to change notification settings - Fork 7
/
restoreImpl.ts
189 lines (163 loc) · 6.06 KB
/
restoreImpl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import * as core from "@actions/core";
import { Events, Inputs, Outputs, State } from "./constants";
import * as inputs from "./inputs";
import {
IStateProvider,
NullStateProvider,
StateProvider
} from "./stateProvider";
import * as utils from "./utils/action";
import * as install from "./utils/install";
import * as restore from "./utils/restore";
export async function restoreImpl(
stateProvider: IStateProvider,
earlyExit?: boolean | undefined
): Promise<string | undefined> {
try {
if (!utils.isCacheFeatureAvailable()) {
return;
}
// Validate inputs, this can cause task failure
if (!utils.isValidEvent()) {
throw new Error(
`Event Validation Error: The event type ${
process.env[Events.Key]
} is not supported because it's not tied to a branch or tag ref.`
);
}
await install.installSQLite3();
let restoredKey: string | undefined;
let lookedUpPrimaryKey: string | undefined;
const restoredKeys: string[] = [];
const errorNot = (message: string) =>
new Error(
`
No cache with the given key ${message}.
Exiting as the input "${Inputs.FailOn}" is set.
`
);
const errorNotFound = errorNot("was found");
const errorNotRestored = errorNot("could be restored");
let hitPrimaryKey = false;
{
const primaryKey = inputs.primaryKey;
stateProvider.setState(State.CachePrimaryKey, primaryKey);
utils.info(`Searching for a cache with the key "${primaryKey}".`);
lookedUpPrimaryKey = await utils.restoreCache({
primaryKey,
restoreKeys: [],
lookupOnly: true
});
if (!lookedUpPrimaryKey) {
if (
inputs.failOn?.keyType == "primary" &&
inputs.failOn?.result == "miss"
) {
throw errorNotFound;
} else {
utils.info(`Could not find a cache.`);
}
} else if (utils.isExactKeyMatch(primaryKey, lookedUpPrimaryKey)) {
utils.info(
`Found a cache with the given "${Inputs.PrimaryKey}".`
);
hitPrimaryKey = true;
if (!inputs.skipRestoreOnHitPrimaryKey) {
restoredKey = await restore.restoreCache(primaryKey);
if (restoredKey) {
restoredKeys.push(...[restoredKey]);
} else if (
inputs.failOn?.keyType == "primary" &&
inputs.failOn?.result == "not-restored"
) {
throw errorNotRestored;
}
}
}
}
let hitFirstMatch = false;
if (
inputs.restorePrefixesFirstMatch.length > 0 &&
!(lookedUpPrimaryKey || inputs.skipRestoreOnHitPrimaryKey)
) {
utils.info(
`
Searching for a cache using the "${
Inputs.RestorePrefixesFirstMatch
}":
${JSON.stringify(inputs.restorePrefixesFirstMatch)}
`
);
const lookedUpFirstMatch = await utils.restoreCache({
primaryKey: "",
restoreKeys: inputs.restorePrefixesFirstMatch,
lookupOnly: true
});
if (!lookedUpFirstMatch) {
if (
inputs.failOn?.keyType == "first-match" &&
inputs.failOn.result == "miss"
) {
throw errorNotFound;
} else {
utils.info(`Could not find a cache.`);
}
} else {
utils.info(
`Found a cache using the "${Inputs.RestorePrefixesFirstMatch}".`
);
hitFirstMatch = true;
restoredKey = await restore.restoreCache(lookedUpFirstMatch);
if (restoredKey) {
restoredKeys.push(...[restoredKey]);
} else if (
inputs.failOn?.keyType == "first-match" &&
inputs.failOn?.result == "not-restored"
) {
throw errorNotRestored;
}
}
}
if (!(lookedUpPrimaryKey || inputs.skipRestoreOnHitPrimaryKey)) {
restoredKeys.push(...(await restore.restoreAllMatches()));
}
restoredKey ??= "";
// Store the matched cache key in states
stateProvider.setState(State.CacheRestoredKey, restoredKey);
core.setOutput(Outputs.HitPrimaryKey, hitPrimaryKey);
core.setOutput(Outputs.HitFirstMatch, hitFirstMatch);
core.setOutput(Outputs.Hit, hitPrimaryKey || hitFirstMatch);
core.setOutput(Outputs.RestoredKey, restoredKey);
core.setOutput(Outputs.RestoredKeys, restoredKeys);
return restoredKey;
} catch (error: unknown) {
core.setFailed((error as Error).message);
if (earlyExit) {
process.exit(1);
}
}
}
async function run(
stateProvider: IStateProvider,
earlyExit: boolean | undefined
): Promise<void> {
await restoreImpl(stateProvider, earlyExit);
// node will stay alive if any promises are not resolved,
// which is a possibility if HTTP requests are dangling
// due to retries or timeouts. We know that if we got here
// that all promises that we care about have successfully
// resolved, so simply exit with success.
if (earlyExit) {
process.exit(0);
}
}
export async function restoreOnlyRun(
earlyExit?: boolean | undefined
): Promise<void> {
await run(new NullStateProvider(), earlyExit);
}
export async function restoreRun(
earlyExit?: boolean | undefined
): Promise<void> {
await run(new StateProvider(), earlyExit);
}