How to add a user to a Tempo Team using API

Sam March 27, 2020

Hi, I'm new to using REST APIs, and I'm trying to figure out how to use ScriptRunner to automatically add a user to a specific Tempo Team.

I've been looking at the documentation here: https://www.tempo.io/server-api-documentation/teams#operation/addMember

but I haven't been able to get it to work, every time I try, I get a 500 response code and no response message. Has anyone hear been able to add a user to Tempo Teams via REST APIs, especially from ScriptRunner?

My code is below:

import com.atlassian.jira.component.ComponentAccessor
import java.nio.charset.StandardCharsets;
import groovy.json.StreamingJsonBuilder
import groovy.json.JsonSlurper

def jsonSlurper = new JsonSlurper()
def request = jsonSlurper.parseText("\"member\":{\"type\":\"USER\",\"key\":\"<userkey>\"},\"membership\":{\"role\":{\"id\":1},\"dateFrom\":\"2019-05-28\",\"dateTo\":\"2019-06-28\",\"availability\":\"100\"}")
def username = "username"
def password = "password"
def url = new URL("https://<baseURL>/rest/tempo-teams/2/team/67/member")
String encoded = Base64.getEncoder().encodeToString((username+":"+password).getBytes(StandardCharsets.UTF_8));
HttpURLConnection postConnection =(HttpURLConnection)url.openConnection();
postConnection.setRequestMethod("POST")
postConnection.setDoInput(true)
postConnection.setRequestProperty( "Authorization", "Basic ${encoded}" );
postConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
postConnection.setRequestProperty("Accept", "application/json");
postConnection.setDoOutput(true);
postConnection.outputStream.withWriter("UTF-8") { new StreamingJsonBuilder(it, request) }

log.error(postConnection.getResponseCode())
log.error(postConnection.getResponseMessage())
postConnection.connect()
log.error(postConnection.getResponseCode())
log.error(postConnection.getResponseMessage())

 The logs are as follows:

2020-03-27 12:04:39,588 ERROR [runner.AbstractScriptRunner]: 500
2020-03-27 12:04:39,588 ERROR [runner.AbstractScriptRunner]:
2020-03-27 12:04:39,588 ERROR [runner.AbstractScriptRunner]: 500
2020-03-27 12:04:39,588 ERROR [runner.AbstractScriptRunner]:

 

1 answer

1 accepted

0 votes
Answer accepted
Susanne Götz _Tempo_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 30, 2020

Hi @Sam ,

Can you please take a look at the documentation again as it has recently been updated due to an error in the Payload description.

Can you please use "name" : " your_members_name"

instead of "key": "userkey"

Regards,
Susanne Götz
Tempo team

Sam March 30, 2020

Hello Susanne,

Thank you for your response! I have edited the code to the following, but I still get the exact same error. When you mentioned the documentation, is this what you were talking about: https://www.tempo.io/server-api-documentation/teams#operation/getTeamMembers

import com.atlassian.jira.component.ComponentAccessor;
import java.nio.charset.StandardCharsets;
import groovy.json.StreamingJsonBuilder;
import groovy.json.JsonSlurper;

def jsonSlurper = new JsonSlurper();
def url = new URL("https://tracking-dev.ainq.com/rest/tempo-teams/2/team/67/member");
String encoded = Base64.getEncoder().encodeToString((username+":"+password).getBytes(StandardCharsets.UTF_8));
HttpURLConnection postConnection =(HttpURLConnection)url.openConnection();
postConnection.setRequestMethod("POST")
postConnection.setRequestProperty("Content-Type", "application/json; utf-8");
postConnection.setRequestProperty("Accept", "application/json");
postConnection.setDoOutput(true);

def jsonInputString = '''
{
"member": {
"type": "USER",
"name": "svanfossen"
},
"membership": {
"role": {
"id": 1
},
"dateFrom": "",
"dateTo": "",
"availability": "100"
}
}
'''
postConnection.setRequestProperty( "Authorization", "Basic ${encoded}" );
postConnection.requestMethod = "POST";
postConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
postConnection.setRequestProperty("Accept", "application/json");
postConnection.setDoOutput(true);
postConnection.outputStream.withWriter("UTF-8") { new StreamingJsonBuilder(it, jsonInputString) }
postConnection.connect();

Susanne Götz _Tempo_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 30, 2020

Hi @Sam

The documentation I am referring to is for the "Add a member to a team" API ( https://www.tempo.io/server-api-documentation/teams#operation/addMember) ( which is the API you are using.

Can you please try to use this API outside of your script ( f.x using Postman) and check if you can add a user to the team directly. Please note that adding users via API requires the same permissions as when adding the user in the UI, so you will need to make sure that the authenticated user has the "Tempo Team Administrator" permission. 

Regards,
Susanne

Sam March 30, 2020

Hi @Susanne Götz _Tempo_ , 

I haven't tried the API outside of my script, although I'm not sure why it wouldn't work in that context. I've contacted members of my team about using an API platform.

I've also verified that I do have the "Tempo Team Administrator" permission, so it doesn't seem like that's the issue.

Susanne Götz _Tempo_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 30, 2020

@Sam 

It might be useful to know if it is the API that is causing the problem or if it is within a different part of the script. 

Regards,
Susanne

Sam March 30, 2020

@Susanne Götz _Tempo_ , it works outside of ScriptRunner, so it looks like the problem is with ScriptRunner. Thanks for your help!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events