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

feature/985 branch protection checks #1415

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
d42729e
feat: add new schema for check block resource under required_status_c…
TheQueenIsDead Dec 7, 2022
234cc25
feat: iterate provided `check` blocks and build array of RequiredStat…
TheQueenIsDead Dec 7, 2022
b99d568
feat: set default app_id to -1
TheQueenIsDead Dec 7, 2022
f482da2
feat: implement checks flattening for required status checks
TheQueenIsDead Dec 7, 2022
1c32a52
Add resource github_app_installation_repositories (#1376)
david-bain Dec 5, 2022
e2dadad
feat: adds new branch protection options for last reviewer and lockin…
wwsean08 Dec 5, 2022
a6565a9
feat(github_release): adding github_release resource and tests (#1122)
trentmillar Dec 6, 2022
b55408c
🚧 Workflows have changed
nickfloyd Dec 8, 2022
8959009
Issue template tweak (#1422)
kfcampbell Dec 9, 2022
702835a
feat: allow branch protection check app_id to be null
TheQueenIsDead Dec 8, 2022
3032488
chore: change branch protection flatten function to use GetAppID sdk …
TheQueenIsDead Dec 8, 2022
1deac0d
feat: change branch protection v3 utils to flatten and expand context…
TheQueenIsDead Dec 11, 2022
cb9c464
feat: change checks from it's own resource to a list of strings
TheQueenIsDead Dec 12, 2022
f9afc36
Merge branch 'main' into feature/985-branch-protection-contexts
TheQueenIsDead Dec 12, 2022
36e85f5
chore: resolve incorrect merge of main
TheQueenIsDead Dec 12, 2022
47cb610
chore: update deprecation notice on contexts array
TheQueenIsDead Dec 12, 2022
0d42123
chore(docs): Update branch_protection_v3 docs to mention the new `che…
TheQueenIsDead Dec 12, 2022
422f731
fix: Initialise literal empty slice of RequiredStatusCheck to mitigat…
TheQueenIsDead Dec 12, 2022
6cea244
Merge remote-tracking branch 'origin/feature/985-branch-protection-co…
TheQueenIsDead Dec 12, 2022
a2f7f5d
Merge branch 'main' into feature/985-branch-protection-contexts
kfcampbell Dec 13, 2022
6600e7b
chore(lint): resolve gosimple S1082 violation (errors.New => fmt.Errorf)
TheQueenIsDead Dec 13, 2022
b6e5785
Merge remote-tracking branch 'origin/feature/985-branch-protection-co…
TheQueenIsDead Dec 13, 2022
bfe46cd
Merge branch 'main' into feature/985-branch-protection-contexts
kfcampbell Dec 13, 2022
451b3a2
Merge branch 'main' into feature/985-branch-protection-contexts
TheQueenIsDead Dec 15, 2022
65264b5
Merge branch 'main' into feature/985-branch-protection-contexts
kfcampbell Jan 3, 2023
31989d9
Merge branch 'main' into feature/985-branch-protection-contexts
kfcampbell Jan 4, 2023
cdf30eb
chore: remove unused code comment
TheQueenIsDead Jan 10, 2023
d4bcf23
Merge remote-tracking branch 'origin/feature/985-branch-protection-co…
TheQueenIsDead Jan 10, 2023
7ee2fec
Merge branch 'main' into feature/985-branch-protection-contexts
TheQueenIsDead Jan 10, 2023
9c5eb96
Merge branch 'main' into feature/985-branch-protection-contexts
kfcampbell Jan 11, 2023
c71d939
Merge branch 'integrations:main' into feature/985-branch-protection-c…
TheQueenIsDead Jan 16, 2023
bd524a7
Merge branch 'main' into feature/985-branch-protection-contexts
kfcampbell Jan 17, 2023
5cb8eec
Merge branch 'main' into feature/985-branch-protection-contexts
kfcampbell Jan 18, 2023
736c824
Merge branch 'main' into feature/985-branch-protection-contexts
TheQueenIsDead Jan 20, 2023
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
8 changes: 8 additions & 0 deletions github/resource_github_branch_protection_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ func resourceGithubBranchProtectionV3() *schema.Resource {
Default: false,
},
"contexts": {
Type: schema.TypeSet,
Optional: true,
Deprecated: "GitHub is deprecating the use of `contexts`. Use a `checks` array instead.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"checks": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Expand Down
78 changes: 72 additions & 6 deletions github/resource_github_branch_protection_v3_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"
"errors"
"fmt"
"log"
"strings"

"github.com/google/go-github/v49/github"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
"strconv"
"strings"
)

func buildProtectionRequest(d *schema.ResourceData) (*github.ProtectionRequest, error) {
Expand Down Expand Up @@ -40,16 +40,34 @@ func buildProtectionRequest(d *schema.ResourceData) (*github.ProtectionRequest,

func flattenAndSetRequiredStatusChecks(d *schema.ResourceData, protection *github.Protection) error {
rsc := protection.GetRequiredStatusChecks()

if rsc != nil {
contexts := make([]interface{}, 0, len(rsc.Contexts))

// Contexts and Checks arrays to flatten into
var contexts []interface{}
var checks []interface{}

// TODO: Remove once contexts is fully deprecated.
// Flatten contexts
for _, c := range rsc.Contexts {
// Parse into contexts
contexts = append(contexts, c)
checks = append(contexts, c)
}

// Flatten checks
for _, chk := range rsc.Checks {
// Parse into contexts
contexts = append(contexts, chk.Context)
checks = append(contexts, fmt.Sprintf("%s:%d", chk.Context, chk.AppID))
}

return d.Set("required_status_checks", []interface{}{
map[string]interface{}{
"strict": rsc.Strict,
"strict": rsc.Strict,
// TODO: Remove once contexts is fully deprecated.
"contexts": schema.NewSet(schema.HashString, contexts),
"checks": schema.NewSet(schema.HashString, checks),
},
})
}
Expand Down Expand Up @@ -191,8 +209,56 @@ func expandRequiredStatusChecks(d *schema.ResourceData) (*github.RequiredStatusC
m := v.(map[string]interface{})
rsc.Strict = m["strict"].(bool)

// Initialise empty literal to ensure an empty array is passed mitigating schema errors like so:
// For 'anyOf/1', {"strict"=>true, "checks"=>nil} is not a null. []
rscChecks := []*github.RequiredStatusCheck{}

// TODO: Remove once contexts is deprecated
// Iterate and parse contexts into checks using -1 as default to allow checks from all apps.
kfcampbell marked this conversation as resolved.
Show resolved Hide resolved
contexts := expandNestedSet(m, "contexts")
rsc.Contexts = contexts
for _, c := range contexts {
appID := int64(-1) // Default
rscChecks = append(rscChecks, &github.RequiredStatusCheck{
Context: c,
AppID: &appID,
})
}

// Iterate and parse checks
checks := expandNestedSet(m, "checks")
for _, c := range checks {

// Expect a string of "context:app_id", allowing for the absence of "app_id"
parts := strings.SplitN(c, ":", 2)
var cContext, cAppId string
switch len(parts) {
case 1:
cContext, cAppId = parts[0], ""
case 2:
cContext, cAppId = parts[0], parts[1]
default:
return nil, fmt.Errorf("Could not parse check '%s'. Expected `context:app_id` or `context`", c)
}

var rscCheck *github.RequiredStatusCheck
if cAppId != "" {
// If we have a valid app_id, include it in the RSC
rscAppId, err := strconv.Atoi(cAppId)
if err != nil {
return nil, fmt.Errorf("Could not parse %v as valid app_id", cAppId)
}
rscAppId64 := int64(rscAppId)
rscCheck = &github.RequiredStatusCheck{Context: cContext, AppID: &rscAppId64}
} else {
// Else simply provide the context
rscCheck = &github.RequiredStatusCheck{Context: cContext}
}

// Append
rscChecks = append(rscChecks, rscCheck)
}
// Assign after looping both checks and contexts
rsc.Checks = rscChecks
}
return rsc, nil
}
Expand Down
11 changes: 7 additions & 4 deletions website/docs/r/branch_protection_v3.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ resource "github_branch_protection_v3" "example" {

```hcl
# Protect the main branch of the foo repository. Additionally, require that
# the "ci/travis" context to be passing and only allow the engineers team merge
# to the branch.
# the "ci/check" check ran by the Github Actions app is passing and only allow
# the engineers team merge to the branch.

resource "github_branch_protection_v3" "example" {
repository = github_repository.example.name
Expand All @@ -39,7 +39,9 @@ resource "github_branch_protection_v3" "example" {

required_status_checks {
strict = false
contexts = ["ci/travis"]
checks = [
"ci/check:824642007264"
]
}

required_pull_request_reviews {
Expand Down Expand Up @@ -88,7 +90,8 @@ The following arguments are supported:
`required_status_checks` supports the following arguments:

* `strict`: (Optional) Require branches to be up to date before merging. Defaults to `false`.
* `contexts`: (Optional) The list of status checks to require in order to merge into this branch. No status checks are required by default.
* `contexts`: [**DEPRECATED**] (Optional) The list of status checks to require in order to merge into this branch. No status checks are required by default.
* `checks`: (Optional) The list of status checks to require in order to merge into this branch. No status checks are required by default. Checks should be strings containing the context and app_id like so "context:app_id".

### Required Pull Request Reviews

Expand Down