Using CLI for Jira for Adding and Removing Users from Groups
You may want to fine-tune your license usage in Jira. For instance, some of your employees might work only from Monday to Wednesday, while…
You may want to fine-tune your license usage in Jira. For instance, some of your employees might work only from Monday to Wednesday, while another group may work from Thursday to Sunday. For this reason, you might need to add and remove different sets of users from the user group of service desk agents depending on the weekday. While you can manage this manually through Jira’s group membership screen using the UI, our CLI for Jira app makes it much easier.
You can use below commands to add any user to a jira user group or remove from jira user group.
jira group add-user --group=jira-servicemanagement-users-worklogpro --user=557159:af2a835d-85c1-41e3-12c1-3cfae2a2d1be --site=worklogpro
jira group remove-user --group=jira-servicemanagement-users-worklogpro --user=557159:af2a835d-85c1-41e3-12c1-3cfae2a2d1be --site=worklogpro
You can write corresponding commands to a shell script file and manually execute it at appropriate times. If you want, you can also automate it using Cron or similar program.
Steps to Automate with a Cron Script:
Create a Shell Script: Write a shell script that uses the CLI commands to add or remove users based on the day of the week.
#!/bin/bash
# Get the current day of the week
DAY_OF_WEEK=$(date +%u) # Monday=1, Sunday=7
# Define CLI command for your site
CLI_COMMAND="jira group"
if [ "$DAY_OF_WEEK" -ge 1 ] && [ "$DAY_OF_WEEK" -le 3 ]; then
# Monday to Wednesday group modification
$CLI_COMMAND add-user --group=jira-servicemanagement-agents --user=user1 --site=worklogpro
$CLI_COMMAND remove-user --group=jira-servicemanagement-agents --user=user2 --site=worklogpro
elif [ "$DAY_OF_WEEK" -ge 4 ] && [ "$DAY_OF_WEEK" -le 7 ]; then
# Thursday to Sunday group modification
$CLI_COMMAND add-user --group=jira-servicemanagement-agents --user=user2 --site=worklogpro
$CLI_COMMAND remove-user --group=jira-servicemanagement-agents --user=user1 --site=worklogpro
fi
Make the Script Executable: Run the following command to make your script executable:
chmod +x your_script_name.sh
Schedule the Script with Cron: Open the crontab editor:
crontab -e
Add an entry to run the script at a specific time each day. For instance, to run it at midnight:
0 0 * * * /path/to/your_script_name.sh
Monitor Logs: If you encounter issues, check the cron job’s log or configure your script to log outputs and errors:
./your_script_name.sh >> /path/to/logfile.log 2>&1