Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop PRs to same repo interfering with each other #89

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions terraform-plans/modules/GitHub/templates/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,16 @@ locals {
}
}

locals {
pr_branch = var.branch == "main" ? var.pr_branch_prefix : "${var.pr_branch_prefix}-${var.branch}"
}

# Create a new branch only if there are changed files
resource "github_branch" "managed_files_branch" {
count = length(local.changed_files) > 0 ? 1 : 0

repository = var.repository
branch = var.pr_branch
branch = local.pr_branch
source_branch = var.branch
}

Expand All @@ -79,11 +83,11 @@ data "github_repository_pull_requests" "open" {
}

locals {
pr_exists = length([for pr in data.github_repository_pull_requests.open.results : pr if pr.head_ref == var.pr_branch]) > 0
pr_exists = length([for pr in data.github_repository_pull_requests.open.results : pr if pr.head_ref == local.pr_branch]) > 0
}

locals {
existing_pr = local.pr_exists ? [for pr in data.github_repository_pull_requests.open.results : pr if pr.head_ref == var.pr_branch][0] : null
existing_pr = local.pr_exists ? [for pr in data.github_repository_pull_requests.open.results : pr if pr.head_ref == local.pr_branch][0] : null
}

locals {
Expand Down Expand Up @@ -125,7 +129,7 @@ output "pr_created" {
}

output "pr_branch" {
value = var.pr_branch
value = local.pr_branch
}

output "pr_url" {
Expand Down
4 changes: 2 additions & 2 deletions terraform-plans/modules/GitHub/templates/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ variable "branch" {
default = "main"
}

variable "pr_branch" {
variable "pr_branch_prefix" {
type = string
description = "Pull request branch name."
description = "Pull request branch name prefix."
default = "automation/update-managed-files"
}

Expand Down