Skip to content

Commit

Permalink
Control draft pr trigger (#907)
Browse files Browse the repository at this point in the history
* Introduce draft_pull_request_enabled on TriggerMapItemModel and update String func and related tests

* Start migrating CheckDuplicatedTriggerMapItems tests

* Update trigger item duplication test to support draft pr enabled status

* Introduce draft-pr flag for trigger and trigger-check commands

* Code cleanup

* Typo fix

* Migrate trigger map item tests to table driven tests, add draft pr match and string tests

* Update trigger map item tests

* Add integration tests for draft pr trigger and fix run_and_trigger_params override

* Resolve TODOs

* Improve TriggerMapModel.checkDuplicatedTriggerMapItems
  • Loading branch information
godrei authored Jan 16, 2024
1 parent 0932361 commit 4dfbba7
Show file tree
Hide file tree
Showing 14 changed files with 834 additions and 682 deletions.
2 changes: 1 addition & 1 deletion _tests/integration/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func toBase64(t *testing.T, str string) string {
return string(bytes)
}

func toJSON(t *testing.T, stringStringMap map[string]string) string {
func toJSON(t *testing.T, stringStringMap map[string]interface{}) string {
bytes, err := json.Marshal(stringStringMap)
require.NoError(t, err)
return string(bytes)
Expand Down
16 changes: 8 additions & 8 deletions _tests/integration/json_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func Test_JsonParams(t *testing.T) {

t.Log("run test")
{
config := map[string]string{
config := map[string]interface{}{
"config": configPth,
"workflow": "json_params_test_target",
}
Expand All @@ -24,7 +24,7 @@ func Test_JsonParams(t *testing.T) {

t.Log("run test - param override")
{
config := map[string]string{
config := map[string]interface{}{
"config": configPth,
"workflow": "exit_code_test_fail",
}
Expand All @@ -36,7 +36,7 @@ func Test_JsonParams(t *testing.T) {

t.Log("trigger test")
{
config := map[string]string{
config := map[string]interface{}{
"config": configPth,
"pattern": "json_params_test_target",
}
Expand All @@ -48,7 +48,7 @@ func Test_JsonParams(t *testing.T) {

t.Log("trigger test - param override")
{
config := map[string]string{
config := map[string]interface{}{
"config": configPth,
"pattern": "exit_code_test_fail",
}
Expand All @@ -60,7 +60,7 @@ func Test_JsonParams(t *testing.T) {

t.Log("run test base64")
{
config := map[string]string{
config := map[string]interface{}{
"config": configPth,
"workflow": "json_params_test_target",
}
Expand All @@ -72,7 +72,7 @@ func Test_JsonParams(t *testing.T) {

t.Log("trigger test base64")
{
config := map[string]string{
config := map[string]interface{}{
"config": configPth,
"pattern": "json_params_test_target",
}
Expand All @@ -84,7 +84,7 @@ func Test_JsonParams(t *testing.T) {

t.Log("trigger check test")
{
config := map[string]string{
config := map[string]interface{}{
"config": configPth,
"pattern": "json_params_test_target",
"format": "json",
Expand All @@ -98,7 +98,7 @@ func Test_JsonParams(t *testing.T) {

t.Log("trigger check test - param override")
{
config := map[string]string{
config := map[string]interface{}{
"config": configPth,
"pattern": "json_params_test_target",
"format": "raw",
Expand Down
51 changes: 40 additions & 11 deletions _tests/integration/new_trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func Test_NewTrigger(t *testing.T) {

t.Log("deprecated trigger test")
{
config := map[string]string{
config := map[string]interface{}{
"config": configPth,
"pattern": "deprecated_code_push",
"format": "json",
Expand All @@ -26,7 +26,7 @@ func Test_NewTrigger(t *testing.T) {

t.Log("deprecated trigger test - PR mode")
{
config := map[string]string{
config := map[string]interface{}{
"config": configPth,
"pattern": "deprecated_pr",
"format": "json",
Expand All @@ -40,7 +40,7 @@ func Test_NewTrigger(t *testing.T) {

t.Log("deprecated trigger test - pipeline")
{
config := map[string]string{
config := map[string]interface{}{
"config": configPth,
"pattern": "deprecated_pipeline",
"format": "json",
Expand All @@ -54,7 +54,7 @@ func Test_NewTrigger(t *testing.T) {

t.Log("new trigger test - code push")
{
config := map[string]string{
config := map[string]interface{}{
"config": configPth,
"push-branch": "code_push",
"format": "json",
Expand All @@ -68,7 +68,7 @@ func Test_NewTrigger(t *testing.T) {

t.Log("new trigger test - code push - no match")
{
config := map[string]string{
config := map[string]interface{}{
"config": configPth,
"push-branch": "no_match",
"format": "json",
Expand All @@ -81,7 +81,7 @@ func Test_NewTrigger(t *testing.T) {

t.Log("new trigger test - pull request - defined source and target pattern")
{
config := map[string]string{
config := map[string]interface{}{
"config": configPth,
"pr-source-branch": "pr_source",
"pr-target-branch": "pr_target",
Expand All @@ -96,7 +96,7 @@ func Test_NewTrigger(t *testing.T) {

t.Log("new trigger test - pull request - defined source and target pattern - no match")
{
config := map[string]string{
config := map[string]interface{}{
"config": configPth,
"pr-source-branch": "no_match",
"pr-target-branch": "no_match",
Expand All @@ -110,7 +110,7 @@ func Test_NewTrigger(t *testing.T) {

t.Log("new trigger test base64 - pull request - defined source and target pattern")
{
config := map[string]string{
config := map[string]interface{}{
"config": configPth,
"pr-source-branch": "pr_source",
"pr-target-branch": "pr_target",
Expand All @@ -125,7 +125,7 @@ func Test_NewTrigger(t *testing.T) {

t.Log("new trigger test - pull request - defined target pattern")
{
config := map[string]string{
config := map[string]interface{}{
"config": configPth,
"pr-source-branch": "pr_source",
"pr-target-branch": "pr_target_only",
Expand All @@ -140,7 +140,7 @@ func Test_NewTrigger(t *testing.T) {

t.Log("new trigger test - pull request - defined source pattern")
{
config := map[string]string{
config := map[string]interface{}{
"config": configPth,
"pr-source-branch": "pr_source_only",
"pr-target-branch": "pr_target",
Expand All @@ -155,7 +155,7 @@ func Test_NewTrigger(t *testing.T) {

t.Log("new trigger test - pipeline")
{
config := map[string]string{
config := map[string]interface{}{
"config": configPth,
"push-branch": "pipeline_code_push",
"format": "json",
Expand All @@ -166,4 +166,33 @@ func Test_NewTrigger(t *testing.T) {
require.NoError(t, err, out)
require.Equal(t, `{"pipeline":"pipeline_code_push","push-branch":"pipeline_code_push"}`, out)
}

t.Log("draft pr control test - draft pr disabled - ready to review pr trigger")
{
config := map[string]interface{}{
"config": configPth,
"pr-source-branch": "no_draft_pr",
"format": "json",
}

cmd := command.New(binPath(), "trigger-check", "--json-params", toJSON(t, config))
out, err := cmd.RunAndReturnTrimmedCombinedOutput()
require.NoError(t, err, out)
require.Equal(t, `{"pr-source-branch":"no_draft_pr","workflow":"no_draft_pr"}`, out)
}

t.Log("draft pr control test - draft pr disabled - draft pr trigger")
{
config := map[string]interface{}{
"config": configPth,
"pr-source-branch": "no_draft_pr",
"draft-pr": true,
"format": "json",
}

cmd := command.New(binPath(), "trigger-check", "--json-params", toJSON(t, config))
out, err := cmd.RunAndReturnTrimmedCombinedOutput()
require.Error(t, err, out)
require.Equal(t, `{"is_valid":true,"error":"no matching pipeline \u0026 workflow found with trigger params: push-branch: , pr-source-branch: no_draft_pr, pr-target-branch: , tag: "}`, out)
}
}
4 changes: 4 additions & 0 deletions _tests/integration/new_trigger_test_bitrise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ trigger_map:
workflow: pr_target
- pull_request_source_branch: pr_source_only
workflow: pr_source
- pull_request_source_branch: no_draft_pr
draft_pull_request_enabled: false
workflow: no_draft_pr

pipelines:
deprecated_pipeline:
Expand All @@ -48,3 +51,4 @@ workflows:
pr_source_and_target:
pr_target:
pr_source:
no_draft_pr:
10 changes: 5 additions & 5 deletions cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ const (
PushBranchKey = "push-branch"
PRSourceBranchKey = "pr-source-branch"
PRTargetBranchKey = "pr-target-branch"
DraftPRKey = "draft-pr"

IncludeWorkflowMetaKey = "include-workflow-meta"
ConfigKey = "config"
InventoryKey = "inventory"
OuputFormatKey = "format"
ConfigKey = "config"
InventoryKey = "inventory"
OuputFormatKey = "format"
)

var (
Expand Down Expand Up @@ -60,6 +60,7 @@ var (
cli.StringFlag{Name: PushBranchKey, Usage: "Git push branch name."},
cli.StringFlag{Name: PRSourceBranchKey, Usage: "Git pull request source branch name."},
cli.StringFlag{Name: PRTargetBranchKey, Usage: "Git pull request target branch name."},
cli.BoolFlag{Name: DraftPRKey, Usage: "Is the pull request in draft state?"},
cli.StringFlag{Name: TagKey, Usage: "Git tag name."},

cli.StringFlag{Name: OuputFormatKey, Usage: "Output format. Accepted: json, yml."},
Expand All @@ -68,7 +69,6 @@ var (
cli.StringFlag{Name: JSONParamsKey, Usage: "Specify command flags with json string-string hash."},
cli.StringFlag{Name: JSONParamsBase64Key, Usage: "Specify command flags with base64 encoded json string-string hash."},


// should deprecate
cli.StringFlag{Name: ConfigBase64Key, Usage: "base64 encoded config data."},
cli.StringFlag{Name: InventoryBase64Key, Usage: "base64 encoded inventory data."},
Expand Down
18 changes: 11 additions & 7 deletions cli/run_trigger_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type RunAndTriggerParamsModel struct {
PushBranch string `json:"push-branch"`
PRSourceBranch string `json:"pr-source-branch"`
PRTargetBranch string `json:"pr-target-branch"`
IsDraftPR bool `json:"draft-pr"`
Tag string `json:"tag"`

// Trigger Check Params
Expand All @@ -44,7 +45,7 @@ func parseRunAndTriggerJSONParams(jsonParams string) (RunAndTriggerParamsModel,
func parseRunAndTriggerParams(
workflowToRunID,
triggerPattern,
pushBranch, prSourceBranch, prTargetBranch, tag,
pushBranch, prSourceBranch, prTargetBranch string, isDraftPR *bool, tag,
format,
bitriseConfigPath, bitriseConfigBase64Data,
inventoryPath, inventoryBase64Data,
Expand All @@ -68,7 +69,7 @@ func parseRunAndTriggerParams(
}
}

// Owerride params
// Override params
if workflowToRunID != "" {
params.WorkflowToRunID = workflowToRunID
}
Expand All @@ -86,6 +87,9 @@ func parseRunAndTriggerParams(
if prTargetBranch != "" {
params.PRTargetBranch = prTargetBranch
}
if isDraftPR != nil {
params.IsDraftPR = *isDraftPR
}
if tag != "" {
params.Tag = tag
}
Expand Down Expand Up @@ -115,24 +119,24 @@ func parseRunParams(
bitriseConfigPath, bitriseConfigBase64Data,
inventoryPath, inventoryBase64Data,
jsonParams, base64JSONParams string) (RunAndTriggerParamsModel, error) {
return parseRunAndTriggerParams(workflowToRunID, "", "", "", "", "", "", bitriseConfigPath, bitriseConfigBase64Data, inventoryPath, inventoryBase64Data, jsonParams, base64JSONParams)
return parseRunAndTriggerParams(workflowToRunID, "", "", "", "", nil, "", "", bitriseConfigPath, bitriseConfigBase64Data, inventoryPath, inventoryBase64Data, jsonParams, base64JSONParams)
}

func parseTriggerParams(
triggerPattern,
pushBranch, prSourceBranch, prTargetBranch, tag,
pushBranch, prSourceBranch, prTargetBranch string, isDraftPR *bool, tag,
bitriseConfigPath, bitriseConfigBase64Data,
inventoryPath, inventoryBase64Data,
jsonParams, base64JSONParams string) (RunAndTriggerParamsModel, error) {
return parseRunAndTriggerParams("", triggerPattern, pushBranch, prSourceBranch, prTargetBranch, tag, "", bitriseConfigPath, bitriseConfigBase64Data, inventoryPath, inventoryBase64Data, jsonParams, base64JSONParams)
return parseRunAndTriggerParams("", triggerPattern, pushBranch, prSourceBranch, prTargetBranch, isDraftPR, tag, "", bitriseConfigPath, bitriseConfigBase64Data, inventoryPath, inventoryBase64Data, jsonParams, base64JSONParams)
}

func parseTriggerCheckParams(
triggerPattern,
pushBranch, prSourceBranch, prTargetBranch, tag,
pushBranch, prSourceBranch, prTargetBranch string, isDraftPR *bool, tag,
format,
bitriseConfigPath, bitriseConfigBase64Data,
inventoryPath, inventoryBase64Data,
jsonParams, base64JSONParams string) (RunAndTriggerParamsModel, error) {
return parseRunAndTriggerParams("", triggerPattern, pushBranch, prSourceBranch, prTargetBranch, tag, format, bitriseConfigPath, bitriseConfigBase64Data, inventoryPath, inventoryBase64Data, jsonParams, base64JSONParams)
return parseRunAndTriggerParams("", triggerPattern, pushBranch, prSourceBranch, prTargetBranch, isDraftPR, tag, format, bitriseConfigPath, bitriseConfigBase64Data, inventoryPath, inventoryBase64Data, jsonParams, base64JSONParams)
}
Loading

0 comments on commit 4dfbba7

Please sign in to comment.