diff --git a/README.md b/README.md index be67190bf009d320a7461282f03c21fc9149d15b..e8136ee3277602ca42ad87f99c1562d075d4d954 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,3 @@ - jq Should work on all POSIX-compatible systems (`#!/usr/bin/env`) - -## TODO - -- allow to specify repo_id so we needn't to query it again -- same as the above one but for build-definition-id diff --git a/apply.sh b/apply.sh index ce74f4e16022c652e0a02ce8692b0f2510284ba6..ed207fdab75356e96051dae66d912704fcf28efc 100755 --- a/apply.sh +++ b/apply.sh @@ -1,107 +1,148 @@ #!/usr/bin/env bash -if [ $# != 5 ] -then +show_usage() { cat <<-EOF Usage: -pat=... $0 {org_url} {proj_name} {repo_name} {branch} {pipeline_name} +$0 --org-url {org_url} --proj-name {proj_name} [--repo-name {repo_name} | --repo-id {repo_id}] [--branch {branch}] [--pipeline-name {pipeline_name} | --build-definition-id {build_definition_id}] Examples: - pat=... $0 https://dev.azure.com/azvse aztest aztest bensl/tmpbuild/1201 Overlake-Build-PullRequest +$0 --org-url https://dev.azure.com/azvse --proj-name aztest --repo-name aztest --branch bensl/tmpbuild/1201 --pipeline-name Overlake-Build-PullRequest +$0 --org-url https://dev.azure.com/azvse --proj-name aztest --repo-id a4822210-511f-427f-a36d-26a14c29cc89 --branch bensl/tmpbuild/1201 --build-definition-id 2 EOF - exit 1 -fi - -export org_url=$1 -export proj_name=$2 -export repo_name=$3 -export branch=$4 -export pipeline_name=$5 - -if ! [[ ${pat} ]] -then - echo please set pat as your personalAccessToken - exit 2 -fi +} -function get_repo_id { - az repos list \ - --org "${org_url}" \ - --query '[].{name:name, id:id}' \ - --proj "${proj_name}" | \ - jq -r ".[] | select(.name == \"${repo_name}\") | .id" +get_repo_id() { + az repos list \ + --org "${org_url}" \ + --query '[].{name:name, id:id}' \ + --proj "${proj_name}" | \ + jq -r ".[] | select(.name == \"${repo_name}\") | .id" } # https://unix.stackexchange.com/a/383166 # export function to subshells for bash typeset -fx get_repo_id -function get_pipeline_definition_id { - az pipelines build definition show \ - --name "${pipeline_name}" \ - --org "${org_url}" \ - --proj "${proj_name}" \ - --query 'id' +get_build_definition_id() { + az pipelines build definition show \ + --name "${pipeline_name}" \ + --org "${org_url}" \ + --proj "${proj_name}" \ + --query 'id' } -typeset -fx get_pipeline_definition_id +typeset -fx get_build_definition_id + +while [[ $# -gt 0 ]]; do + case "$1" in + --org-url) + org_url=$2 + shift 2 + ;; + --proj-name) + proj_name=$2 + shift 2 + ;; + --repo-name) + repo_name=$2 + shift 2 + ;; + --repo-id) + repo_id=$2 + shift 2 + ;; + --branch) + branch=$2 + shift 2 + ;; + --pipeline-name) + pipeline_name=$2 + shift 2 + ;; + --build-definition-id) + build_definition_id=$2 + shift 2 + ;; + *) + show_usage + exit 1 + ;; + esac +done -function print_policy_list() { - az repos policy list \ - --branch ${branch} \ - --repository-id "${repo_id}" \ - --org "${org_url}" \ - --project "${proj_name}" +if [[ ! ${org_url} || ! ${proj_name} || (! ${repo_name} && ! ${repo_id}) || (! ${pipeline_name} && ! ${build_definition_id}) ]]; then + show_usage + exit 1 +fi + +if [[ ! "${pat}" ]]; then + echo "Please set 'pat' as your personalAccessToken" + exit 2 +fi + +if [[ ! ${repo_id} && ${repo_name} ]]; then + repo_id=$(get_repo_id) + echo repo_id: ${repo_id} +fi + +if [[ ! ${build_definition_id} && ${pipeline_name} ]]; then + build_definition_id=$(get_build_definition_id) + echo build_definition_id: ${build_definition_id} +fi + +print_policy_list() { + az repos policy list \ + --branch ${branch} \ + --repository-id "${repo_id}" \ + --org "${org_url}" \ + --project "${proj_name}" } -function setup_build_policy() { - curl -X POST "${org_url}"/"${proj_name}"/_apis/policy/configurations?api-version=7.2-preview.1 \ - -H "Authorization: Basic "$(echo -n :${pat} | base64) \ - -H 'Content-Type: application/json' \ - -d \ - '{ - "isEnabled": true, - "isBlocking": false, - "type": { - "id": "fa4e907d-c16b-4a4c-9dfa-4906e5d171dd" - }, - "settings": { - "allowDownvotes": false, - "blockLastPusherVote": true, - "creatorVoteCounts": false, - "minimumApproverCount": 2, - "requireVoteOnEachIteration": true, - "requireVoteOnLastIteration": true, - "resetOnSourcePush": false, - "resetRejectionsOnSourcePush": false, - "scope": [ - { - "repositoryId": "'${repo_id}'", - "refName": "refs/heads/'${branch}'", - "matchKind": "exact" +setup_build_policy() { + curl -X POST "${org_url}/${proj_name}/_apis/policy/configurations?api-version=7.2-preview.1" \ + -H "Authorization: Basic $(echo -n :${pat} | base64)" \ + -H 'Content-Type: application/json' \ + -d ' + { + "isEnabled": true, + "isBlocking": false, + "type": { + "id": "fa4e907d-c16b-4a4c-9dfa-4906e5d171dd" + }, + "settings": { + "allowDownvotes": false, + "blockLastPusherVote": true, + "creatorVoteCounts": false, + "minimumApproverCount": 2, + "requireVoteOnEachIteration": true, + "requireVoteOnLastIteration": true, + "resetOnSourcePush": false, + "resetRejectionsOnSourcePush": false, + "scope": [ + { + "repositoryId": "'${repo_id}'", + "refName": "refs/heads/'${branch}'", + "matchKind": "exact" + } + ] } - ] - } - }' + }' } -function setup_build_validation() { - build_definition_id=$( get_pipeline_definition_id ) - az repos policy build create \ - --blocking true \ - --branch ${branch} \ - --build-definition-id ${build_definition_id}\ - --display-name "" \ - --enabled true \ - --manual-queue-only false \ - --queue-on-source-update-only true \ - --valid-duration 720 \ - --repository-id ${repo_id} \ - --org ${org_url} \ - --project ${proj_name} +setup_build_validation() { + az repos policy build create \ + --blocking true \ + --branch ${branch} \ + --build-definition-id ${build_definition_id} \ + --display-name "" \ + --enabled true \ + --manual-queue-only false \ + --queue-on-source-update-only true \ + --valid-duration 720 \ + --repository-id ${repo_id} \ + --org ${org_url} \ + --project ${proj_name} } -export repo_id=$( get_repo_id ) - -# print_policy_list +print_policy_list setup_build_policy setup_build_validation \ No newline at end of file