通过Python实现批量创建用户
1、通过Python库创建
import pandas as pd
from jira import JIRA # type: ignore
from jira.exceptions import JIRAError # type: ignore
import json
# https://jira.readthedocs.io/api.html#jira.client.JIRA.add_user
# https://jira.readthedocs.io/api.html#jira.client.JIRA.add_user_to_group
jira = JIRA('http://10.4.0.xx', basic_auth=('username', 'password'))
# jira = JIRA('http://10.4.0.xx', auth=('username', 'password'))
# jira = JIRA('http://10.4.0.xx', token_auth='MTMxMzY5Nzc5NjU0Ol4RwJLoyWIdyMQPQSgFaEoC3dao')
# jira = JIRA('http://10.4.0.xx', basic_auth=('test@qq.com', 'MTMxMzY5Nzc5NjU0Ol4RwJLoyWIdyMQPQSgFaEoC3dao'))
excel_file_path = r'D:\\WORKSPACE\\Python\\ops-python\\jira\\xxx.xlsx'
df = pd.read_excel(excel_file_path)
for index, row in df.iterrows():
email = row['邮箱']
username = email.split('@')[0]
fullname = username
group = row['组']
try:
create_user_response = jira.add_user(
username = username,
email = email,
fullname = fullname,
ignore_existing = True
)
if create_user_response == True:
print(f'用户{username}创建成功')
except JIRAError as e:
err = e.response.json()["errors"]
print(f'用户{username}创建失败,{err["username"]}')
try:
add_user_to_group_response = jira.add_user_to_group(
username = username,
group = group
)
if add_user_to_group_response != False:
print(f'添加用户{username}到组{group}成功')
except JIRAError as e:
err = e.response.json()
print(f'添加用户{username}到组{group}失败,{err["errorMessages"]}\n')
2、通过创建用户的API创建

curl -u username:password -X GET -H "Content-Type: application/json" http://localhost:8080/rest/api/2/issue/createmeta
curl -H "Authorization: Basic ZnJlZDpmcmVk" -X GET -H "Content-Type: application/json" http://localhost:8080/rest/api/2/issue/createmeta
import pandas as pd
import requests
from requests.auth import HTTPBasicAuth
import json
jira_url = 'http://10.4.0.xx'
jira_username = 'username'
jira_password = 'password'
create_user_url = f"{jira_url}/rest/api/2/user"
add_user_to_group_url = f"{jira_url}/rest/api/2/group/user"
excel_file_path = r'D:\\WORKSPACE\\Python\\ops-python\\jira\\xxx.xlsx'
df = pd.read_excel(excel_file_path)
for index, row in df.iterrows():
email = row['邮箱']
username = email.split('@')[0]
fullname = username
group = row['组']
headers = {
"Accept": "application/json",
"Content-Type": "application/json"
}
create_user_payload = json.dumps( {
'name': username,
'emailAddress': email,
'displayName': fullname
} )
create_user_response = requests.request(
"POST",
create_user_url,
data=create_user_payload,
headers=headers,
auth=(jira_username, jira_password)
)
if create_user_response.status_code == 201:
print(f'用户{username}创建成功')
else:
print(f'用户{username}创建失败,状态码:{create_user_response.status_code}')
query = {
'groupname': group
}
add_user_to_group_payload = json.dumps( {
'name': username
} )
add_user_to_group_response = requests.request(
"POST",
add_user_to_group_url,
data=add_user_to_group_payload,
headers=headers,
params=query,
auth=(jira_username, jira_password)
)
if add_user_to_group_response.status_code == 201:
print(f'添加用户{username}到组{group}成功')
else:
print(f'添加用户{username}到组{group}失败,状态码:{add_user_to_group_response.status_code}')
print(f'{add_user_to_group_response.text}')
参考:
https://developer.atlassian.com/cloud/jira/platform/rest/v2/intro/#version
https://docs.atlassian.com/software/jira/docs/api/REST/8.16.1/
https://developer.atlassian.com/server/jira/platform/basic-authentication/
https://support.atlassian.com/atlassian-account/docs/manage-api-tokens-for-your-atlassian-account/
https://pypi.org/project/jira/
https://jira.readthedocs.io/
网友评论