...
코드 블럭 | ||||
---|---|---|---|---|
| ||||
# 프로젝트 키로 조회(최대 200개 repository까지 반영, 기존에 webhook 하나라도 존재 시 실행 안함)
$projectkey=""
#Url 및 api 주소
$base_url = "https://bitbucket.almdemo.curvc.com"
$get_repo_list = "/rest/api/latest/projects/${projectkey}/repos?limit=200"
# admin 계정 정보
$user =""
$password = ""
$LogArray = @()
# SSL 인증서 오류 일 경우, SSL 인증서 문제 없을 경우 해당 부분 필요 X
Add-Type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem){
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls, [Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::Tls12, [Net.SecurityProtocolType]::Tls13, [Net.SecurityProtocolType]::Ssl3
# 인증 인코딩
function Get-BasicAuthCreds {
param([string]$Username, [string]$Password)
$AuthString = "{0}:{1}" -f $Username,$Password
$AuthBytes = [System.Text.Encoding]::ASCII.GetBytes($AuthString)
return [Convert]::ToBase64String($AuthBytes)
}
$BasicCreds = Get-BasicAuthCreds -Username $user -Password $password;
# 저장소 정보 가져오기
$get_repo = Invoke-WebRequest -Uri "${base_url}${get_repo_list}" -Method Get -ContentType 'application/json' -Headers @{"Authorization"="Basic $BasicCreds"}
# 조회 값 UTF-8, 및 json으로 변환
$repos = [System.Text.Encoding]::UTF8.GetString($get_repo.RawContentStream.ToArray()) | ConvertFrom-Json
#저장소별로 webhook 주기
$repos.values | ForEach-Object{
$reposlug = $_.slug
$reponame = $_.name
$webhook_api = "/rest/api/latest/projects/${projectkey}/repos/${reposlug}/webhooks"
#기존 저장소 webhook 조회
$search = Invoke-WebRequest -Uri "${base_url}${webhook_api}" -Method Get -ContentType 'application/json' -Headers @{"Authorization"="Basic $BasicCreds"}
#기존 저장소 webhook 갯수 조회
if(($search.Content | ConvertFrom-Json).size -lt 1){
#고정된 값 넣을 시 해당 내용
$Body = '{"name":"pullRequest_opened", "events":["pr:opened"], "configuration":{}, "url": "https://test.test.com", "active":true, "statistice":{}}'
#json 타입에 변수 지정 시 변수 사이에 '과 + 사용
#$Body = '{"name":"' + ${test} + '", "events":["pr:opened"], "configuration":{}, "url": "https://test.test.com", "active":true, "statistice":{}}'
#webhook 넣기
Invoke-WebRequest -Uri "${base_url}${webhook_api}" -Method Post -ContentType 'application/json' -Headers @{"Authorization"="Basic $BasicCreds"} -Body $Body
$LogArray += "Action. Repository Name: ${reponame} make webhook"
} else {
$LogArray += "No Action. Repository Name: ${reponame} already exist webhook"
}
}
$LogArray |
...