Skip to content

Commit

Permalink
[INFRA-100] - Added support of API methods for the deletion and renam…
Browse files Browse the repository at this point in the history
…ing of JIRA components

Signed-off-by: Oleg Nenashev <o.v.nenashev@gmail.com>
  • Loading branch information
oleg-nenashev committed Jun 29, 2014
1 parent bbebc3e commit 1ab8354
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Expand Up @@ -21,6 +21,12 @@
<artifactId>jira-rest-java-client</artifactId>
<version>0.4</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>2.0.1</version>
<type>jar</type>
</dependency>
</dependencies>

<scm>
Expand Down
72 changes: 65 additions & 7 deletions src/main/java/org/jenkinsci/jira_scraper/JiraScraper.java
Expand Up @@ -5,12 +5,15 @@
import com.atlassian.jira.rest.client.auth.BasicHttpAuthenticationHandler;
import com.atlassian.jira.rest.client.domain.AssigneeType;
import com.atlassian.jira.rest.client.domain.BasicComponent;
import com.atlassian.jira.rest.client.domain.BasicUser;
import com.atlassian.jira.rest.client.domain.Component;
import com.atlassian.jira.rest.client.domain.Component.AssigneeInfo;
import com.atlassian.jira.rest.client.domain.input.ComponentInput;
import com.atlassian.jira.rest.client.internal.jersey.JerseyJiraRestClientFactory;

import java.io.IOException;
import java.net.URI;
import javax.annotation.Nonnull;

/**
* @author Kohsuke Kawaguchi
Expand All @@ -33,22 +36,77 @@ public void createComponent(String projectKey, String subcomponent, String owner
subcomponent, subcomponent + " plugin", owner, defaultAssignee), pm);
}

/**
* Deletes the specified component
* @param projectKey Project Id
* @param deletedComponentName Name of the component to be deleted
* @param backupComponentName Existing issues will be moved to this component
* @throws IOException Missing components
* @since TODO: define the version
*/
public void deleteComponent(String projectKey, String deletedComponentName, String backupComponentName)
throws IOException {
BasicComponent deletedComponent = getBasicComponent(projectKey, deletedComponentName);
BasicComponent backupComponent = getBasicComponent(projectKey, backupComponentName);

restClient.getComponentClient().removeComponent(deletedComponent.getSelf(), backupComponent.getSelf(), pm);
}

/**
* Deletes the specified component
* @param projectKey Project Id
* @param oldName Name of the component to be renamed
* @param newName New name to be set
* @throws IOException Cannot find the initial component or the new name is exist
* @since TODO: define the version
*/
public void renameComponent(String projectKey, String oldName, String newName)
throws IOException {
BasicComponent c = getBasicComponent(projectKey, oldName);
Component comp = getComponent(c);

AssigneeInfo info = comp.getAssigneeInfo();
AssigneeType assigneeType = info != null ? info.getAssigneeType() : null;
BasicUser leadUser = info != null ? info.getAssignee() : null;

restClient.getComponentClient().updateComponent(c.getSelf(),
new ComponentInput(newName, c.getDescription(),
leadUser != null ? leadUser.getName() : null, assigneeType), pm);
}

public void setDefaultAssignee(String projectId, String component, AssigneeType assignee) throws IOException {
setDefaultAssignee(projectId, component, assignee, null);
}

public void setDefaultAssignee(String projectId, String component, AssigneeType assignee, String name) throws IOException {
for (BasicComponent c : restClient.getProjectClient().getProject(projectId,pm).getComponents()) {
BasicComponent c = getBasicComponent(projectId, component);
Component comp = getComponent(c);

String componentLead = name == null ? comp.getLead().getName() : name;
ComponentInput ci = new ComponentInput(component, c.getDescription(), componentLead, assignee);
restClient.getComponentClient().updateComponent(c.getSelf(), ci, pm);
}

/**
* Gets JIRA component by the specified name.
* @param projectId Project Id (e.g. JENKINS)
* @param component Component name
* @return The requested component
* @throws IOException Component cannot be found
*/
private @Nonnull BasicComponent getBasicComponent (String projectId, String component)
throws IOException {
for (BasicComponent c : restClient.getProjectClient().getProject(projectId, pm).getComponents()) {
if (c.getName().equals(component)) {
Component comp = restClient.getComponentClient().getComponent(c.getSelf(),pm);
String componentLead = name == null ? comp.getLead().getName() : name;
ComponentInput ci = new ComponentInput(component,c.getDescription(),componentLead,assignee);
restClient.getComponentClient().updateComponent(c.getSelf(), ci, pm);
return;
return c;
}
}

throw new IOException("Unable to find component "+component+" in the issue tracker");
throw new IOException("Unable to find component "+component+" in the "+ projectId +" issue tracker");
}

private @Nonnull Component getComponent (@Nonnull BasicComponent c) {
return restClient.getComponentClient().getComponent(c.getSelf(),pm);
}

// test
Expand Down

0 comments on commit 1ab8354

Please sign in to comment.