Create project via REST api and copy workflow

Marvin Schmidtke April 12, 2019

Hi guys,

 

im trying to find a way to create projects via the REST api.

i've found the usual: rest/api/2/project PUBLIC api.

 

Sadly i can't find a solution to copy the workflow of another project to also use it for the new project

I've seen some old comments from 2016 that the rest api doesnt support this function, is it still that case ?

 

Has anybody found a workaround?

Has somebody done this with another api already or with scriptrunner?

3 answers

1 accepted

0 votes
Answer accepted
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 12, 2019

Hi @Marvin Schmidtke ,

Talking about scriptrunner, I have not done it with workflows but with issue types. Looking at the documentation, something like : 

Project sourceProject = ComponentAccessor.getProjectManager().getProjectObjByKey("source-project");
Project targetProject = ComponentAccessor.getProjectManager().getProjectObjByKey("target-project");
def workflowSchemeManager = ComponentAccessor.getWorkflowSchemeManager()
def sourceWorkflowScheme = workflowSchemeManager.getWorkflowSchemeObj(sourceProject)

workflowSchemeManager.addSchemeToProject(targetProject, sourceWorkflowScheme)

could work.

Antoine

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 16, 2019

Hi @Marvin Schmidtke ,

Did that help ?

1 vote
James Barwick December 8, 2020

I found that this answer has been asked and answered elsewhere.  Stackoverflow is awesome.

Anyway, I'll post my solution here.  First and foremost we assume that you know how to login and get websudo privileges.  It's needed.  Or this won't work.

2nd, I assume you know how to setup your own ApacheHttpClient or whatever you want to use.  So we won't go there.  And I assume you know how to serialize and deserialize objects to/from JSON.  

And, I'll apologize...I'm 60 years old and don't work with JSON.  I work with POJO's.  And lumbok and gson are my friends.  And, I am not going to post my 50 POJO's..  Assume all POJO's are JSON.

Anyway:

// This is the pid of MY template.  Not yours.  Change to your template
private
static final int TEMPLATE_ARTIFACT_SCRUM = 10457;

// Create a project creation request for use with the createshared API
JiraProjectCreateTemplateRequest
template = new JiraProjectCreateTemplateRequest();
template.setKey(key);
template.setName(name);
template.setLead(admins[0]);

JiraProject jp = createProjectWithTemplate(template, TEMPLATE_ARTIFACT_SCRUM);
// create a project from a template or Shared Configuration project
public
JiraProject createProjectWithTemplate(JiraProjectCreateTemplateRequest request, int template) throws HttpRequestException {
String path = String.format("/rest/project-templates/1.0/createshared/%d", template);
JiraProjectCreateTemplateResponse response = post(path, request, JiraProjectCreateTemplateResponse.class);
if (response == null)
throw new HttpRequestException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Project not created");
return getProject(response.getProjectKey());
}

 The 'post' method in this function is my little wrapper to deserialize and serialize POJO's.  The POJO for the response is:

@Data
public class JiraProjectCreateTemplateResponse implements Serializable {

String returnUrl;
int projectId;
String projectKey;
String projectName;
JiraRemoteProjectLinks remoteProjectLinks;
}

 In the end, this works for me because this is how I create projects in the GUI anyway.

Enjoy,

1 vote
James Barwick December 7, 2020

I cannot find the answer either.  

There is the REST api using the com.pyxis.greenhopper.jira:gh-scrum-template default template as the projectTemplateKey.

But, doesn't appear that you can specify anything else.

So, I'm abandoning the REST API and will simply use the form POST as done from the GUI.

I've just decided to do this.  So, if you found another answer, let me know.  Else, I'll post my final code once I get it working (need to dive into Javascript...)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events