Skip to content

Commit

Permalink
feat(atlassian_mu): add 'del_' prefix to inactive users
Browse files Browse the repository at this point in the history
* when processing to atlassian, inactive users are now marked with the 'del_' prefix
* an extra function to add the prefix to all currently inactive users was also added (can be removed upon initialization)

DEPLOYMENT NOTE: script needs to be run once with DEACTIVATED_PREFIX_INIT=1
  • Loading branch information
xflord committed Sep 14, 2023
1 parent 32141c0 commit 71afe61
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions send/atlassian_mu_process.pl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
# (0) no debug, (1) only messages of performed operations, (2) communication with server.
my $DEBUG=0;

my $DEACTIVATED_PREFIX_INIT=0;

# Required setting for connection
my $RESULTS_COUNT = 200; # This many objects will be fetched with one GET request on server. Limited by Atlassian ANYWAY!
my $directoryUrl;
Expand Down Expand Up @@ -100,9 +102,12 @@ sub help {
my $ourGroups = fetchOurGroups();
# Data from Atlassian into hash
my $theirUsers = fetchTheirUsers();
my $theirGroups = fetchTheirGroups();

if ($DEACTIVATED_PREFIX_INIT) {
addPrefixToDeactivatedUsers();
}
evaluateUsers();
my $theirGroups = fetchTheirGroups(); # Acquire groups now to account for updated usernames with prefixes
$theirUsers = fetchTheirUsers(); # Acquire created users' IDs
evaluateGroups();

Expand Down Expand Up @@ -288,22 +293,45 @@ sub fetchOurGroups {
return JSON::XS->new->decode($json_groups);
}

# temporary function to update atlassian with the new feature of prefixes for deactivated users
sub addPrefixToDeactivatedUsers {
print("Updating usernames of deactivated users to contain the 'del' prefix. \n");
my $theirUserName;
my $theirUserInfo;
while (($theirUserName, $theirUserInfo) = each(%{$theirUsers})) {
my $prefix = substr $theirUserName, 0, 4;
if (!$theirUserInfo->{'active'} && $prefix ne "del_") {
$theirUserInfo->{"email"} = 'del_'.$theirUserInfo->{"email"};
updateUser('del_'.$theirUserName, $theirUserInfo, $theirUserInfo->{'id'}, 0);
}
}
print("Done updating usernames. \n");
}

# Compares Atlassian users with Perun users.
# Sends request to create missing users, update outdated user data and deactivate users missing in Perun.
sub evaluateUsers {
my $theirUserName;
my $theirUserInfo;
while (($theirUserName, $theirUserInfo) = each(%{$theirUsers})) {
# remove potential del_ prefix to correctly match with our users
$theirUserName =~ s/^del_//;
my $ourUserInfo = $ourUsers->{$theirUserName};
if (!$ourUserInfo && $theirUserInfo->{'active'}) {
updateUser($theirUserName, $theirUserInfo, $theirUserInfo->{'id'}, 0);
# add del_ prefix since we're deactivating user
$theirUserInfo->{"email"} = 'del_'.$theirUserInfo->{"email"};
updateUser('del_'.$theirUserName, $theirUserInfo, $theirUserInfo->{'id'}, 0);
}
}

my $ourUserName;
my $ourUserInfo;
while (($ourUserName, $ourUserInfo) = each(%{$ourUsers})) {
$theirUserInfo = $theirUsers->{$ourUserName};
# check if user isn't marked as deleted
if (!defined $theirUserInfo) {
$theirUserInfo = $theirUsers->{'del_'.$ourUserName};
}
if (defined $theirUserInfo) {
# check if attributes changed or our user got active again
if (checkUserAttributesDiffer($ourUserInfo, $theirUserInfo) || !$theirUserInfo->{"active"}) {
Expand Down Expand Up @@ -437,7 +465,8 @@ sub updateUser {
my $userInfo = shift;
my $userId = shift;
my $statusActive = shift;
if ($DEBUG) {print $statusActive ? "Updating user $userName.\n" : "Deactivating user $userName.\n" ;}
my $nameWithoutPrefix = substr $userName, 4;
if ($DEBUG) {print $statusActive ? "Updating user $userName.\n" : "Deactivating user $nameWithoutPrefix.\n" ;}

my $updateContent = {
"userName" => $userName,
Expand Down

0 comments on commit 71afe61

Please sign in to comment.