Skip to content

Commit 7d7b497

Browse files
author
Pedro Pombeiro
committed
fix(gpsup): avoid GitLab ruby runtime dependency
1 parent 97c8a44 commit 7d7b497

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

.agents/docs/shell.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ runs. History search widgets are registered early in the same file for this reas
126126
- Document environment variables in comments
127127
- Source `~/.zshrc` to test changes
128128

129+
## Standalone Ruby Helpers
130+
131+
Shell helper scripts under `~/.shellrc/zshrc.d/functions/scripts/` may run via plain Ruby
132+
(`ruby`, `mise x ruby -- ruby`, etc.) outside the GitLab application runtime.
133+
134+
- Do not use GitLab app-only Ruby constants or helpers such as `Gitlab::*`
135+
- Prefer stdlib and gem dependencies declared for the helper itself, such as `JSON.parse`
136+
- If a helper consumes external command output, handle parse failures explicitly so shell
137+
functions can choose whether to fail hard or continue with degraded behavior
138+
129139
## File Permissions
130140

131141
- **Config files** (`configs/`, `configs/pre/`, `configs/post/`): Must be executable (`chmod +x`)

.shellrc/zshrc.d/functions/scripts/gitlab-helpers.rb

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# frozen_string_literal: true
33

44
require "English"
5+
require "json"
56

67
REJECTED_LABELS = /^(workflow:|missed:|estimate:|Effort:|\[Deprecated|auto updated).*/
78

@@ -64,14 +65,21 @@ def graphql_execute(query, **kwargs)
6465
`op plugin run -- glab api graphql -f query='#{query}' #{kwargs.map { |k, v| "--field #{k}='#{v}'" }.join(" ")}`
6566
end
6667

68+
def parse_json(res, fatal: true)
69+
JSON.parse(res)
70+
rescue JSON::ParserError => e
71+
warn "Failed to parse GraphQL response: #{e.message}"
72+
exit(1) if fatal
73+
74+
nil
75+
end
76+
6777
BASELINE_MR_RATE = 13
6878

6979
def gitlab_mr_rate(*author)
7080
author = ARGV[0] if author.empty?
7181

7282
require "date"
73-
require "json"
74-
7583
mrs = []
7684
start_cursor = nil
7785
best_month = nil
@@ -106,7 +114,7 @@ def gitlab_mr_rate(*author)
106114
GQL
107115
return if $CHILD_STATUS != 0
108116

109-
json_res = Gitlab::Json.safe_parse(res)
117+
json_res = parse_json(res)
110118
merge_requests = json_res.dig("data", "group", "mergeRequests")
111119
mrs +=
112120
merge_requests["nodes"]
@@ -129,7 +137,10 @@ def gitlab_mr_rate(*author)
129137
now = DateTime.now
130138
mrs_merged_by_month = mrs
131139
.sort_by { |mr| now - mr[:merged_at] }
132-
.group_by { |mr| [now, DateTime.civil(mr[:merged_at].year, mr[:merged_at].month, -1)].min }
140+
.group_by do |mr|
141+
[now,
142+
DateTime.civil(mr[:merged_at].year, mr[:merged_at].month, -1)].min
143+
end
133144
mrs_merged_by_month.reverse_each do |ym, monthly_mrs|
134145
prorated_mr_count = monthly_mrs.count
135146
if ym.year == now.year && ym.month == now.month
@@ -203,17 +214,18 @@ def gpsup(remote, issue_iid)
203214
}
204215
GQL
205216

206-
require "json"
207217
milestone = nil
208218
labels = []
209219
if $CHILD_STATUS == 0
210-
json_res = Gitlab::Json.safe_parse(res)
211-
milestone = json_res.dig(*%w[data project group milestones nodes])
212-
.map { |h| h["title"] }
213-
.find { |title| title.match?(/^[0-9]+\.[0-9]+/) }
214-
labels = json_res.dig(*%w[data project issue labels nodes])
215-
&.map { |h| h["title"] }
216-
&.reject { |label| label.match?(REJECTED_LABELS) }
220+
json_res = parse_json(res, fatal: false)
221+
if json_res
222+
milestone = json_res.dig(*%w[data project group milestones nodes])
223+
.map { |h| h["title"] }
224+
.find { |title| title.match?(/^[0-9]+\.[0-9]+/) }
225+
labels = json_res.dig(*%w[data project issue labels nodes])
226+
&.map { |h| h["title"] }
227+
&.reject { |label| label.match?(REJECTED_LABELS) }
228+
end
217229
end
218230

219231
require_relative "git-helpers"

0 commit comments

Comments
 (0)