Workflow failing with TypeError: Access to host class jetbrains.youtrack.workflow.model.Assertion is not allowed or does not exist
I'm attempting to modify @jetbrains/youtrack-spreadsheets-client (below). I added this statement:
const entities = require('@jetbrains/youtrack-scripting-api/entities');
which results in a runtime error:
TypeError: Access to host class jetbrains.youtrack.workflow.model.Assertion is not allowed or does not exist.
Can't imagine why I'd receive this error,
What I'm trying to accomplish is to to get the logged in user so I can set Author. Apparently there is no currentUser in the context so I was hoping (perhaps foolishly) to be able to get it via the entities interface.
Thanks for the help.
/**
* Copyright JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const ProjectFieldConverter = require('./project-field-converter');
const FieldConverter = require('./field-converter');
const Util = require('./util');
const entities = require('@jetbrains/youtrack-scripting-api/entities');
function getIssueFields(project, issue) {
let theAuthor=null;
FieldConverter.getConvertedFields(project, issue)
.filter(field => {
let fieldName = field.name;
fieldName = fieldName.toLowerCase().replace(/ /g, '');
if (fieldName === 'theauthor') {
//theAuthor = entities.User.current;
//theAuthor = entities.User.findByLogin(field.value);
}
});
let ctx = Util.getCtx();
let baseFields = {
summary: '',
description: '',
created: new Date().toISOString(),
updated: new Date().toISOString(),
links: [],
/*author: theAuthor,*/
/* author: {
login: ctx.currentUser.login,
name: ctx.currentUser.name,
type: 'user'
},*/
comments: [],
attachments: [],
tags: [],
watchers: [],
voters: []
};
let extraFields = [];
FieldConverter.getConvertedFields(project, issue)
.filter(field => {
let fieldName = field.name;
fieldName = fieldName.toLowerCase().replace(/ /g, '');
let isReservedFieldTitle = !(fieldName !== 'description'
&& fieldName !== 'summary'
&& fieldName !== 'tags'
&& fieldName !== 'links'
&& fieldName !== 'attachments'
&& fieldName !== 'comments'
&& fieldName !== 'watchers'
&& fieldName !== 'created'
&& fieldName !== 'updated'
&& fieldName !== 'reporter'
&& fieldName !== 'issue id'
&& fieldName !== 'reporter'
&& fieldName !== 'project'
&& fieldName !== 'votes'
&& fieldName !== 'voters');
if (isReservedFieldTitle) {
switch (fieldName) {
case 'summary':
case 'description':
field.name = fieldName;
extraFields.push(field);
}
return false;
} else {
return true;
}
})
.forEach(field => {
let name = field.name;
if (!(field.type === 'enum' || field.type === 'enum multi' || field.type === 'user')) {
delete field.name;
baseFields[name] = field;
} else {
switch (field.type) {
case 'enum': {
delete field.name;
baseFields[name] = field;
break;
}
case 'user': {
let values = field.value;
let result = [];
if (values) {
values.forEach(value => {
result.push({
multiValue: true,
type: 'user',
value: value
});
});
baseFields[name] = result;
}
break;
}
case 'enum multi': {
let values = field.value;
let result = [];
if (values) {
values.forEach(value => {
result.push({
multiValue: true,
type: 'enum',
value: value
});
});
baseFields[name] = result;
}
break;
}
}
}
});
extraFields.forEach(field => baseFields[field.name] = field.value);
return baseFields;
}
function getProjectFields(project) {
let baseFields = [];
ProjectFieldConverter.getConvertedProjectFields(project)
.filter(field => {
let fieldName = field.name;
fieldName = fieldName.toLowerCase().replace(/ /g, '');
return fieldName !== 'description'
&& fieldName !== 'summary'
&& fieldName !== 'tags'
&& fieldName !== 'links'
&& fieldName !== 'attachments'
&& fieldName !== 'comments'
&& fieldName !== 'watchers'
&& fieldName !== 'created'
&& fieldName !== 'updated'
&& fieldName !== 'author'
&& fieldName !== 'voters';
})
.forEach(field => {
switch (field.type) {
case 'enum multi': {
delete field.type;
field.type = 'enum';
field.multiValue = true;
baseFields.push(field);
break;
}
case 'user': {
field.multiValue = true;
baseFields.push(field);
break;
}
default: {
baseFields.push(field);
}
}
});
return baseFields;
}
module.exports = {
getIssueFields,
getProjectFields
};
Please sign in to leave a comment.
Hi!
I'm Sergey from the YouTrack team.
It appears as though you are mixing up workflow and import scripts. currentUser is a part of the workflow context and is not present in the import. There's no currentUser property in the import's context.
In the import scripts, you can either specify an existing user or one from the import data, as there's little sense in the currentUser definition in the import.
As for the error, this line likely causes:
let ctx = Util.getCtx();
. I guess you tried to access some workflow context properties in the import scripts this way, but I'm afraid it won't work like that.I also recommend checking the import documentation and API on our dev portal.
I'm running in the cloud
Thanks. I thought that might be the issue.