背景
新配置一个标准项目权限方案模板,需要应用到所有项目中,项目数近千,一个个设置费时费力,需要利用脚本实现。
脚本
import okhttp3.*
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
// 配置参数
def baseUrl = "http://127.0.0.1:8080/rest/api/2"
def authToken = "Bearer MjUyNjIyNTEyNDQxOuXz1tDziol2WK0425iF7PSTNMkw"
def targetScheme = [id: "10101"]
def projectIds = ["10558", "10401", "10521", "10104", "10519", "10520", "10105", "10515", "10163", "10510", "10173", "10516", "10561", "10410", "10109", "10155", "10400", "10151", "10159", "10557", "10524", "10526", "10505", "10601", "10523","10502", "10522", "10147", "10525", "10140", "10170", "10171", "10169", "10168", "10136", "10412", "10156", "10413", "10130", "10142", "10411", "10135", "10518", "10503", "10149", "10146", "10560", "10559", "10133", "10600","10504", "10145", "10138", "10128", "10528", "10507", "10508", "10512", "10509", "10511", "10506", "10141", "10513", "10514", "10144", "10607", "10608", "10609", "10603", "10604", "10132", "10602", "10606", "10605", "10517","10137", "10406", "10501", "10134", "10562", "10148", "10131", "10527", "10139", "10143"]
// 共享的 HTTP 客户端实例
def client = new OkHttpClient.Builder()
.connectTimeout(30, java.util.concurrent.TimeUnit.SECONDS)
.readTimeout(60, java.util.concurrent.TimeUnit.SECONDS)
.build()
// 共享的 JSON 解析器
def jsonSlurper = new JsonSlurper()
// 处理结果统计
def results = [
total: projectIds.size(),
success: 0,
failed: [],
errors: []
]
// 遍历项目ID并更新权限方案
projectIds.each { projectId ->
log.info("开始处理项目ID: ${projectId}")
try {
// 验证项目ID格式
if (!projectId.isInteger()) {
throw new IllegalArgumentException("项目ID '${projectId}'不是有效的整数")
}
// 构建请求
def requestBody = RequestBody.create(
JsonOutput.toJson(targetScheme).toString(),
MediaType.get("application/json; charset=utf-8")
)
def request = new Request.Builder()
.url("${baseUrl}/project/${projectId}/permissionscheme")
.put(requestBody)
.addHeader("Authorization", authToken)
.addHeader("Content-Type", "application/json")
.build()
// 执行请求
def response = client.newCall(request).execute()
try {
if (response.isSuccessful()) {
def responseData = response.body().string()
log.info("项目 ${projectId} 权限方案更新成功,状态码: ${response.code()}")
// 解析响应数据
def jsonResponse = jsonSlurper.parseText(responseData)
log.debug("响应JSON: ${jsonResponse}")
results.success++
} else {
def errorBody = response.body()?.string() ?: "无错误详情"
def errorMsg = "项目 ${projectId} 更新失败,状态码: ${response.code()},错误信息: ${errorBody}"
log.error(errorMsg)
results.failed.add(projectId)
results.errors.add(errorMsg)
}
} finally {
// 确保响应资源被关闭
response.body()?.close()
}
} catch (Exception e) {
def errorMsg = "处理项目 ${projectId} 时发生异常: ${e.message}"
log.error(errorMsg, e)
results.failed.add(projectId)
results.errors.add(errorMsg)
}
}
// 输出处理摘要
log.warn("=" * 50)
log.warn("权限方案更新处理完成")
log.warn("总项目数: ${results.total}")
log.warn("成功: ${results.success}")
log.warn("失败: ${results.failed.size()}")
if (results.failed) {
log.warn("失败项目ID: ${results.failed.join(', ')}")
log.warn("错误详情:")
results.errors.eachWithIndex { error, idx ->
log.warn("${idx+1}. ${error}")
}
}
log.warn("=" * 50)
项目ID如何批量获取?
1.REST-API-根据项目名称搜索
2.如果需要根据项目分类做筛选,目前无法实现,只能手动在“所有项目”-》“项目分类”,一页页将分类项目的页面 table数据复制出来,利用ai抽出里面的id