Class: Aidp::Watch::ProjectsProcessor

Inherits:
Object
  • Object
show all
Includes:
MessageDisplay
Defined in:
lib/aidp/watch/projects_processor.rb

Overview

Synchronizes GitHub issues with GitHub Projects V2. Updates project fields based on issue state and handles blocking relationships.

Constant Summary collapse

DEFAULT_FIELD_MAPPINGS =

Default field mapping configuration

{
  status: "Status",
  priority: "Priority",
  skills: "Skills",
  personas: "Personas",
  blocking: "Blocking"
}.freeze
STATUS_VALUES =

Status values for different issue states

{
  backlog: "Backlog",
  todo: "Todo",
  in_progress: "In Progress",
  in_review: "In Review",
  done: "Done",
  blocked: "Blocked"
}.freeze

Constants included from MessageDisplay

MessageDisplay::COLOR_MAP, MessageDisplay::CRITICAL_TYPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MessageDisplay

#display_message, included, #message_display_prompt, #quiet_mode?

Constructor Details

#initialize(repository_client:, state_store:, project_id:, config: {}) ⇒ ProjectsProcessor

Returns a new instance of ProjectsProcessor.



33
34
35
36
37
38
39
40
41
# File 'lib/aidp/watch/projects_processor.rb', line 33

def initialize(repository_client:, state_store:, project_id:, config: {})
  @repository_client = repository_client
  @state_store = state_store
  @project_id = project_id
  @config = config
  @field_mappings = config[:field_mappings] || DEFAULT_FIELD_MAPPINGS
  @auto_create_fields = config[:auto_create_fields] != false
  @project_fields_cache = nil
end

Instance Attribute Details

#project_idObject (readonly)

Returns the value of attribute project_id.



31
32
33
# File 'lib/aidp/watch/projects_processor.rb', line 31

def project_id
  @project_id
end

#repository_clientObject (readonly)

Returns the value of attribute repository_client.



31
32
33
# File 'lib/aidp/watch/projects_processor.rb', line 31

def repository_client
  @repository_client
end

#state_storeObject (readonly)

Returns the value of attribute state_store.



31
32
33
# File 'lib/aidp/watch/projects_processor.rb', line 31

def state_store
  @state_store
end

Instance Method Details

#check_blocking_dependencies(issue_number) ⇒ Hash

Check if an issue is blocked by any of its sub-issues

Parameters:

  • issue_number (Integer)

    The parent issue number

Returns:

  • (Hash)

    Blocking status with :blocked flag and :blockers list



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/aidp/watch/projects_processor.rb', line 123

def check_blocking_dependencies(issue_number)
  Aidp.log_debug("projects_processor", "check_blocking_dependencies", issue_number: issue_number)

  status = @state_store.blocking_status(issue_number)

  if status[:blocked]
    # Fetch current status of sub-issues
    open_blockers = []
    status[:blockers].each do |sub_number|
      issue = @repository_client.fetch_issue(sub_number)
      open_blockers << sub_number if issue[:state] == "open"
    rescue => e
      Aidp.log_warn("projects_processor", "Failed to fetch sub-issue",
        sub_issue: sub_number, error: e.message)
      # Assume still blocking if we can't check
      open_blockers << sub_number
    end

    if open_blockers.any?
      update_blocking_field(issue_number, open_blockers)
      display_message("⚠️  Issue ##{issue_number} is blocked by #{open_blockers.size} open sub-issues",
        type: :warn)
      {blocked: true, blockers: open_blockers}
    else
      # All sub-issues are closed - unblock parent
      clear_blocking_field(issue_number)
      update_issue_status(issue_number, STATUS_VALUES[:todo])
      display_message("✓ Issue ##{issue_number} is no longer blocked", type: :success)
      {blocked: false, blockers: []}
    end
  else
    {blocked: false, blockers: []}
  end
end

#ensure_project_fieldsBoolean

Initialize required project fields if they don’t exist

Returns:

  • (Boolean)

    True if all fields are ready



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/aidp/watch/projects_processor.rb', line 183

def ensure_project_fields
  return true unless @auto_create_fields

  Aidp.log_debug("projects_processor", "ensure_project_fields", project_id: @project_id)

  required_fields = [
    {name: @field_mappings[:status], type: "SINGLE_SELECT", options: STATUS_VALUES.values},
    {name: @field_mappings[:blocking], type: "TEXT"}
  ]

  all_ready = true
  required_fields.each do |field_spec|
    field = find_or_create_field(field_spec[:name], field_spec[:type], field_spec[:options])
    all_ready = false unless field
  end

  all_ready
end

#sync_all_issues(issues) ⇒ Object

Sync all active issues in a project

Parameters:

  • issues (Array<Hash>)

    Array of issue data with :number keys



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/aidp/watch/projects_processor.rb', line 160

def sync_all_issues(issues)
  Aidp.log_debug("projects_processor", "sync_all_issues", count: issues.size)

  display_message("📊 Syncing #{issues.size} issues to project...", type: :info)

  synced = 0
  failed = 0

  issues.each do |issue|
    success = sync_issue_to_project(issue[:number])
    if success
      synced += 1
    else
      failed += 1
    end
  end

  display_message("📊 Sync complete: #{synced} synced, #{failed} failed", type: :info)
  {synced: synced, failed: failed}
end

#sync_issue_to_project(issue_number, status: nil) ⇒ Boolean

Sync a single issue to the project

Parameters:

  • issue_number (Integer)

    The issue number

  • status (String, nil) (defaults to: nil)

    Optional status to set

Returns:

  • (Boolean)

    True if sync was successful



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/aidp/watch/projects_processor.rb', line 47

def sync_issue_to_project(issue_number, status: nil)
  Aidp.log_debug("projects_processor", "sync_issue_to_project", issue_number: issue_number, status: status)

  # Check if issue is already linked to project
  item_id = @state_store.project_item_id(issue_number)

  unless item_id
    # Link issue to project
    begin
      item_id = @repository_client.link_issue_to_project(@project_id, issue_number)
      @state_store.record_project_item_id(issue_number, item_id)
      display_message("📊 Linked issue ##{issue_number} to project", type: :success)
    rescue => e
      Aidp.log_error("projects_processor", "Failed to link issue to project",
        issue_number: issue_number, error: e.message)
      display_message("⚠️  Failed to link issue ##{issue_number} to project: #{e.message}", type: :warn)
      return false
    end
  end

  # Update status if provided
  if status
    update_issue_status(issue_number, status)
  end

  # Check and update blocking status
  check_blocking_dependencies(issue_number)

  @state_store.record_project_sync(issue_number, {
    last_sync: Time.now.utc.iso8601,
    status: status
  })

  true
rescue => e
  Aidp.log_error("projects_processor", "Failed to sync issue to project",
    issue_number: issue_number, error: e.message)
  false
end

#update_issue_status(issue_number, status) ⇒ Boolean

Update the status field for an issue in the project

Parameters:

  • issue_number (Integer)

    The issue number

  • status (String)

    The status value (e.g., “In Progress”, “Done”)

Returns:

  • (Boolean)

    True if update was successful



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/aidp/watch/projects_processor.rb', line 91

def update_issue_status(issue_number, status)
  Aidp.log_debug("projects_processor", "update_issue_status",
    issue_number: issue_number, status: status)

  item_id = @state_store.project_item_id(issue_number)
  return false unless item_id

  status_field = find_or_create_field(@field_mappings[:status], "SINGLE_SELECT", STATUS_VALUES.values)
  return false unless status_field

  option_id = find_option_id(status_field, status)
  return false unless option_id

  begin
    @repository_client.update_project_item_field(
      item_id,
      status_field[:id],
      {project_id: @project_id, option_id: option_id}
    )
    display_message("✓ Updated status for ##{issue_number} to '#{status}'", type: :success)
    true
  rescue => e
    Aidp.log_error("projects_processor", "Failed to update status",
      issue_number: issue_number, status: status, error: e.message)
    display_message("⚠️  Failed to update status: #{e.message}", type: :warn)
    false
  end
end