Skip to content

Commit

Permalink
fix: add pull_request_url and branch_name to CodeBuild success response
Browse files Browse the repository at this point in the history
returning repository_url/repository_name, causing CFN to fail with:
  'Vendor response doesn't contain pull_request_url attribute'

After CodeBuild SUCCEEDED, query GitHub API /pulls?state=open on the
created repo to get the real PR URL and branch name.
  • Loading branch information
Your Name committed Apr 7, 2026
1 parent 5d3ff19 commit 26c6fe9
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions template_automation/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,11 +736,40 @@ def lambda_handler(event: dict, context) -> dict:
repo_base = github_api.rstrip("/").removesuffix("/api/v3")
github_org = os.environ.get("GITHUB_ORG_NAME", "SCT-Engineering")
repo_url = f"{repo_base}/{github_org}/{cfn_input.project_name}"

# Fetch the open PR so we can return pull_request_url and branch_name
# (both are required by the CloudFormation template Outputs section).
pull_request_url = "N/A"
branch_name = "repo-init"
try:
import urllib.request as _urllib
api_base = github_api.rstrip("/")
prs_url = f"{api_base}/repos/{github_org}/{cfn_input.project_name}/pulls?state=open"
req = _urllib.Request(prs_url, headers={"Authorization": f"token {github_token}"})
import ssl as _ssl
ctx = _ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = _ssl.CERT_NONE
with _urllib.urlopen(req, context=ctx, timeout=10) as resp:
prs = json.loads(resp.read())
if prs:
pull_request_url = prs[0].get("html_url", "N/A")
branch_name = prs[0].get("head", {}).get("ref", "repo-init")
logger.info(f"[{request_id}] Found PR: {pull_request_url} (branch: {branch_name})")
else:
logger.warning(f"[{request_id}] No open PRs found on {cfn_input.project_name}")
except Exception as pr_err:
logger.warning(f"[{request_id}] Could not fetch PR URL: {pr_err}")

response_data = {
"RepositoryUrl": repo_url,
"repository_url": repo_url,
"RepositoryName": cfn_input.project_name,
"repository_name": cfn_input.project_name,
"PullRequestUrl": pull_request_url,
"pull_request_url": pull_request_url,
"BranchName": branch_name,
"branch_name": branch_name,
"CodeBuildBuildId": build_id,
}
physical_resource_id = f"{cfn_input.project_name}-repository"
Expand Down

0 comments on commit 26c6fe9

Please sign in to comment.