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,118 @@
1
+ module CollavreGithub
2
+ # Reconciles every ID-backed repository hook with WebhookProvisioner's current
3
+ # event list. New event subscriptions in code do not change hooks already
4
+ # stored at GitHub, so deploys run this once after the new app is live.
5
+ #
6
+ # Name-only legacy rows are never mutated automatically. Older provisioners
7
+ # copied a hook registration and secret to every same-name row, so neither
8
+ # value is independent proof that a legacy row belongs to the repository
9
+ # currently owning that name. Their presence is reported as requiring manual
10
+ # verification so deploys cannot silently leave them unreconciled.
11
+ class WebhookReprovisioner
12
+ def self.call(webhook_url: default_webhook_url)
13
+ new(webhook_url: webhook_url).call
14
+ end
15
+
16
+ def self.default_webhook_url
17
+ CollavreGithub::Engine.routes.url_helpers.webhooks_url(
18
+ Rails.application.config.action_mailer.default_url_options
19
+ )
20
+ end
21
+
22
+ def initialize(webhook_url:)
23
+ @webhook_url = webhook_url
24
+ end
25
+
26
+ def call
27
+ repository_names.map do |repository_name|
28
+ [ repository_name, reprovision(repository_name) ]
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ attr_reader :webhook_url
35
+
36
+ def repository_names
37
+ CollavreGithub::RepositoryLink
38
+ .order(:id)
39
+ .pluck(:repository_full_name)
40
+ .filter_map { |name| name.to_s.downcase.presence }
41
+ .uniq
42
+ .sort
43
+ end
44
+
45
+ def reprovision(repository_name)
46
+ links = CollavreGithub::RepositoryLink
47
+ .where("LOWER(repository_full_name) = ?", repository_name)
48
+ .order(:id)
49
+ .to_a
50
+ manual_verification_required = links.any? { |link| link.repository_id.blank? }
51
+ failed = false
52
+
53
+ links.select { |link| link.repository_id.present? }.each do |candidate|
54
+ repository_id = candidate.repository_id
55
+ status = CollavreGithub::RepositoryProvisioningLock.with_lock(repository_id) do
56
+ locked_candidate = CollavreGithub::RepositoryLink.find_by(
57
+ id: candidate.id,
58
+ repository_id: repository_id
59
+ )
60
+ next :skipped_unverified unless locked_candidate
61
+ next :failed unless locked_candidate.github_account
62
+
63
+ link = establish_repository_identity(locked_candidate)
64
+ next :skipped_unverified unless link
65
+
66
+ result = CollavreGithub::WebhookProvisioner.ensure_for_links(
67
+ account: link.github_account,
68
+ links: [ link ],
69
+ webhook_url: webhook_url,
70
+ force_hook_refresh: true
71
+ )
72
+ result.first&.last || :failed
73
+ end
74
+
75
+ if status == :failed
76
+ failed = true
77
+ next
78
+ end
79
+ next if status == :skipped_unverified
80
+
81
+ return :manual_verification_required if manual_verification_required
82
+
83
+ return status
84
+ rescue => e
85
+ failed = true
86
+ log_failure(repository_name, e)
87
+ end
88
+
89
+ return :failed if failed
90
+ return :manual_verification_required if manual_verification_required
91
+
92
+ :skipped_unverified
93
+ end
94
+
95
+ def log_failure(repository_name, error)
96
+ Rails.logger.warn(
97
+ "[CollavreGithub::WebhookReprovisioner] #{repository_name} failed: " \
98
+ "#{error.class}: #{error.message}"
99
+ )
100
+ end
101
+
102
+ def establish_repository_identity(link)
103
+ return if link.repository_id.blank?
104
+
105
+ client = CollavreGithub::Client.new(link.github_account)
106
+ identity = client.repository_identity(link.repository_full_name)
107
+ return if identity&.id.blank? || identity&.full_name.blank?
108
+ return if link.repository_id.to_s != identity.id.to_s
109
+
110
+ synchronized = CollavreGithub::RepositoryIdentitySynchronizer.call(
111
+ anchor: link,
112
+ repository_id: identity.id,
113
+ full_name: identity.full_name
114
+ )
115
+ synchronized.find { |candidate| candidate.creative_id == link.creative_id }
116
+ end
117
+ end
118
+ end
@@ -1,5 +1,6 @@
1
1
  en:
2
2
  collavre_github:
3
+ tool_category: "GitHub"
3
4
  integration:
4
5
  label: "GitHub"
5
6
  description: "Connect repositories and receive webhook events"
@@ -106,7 +107,10 @@ en:
106
107
  review_submitted_message: "**@%{author}** submitted a review (`%{state}`) on %{label}:\n\n%{body}"
107
108
  closed_message: "%{label} was **%{verb}**. Detaching channel."
108
109
  attached_message: "%{label} monitoring started.\n\n%{url}"
110
+ merge_conflict_message: "%{label} has a merge conflict.\n\n%{url}"
111
+ failed_checks_message: "%{label} has failed CI checks: %{checks}.\n\n%{url}/checks"
109
112
  reopened_message: "%{label} was reopened. Monitoring resumed.\n\n%{url}"
113
+ auth_failure_message: "⚠️ A GitHub webhook delivery for `%{repo}` could not be verified, so events for %{label} are **not being received**.\n\nThis usually means the repository was renamed or its webhook secret was changed. Re-run the GitHub integration setup for this repository to restore delivery.\n\n%{url}"
110
114
  badge:
111
115
  open: "Open"
112
116
  merged: "Merged"
@@ -1,5 +1,6 @@
1
1
  ko:
2
2
  collavre_github:
3
+ tool_category: "GitHub"
3
4
  integration:
4
5
  label: "GitHub"
5
6
  description: "저장소를 연결하고 웹훅 이벤트를 받습니다"
@@ -106,7 +107,10 @@ ko:
106
107
  review_submitted_message: "**@%{author}**님이 %{label}에 리뷰를 제출했습니다 (`%{state}`):\n\n%{body}"
107
108
  closed_message: "%{label}이(가) **%{verb}** 되었습니다. 채널을 해제합니다."
108
109
  attached_message: "%{label} 모니터링이 시작되었습니다.\n\n%{url}"
110
+ merge_conflict_message: "%{label}에 병합 충돌이 있습니다.\n\n%{url}"
111
+ failed_checks_message: "%{label}에서 CI 검사에 실패했습니다: %{checks}.\n\n%{url}/checks"
109
112
  reopened_message: "%{label}이(가) 재오픈되어 모니터링이 재개되었습니다.\n\n%{url}"
113
+ auth_failure_message: "⚠️ `%{repo}` 저장소의 GitHub 웹훅 전달을 검증하지 못해 %{label}의 이벤트를 **수신하지 못하고 있습니다**.\n\n저장소 이름이 변경되었거나 웹훅 시크릿이 바뀐 경우입니다. 해당 저장소의 GitHub 연동 설정을 다시 실행하면 복구됩니다.\n\n%{url}"
110
114
  badge:
111
115
  open: "열림"
112
116
  merged: "머지됨"
@@ -0,0 +1,20 @@
1
+ en:
2
+ collavre:
3
+ tool_descriptions:
4
+ github_pr_commits:
5
+ summary: Get the commit messages of a GitHub pull request.
6
+ details: Requires the Creative to have a connected GitHub repository.
7
+ github_pr_details:
8
+ summary: Get a GitHub pull request's title, body, author, and changed files.
9
+ details: The PR body is truncated to 2,000 characters, so the end of a long description may be missing. Requires the Creative to have a connected GitHub repository.
10
+ github_pr_diff:
11
+ summary: Get the diff of a GitHub pull request.
12
+ details: Returns the combined patch of the changed files that have a text patch; files without one (such as binary or very large files) are left out. The result is truncated to max_length characters (default 10,000), so later files may be cut off in large PRs. Requires the Creative to have a connected GitHub repository.
13
+ pr_monitor:
14
+ summary: Follow a GitHub pull request in a topic.
15
+ details: PR comments, reviews, merge conflicts, and failed CI checks are posted as topic messages. For verified repository links, polling checks every five minutes for missed comments and reviews, merge conflicts, and failed CI checks. Calling it again for the same PR does not add a duplicate.
16
+ pr_state_set:
17
+ summary: Fix a monitored pull request's state when GitHub's update was missed.
18
+ details: |-
19
+ Without a state, the tool resynchronizes from GitHub, which is the preferred form. Pass a state only when no GitHub account is connected for the repository.
20
+ Marking the PR merged or closed posts the closing message and stops monitoring; reopening resumes it.
@@ -0,0 +1,20 @@
1
+ ko:
2
+ collavre:
3
+ tool_descriptions:
4
+ github_pr_commits:
5
+ summary: GitHub 풀 리퀘스트의 커밋 메시지를 가져옵니다.
6
+ details: 크리에이티브에 GitHub 저장소가 연결되어 있어야 합니다.
7
+ github_pr_details:
8
+ summary: GitHub 풀 리퀘스트의 제목, 본문, 작성자, 변경 파일을 가져옵니다.
9
+ details: PR 본문은 2,000자에서 잘리므로 긴 설명은 뒷부분이 빠질 수 있습니다. 크리에이티브에 GitHub 저장소가 연결되어 있어야 합니다.
10
+ github_pr_diff:
11
+ summary: GitHub 풀 리퀘스트의 diff를 가져옵니다.
12
+ details: 텍스트 패치가 있는 변경 파일들의 패치를 합쳐 반환하며, 패치가 없는 파일(바이너리나 너무 큰 파일 등)은 제외됩니다. 결과는 max_length 글자(기본 10,000자)를 넘으면 잘리므로 큰 PR에서는 뒤쪽 파일이 빠질 수 있습니다. 크리에이티브에 GitHub 저장소가 연결되어 있어야 합니다.
13
+ pr_monitor:
14
+ summary: 토픽에서 GitHub 풀 리퀘스트를 모니터링합니다.
15
+ details: PR 코멘트, 리뷰, 병합 충돌, CI 검사 실패가 토픽 메시지로 게시됩니다. 확인된 저장소 연결에서는 5분마다 웹훅이 놓친 코멘트와 리뷰, 병합 충돌, CI 검사 실패를 확인합니다. 같은 PR로 다시 호출해도 중복되지 않습니다.
16
+ pr_state_set:
17
+ summary: GitHub 업데이트를 놓쳤을 때 모니터링 중인 풀 리퀘스트의 상태를 바로잡습니다.
18
+ details: |-
19
+ 상태를 지정하지 않으면 GitHub에서 다시 동기화하며, 이 방식을 권장합니다. 저장소에 연결된 GitHub 계정이 없을 때만 상태를 직접 지정하세요.
20
+ 머지 또는 닫힘으로 표시하면 종료 메시지를 게시하고 모니터링을 멈추며, 다시 열면 모니터링을 재개합니다.
data/config/routes.rb CHANGED
@@ -1,9 +1,19 @@
1
1
  CollavreGithub::Engine.routes.draw do
2
- # Webhook endpoint (public, no auth)
3
- # Both singular and plural for backward compatibility
4
- post "webhook", to: "webhooks#create", as: :webhook
2
+ # Webhook endpoint (public, no auth).
5
3
  post "webhooks", to: "webhooks#create"
6
4
 
5
+ # Deprecated singular alias, kept only until no repository points a hook at
6
+ # it. Repos provisioned before the plural path existed still target this URL,
7
+ # and nothing migrates them at deploy time — `WebhookProvisioner` deletes such
8
+ # hooks, but only for repositories it happens to touch when an integration is
9
+ # saved or `pr_monitor` runs. Dropping the route outright would 404 every
10
+ # untouched repository's hook and silently stop its events.
11
+ #
12
+ # Serving both spellings no longer duplicates anything: a repo carrying a hook
13
+ # on each receives the same X-GitHub-Delivery GUID twice and the second is
14
+ # discarded by the idempotency ledger.
15
+ post "webhook", to: "webhooks#create", as: :webhook
16
+
7
17
  # OAuth setup flow (popup wizard after callback)
8
18
  get "auth/setup", to: "auth#setup", as: :setup_auth
9
19
  post "auth/store_creative", to: "auth#store_creative", as: :store_creative_auth
@@ -0,0 +1,17 @@
1
+ class AddWebhookHookIdToGithubRepositoryLinks < ActiveRecord::Migration[8.1]
2
+ # The GitHub hook id that an instance of this app created for the repository.
3
+ #
4
+ # It is the only positive evidence that a hook found on GitHub belongs to a
5
+ # deployment sharing THIS database: the id was written here by whichever
6
+ # instance created it. Host or path similarity proves nothing — an unrelated
7
+ # deployment can serve the very same path — and deferring to a hook that
8
+ # feeds someone else's database would leave this instance receiving nothing.
9
+ #
10
+ # Stored on every link for the repository rather than only the primary one so
11
+ # that deleting a link does not lose the registration and let the next
12
+ # provisioning run create a second hook. This mirrors how `webhook_secret` is
13
+ # already aligned across a repository's links.
14
+ def change
15
+ add_column :github_repository_links, :webhook_hook_id, :bigint
16
+ end
17
+ end
@@ -0,0 +1,30 @@
1
+ class CreateGithubWebhookDeliveries < ActiveRecord::Migration[8.1]
2
+ # Durable idempotency key for GitHub webhook deliveries. GitHub stamps the
3
+ # `X-GitHub-Delivery` GUID per EVENT, not per delivery, so one GUID covers
4
+ # redeliveries AND the fan-out to every hook configured on the repo. A
5
+ # UNIQUE index therefore collapses both duplicate sources into one processed
6
+ # request. It must live in the DB, not in a process-local cache: multiple app
7
+ # instances share this database and each one receives its own copy.
8
+ #
9
+ # `processed_at` is what actually suppresses a redelivery. The row alone only
10
+ # records that some request claimed the GUID; if that request then died, the
11
+ # claim must not outlive it, or the redelivery would be answered 200 and the
12
+ # event dropped for good.
13
+ #
14
+ # `claim_token` identifies WHICH run holds the claim. A run that outlives
15
+ # STALE_CLAIM_AFTER has its claim taken over by the next delivery of the same
16
+ # GUID, and without a token the original run's cleanup would act on the GUID
17
+ # alone and delete the replacement's claim.
18
+ def change
19
+ create_table :github_webhook_deliveries do |t|
20
+ t.string :delivery_guid, null: false
21
+ t.string :event
22
+ t.string :claim_token
23
+ t.datetime :created_at, null: false
24
+ t.datetime :processed_at
25
+ end
26
+
27
+ add_index :github_webhook_deliveries, :delivery_guid, unique: true
28
+ add_index :github_webhook_deliveries, :created_at
29
+ end
30
+ end
@@ -0,0 +1,13 @@
1
+ class AddRepositoryIdIndexToGithubRepositoryLinks < ActiveRecord::Migration[8.1]
2
+ # The column has existed since the engine's first migration but was never
3
+ # written or read. Webhook routing now matches on it (it is the only
4
+ # repository identifier that survives a rename), so it needs an index.
5
+ # `webhook_hook_id` is also used as a request-time fallback for links that
6
+ # have not received a repository id yet, and must not require a table scan.
7
+ #
8
+ # Not unique: one repository is deliberately linked to many creatives.
9
+ def change
10
+ add_index :github_repository_links, :repository_id
11
+ add_index :github_repository_links, :webhook_hook_id
12
+ end
13
+ end
data/db/seeds.rb CHANGED
@@ -77,6 +77,7 @@ module CollavreGithub
77
77
  creative_retrieval_service
78
78
  creative_batch_service
79
79
  pr_monitor
80
+ pr_state_set
80
81
  ].freeze
81
82
 
82
83
  def self.call
@@ -122,7 +123,7 @@ module CollavreGithub
122
123
  end
123
124
 
124
125
  def default_llm_model
125
- ENV.fetch("COLLAVRE_DEFAULT_LLM_MODEL", "gemini-3.1-flash-lite")
126
+ ENV.fetch("COLLAVRE_DEFAULT_LLM_MODEL", "gemini-3.5-flash-lite")
126
127
  end
127
128
  end
128
129
  end
@@ -41,6 +41,15 @@ module CollavreGithub
41
41
  end
42
42
  end
43
43
 
44
+ initializer "collavre_github.register_tool_category", after: :load_config_initializers do
45
+ Rails.application.config.to_prepare do
46
+ if defined?(Collavre::McpToolCategories)
47
+ Collavre::McpToolCategories.register(:github, prefixes: %w[github_ pr_],
48
+ label_key: "collavre_github.tool_category")
49
+ end
50
+ end
51
+ end
52
+
44
53
  initializer "collavre_github.user_associations", after: :load_config_initializers do
45
54
  Rails.application.config.to_prepare do
46
55
  user_class = Collavre.user_class rescue nil
@@ -1,3 +1,3 @@
1
1
  module CollavreGithub
2
- VERSION = "0.6.3"
2
+ VERSION = "0.8.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: collavre_github
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Collavre
@@ -71,28 +71,42 @@ files:
71
71
  - app/javascript/github_wizard_state.js
72
72
  - app/jobs/collavre_github/initial_markdown_sync_job.rb
73
73
  - app/jobs/collavre_github/markdown_sync_job.rb
74
+ - app/jobs/collavre_github/pr_blocker_sweep_job.rb
75
+ - app/jobs/collavre_github/webhook_delivery_prune_job.rb
74
76
  - app/models/collavre_github/account.rb
75
77
  - app/models/collavre_github/application_record.rb
76
78
  - app/models/collavre_github/github_pr_channel.rb
77
79
  - app/models/collavre_github/repository_link.rb
80
+ - app/models/collavre_github/webhook_delivery.rb
78
81
  - app/services/collavre_github/client.rb
82
+ - app/services/collavre_github/markdown_sync/content_creative.rb
79
83
  - app/services/collavre_github/markdown_sync/content_processor.rb
80
84
  - app/services/collavre_github/markdown_sync/incremental_sync_service.rb
81
85
  - app/services/collavre_github/markdown_sync/initial_import_service.rb
86
+ - app/services/collavre_github/pr_blocker_monitor.rb
87
+ - app/services/collavre_github/pr_channel_state_updater.rb
88
+ - app/services/collavre_github/pr_discussion_event.rb
82
89
  - app/services/collavre_github/pr_topic_link_parser.rb
90
+ - app/services/collavre_github/repository_identity_synchronizer.rb
91
+ - app/services/collavre_github/repository_provisioning_lock.rb
83
92
  - app/services/collavre_github/tools/concerns/github_client_finder.rb
93
+ - app/services/collavre_github/tools/concerns/pr_channel_locator.rb
84
94
  - app/services/collavre_github/tools/github_pr_commits_service.rb
85
95
  - app/services/collavre_github/tools/github_pr_details_service.rb
86
96
  - app/services/collavre_github/tools/github_pr_diff_service.rb
87
97
  - app/services/collavre_github/tools/permission_denied_error.rb
88
98
  - app/services/collavre_github/tools/pr_monitor_service.rb
99
+ - app/services/collavre_github/tools/pr_state_set_service.rb
89
100
  - app/services/collavre_github/webhook_provisioner.rb
101
+ - app/services/collavre_github/webhook_reprovisioner.rb
90
102
  - app/views/collavre_github/auth/setup.html.erb
91
103
  - app/views/collavre_github/integrations/_modal.html.erb
92
104
  - config/initializers/integration_settings.rb
93
105
  - config/initializers/read_only_source.rb
94
106
  - config/locales/en.yml
95
107
  - config/locales/ko.yml
108
+ - config/locales/tool_descriptions.en.yml
109
+ - config/locales/tool_descriptions.ko.yml
96
110
  - config/routes.rb
97
111
  - db/migrate/20250925000000_create_github_integrations.rb
98
112
  - db/migrate/20250927000000_add_webhook_secret_to_github_repository_links.rb
@@ -101,6 +115,9 @@ files:
101
115
  - db/migrate/20260412000000_add_markdown_sync_to_repository_links.rb
102
116
  - db/migrate/20260416000000_add_markdown_root_creative_index.rb
103
117
  - db/migrate/20260711000000_encrypt_github_repository_link_webhook_secrets.rb
118
+ - db/migrate/20260727000001_add_webhook_hook_id_to_github_repository_links.rb
119
+ - db/migrate/20260727000002_create_github_webhook_deliveries.rb
120
+ - db/migrate/20260729000000_add_repository_id_index_to_github_repository_links.rb
104
121
  - db/seeds.rb
105
122
  - lib/collavre_github.rb
106
123
  - lib/collavre_github/engine.rb
@@ -127,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
144
  - !ruby/object:Gem::Version
128
145
  version: '0'
129
146
  requirements: []
130
- rubygems_version: 3.6.7
147
+ rubygems_version: 3.6.9
131
148
  specification_version: 4
132
149
  summary: GitHub integration for Collavre
133
150
  test_files: []