Skip to content

Commit

Permalink
change to python
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasSielski committed Sep 12, 2024
1 parent 65953d1 commit c3523ad
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 25 deletions.
53 changes: 29 additions & 24 deletions .github/workflows/actions/ado_tag/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
3 changes: 2 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
40 changes: 40 additions & 0 deletions scripts/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import argparse
import subprocess
import json

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}'

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']
print(prBody)


def main():
args = parseArguments()
extractAdoReferenceFromPR(args)


if __name__ == '__main__':
main()

0 comments on commit c3523ad

Please sign in to comment.