Skip to content

Commit

Permalink
Team ID fix in Schedule and Policy importer (#57)
Browse files Browse the repository at this point in the history
* fixed the schedule and policy importer updating team id

* updating the build versions to 23.9
  • Loading branch information
sghosh4-atlassian authored Jul 24, 2023
1 parent 0506fc7 commit 356ab17
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 13 deletions.
2 changes: 1 addition & 1 deletion backup-commons/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'java'
apply plugin: 'maven'

group 'com.opsgenie.tools'
version '0.23.8'
version '0.23.9'

sourceCompatibility = 1.6
targetCompatibility = 1.6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ public static String readFile(String fileName) throws IOException {

return sb.toString();
}
public static OptionalUtil<String> getTeamNameFromId(File teamsDirectory, String teamId) {
String[] files = getFileListOf(teamsDirectory);
OptionalUtil<String> teamName = OptionalUtil.empty();
if (files.length == 0) {
logger.warn("Warning: {} is empty", teamsDirectory);
}
else {
for (String fileName : files) {
if(fileName.contains(teamId)){
String[] fileNameSplitArr = fileName.split("-");
if(fileNameSplitArr.length > 0){
teamName = OptionalUtil.of(fileNameSplitArr[0]);
}
break;
}
}
}
return teamName;
}


public static RateLimitsDto generateRateLimits(String apiKey, String opsGenieHost) throws IOException {
try {
Expand Down Expand Up @@ -74,7 +94,7 @@ public boolean accept(File file, String s) {
}
});
}
return null;
return new String[]{};
}

public static boolean checkValidString(String s) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.opsgenie.tools.backup.util;

public class OptionalUtil<T> {
private final T value;
private static final OptionalUtil<?> EMPTY = new OptionalUtil();

private OptionalUtil() {
this.value = null;
}

private OptionalUtil(T value) {
this.value = value;
}

public static<T> OptionalUtil<T> empty() {
return (OptionalUtil<T>) EMPTY;
}

public static <T> OptionalUtil<T> of(T value) {
if (value == null) {
throw new NullPointerException("Value cannot be null");
}
return new OptionalUtil<T>(value);
}

public T get() {
if (value == null) {
throw new IllegalStateException("Value is not present");
}
return value;
}

public boolean isPresent() {
return value != null;
}

public T orElse(T defaultValue) {
return value != null ? value : defaultValue;
}
}
2 changes: 1 addition & 1 deletion backup-export/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'com.opsgenie.tools'
version '0.23.8'
version '0.23.9'

apply plugin: 'java'

Expand Down
2 changes: 1 addition & 1 deletion backup-import/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'com.opsgenie.tools'
version '0.23.8'
version '0.23.9'

apply plugin: 'java'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private void initializeImporters(String rootPath) {
importers.add(new TeamTemplateImporter(rootPath, rateLimitManager, config.isAddNewTeams(), config.isUpdateExistingTeams()));
importers.add(new ScheduleTemplateImporter(rootPath, config.isAddNewSchedules(), config.isUpdateExistingSchedules()));
importers.add(new EscalationImporter(rootPath, config.isAddNewEscalations(), config.isUpdateExistingEscalations()));
importers.add(new ScheduleImporter(rootPath, config.isAddNewSchedules(), config.isUpdateExistingSchedules()));
importers.add(new ScheduleImporter(rootPath, rateLimitManager, config.isAddNewSchedules(), config.isUpdateExistingSchedules()));
importers.add(new TeamImporter(rootPath, rateLimitManager, config.isAddNewTeams(), config.isUpdateExistingTeams()));
importers.add(new UserForwardingImporter(rootPath, config.isAddNewUserForwarding(), config.isUpdateExistingUserForwarding()));
importers.add(new DeprecatedPolicyImporter(rootPath, config.isAddNewPolicies(), config.isUpdateExistingPolicies()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
abstract class BaseImporter<T> implements Importer {
protected final Logger logger = LoggerFactory.getLogger(getClass());
protected File importDirectory;
protected File backupRootDirectory;
private boolean addEntityEnabled;
private boolean updateEntityEnabled;
protected static Map<String, String> oldTeamIdMap = new HashMap<String, String>();
Expand All @@ -25,6 +26,7 @@ abstract class BaseImporter<T> implements Importer {
BaseImporter(String backupRootDirectory, boolean addEntityEnabled, boolean updateEntityEnabled) {
this.addEntityEnabled = addEntityEnabled;
this.updateEntityEnabled = updateEntityEnabled;
this.backupRootDirectory = new File(backupRootDirectory + "/");
this.importDirectory = new File(backupRootDirectory + "/" + getImportDirectoryName() + "/");
}

Expand All @@ -42,7 +44,7 @@ public void restore() throws Exception {
}

String[] files = BackupUtils.getFileListOf(importDirectory);
if (files == null || files.length == 0) {
if (files.length == 0) {
logger.warn("Warning: " + getImportDirectoryName() + " is empty. Restoring " + getImportDirectoryName() + " skipped");
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import com.opsgenie.tools.backup.retry.RateLimitManager;
import com.opsgenie.tools.backup.retry.RetryPolicyAdapter;
import com.opsgenie.tools.backup.util.BackupUtils;
import com.opsgenie.tools.backup.util.OptionalUtil;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
Expand Down Expand Up @@ -148,8 +150,15 @@ protected void updateTeamIds(PolicyWithTeamInfo entity) throws Exception {
if(entity.getPolicy().getClass() == AlertPolicy.class) {
AlertPolicy alertPolicy = (AlertPolicy) entity.getPolicy();
for(Responder responder : alertPolicy.getResponders()) {
if(responder.getId().equals(entity.getTeamId())) {
responder.setId(teamIdMap.get(teamName));
if(responder.getType() == Responder.TypeEnum.TEAM) {
File teamsDirectory = new File(backupRootDirectory + "/teams/");
OptionalUtil<String> responderTeamName = BackupUtils.getTeamNameFromId(teamsDirectory, responder.getId());
if (responderTeamName.isPresent()){
responder.setId(teamIdMap.get(responderTeamName.get()));
}
else {
logger.info("Could not find team name for team Id {} in the backup folder", responder.getId());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@
import com.opsgenie.tools.backup.retrieval.EntityRetriever;
import com.opsgenie.tools.backup.retrieval.ScheduleRetriever;
import com.opsgenie.tools.backup.retry.RetryPolicyAdapter;
import com.opsgenie.tools.backup.retry.RateLimitManager;
import com.opsgenie.tools.backup.util.BackupUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

public class ScheduleImporter extends BaseImporter<ScheduleConfig> {
public class ScheduleImporter extends BaseImporterWithRateLimiting<ScheduleConfig> {

private static ScheduleApi scheduleApi = new ScheduleApi();
private static ScheduleOverrideApi overrideApi = new ScheduleOverrideApi();

public ScheduleImporter(String backupRootDirectory, boolean addEntity, boolean updateEntitiy) {
super(backupRootDirectory, addEntity, updateEntitiy);
public ScheduleImporter(String backupRootDirectory, RateLimitManager rateLimitManager, boolean addEntity, boolean updateEntitiy) {
super(backupRootDirectory, rateLimitManager, addEntity, updateEntitiy);
}

@Override
Expand Down Expand Up @@ -205,5 +207,16 @@ protected String getEntityIdentifierName(ScheduleConfig entity) {
}

@Override
protected void updateTeamIds(ScheduleConfig entity) {}
protected void updateTeamIds(ScheduleConfig entity) throws Exception {
Map<String, String> teamIdMap = new TeamIdMapper(rateLimitManager).getTeamIdMap();
if(entity.getSchedule() != null){
TeamMeta ownerTeam = entity.getSchedule().getOwnerTeam();
if(ownerTeam != null){
String newTeamId = teamIdMap.get(ownerTeam.getName());
if(newTeamId != null) {
ownerTeam.setId(newTeamId);
}
}
}
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'java'
apply plugin: 'maven'

group 'com.opsgenie.tools'
version '0.23.8'
version '0.23.9'

sourceCompatibility = 1.6
targetCompatibility = 1.6
Expand Down

0 comments on commit 356ab17

Please sign in to comment.