From cc2333e658de813a03f600bacc04415dd8a52537 Mon Sep 17 00:00:00 2001 From: Lukasz Sielski Date: Thu, 12 Sep 2024 11:14:24 +0200 Subject: [PATCH] change to python --- .github/workflows/actions/ado_tag/action.yml | 49 ++++++++------- .github/workflows/deploy.yml | 22 +++++++ .github/workflows/main.yml | 15 ++--- scripts/script.py | 64 ++++++++++++++++++++ test200.txt | 1 + 5 files changed, 116 insertions(+), 35 deletions(-) create mode 100644 .github/workflows/deploy.yml create mode 100644 scripts/script.py diff --git a/.github/workflows/actions/ado_tag/action.yml b/.github/workflows/actions/ado_tag/action.yml index 45f1582..3c520f4 100644 --- a/.github/workflows/actions/ado_tag/action.yml +++ b/.github/workflows/actions/ado_tag/action.yml @@ -4,42 +4,41 @@ inputs: gh-token: description: GitHub token necessary for interacting with GitHub CLI required: true - repository: - description: Repsoitory name in form of OWNER/REPOSITORY_NAME - required: true commit-sha: description: Commit SHA by which related PR is searched for AB# tag required: true + azure-pat: + description: PAT + required: true deploy_env: description: Environment to which changes were deployed required: true runs: using: composite steps: - - name: Get PR's body for commit identified by ${{ inputs.commit-sha }} SHA - id: get-pr-body + - shell: bash env: + EVENT_CONTEXT: ${{ toJSON(github.event) }} + run: $EVENT_CONTEXT + - env: GH_TOKEN: ${{ inputs.gh-token }} - run: | - PRBODY=$(gh pr list --json body --repo ${{ inputs.repository }} --state merged --search ${{ inputs.commit-sha }} --jq '.[].body') - echo "PRs body: $PRBODY" - echo "PRBODY=${PRBODY}" >> $GITHUB_ENV + AZURE_DEVOPS_EXT_PAT: ${{ inputs.azure-pat }} + if: ${{ github.event_name == 'workflow_run' }} shell: bash - - name: Extract work item reference number from PR's body - id: extract-reference-number run: | - reference_num=$(echo $PRBODY | sed -n "s/^.*AB#\([0-9]*\).*$/\1/p") - if [ -z "$reference_num" ] - then - echo "Reference number not found!" - exit 1 - else - echo "Reference number is $reference_num" - echo "REFERENCE=${reference_num}" >> $GITHUB_ENV - fi + python /home/runner/work/workflows_tests/workflows_tests/scripts/script.py --commit-sha ${{ inputs.commit-sha }} --deploy_env ${{ inputs.deploy_env }} + - if: ${{ github.event_name == 'workflow_dispatch' }} shell: bash - - name: Add env tag for workitem - id: tag-workitem - run: | - echo "Going to add tag ${{ inputs.deploy_env }} to a work item identified by $REFERENCE number." - shell: bash \ No newline at end of file + run: echo 'workflow dispatch xD' + # - id: failing_step + # continue-on-error: true + # shell: bash + # run: exit 0 + # - id: test_failure + # if: ${{ steps.failing_step.outcome == 'failure' }} + # shell: bash + # run: echo "Failed" + # - id: test_success + # if: ${{ steps.failing_step.outcome == 'success' }} + # shell: bash + # run: echo "Success" diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..9972e5e --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,22 @@ +name: deploy + +on: + workflow_dispatch: + workflow_run: + workflows: + - build + + +jobs: + tag-azure-board-work-item: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + name: Checkout repository + - uses: ./.github/workflows/actions/ado_tag + with: + gh-token: ${{ secrets.GITHUB_TOKEN }} + # commit-sha: ${{ github.event.after }} + commit-sha: 65953d1e6d491254ed704b80072d165ada95052f + azure-pat: ${{ secrets.ADO_PAT }} + deploy_env: DEV \ No newline at end of file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9068f4a..83ebe6c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,18 +1,13 @@ -name: test +name: build on: + workflow_dispatch: push: branches: [main] jobs: - tag-azure-board-work-item: + build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - name: Checkout repository - - uses: ./.github/workflows/actions/ado_tag - with: - gh-token: ${{ secrets.GITHUB_TOKEN }} - repository: ${{ github.repository }} - commit-sha: ${{ github.event.after }} - deploy_env: DEV \ No newline at end of file + - shell: bash + run: echo "Build" \ No newline at end of file diff --git a/scripts/script.py b/scripts/script.py new file mode 100644 index 0000000..aa98767 --- /dev/null +++ b/scripts/script.py @@ -0,0 +1,64 @@ +import argparse +import subprocess +import json +import re + +def constant(f): + def fset(self, value): + raise TypeError + def fget(self): + return f() + return property(fget, fset) + +class _Const(object): + @constant + def GET_PR_DETAILS_CLI_COMMAND() -> str: + return 'gh pr list --json body --state merged --search {commit_sha}' + @constant + def ADO_TAG_PATTERN() -> re.Pattern: + return r"AB#\d{6}" + @constant + def ADO_TAGGING_COMMAND() -> str: + return 'az boards work-item update --id {work_item_id} --org https://dev.azure.com/lukaszadamsielski0187 --fields "System.Tags={deploy_env}" --output json' + +CONST = _Const() + +def parseArguments(): + parser = argparse.ArgumentParser(description="Utility to tag ADO workitem with deployment environemnt", exit_on_error=False) + parser.add_argument('--commit-sha', type=str, required=True, dest='commit_sha') + parser.add_argument('--deploy_env', type=str, required=True, dest='deploy_env') + return parser.parse_args() + +def extractWorkItemIdFromPR(commitSha): + extractPrBodyCommand = CONST.GET_PR_DETAILS_CLI_COMMAND.format(commit_sha = commitSha) + + prBodySubprocessResponse = subprocess.run(extractPrBodyCommand, capture_output=True, shell=True, text=True) + if prBodySubprocessResponse.returncode != 0: + raise Exception('Failed to retrieve PR body for commit identified by {} SHA! Reason: {}'.format(commitSha, prBodySubprocessResponse.stderr)) + adoTagsFromPrBody = re.findall(CONST.ADO_TAG_PATTERN, json.loads(prBodySubprocessResponse.stdout)[0]['body']) + if len(adoTagsFromPrBody) != 1: + raise Exception('Could not retrieve ADO tag from PR body or there is more than one!') + + return adoTagsFromPrBody[0].split('#')[1] + +def tagAdoWorkItem(workItemId: str, deploymentEnv: str): + adoTaggingCommand = CONST.ADO_TAGGING_COMMAND.format(work_item_id = workItemId, deploy_env = deploymentEnv) + + adoTaggingSubprocessResponse = subprocess.run(adoTaggingCommand, capture_output=True, shell=True, text=True) + if adoTaggingSubprocessResponse.returncode != 0: + raise Exception('Failed to tag ADO work item identified by id [{}] with deployment env [{}]! Reason: {}'.format(workItemId, deploymentEnv, adoTaggingSubprocessResponse.stderr)) + print('Successfully tagged work item identified by id [{}] with deployment env [{}].'.format(workItemId, deploymentEnv)) + +def main(): + args = parseArguments() + commitSha = args.commit_sha + deploymentEnv = args.deploy_env + tagAdoWorkItem(extractWorkItemIdFromPR(commitSha), deploymentEnv) + +if __name__ == '__main__': + try: + main() + exit(0) + except Exception as e: + print(f'An error occured: {e}') + exit(1) diff --git a/test200.txt b/test200.txt index 5ad28e2..57f1098 100644 --- a/test200.txt +++ b/test200.txt @@ -1 +1,2 @@ haha +s2 \ No newline at end of file