Join 34,000+ subscribers and receive articles from our
blog about software quality, testing, QA and security.

Redmine’s custom fields


#1

Hi!

I have custom field in my Redmine and i’d like to use it as mandatory field when i’m pushing defect from testrail.
I’m experiencing difficulties with realization.
I read http://docs.gurock.com/testrail-integration/defects-plugins-examples#adding_built-in_fields and tried do like it says, but nothing.

I’m trying to get my custom field like:

public function get_opened_in_version($project_id)
{
$data = array($project_id);
$response = $this->_send_command(‘GET’, ‘custom_fields’);

	if (!$response)
	{
		return array();
	}
 
	$result = array();
	foreach ($response as $custom_field)
	{
		$c = obj::create();
		$c->id = (string) $opened_in_version->id;
		$c->name = (string) $opened_in_version->name;
		$result[] = $c;
	}
 
	return $result;
}

And i’m trying to use it in add_issue function like this:

if (isset($options[‘opened_in_version’]))
{
$version = array(
‘id’ => $options[‘opened_in_version’],
‘archived’ => false,
‘released’ => false
);
$options[‘opened_in_version’] = array(
$version
);
}

Please give me some hint


#2

Hi Pavlo,

Thanks for your posting. Have you also added the field to the actual form (prepare_push and prepare_field)? The code you posted is just the API related code, is this correct? Customizing the form would be the first step and you would need to add the field definition to the form (prepare_push) and then add the field implementation (prepare_field).

http://docs.gurock.com/testrail-integration/defects-plugins-examples#adding_custom_fields

I hope this helps!

Cheers,
Tobias


#3

Hi!

Thank you for your response.
Sure i added necessary fields to prepare_push and prepare_field.

This is from prepare_push

public function prepare_push($context)
{
// Return a form with the following fields/properties
return array(
‘fields’ => array(
‘subject’ => array(
‘type’ => ‘string’,
‘label’ => ‘Subject’,
‘required’ => true,
‘size’ => ‘full’
),
‘tracker’ => array(
‘type’ => ‘dropdown’,
‘label’ => ‘Tracker’,
‘required’ => true,
‘remember’ => true,
‘size’ => ‘compact’
),
‘project’ => array(
‘type’ => ‘dropdown’,
‘label’ => ‘Project’,
‘required’ => true,
‘remember’ => true,
‘cascading’ => true,
‘size’ => ‘compact’
),
‘parent’ => array(
‘type’ => ‘string’,
‘label’ => ‘Parent task’,
‘remember’ => true,
// ‘depends_on’ => ‘project’,
‘size’ => ‘compact’
),
/* ‘priority’ => array(
‘type’ => ‘dropdown’,
‘label’ => ‘Priority’,
‘remember’ => true,
‘size’ => ‘compact’
),
‘status’ => array(
‘type’ => ‘dropdown’,
‘label’ => ‘Status’,
‘remember’ => true,
‘size’ => ‘compact’
),
*/ ‘opened_in_version’ => array(
‘type’ => ‘dropdown’,
‘label’ => ‘Opened in Version’,
‘required’ => true,
‘remember’ => true,
‘depends_on’ => ‘project’,
‘size’ => ‘compact’
),
‘description’ => array(
‘type’ => ‘text’,
‘label’ => ‘Description’,
‘rows’ => 10
)
)
);
}

This is from prepare_push

public function prepare_field($context, $input, $field)
{
	$data = array();

	if ($context['event'] == 'prepare')
	{
		$prefs = arr::get($context, 'preferences');
	}
	else
	{
		$prefs = null;
	}
	
	// Process those fields that do not need a connection to the
	// Redmine installation.		
	if ($field == 'subject' || $field == 'description' || $field == 'opened_in_version')
	{
		switch ($field)
		{
			case 'subject':
				$data['default'] = $this->_get_subject_default(
					$context);
				break;
				
			case 'description':
				$data['default'] = $this->_get_description_default(
					$context);
				break;				

/*
case ‘opened_in_version’:
if (isset($input[‘project’]))
{
$data[‘default’] = arr::get($prefs, ‘opened_in_version’);
$data[‘options’] = $this->_to_id_name_lookup(
$api->get_opened_in_version($input[‘project’])
);
}
*/
break;
}

		return $data;
	}
	
	// Take into account the preferences of the user, but only
	// for the initial form rendering (not for dynamic loads).
	
	// And then try to connect/login (in case we haven't set up a
	// working connection previously in this request) and process
	// the remaining fields.
	$api = $this->_get_api();
	
	switch ($field)
	{
		case 'tracker':
			$data['default'] = arr::get($prefs, 'tracker');
			$data['options'] = $this->_get_trackers($api);
			break;

		case 'project':
			$data['default'] = arr::get($prefs, 'project');
			$data['options'] = $this->_to_id_name_lookup(
				$api->get_projects()
			);
			break;
		
		case 'opened_in_version':
			$data['default'] = arr::get($prefs, 'opened_in_version');
			$data['options'] = $this->_to_id_name_lookup(
				$api->get_opened_in_version()
			);
			break;

case ‘category’:

if (isset($input[‘project’]))

{

$data[‘default’] = arr::get($prefs, ‘category’);

$data[‘options’] = $this->_get_categories(

$api,

$input[‘project’]

);

}

break;

	}
	
	return $data;
}

#4

Thanks! It looks like the relevant code is commented out in prepare_field, can you check this again? Do you see the versions in the dropdown on the Push dialog?

Cheers,
Tobias


#5

Yeah. I see it


But when i choose relevant project, for which this profile set, 'opened in version 'dropdown list is empty


#6

Guys, lets do it in another way.
Here defects-plugins-custom you said that you can build plugin for us.

We have added new custom field to our redmine ‘Opened in version’, where we use versions.
We want add this field to ‘push window’ in testrail. I would love to do it by myself, but my programming knowledge is 0. I saw your example how to add new field with Jira. I have tried, but didn’t work with redmine.
Thus i asking you to help.

Please provide me with such customization.
Thank you in advance!


#7

Hi Pavlo,

Thanks for your reply! We would usually recommend leaving the plugins unchanged as you will automatically benefit from newer plugin versions and new features when you update TestRail and if we change anything (this is not the case with a customized version). If you have additional fields that are not covered by the default plugins, we would recommend changing those fields in Redmine as a second step (and making this field optional so you can still push new issues from TestRail).

Cheers,
Tobias


#8

Thanks Tobias,
We have managed to solve this issue. All what was needed to make appropriate custom array in function add issue, like:

        $custom_fields_array = array();
        array_push($custom_fields_array, array('id' => 56 ,'value' => $options['version']));
        $issue->custom_fields = $custom_fields_array;

Probably it will help some others in the future. In general it will be nice if you add such examples to your howto documentations for creating plugin to Redmine.


#9

Hi Sergey,

Thanks for your feedback and posting this, that’s appreciated!

Cheers,
Tobias


#10

Hello,
Please, say what is request me need to use if I want to get info about the custom fields from Redmine?
For example for projects, I use
$response = $this->_send_command('GET', 'projects'); (It works good.)
When I try to get custom fields
$response = $this->_send_command('GET', 'custom_fields'); (not works)
I catch 403 error?
Please any idea


#11

Hi Maria,

Please see my answer in the other thread:

Cheers,
Tobias


#12

It very helped me. Thanks a lot!


#13

Hi Maria,

Good to hear this works and thanks for the update!

Cheers,
Tobias