Skip to content

Commit

Permalink
Merge branch 'tdd-upsert-doi' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
jspaaks committed Jun 30, 2021
2 parents db50194 + cd37b7b commit 9cbadf8
Show file tree
Hide file tree
Showing 29 changed files with 1,408 additions and 107 deletions.
11 changes: 9 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ exports.main = void 0;
const core_1 = require("@actions/core");
const exec_1 = require("@actions/exec");
const zenodraft_1 = __importDefault(require("zenodraft"));
const upserting_1 = require("./upserting");
const main = () => __awaiter(void 0, void 0, void 0, function* () {
try {
const collection_id = core_1.getInput('collection');
const compression = core_1.getInput('compression');
const filenames = core_1.getInput('filenames');
const metadata = core_1.getInput('metadata');
const publish = core_1.getInput('publish');
const publish = core_1.getInput('publish') === 'true' ? true : false;
const sandbox = core_1.getInput('sandbox') === 'false' ? false : true;
const upsert_doi = core_1.getInput('upsert-doi') === 'true' ? true : false;
const upsert_location = core_1.getInput('upsert-location');
const verbose = false;
// create the deposition as a new version in a new collection or
// as a new version in an existing collection:
Expand All @@ -34,6 +37,10 @@ const main = () => __awaiter(void 0, void 0, void 0, function* () {
else {
latest_id = yield zenodraft_1.default.deposition_create_in_existing_collection(sandbox, collection_id, verbose);
}
if (upsert_doi === true) {
const prereserved_doi = yield zenodraft_1.default.deposition_show_prereserved(sandbox, latest_id, verbose);
upserting_1.upsert_prereserved_doi(upsert_location, prereserved_doi);
}
// upload only the files specified in the filenames argument, or
// upload a snapshot of the complete repository
if (filenames === '') {
Expand All @@ -59,7 +66,7 @@ const main = () => __awaiter(void 0, void 0, void 0, function* () {
if (metadata !== '') {
yield zenodraft_1.default.metadata_update(sandbox, latest_id, metadata, verbose);
}
if (publish === 'true') {
if (publish === true) {
yield zenodraft_1.default.deposition_publish(sandbox, latest_id, verbose);
}
}
Expand Down
83 changes: 47 additions & 36 deletions lib/upserting.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,35 @@ exports.upsert_prereserved_doi = void 0;
const yaml = __importStar(require("js-yaml"));
const fs = __importStar(require("fs"));
const assert_1 = __importDefault(require("assert"));
const assert_2 = require("assert");
const has_cff_version_key = (cff) => {
return Object.keys(cff).includes('cff-version');
};
const load_cff_file = () => {
const cffstr = fs.readFileSync('CITATION.cff', 'utf8');
const doc = yaml.load(cffstr);
let cffstr;
try {
cffstr = fs.readFileSync('CITATION.cff', 'utf8');
}
catch (err) {
if (err.code === 'ENOENT') {
// tell user file doesnt exist
throw new assert_2.AssertionError({ message: 'File CITATION.cff doesn\'t exist.' });
}
throw err;
}
let doc;
try {
doc = yaml.load(cffstr);
}
catch (err) {
if (err instanceof yaml.YAMLException) {
// tell user problem was yaml parsing
throw new assert_2.AssertionError({ message: 'Could not parse the contents of CITATION.cff as YAML. Try https://yamllint.com to fix the problem.' });
}
throw err;
}
assert_1.default(typeof (doc) === 'object', 'Could not parse the contents of CITATION.cff into an object.');
assert_1.default(has_cff_version_key(doc), 'CITATION.cff is missing required key \'cff-version\'.');
return doc;
};
const supports_identifiers_description_key = (cff) => {
Expand All @@ -45,47 +67,35 @@ const supports_identifiers_key = (cff) => {
const versions = ['1.1.0', '1.2.0'];
return versions.includes(cff_version);
};
const upsert_prereserved_doi = (upsert_doi, upsert_location, prereserved_doi) => {
if (upsert_doi === false) {
return;
}
let cff;
try {
cff = load_cff_file();
}
catch (err) {
if (err.code === 'ENOENT') {
// tell user file doesnt exist
throw new Error('File CITATION.cff doesn\'t exist.');
}
if (err instanceof yaml.YAMLException) {
// tell user problem was yaml parsing
throw new Error('Could not parse the contents of CITATION.cff as YAML. Try https://yamllint.com to fix the problem.');
}
// other error
throw err;
}
assert_1.default(has_cff_version_key(cff), 'CITATION.cff is missing required key \'cff-version\'.');
const upsert_prereserved_doi = (upsert_location, prereserved_doi) => {
const cff = load_cff_file();
if (upsert_location === 'identifiers') {
upsert_location = 'identifiers[0]';
}
const identifiers_regex = new RegExp('^identifiers\\[\\d\\]$');
const identifiers_regex = new RegExp('^identifiers\\[\\d+\\]$');
if (upsert_location === 'doi') {
cff.doi = prereserved_doi;
}
else if (identifiers_regex.test(upsert_location)) {
assert_1.default(supports_identifiers_key(cff), `Your CITATION.cff file does not support key \'identifiers\'. Consider updating its \'cff-version\' value.`);
const index = parseInt(upsert_location.split(new RegExp('[\\[\\]]'))[1]);
let obj = {
type: 'doi',
value: prereserved_doi,
};
let obj;
if (supports_identifiers_description_key(cff)) {
// include 'description' in the object if the cff version supports its use
obj = Object.assign(Object.assign({}, obj), { description: 'Version doi for this work.' });
obj = {
description: 'Version doi for this work.',
value: prereserved_doi,
type: 'doi'
};
}
else {
obj = {
value: prereserved_doi,
type: 'doi'
};
}
if (Object.keys(cff).includes('identifiers') && cff.identifiers instanceof Array) {
assert_1.default(0 <= index && index <= cff.identifiers.length);
const index = parseInt(upsert_location.split(new RegExp('[\\[\\]]'))[1]);
if (Object.keys(cff).includes('identifiers')) {
assert_1.default(cff.identifiers instanceof Array, 'Expected \'identifiers\' to be of type Array.');
assert_1.default(0 <= index && index <= cff.identifiers.length, 'Invalid upsert location index.');
if (index < cff.identifiers.length) {
// partially overwrite existing object
cff.identifiers[index].value = prereserved_doi;
Expand All @@ -96,12 +106,12 @@ const upsert_prereserved_doi = (upsert_doi, upsert_location, prereserved_doi) =>
}
}
else {
assert_1.default(index === 0);
assert_1.default(index === 0, 'Invalid upsert location index.');
cff.identifiers = [obj];
}
}
else {
throw new Error('Invalid value for variable \'upsert-location\'.');
throw new assert_2.AssertionError({ message: 'Invalid value for variable \'upsert-location\'.' });
}
write_cff_file(cff);
// use octokit to do the equivalent of
Expand All @@ -115,6 +125,7 @@ const upsert_prereserved_doi = (upsert_doi, upsert_location, prereserved_doi) =>
};
exports.upsert_prereserved_doi = upsert_prereserved_doi;
const write_cff_file = (cff) => {
yaml.dump(cff, { sortKeys: false });
const cffstr = yaml.dump(cff, { sortKeys: false });
fs.writeFileSync('CITATION.cff', cffstr, 'utf8');
return;
};
49 changes: 49 additions & 0 deletions tests/upserting/1.0.3/doi/append/upserting.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const upserting_1 = require("../../../../../src/upserting");
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
let temporary_directory;
beforeEach(() => {
const src = (f) => {
return path.join(__dirname, f);
};
const dest = (f) => {
return path.join(temporary_directory, f);
};
temporary_directory = fs.mkdtempSync(`${os.tmpdir()}${path.sep}zenodraft-action-testing.`);
fs.copyFileSync(src('CITATION.cff'), dest('CITATION.cff'));
fs.copyFileSync(src('expected.yml'), dest('expected.yml'));
process.chdir(temporary_directory);
});
test('upserting a doi', () => {
const upsert_location = 'doi';
const prereserved_doi = '10.5281/upserted.1234567';
upserting_1.upsert_prereserved_doi(upsert_location, prereserved_doi);
const actual = fs.readFileSync('CITATION.cff', 'utf8');
const expected = fs.readFileSync('expected.yml', 'utf8');
expect(actual).toEqual(expected);
});
afterEach(() => {
fs.rmdirSync(temporary_directory, { recursive: true });
});
49 changes: 49 additions & 0 deletions tests/upserting/1.0.3/doi/overwrite/upserting.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const upserting_1 = require("../../../../../src/upserting");
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
let temporary_directory;
beforeEach(() => {
const src = (f) => {
return path.join(__dirname, f);
};
const dest = (f) => {
return path.join(temporary_directory, f);
};
temporary_directory = fs.mkdtempSync(`${os.tmpdir()}${path.sep}zenodraft-action-testing.`);
fs.copyFileSync(src('CITATION.cff'), dest('CITATION.cff'));
fs.copyFileSync(src('expected.yml'), dest('expected.yml'));
process.chdir(temporary_directory);
});
test('upserting a doi', () => {
const upsert_location = 'doi';
const prereserved_doi = '10.5281/upserted.1234567';
upserting_1.upsert_prereserved_doi(upsert_location, prereserved_doi);
const actual = fs.readFileSync('CITATION.cff', 'utf8');
const expected = fs.readFileSync('expected.yml', 'utf8');
expect(actual).toEqual(expected);
});
afterEach(() => {
fs.rmdirSync(temporary_directory, { recursive: true });
});
55 changes: 55 additions & 0 deletions tests/upserting/1.0.3/errors/upserting.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const upserting_1 = require("../../../../src/upserting");
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const assert_1 = require("assert");
let temporary_directory;
beforeEach(() => {
const src = (f) => {
return path.join(__dirname, f);
};
const dest = (f) => {
return path.join(temporary_directory, f);
};
temporary_directory = fs.mkdtempSync(`${os.tmpdir()}${path.sep}zenodraft-action-testing.`);
fs.copyFileSync(src('CITATION.cff'), dest('CITATION.cff'));
process.chdir(temporary_directory);
});
test('upserting a doi', () => {
const upsert_location = 'identifiers[0]';
const prereserved_doi = '10.5281/upserted.1234567';
const throwfun = () => {
upserting_1.upsert_prereserved_doi(upsert_location, prereserved_doi);
};
expect(throwfun).toThrow(assert_1.AssertionError);
try {
throwfun();
}
catch (err) {
expect(err.message).toBe('Your CITATION.cff file does not support key \'identifiers\'. Consider updating its \'cff-version\' value.');
}
});
afterEach(() => {
fs.rmdirSync(temporary_directory, { recursive: true });
});
49 changes: 49 additions & 0 deletions tests/upserting/1.1.0/doi/append/upserting.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const upserting_1 = require("../../../../../src/upserting");
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
let temporary_directory;
beforeEach(() => {
const src = (f) => {
return path.join(__dirname, f);
};
const dest = (f) => {
return path.join(temporary_directory, f);
};
temporary_directory = fs.mkdtempSync(`${os.tmpdir()}${path.sep}zenodraft-action-testing.`);
fs.copyFileSync(src('CITATION.cff'), dest('CITATION.cff'));
fs.copyFileSync(src('expected.yml'), dest('expected.yml'));
process.chdir(temporary_directory);
});
test('upserting a doi', () => {
const upsert_location = 'doi';
const prereserved_doi = '10.5281/upserted.1234567';
upserting_1.upsert_prereserved_doi(upsert_location, prereserved_doi);
const actual = fs.readFileSync('CITATION.cff', 'utf8');
const expected = fs.readFileSync('expected.yml', 'utf8');
expect(actual).toEqual(expected);
});
afterEach(() => {
fs.rmdirSync(temporary_directory, { recursive: true });
});
Loading

0 comments on commit 9cbadf8

Please sign in to comment.