Skip to content

Commit

Permalink
Handle error during rehydration
Browse files Browse the repository at this point in the history
  • Loading branch information
dsame committed Jun 22, 2023
1 parent eae709c commit 6afea3f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 30 deletions.
31 changes: 19 additions & 12 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,7 @@ const crypto_1 = __importDefault(__nccwpck_require__(6113));
const fs_1 = __importDefault(__nccwpck_require__(7147));
const path_1 = __importDefault(__nccwpck_require__(1017));
const artifact = __importStar(__nccwpck_require__(2605));
const core = __importStar(__nccwpck_require__(2186));
class State {
constructor() {
this.processedIssuesIDs = new Set();
Expand Down Expand Up @@ -1589,19 +1590,25 @@ class State {
this.reset();
const tmpDir = os_1.default.tmpdir();
const artifactClient = artifact.create();
const downloadResponse = yield artifactClient.downloadArtifact(State.ARTIFACT_NAME, tmpDir);
const downloadedFiles = fs_1.default.readdirSync(downloadResponse.downloadPath);
if (downloadedFiles.length === 0) {
// TODO: handle error
try {
const downloadResponse = yield artifactClient.downloadArtifact(State.ARTIFACT_NAME, tmpDir);
const downloadedFiles = fs_1.default.readdirSync(downloadResponse.downloadPath);
if (downloadedFiles.length === 0) {
throw Error('There is no data in the state artifact, probably because of the previous run failed');
}
const serialized = fs_1.default.readFileSync(path_1.default.join(downloadResponse.downloadPath, downloadedFiles[0]), { encoding: 'utf8' });
if (serialized.length === 0)
return;
const issueIDs = serialized
.split('|')
.map(parseInt)
.filter(i => !isNaN(i));
this.processedIssuesIDs = new Set(issueIDs);
core.debug(`Rehydrated state includes info about ${issueIDs.length} issue(s)`);
}
catch (error) {
core.warning(`Rehydrating the state was not successful due to "${error.message || 'unknown reason'}"`);
}
const serialized = fs_1.default.readFileSync(path_1.default.join(downloadResponse.downloadPath, downloadedFiles[0]), { encoding: 'utf8' });
if (serialized.length === 0)
return;
const issueIDs = serialized
.split('|')
.map(parseInt)
.filter(i => !isNaN(i));
this.processedIssuesIDs = new Set(issueIDs);
});
}
}
Expand Down
50 changes: 32 additions & 18 deletions src/classes/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import crypto from 'crypto';
import fs from 'fs';
import path from 'path';
import * as artifact from '@actions/artifact';
import * as core from '@actions/core';

type IssueID = number;
export class State implements IState {
Expand Down Expand Up @@ -43,27 +44,40 @@ export class State implements IState {

const tmpDir = os.tmpdir();
const artifactClient = artifact.create();
const downloadResponse = await artifactClient.downloadArtifact(
State.ARTIFACT_NAME,
tmpDir
);
try {
const downloadResponse = await artifactClient.downloadArtifact(
State.ARTIFACT_NAME,
tmpDir
);

const downloadedFiles = fs.readdirSync(downloadResponse.downloadPath);
if (downloadedFiles.length === 0) {
// TODO: handle error
}
const serialized = fs.readFileSync(
path.join(downloadResponse.downloadPath, downloadedFiles[0]),
{encoding: 'utf8'}
);
const downloadedFiles = fs.readdirSync(downloadResponse.downloadPath);
if (downloadedFiles.length === 0) {
throw Error(
'There is no data in the state artifact, probably because of the previous run failed'
);
}
const serialized = fs.readFileSync(
path.join(downloadResponse.downloadPath, downloadedFiles[0]),
{encoding: 'utf8'}
);

if (serialized.length === 0) return;
if (serialized.length === 0) return;

const issueIDs = serialized
.split('|')
.map(parseInt)
.filter(i => !isNaN(i));
const issueIDs = serialized
.split('|')
.map(parseInt)
.filter(i => !isNaN(i));

this.processedIssuesIDs = new Set(issueIDs);
this.processedIssuesIDs = new Set(issueIDs);
core.debug(
`Rehydrated state includes info about ${issueIDs.length} issue(s)`
);
} catch (error) {
core.warning(
`Rehydrating the state was not successful due to "${
error.message || 'unknown reason'
}"`
);
}
}
}

0 comments on commit 6afea3f

Please sign in to comment.