Skip to content

[duplicate-code] Duplicate Code: Close Issue/PR/Discussion JobsΒ #4779

@github-actions

Description

@github-actions

πŸ” Duplicate Code Detected: Close Issue/PR/Discussion Jobs

Analysis of commit 6e2795a

Assignee: @copilot

Summary

Three separate safe-output handlers (close_issue, close_pull_request, and close_discussion) duplicate the same configuration parsing logic and job scaffolding, differing only in string literals and resource-specific field names. This copy/paste spans ~110 lines per file and forces all behavioral fixes or feature flags to be applied three times.

Duplication Details

Pattern: Repeated close-* config parsing and job builders

  • Severity: Medium
  • Occurrences: 3
  • Locations:
    • pkg/workflow/close_issue.go:21
    • pkg/workflow/close_pull_request.go:21
    • pkg/workflow/close_discussion.go:23
  • Code Sample:
    if len(data.SafeOutputs.CloseIssues.RequiredLabels) > 0 {
        customEnvVars = append(customEnvVars,
            fmt.Sprintf("          GH_AW_CLOSE_ISSUE_REQUIRED_LABELS: %q\n",
                strings.Join(data.SafeOutputs.CloseIssues.RequiredLabels, ",")))
    }
    if data.SafeOutputs.CloseIssues.RequiredTitlePrefix != "" {
        customEnvVars = append(customEnvVars,
            fmt.Sprintf("          GH_AW_CLOSE_ISSUE_REQUIRED_TITLE_PREFIX: %q\n",
                data.SafeOutputs.CloseIssues.RequiredTitlePrefix))
    }
    // ... identical logic appears in close_pull_request & close_discussion with only renamed env vars

Impact Analysis

  • Maintainability: Any future change to validation, logging, or environment wiring requires editing three files, increasing review overhead and the risk of inconsistent behavior.
  • Bug Risk: A fix applied to one close-* path (for example, tightening target validations) can be accidentally skipped in another file, leading to inconsistent enforcement across issue/PR/discussion closures.
  • Code Bloat: ~330 lines of near-identical code inflate the workflow package and obscure the core differences between the handlers.

Refactoring Recommendations

  1. Introduce shared close-entity helpers
    • Extract a generic closeEntityConfig and buildCloseEntityJob that accept the resource-specific options (env var suffixes, permission set, output keys) and reuse them across issue/PR/discussion closures.
    • Estimated effort: Moderate (2–3 hours) because the existing functions already follow the same shape.
    • Benefits: Single source of truth for validation, less risk of configuration drift.
  2. Table-driven configuration parsing
    • Define a descriptor slice (required fields, env var prefixes, condition builder) and loop over it instead of duplicating each switch.
    • Benefits: makes it cheaper to add another close-* safe output in the future.

Implementation Checklist

  • Review duplication findings
  • Prioritize refactoring tasks
  • Create refactoring plan
  • Implement changes
  • Update tests
  • Verify no functionality broken

Analysis Metadata

  • Analyzed Files: 3
  • Detection Method: Serena semantic code analysis
  • Commit: 6e2795a
  • Analysis Date: 2025-11-25T21:07:24Z

AI generated by Duplicate Code Detector

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions