가이드 제목은 [도구명] [내용]-하기 형태(ex Bitbucket Cloud 저장소 생성하기)로 입력한다.

이 문서에 대한 요약 /개요를 입력한다. (구글에서 검색되었을때 표시되는 문장)

이 문서는 Bitbucket Powershell Script 활용하여 Project 내 Repository Webhook 부여 가이드를 공유하기 위해 작성되었다.


가이드에 사용되는 도구 정보를 입력한다.

도구명Bitbucket, Powershell
버전6.10.1
OSWindows
비고추가할 내용 있을 경우 기입





첨부한  이미지 너비는 800px 을 넘기지 않는다.

Powershell Script 활용하여 Project 별로 Repository Webhook

프로젝트 키 찾기


Powershell


# 프로젝트 키로 조회(최대 200개 repository까지 반영, 기존에 webhook 하나라도 존재 시 실행 안함)
$projectkey=""

#Url 및 api 주소
$base_url = ""
$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 exists webhook"
    }
}
$LogArray





참조링크