Skip to content

Commit 1671255

Browse files
authored
Refactor update entity job builders to eliminate duplication (#5959)
1 parent 4f77f95 commit 1671255

4 files changed

Lines changed: 113 additions & 78 deletions

File tree

pkg/workflow/update_entity_helpers.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ type UpdateEntityJobParams struct {
3535
Condition ConditionNode // Job condition expression
3636
}
3737

38+
// UpdateEntityJobBuilder encapsulates entity-specific configuration for building update jobs
39+
type UpdateEntityJobBuilder struct {
40+
EntityType UpdateEntityType
41+
ConfigKey string
42+
JobName string
43+
StepName string
44+
ScriptGetter func() string
45+
PermissionsFunc func() *Permissions
46+
BuildCustomEnvVars func(*UpdateEntityConfig) []string
47+
BuildOutputs func() map[string]string
48+
BuildEventCondition func(string) ConditionNode // Optional: builds event condition if target is empty
49+
}
50+
3851
// parseUpdateEntityConfig is a generic function to parse update entity configurations
3952
func (c *Compiler) parseUpdateEntityConfig(outputMap map[string]any, params UpdateEntityJobParams, logger *logger.Logger, parseSpecificFields func(map[string]any, *UpdateEntityConfig)) *UpdateEntityConfig {
4053
if configData, exists := outputMap[params.ConfigKey]; exists {
@@ -94,3 +107,51 @@ func (c *Compiler) buildUpdateEntityJob(data *WorkflowData, mainJobName string,
94107
TargetRepoSlug: config.TargetRepoSlug,
95108
})
96109
}
110+
111+
// buildUpdateEntityJobWithConfig is a higher-level helper that encapsulates the common pattern
112+
// of building update entity jobs, reducing duplication across issue/PR/release builders
113+
func (c *Compiler) buildUpdateEntityJobWithConfig(
114+
data *WorkflowData,
115+
mainJobName string,
116+
config *UpdateEntityConfig,
117+
builder UpdateEntityJobBuilder,
118+
logger *logger.Logger,
119+
) (*Job, error) {
120+
if config == nil {
121+
return nil, fmt.Errorf("safe-outputs.%s configuration is required", builder.ConfigKey)
122+
}
123+
124+
// Build entity-specific custom environment variables
125+
customEnvVars := builder.BuildCustomEnvVars(config)
126+
127+
// Append target configuration environment variables
128+
customEnvVars = append(customEnvVars, BuildTargetEnvVar("GH_AW_UPDATE_TARGET", config.Target)...)
129+
130+
// Build entity-specific outputs
131+
outputs := builder.BuildOutputs()
132+
133+
// Build job condition with safe output type check
134+
jobCondition := BuildSafeOutputType(builder.JobName)
135+
136+
// Add optional event condition if target is empty and event condition builder is provided
137+
if builder.BuildEventCondition != nil && config.Target == "" {
138+
eventCondition := builder.BuildEventCondition(config.Target)
139+
jobCondition = buildAnd(jobCondition, eventCondition)
140+
}
141+
142+
// Create UpdateEntityJobParams with all the configuration
143+
params := UpdateEntityJobParams{
144+
EntityType: builder.EntityType,
145+
ConfigKey: builder.ConfigKey,
146+
JobName: builder.JobName,
147+
StepName: builder.StepName,
148+
ScriptGetter: builder.ScriptGetter,
149+
PermissionsFunc: builder.PermissionsFunc,
150+
CustomEnvVars: customEnvVars,
151+
Outputs: outputs,
152+
Condition: jobCondition,
153+
}
154+
155+
// Use the existing buildUpdateEntityJob to create the job
156+
return c.buildUpdateEntityJob(data, mainJobName, config, params, logger)
157+
}

pkg/workflow/update_issue.go

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,32 @@ func (c *Compiler) buildCreateOutputUpdateIssueJob(data *WorkflowData, mainJobNa
2424

2525
cfg := data.SafeOutputs.UpdateIssues
2626

27-
// Build custom environment variables specific to update-issue
28-
customEnvVars := []string{
29-
fmt.Sprintf(" GH_AW_UPDATE_STATUS: %t\n", cfg.Status != nil),
30-
fmt.Sprintf(" GH_AW_UPDATE_TITLE: %t\n", cfg.Title != nil),
31-
fmt.Sprintf(" GH_AW_UPDATE_BODY: %t\n", cfg.Body != nil),
32-
}
33-
34-
// Pass the target configuration
35-
customEnvVars = append(customEnvVars, BuildTargetEnvVar("GH_AW_UPDATE_TARGET", cfg.Target)...)
36-
37-
// Create outputs for the job
38-
outputs := map[string]string{
39-
"issue_number": "${{ steps.update_issue.outputs.issue_number }}",
40-
"issue_url": "${{ steps.update_issue.outputs.issue_url }}",
41-
}
42-
43-
// Build job condition with event check if target is not specified
44-
jobCondition := BuildSafeOutputType("update_issue")
45-
if cfg.Target == "" {
46-
eventCondition := BuildPropertyAccess("github.event.issue.number")
47-
jobCondition = buildAnd(jobCondition, eventCondition)
48-
}
49-
50-
params := UpdateEntityJobParams{
27+
builder := UpdateEntityJobBuilder{
5128
EntityType: UpdateEntityIssue,
5229
ConfigKey: "update-issue",
5330
JobName: "update_issue",
5431
StepName: "Update Issue",
5532
ScriptGetter: getUpdateIssueScript,
5633
PermissionsFunc: NewPermissionsContentsReadIssuesWrite,
57-
CustomEnvVars: customEnvVars,
58-
Outputs: outputs,
59-
Condition: jobCondition,
34+
BuildCustomEnvVars: func(config *UpdateEntityConfig) []string {
35+
return []string{
36+
fmt.Sprintf(" GH_AW_UPDATE_STATUS: %t\n", cfg.Status != nil),
37+
fmt.Sprintf(" GH_AW_UPDATE_TITLE: %t\n", cfg.Title != nil),
38+
fmt.Sprintf(" GH_AW_UPDATE_BODY: %t\n", cfg.Body != nil),
39+
}
40+
},
41+
BuildOutputs: func() map[string]string {
42+
return map[string]string{
43+
"issue_number": "${{ steps.update_issue.outputs.issue_number }}",
44+
"issue_url": "${{ steps.update_issue.outputs.issue_url }}",
45+
}
46+
},
47+
BuildEventCondition: func(target string) ConditionNode {
48+
return BuildPropertyAccess("github.event.issue.number")
49+
},
6050
}
6151

62-
return c.buildUpdateEntityJob(data, mainJobName, &cfg.UpdateEntityConfig, params, updateIssueLog)
52+
return c.buildUpdateEntityJobWithConfig(data, mainJobName, &cfg.UpdateEntityConfig, builder, updateIssueLog)
6353
}
6454

6555
// parseUpdateIssuesConfig handles update-issue configuration

pkg/workflow/update_pull_request.go

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,45 +24,34 @@ func (c *Compiler) buildCreateOutputUpdatePullRequestJob(data *WorkflowData, mai
2424

2525
cfg := data.SafeOutputs.UpdatePullRequests
2626

27-
// Default to true for both title and body unless explicitly set to false
28-
canUpdateTitle := cfg.Title == nil || *cfg.Title
29-
canUpdateBody := cfg.Body == nil || *cfg.Body
30-
31-
// Build custom environment variables specific to update-pull-request
32-
customEnvVars := []string{
33-
fmt.Sprintf(" GH_AW_UPDATE_TITLE: %t\n", canUpdateTitle),
34-
fmt.Sprintf(" GH_AW_UPDATE_BODY: %t\n", canUpdateBody),
35-
}
36-
37-
// Pass the target configuration
38-
customEnvVars = append(customEnvVars, BuildTargetEnvVar("GH_AW_UPDATE_TARGET", cfg.Target)...)
39-
40-
// Create outputs for the job
41-
outputs := map[string]string{
42-
"pull_request_number": "${{ steps.update_pull_request.outputs.pull_request_number }}",
43-
"pull_request_url": "${{ steps.update_pull_request.outputs.pull_request_url }}",
44-
}
45-
46-
// Build job condition with event check if target is not specified
47-
jobCondition := BuildSafeOutputType("update_pull_request")
48-
if cfg.Target == "" {
49-
eventCondition := BuildPropertyAccess("github.event.pull_request.number")
50-
jobCondition = buildAnd(jobCondition, eventCondition)
51-
}
52-
53-
params := UpdateEntityJobParams{
27+
builder := UpdateEntityJobBuilder{
5428
EntityType: UpdateEntityPullRequest,
5529
ConfigKey: "update-pull-request",
5630
JobName: "update_pull_request",
5731
StepName: "Update Pull Request",
5832
ScriptGetter: getUpdatePullRequestScript,
5933
PermissionsFunc: NewPermissionsContentsReadPRWrite,
60-
CustomEnvVars: customEnvVars,
61-
Outputs: outputs,
62-
Condition: jobCondition,
34+
BuildCustomEnvVars: func(config *UpdateEntityConfig) []string {
35+
// Default to true for both title and body unless explicitly set to false
36+
canUpdateTitle := cfg.Title == nil || *cfg.Title
37+
canUpdateBody := cfg.Body == nil || *cfg.Body
38+
return []string{
39+
fmt.Sprintf(" GH_AW_UPDATE_TITLE: %t\n", canUpdateTitle),
40+
fmt.Sprintf(" GH_AW_UPDATE_BODY: %t\n", canUpdateBody),
41+
}
42+
},
43+
BuildOutputs: func() map[string]string {
44+
return map[string]string{
45+
"pull_request_number": "${{ steps.update_pull_request.outputs.pull_request_number }}",
46+
"pull_request_url": "${{ steps.update_pull_request.outputs.pull_request_url }}",
47+
}
48+
},
49+
BuildEventCondition: func(target string) ConditionNode {
50+
return BuildPropertyAccess("github.event.pull_request.number")
51+
},
6352
}
6453

65-
return c.buildUpdateEntityJob(data, mainJobName, &cfg.UpdateEntityConfig, params, updatePullRequestLog)
54+
return c.buildUpdateEntityJobWithConfig(data, mainJobName, &cfg.UpdateEntityConfig, builder, updatePullRequestLog)
6655
}
6756

6857
// parseUpdatePullRequestsConfig handles update-pull-request configuration

pkg/workflow/update_release.go

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,28 @@ func (c *Compiler) buildCreateOutputUpdateReleaseJob(data *WorkflowData, mainJob
2121

2222
cfg := data.SafeOutputs.UpdateRelease
2323

24-
// Build custom environment variables specific to update-release
25-
// Uses buildStandardSafeOutputEnvVars for consistency with other update jobs
26-
var customEnvVars []string
27-
28-
// Create outputs for the job
29-
outputs := map[string]string{
30-
"release_id": "${{ steps.update_release.outputs.release_id }}",
31-
"release_url": "${{ steps.update_release.outputs.release_url }}",
32-
"release_tag": "${{ steps.update_release.outputs.release_tag }}",
33-
}
34-
35-
// Build job condition - update_release doesn't have event context checks
36-
jobCondition := BuildSafeOutputType("update_release")
37-
38-
params := UpdateEntityJobParams{
24+
builder := UpdateEntityJobBuilder{
3925
EntityType: UpdateEntityRelease,
4026
ConfigKey: "update-release",
4127
JobName: "update_release",
4228
StepName: "Update Release",
4329
ScriptGetter: getUpdateReleaseScript,
4430
PermissionsFunc: NewPermissionsContentsWrite,
45-
CustomEnvVars: customEnvVars,
46-
Outputs: outputs,
47-
Condition: jobCondition,
31+
BuildCustomEnvVars: func(config *UpdateEntityConfig) []string {
32+
// Update-release doesn't have entity-specific env vars like status/title/body
33+
return []string{}
34+
},
35+
BuildOutputs: func() map[string]string {
36+
return map[string]string{
37+
"release_id": "${{ steps.update_release.outputs.release_id }}",
38+
"release_url": "${{ steps.update_release.outputs.release_url }}",
39+
"release_tag": "${{ steps.update_release.outputs.release_tag }}",
40+
}
41+
},
42+
// BuildEventCondition is nil - update_release doesn't have event context checks
4843
}
4944

50-
return c.buildUpdateEntityJob(data, mainJobName, &cfg.UpdateEntityConfig, params, updateReleaseLog)
45+
return c.buildUpdateEntityJobWithConfig(data, mainJobName, &cfg.UpdateEntityConfig, builder, updateReleaseLog)
5146
}
5247

5348
// parseUpdateReleaseConfig handles update-release configuration

0 commit comments

Comments
 (0)