1

有没有办法使用 jenkins api 修改用户详细信息(特别是电子邮件)?将修改后的 json 文件发布到 {root}/user/{username}/api/json 似乎不会修改底层数据:

import urllib2
import json 

root = 'your_url'
username = 'your_username'
user_url = root +'/user/{username}/api/json'.format(username=username)

orig_d = json.loads((urllib2.urlopen(user_url).read()))

d = dict(orig_d)

d['fullName'] = 'XXXXX'
json_data = json.dumps(d)

request = urllib2.Request(user_url)
request.add_header('Content-type', 'application/json')
new_d = json.loads(urllib2.urlopen(request, json_data).read())

print new_d, '\n', orig_d
assert orig_d!=new_d, 'They are equal!'
4

1 回答 1

0

在查看 jenkins 源代码model/User.java之后,看起来用户模型支持的唯一“POST”配置是通过doConfigSubmit()成员函数完成的。所以我在jenkins_url/user/{username}/configSubmit. 以下片段对我有用:

import urllib2, urllib
import json 

root = jenkins_url
username = username
user_url = root +'/user/{username}/api/json'.format(username=username)
config_url = root + '/user/{username}/configSubmit'.format(username=username)

d = json.loads((urllib2.urlopen(user_url).read()))

fullname = d['fullName']
description = d['description']
new_email = 'new_user_email'

post_d = {"userProperty1": {"address": new_email}}

request = urllib2.Request(config_url)
values = {'json': json.dumps(post_d),
          'fullName': fullname,
          'email.address': new_email,
          'description': description}

data = urllib.urlencode(values)

response = urllib2.urlopen(request, data).read()
于 2013-06-04T22:16:05.183 回答