有时我们需要定期批量设置一批项目为只读

下面是一个pthon脚本,可以一调用Jira的API 来批量修改文件权限

前提和准备工作

依赖的Python库

python >= 3.6

本脚本依赖的第三方库是 reqeusts 库,也是一个常用的http库,如果您的系统没有安装需要安装

pip install requests
CODE

Jira的信息

需要以下Jira信息

  • Jira 的BaseUrl地址
  • 管理员用户和密码
  • 设置好的只读的权限方案, 记下这个权限方案的id, 在权限方案的url中可以获取id
  • 准备一个project 列表的文件,每一个project Key 一行

Python脚本

# 
# Copy right by 北京范德敏特
# Authors: Yin Liang
#
import json
import requests
from requests.auth import HTTPBasicAuth
import argparse
import logging

def main():   
    # Log
    # logging.basicConfig(format='%(asctime)s :: %(levelname)s :: %(message)s', filename='output.log', level=logging.INFO)
    logging.basicConfig(format='%(asctime)s :: %(levelname)s :: %(message)s', level=logging.INFO)

    # arg parse
    parser = argparse.ArgumentParser(description='Bulk change Jira project permission scheme')  
    parser.add_argument('--baseUrl', '-j', required=True, 
        help='Jira base url: e.g. http://teams.devpod.cn:8080')
    parser.add_argument('--user', '-u', required=True,
        help='Jira admin user name')
    parser.add_argument('--password', '-p',  required=True,
        help='Jira admin password')
    parser.add_argument('--permissionScheme', '-s',type=int,  required=True,
        help='Jira Permission scheme Id')
    parser.add_argument('--projectsFile', '-f', type=argparse.FileType('r'), required=True,
        help='Pls input the project filse, each project Key as a line')
    args= parser.parse_args()
    logging.info(args)
    logging.info("starting")  

    # auth
    auth = HTTPBasicAuth(args.user, args.password)
    # call
    with args.projectsFile as f:
        for p in f:
            projectKey = p.strip()
            logging.info(f"Project {projectKey}")
            if projectKey:
                changePermissionScheme(args.baseUrl, auth, projectKey, args.permissionScheme)

def changePermissionScheme(baseUrl, auth, projectKey, permmisisonSchemeId):
    logging.info(f"call the chage scheme function for {projectKey}") 
    payload = {'id': permmisisonSchemeId}
    url = f"{baseUrl}/rest/api/latest/project/{projectKey}/permissionscheme"
    r = requests.put(url, auth=auth, json=payload)
    # Add some logs
    logging.info(f"Http response code is: {r.status_code}") 
    logging.info(f"Http response is: {r.text}") 
    code = r.status_code
    if code == 200:
        logging.info(f"Project {projectKey} permission changeed successfully")  

if __name__ == "__main__":
    main()
PY


脚本的使用

查看帮助

python ch_prj_perm.py --help

usage: ch_prj_perm.py [-h] --baseUrl BASEURL --user USER --password PASSWORD --permissionScheme PERMISSIONSCHEME --projectsFile PROJECTSFILE

Bulk change Jira project permission scheme

optional arguments:
  -h, --help            show this help message and exit
  --baseUrl BASEURL, -j BASEURL
                        Jira base url: e.g. http://teams.devpod.cn:8080
  --user USER, -u USER  Jira admin user name
  --password PASSWORD, -p PASSWORD
                        Jira admin password
  --permissionScheme PERMISSIONSCHEME, -s PERMISSIONSCHEME
                        Jira Permission scheme Id
  --projectsFile PROJECTSFILE, -f PROJECTSFILE
                        Pls input the project filse, each project Key as a line
BASH


使用举例

python ch_prj_perm.py -j 'http://teams.devpod.cn:8080'  -u admin -p password -s 11601 -f ./projects
BASH