Python给Gogs项目批量打Tag

Gogs的Rest API不能用,就用下面的方式来实现:

通过csv文件配置要打Tag的项目

1
Project Name,Release,Tag Name,Tag Branch,Title,Content
t1,Y,v1.1.0,master,,test1
t2,Y,v1.1.0,master,v1.1.0,test2
t3,Y,v1.1.0,master,v1.1.0,test3

实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
session = HTMLSession()

# Get crsf token
r = session.get(url=f'{URL}/user/login')
csrf = r.html.xpath('//input[@name="_csrf"]/@value', first=True)

# Login
data = {
'_csrf': csrf,
'user_name': USERNAME,
'password': PASSWORD,
'login_source': 2
}
session.post(url=f'{URL}/user/login', data=data)

# Get login crsf token
r = session.get(url=f'{URL}')
login_csrf = r.html.xpath('//meta[@name="_csrf"]/@content', first=True)

# Set tag for project from csv file
projects = get_test_csv_data('build.csv')
for project in projects:
if project[1] == 'Y':

build_data = {
'tag_name': project[2],
'tag_target': project[3],
'title': project[4],
'content': project[5],
'_csrf': login_csrf
}

print(f'===== set tag {project[2]} for {project[0]} =====')
print(build_data)
r = session.post(url=f'{URL}/{OWNER}/{project[0]}/releases/new', data=build_data)

# Verify set correctly
try:
res = session.get(f'{URL}/{OWNER}/{project[0]}/releases')
version = res.html.xpath('//ul[@id="release-list"]//a', first=True).text
if version == project[2]:
print(f'===== set tag {project[2]} for {project[0]} successfully =====')
except:
print(f'===== set tag {project[2]} for {project[0]} failed =====')
print()

结果

1
2
3
4
5
6
7
8
9
10
11
===== set tag v1.1.0 for t1 =====
{'tag_name': 'v1.1.0', 'tag_target': 'master', 'title': '', 'content': 'test1', '_csrf': 'iE2XUTv0z4jDvjr6HuPYTiFvOrc6MTYzNTUwMDA3OTgyNjgxNTU5Mw=='}
===== set tag v1.1.0 for t1 failed =====

===== set tag v1.1.0 for t2 =====
{'tag_name': 'v1.1.0', 'tag_target': 'master', 'title': 'v1.1.0', 'content': 'test2', '_csrf': 'iE2XUTv0z4jDvjr6HuPYTiFvOrc6MTYzNTUwMDA3OTgyNjgxNTU5Mw=='}
===== set tag v1.1.0 for t2 successfully =====

===== set tag v1.1.0 for t3 =====
{'tag_name': 'v1.1.0', 'tag_target': 'master', 'title': 'v1.1.0', 'content': 'test3', '_csrf': 'iE2XUTv0z4jDvjr6HuPYTiFvOrc6MTYzNTUwMDA3OTgyNjgxNTU5Mw=='}
===== set tag v1.1.0 for t3 successfully =====
唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!