collavre_github 0.6.3 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/collavre_github/creatives/integrations_controller.rb +253 -36
  3. data/app/controllers/collavre_github/webhooks_controller.rb +563 -44
  4. data/app/javascript/collavre_github.js +32 -123
  5. data/app/jobs/collavre_github/initial_markdown_sync_job.rb +6 -4
  6. data/app/jobs/collavre_github/markdown_sync_job.rb +6 -4
  7. data/app/jobs/collavre_github/pr_blocker_sweep_job.rb +18 -0
  8. data/app/jobs/collavre_github/webhook_delivery_prune_job.rb +14 -0
  9. data/app/models/collavre_github/github_pr_channel.rb +124 -28
  10. data/app/models/collavre_github/repository_link.rb +63 -2
  11. data/app/models/collavre_github/webhook_delivery.rb +159 -0
  12. data/app/services/collavre_github/client.rb +37 -0
  13. data/app/services/collavre_github/markdown_sync/content_creative.rb +56 -0
  14. data/app/services/collavre_github/markdown_sync/content_processor.rb +16 -1
  15. data/app/services/collavre_github/markdown_sync/incremental_sync_service.rb +12 -34
  16. data/app/services/collavre_github/markdown_sync/initial_import_service.rb +6 -19
  17. data/app/services/collavre_github/pr_blocker_monitor.rb +93 -0
  18. data/app/services/collavre_github/pr_channel_state_updater.rb +83 -0
  19. data/app/services/collavre_github/pr_discussion_event.rb +25 -0
  20. data/app/services/collavre_github/repository_identity_synchronizer.rb +145 -0
  21. data/app/services/collavre_github/repository_provisioning_lock.rb +95 -0
  22. data/app/services/collavre_github/tools/concerns/pr_channel_locator.rb +50 -0
  23. data/app/services/collavre_github/tools/pr_monitor_service.rb +82 -62
  24. data/app/services/collavre_github/tools/pr_state_set_service.rb +170 -0
  25. data/app/services/collavre_github/webhook_provisioner.rb +601 -42
  26. data/app/services/collavre_github/webhook_reprovisioner.rb +118 -0
  27. data/config/locales/en.yml +4 -0
  28. data/config/locales/ko.yml +4 -0
  29. data/config/locales/tool_descriptions.en.yml +20 -0
  30. data/config/locales/tool_descriptions.ko.yml +20 -0
  31. data/config/routes.rb +13 -3
  32. data/db/migrate/20260727000001_add_webhook_hook_id_to_github_repository_links.rb +17 -0
  33. data/db/migrate/20260727000002_create_github_webhook_deliveries.rb +30 -0
  34. data/db/migrate/20260729000000_add_repository_id_index_to_github_repository_links.rb +13 -0
  35. data/db/seeds.rb +2 -1
  36. data/lib/collavre_github/engine.rb +9 -0
  37. data/lib/collavre_github/version.rb +1 -1
  38. metadata +19 -2
@@ -0,0 +1,170 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sorbet-runtime"
4
+ require "rails_mcp_engine"
5
+
6
+ module CollavreGithub
7
+ module Tools
8
+ class PrStateSetService
9
+ extend T::Sig
10
+ extend ToolMeta
11
+ include Concerns::PrChannelLocator
12
+
13
+ tool_name "pr_state_set"
14
+ tool_description <<~DESC.strip
15
+ Correct the state of a GitHub PR channel attached with pr_monitor, for
16
+ when a `pull_request` webhook was missed (hook added after the merge,
17
+ delivery failure) and the chip is stuck on the wrong badge.
18
+
19
+ Omit `state` to resynchronize from the GitHub API — that is the
20
+ preferred form, because it cannot record a state the PR does not
21
+ actually have. Pass `state` explicitly only as a fallback when no
22
+ GitHub account is connected for the repository.
23
+
24
+ Closing the channel (merged / closed_without_merge) posts the closing
25
+ message and detaches it; reopening it resumes monitoring. Idempotent.
26
+ DESC
27
+
28
+ tool_param :topic_id, description: "The Collavre topic id the PR channel is attached to."
29
+ tool_param :pr_url, description: "Full GitHub PR URL, e.g. https://github.com/owner/repo/pull/123"
30
+ tool_param :state,
31
+ description: "Target state. Omit to read the real state from GitHub.",
32
+ required: false,
33
+ enum: CollavreGithub::GithubPrChannel::PR_STATES
34
+
35
+ sig do
36
+ params(
37
+ topic_id: Integer,
38
+ pr_url: String,
39
+ state: T.nilable(String)
40
+ ).returns(T::Hash[Symbol, T.untyped])
41
+ end
42
+ def call(topic_id:, pr_url:, state: nil)
43
+ repo, pr_number = parse_pr_url(pr_url)
44
+
45
+ topic = Collavre::Topic.find(topic_id)
46
+ Collavre::Tools::TopicAuthorizer.authorize_write!(topic)
47
+
48
+ channel = lookup_channel(topic, repo, pr_number)
49
+ unless channel
50
+ return {
51
+ ok: false,
52
+ error: "No PR channel for #{repo}##{pr_number} on topic #{topic_id}. Attach it with pr_monitor first."
53
+ }
54
+ end
55
+
56
+ target, source = resolve_state(topic, repo, pr_number, state)
57
+ return target if target.is_a?(Hash) # error payload
58
+
59
+ result = CollavreGithub::PrChannelStateUpdater.call(channel: channel, state: target)
60
+ {
61
+ ok: true,
62
+ channel_id: channel.id,
63
+ repo: repo,
64
+ pr_number: pr_number,
65
+ pr_state: target,
66
+ previous_state: result.previous_state,
67
+ status: result.status.to_s,
68
+ state_source: source
69
+ }
70
+ end
71
+
72
+ private
73
+
74
+ # Returns [state, source] or an error hash. An explicit `state` is
75
+ # validated here rather than at the updater so a typo surfaces as an
76
+ # ArgumentError naming the tool parameter the caller actually passed.
77
+ sig do
78
+ params(
79
+ topic: Collavre::Topic,
80
+ repo: String,
81
+ pr_number: Integer,
82
+ state: T.nilable(String)
83
+ ).returns([ T.any(String, T::Hash[Symbol, T.untyped]), String ])
84
+ end
85
+ def resolve_state(topic, repo, pr_number, state)
86
+ if state.present?
87
+ unless CollavreGithub::GithubPrChannel::PR_STATES.include?(state)
88
+ raise ArgumentError,
89
+ "Invalid state: #{state.inspect} (expected one of #{CollavreGithub::GithubPrChannel::PR_STATES.join(', ')})"
90
+ end
91
+ return [ state, "explicit" ]
92
+ end
93
+
94
+ clients = verified_github_clients_for(topic, repo)
95
+ if clients.empty?
96
+ return [ {
97
+ ok: false,
98
+ error: "No verified connected GitHub account for #{repo} in this topic's creative scope, " \
99
+ "so the state could not be read from GitHub. Pass `state` explicitly instead."
100
+ }, "github" ]
101
+ end
102
+
103
+ clients.each do |client|
104
+ remote = remote_state(client, repo, pr_number)
105
+ return [ remote, "github" ] if remote
106
+ end
107
+
108
+ [ {
109
+ ok: false,
110
+ error: "Could not read #{repo}##{pr_number} from GitHub (not found, or the API call failed). " \
111
+ "Pass `state` explicitly instead."
112
+ }, "github" ]
113
+ end
114
+
115
+ # GitHub reports merge and closure separately: `merged` is the
116
+ # authoritative merge flag, and `state` only distinguishes open from
117
+ # closed. Checking `merged` first is what keeps a squash-merged PR from
118
+ # being recorded as closed_without_merge.
119
+ def remote_state(client, repo, pr_number)
120
+ pr = client.pull_request_details(repo, pr_number)
121
+ return nil unless pr
122
+ return "merged" if pr.merged
123
+
124
+ pr.state.to_s == "closed" ? "closed_without_merge" : "open"
125
+ end
126
+
127
+ # A repository name is not an identity: GitHub can reuse it after a
128
+ # rename. Keep only accounts whose live repository id matches a stable id
129
+ # stored on an in-scope RepositoryLink, and reject the entire inherited
130
+ # scope when another link claims the same name with a conflicting id.
131
+ # Then let resync try each verified account until one can read the PR.
132
+ def verified_github_clients_for(topic, repo)
133
+ candidate_ids = scoped_creative_ids(topic)
134
+ return [] if candidate_ids.empty?
135
+
136
+ links = CollavreGithub::RepositoryLink
137
+ .where("LOWER(repository_full_name) = ?", repo.downcase)
138
+ .where(creative_id: candidate_ids)
139
+ .where.not(repository_id: nil)
140
+ .order(:id)
141
+ .to_a
142
+
143
+ links.group_by(&:github_account_id).filter_map do |account_id, account_links|
144
+ verified_client_for(account_id, account_links, topic, repo)
145
+ end
146
+ end
147
+
148
+ def verified_client_for(account_id, links, topic, repo)
149
+ client = CollavreGithub::Client.new(links.first.github_account)
150
+ identity = client.repository_identity(repo)
151
+ return nil if CollavreGithub::RepositoryLink.conflicting_repository_in_scope?(
152
+ creative: topic.creative,
153
+ full_name: repo,
154
+ repository_id: identity.id
155
+ )
156
+
157
+ return client if links.any? { |link| link.repository_id.to_s == identity.id.to_s }
158
+
159
+ nil
160
+ rescue Octokit::Error, Faraday::Error => e
161
+ Rails.logger.warn(
162
+ "[pr_state_set] repository identity lookup failed for #{repo} through " \
163
+ "account #{account_id}: #{e.class}: #{e.message}"
164
+ )
165
+
166
+ nil
167
+ end
168
+ end
169
+ end
170
+ end