From d2cb3bde8d1137dd678dbcea977683a1628ca432 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 | 53 +++++++++++--------- .github/workflows/main.yml | 3 +- scripts/script.py | 52 +++++++++++++++++++ 3 files changed, 83 insertions(+), 25 deletions(-) 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..2cba68d 100644 --- a/.github/workflows/actions/ado_tag/action.yml +++ b/.github/workflows/actions/ado_tag/action.yml @@ -16,30 +16,35 @@ inputs: runs: using: composite steps: - - name: Get PR's body for commit identified by ${{ inputs.commit-sha }} SHA - id: get-pr-body - env: + - 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 - 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 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 + python /home/runner/work/workflows_tests/workflows_tests/scripts/script.py --repository ${{ inputs.repository }} --commit-sha ${{ inputs.commit-sha }} + # - name: Get PR's body for commit identified by ${{ inputs.commit-sha }} SHA + # id: get-pr-body + # 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 + # 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 + # 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 diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9068f4a..e049e23 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -14,5 +14,6 @@ jobs: with: gh-token: ${{ secrets.GITHUB_TOKEN }} repository: ${{ github.repository }} - commit-sha: ${{ github.event.after }} + # commit-sha: ${{ github.event.after }} + commit-sha: 65953d1e6d491254ed704b80072d165ada95052f deploy_env: DEV \ No newline at end of file diff --git a/scripts/script.py b/scripts/script.py new file mode 100644 index 0000000..c4828f6 --- /dev/null +++ b/scripts/script.py @@ -0,0 +1,52 @@ +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 --repo {repository} --state merged --search {commit_sha}' + @constant + def ADO_TAG_PATTERN() -> re.Pattern: + return r"AB#\d{6}" + +CONST = _Const() + +def parseArguments(): + parser = argparse.ArgumentParser(description="Utility to tag ADO workitem with deployment environemnt", exit_on_error=False) + parser.add_argument('--gh-token', type=str, required=False, dest='gh_token') + parser.add_argument('--repository', type=str, required=True, dest='repository') + parser.add_argument('--commit-sha', type=str, required=True, dest='commit_sha') + return parser.parse_args() + +def extractAdoReferenceFromPR(args): + prData = subprocess.run( + CONST.GET_PR_DETAILS_CLI_COMMAND.format(repository = args.repository, commit_sha = args.commit_sha), + capture_output=True, shell=True, text=True) + + if prData.returncode != 0: + raise Exception('Failed to retrieve PR body for commit identified by {} SHA'.format(args.commit_sha)) + + prBody = json.loads(prData.stdout)[0]['body'] + adoTagsFromPrBody = re.findall(CONST.ADO_TAG_PATTERN, prBody) + + 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 main(): + args = parseArguments() + adoReference = extractAdoReferenceFromPR(args) + print(adoReference) + +if __name__ == '__main__': + main() \ No newline at end of file