Skip to content

Commit

Permalink
Merge pull request #38 from deploymenttheory/dev-testing
Browse files Browse the repository at this point in the history
Dev testing
  • Loading branch information
thejoeker12 authored Oct 31, 2024
2 parents f82be6a + 051a1d0 commit 80c2ea7
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 25 deletions.
35 changes: 15 additions & 20 deletions .github/scripts/update_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@

import os
import json
from github import Github
import github
from github.GithubException import GithubException
import github.PullRequest

REPO_PATH = "deploymenttheory/terraform-demo-jamfpro-v2"

# Env var pickup and validation
TOKEN = os.environ.get("GITHUB_TOKEN")
DROPFILE_PATH = os.environ.get("ARTIFACT_PATH")
TYPE = os.environ.get("RUN_TYPE")

ENV_VARS = [TOKEN, DROPFILE_PATH, TYPE]

if any(i == "" for i in ENV_VARS):
raise KeyError(f"one or more env vars are empty: {ENV_VARS}")

GH = Github(TOKEN)

# Global GH connection
GH = github.Github(TOKEN)

def open_drop_file() -> dict:
"""opens the drop file"""
Expand All @@ -24,22 +29,13 @@ def open_drop_file() -> dict:

def get_pr():
"""
Get existing PR or create a new one between branches.
Args:
repo_name (str): Repository name in format "owner/repo"
head_branch (str): Branch containing changes
base_branch (str): Target branch for PR (default: main)
github_token (str): GitHub access token
Returns:
github.PullRequest.PullRequest: Pull request object
Gets PR
"""
file = open_drop_file()
target_pr_id = file["pr_number"]
print(f"LOG: {target_pr_id}")
try:
repo = GH.get_repo("deploymenttheory/terraform-demo-jamfpro-v2")
repo = GH.get_repo(REPO_PATH)
pr = repo.get_pull(int(target_pr_id))

if pr:
Expand All @@ -57,7 +53,7 @@ def get_pr():



def update_pr_with_text(pr):
def update_pr_with_text(pr: github.PullRequest):
"""
Add a comment to the PR with specified text.
Expand All @@ -66,13 +62,12 @@ def update_pr_with_text(pr):
message (str): Comment text to add
"""
comments = []
with open(DROPFILE_PATH + "/outputs.json", "r", encoding="UTF-8") as f:
json_data = json.load(f)
json_data = open_drop_file()

if TYPE == "plan":
comments.append(json.dumps(json_data["plan_output"], indent=2))
if TYPE == "plan":
comments.append(json.dumps(json_data["plan_output"], indent=2))

comments.append(json.dumps(json_data, indent=2))
comments.append(json.dumps(json_data, indent=2))

try:
for c in comments:
Expand Down
55 changes: 55 additions & 0 deletions .github/scripts/update_release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""script to update the text on a release"""

import os
import json
import github
from github.GithubException import GithubException

REPO_PATH = "deploymenttheory/terraform-demo-jamfpro-v2"

TOKEN = os.environ.get("GITHUB_TOKEN")
DROPFILE_PATH = os.environ.get("ARTIFACT_PATH")
GIT_TAG = os.environ.get("RELEASE_TAG")

ENV_VARS = [TOKEN, DROPFILE_PATH]

if any (i == "" for i in ENV_VARS):
raise KeyError(f"one or more env vars are empty: {ENV_VARS}")


GH = github.Github(TOKEN)

def open_drop_file() -> dict:
"""opens the drop file"""
with open(DROPFILE_PATH + "/outputs.json", "r", encoding="UTF-8") as f:
return json.load(f)


def get_update_release():
"""gets a tag"""
try:
repo = GH.get_repo(REPO_PATH)
release = repo.get_release(GIT_TAG)

except GithubException as e:
print(f"GitHub API error: {e}")
raise
except Exception as e:
print(f"Unexpected error: {e}")
raise

file = open_drop_file()
message = f"{release.body}\nApply Result:\n{json.dumps(file, indent=2)}"
release.update_release(
message=message
)


def main():
get_update_release()


if __name__ == "__main__":
main()


31 changes: 26 additions & 5 deletions .github/workflows/demo_3_apply_production_temp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,32 @@ jobs:
uses: hashicorp/tfc-workflows-github/actions/apply-run@v1.3.1
with:
token: ${{ secrets.TF_API_KEY }}
run: ${{ steps.terraform-plan.outputs.run_id }}

run: ${{ steps.terraform-plan.outputs.run_id }}





- name: Save outputs to file
run: |
cat << EOF > ${{ vars.OUTPUTS_FILE_FN }}
{
"status": "${{ steps.terraform-apply.outputs.status }}"
}
EOF

- name: Upload outputs json file as artifact
uses: actions/upload-artifact@v4
with:
name: ${{ vars.APPLY_OUTPUT_ARTIFACT_NAME }}
path: ${{ vars.OUTPUTS_FILE_FN }}
retention-days: 0


update-release:
name: Deposit plan output on trigger release
needs: terraform-upload-plan-apply
uses: ./.github/workflows/update_release.yml
with:
outputs-payload: ${{ vars.APPLY_OUTPUT_ARTIFACT_NAME }}
git-tag: ${{ github.ref_name }}


47 changes: 47 additions & 0 deletions .github/workflows/update_release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Add PR Comment

on:
workflow_call:
inputs:
outputs-payload:
required: true
type: string

git-tag:
required: true
type: string

jobs:

update-release:
runs-on: ubuntu-latest
permissions:
packages: read
pull-requests: write
contents: write

container:
image: ghcr.io/${{ github.repository }}/python:latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Download artifact
id: download-artifact
uses: actions/download-artifact@v4
with:
name: ${{ inputs.outputs-payload }}

- name: Run pr update python utility
run: python .github/scripts/update_pr.py
env:
ARTIFACT_PATH: ${{ steps.download-artifact.outputs.download-path }}
GIT_TAG: ${{ inputs.git-tag }}





4 changes: 4 additions & 0 deletions workload/terraform/jamfpro/categories.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ resource "jamfpro_category" "category_4" {

resource "jamfpro_category" "category_5" {
name = "Marketing - JL Demo V2 - 22"
}

resource "jamfpro_category" "category_6" {
name = "Marketing - JL Demo V2 - 23"
}

0 comments on commit 80c2ea7

Please sign in to comment.