From 6bb5a611b3b6076305e4593ab7520145cdd02510 Mon Sep 17 00:00:00 2001 From: yangzhuolin Date: Tue, 20 Aug 2024 18:14:30 +0800 Subject: [PATCH 1/6] Improve the SubmitRecord structure --- changes.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/changes.go b/changes.go index 9d2bea7..6828178 100644 --- a/changes.go +++ b/changes.go @@ -223,17 +223,40 @@ type TopicInput struct { Topic string `json:"topic,omitempty"` } +type Account struct { + AccountId int `json:"_account_id"` + Name string `json:"name"` + Email string `json:"email"` + Username string `json:"username"` + Tags []string `json:"tags,omitempty"` +} + +type SubmitRecordLabel struct { + Label string `json:"label"` + Status string `json:"status"` + AppliedBy *Account `json:"applied_by,omitempty"` +} + // SubmitRecord entity describes results from a submit_rule. type SubmitRecord struct { + RuleName string `json:"rule_name"` Status string `json:"status"` + Labels []SubmitRecordLabel `json:"labels"` Ok map[string]map[string]AccountInfo `json:"ok,omitempty"` Reject map[string]map[string]AccountInfo `json:"reject,omitempty"` Need map[string]interface{} `json:"need,omitempty"` May map[string]map[string]AccountInfo `json:"may,omitempty"` Impossible map[string]interface{} `json:"impossible,omitempty"` + Requirements []Requirement `json:"requirements,omitempty"` ErrorMessage string `json:"error_message,omitempty"` } +type Requirement struct { + Status string `json:"status"` + FallbackText string `json:"fallback_text"` + Type string `json:"type"` +} + // SubmitInput entity contains information for submitting a change. type SubmitInput struct { OnBehalfOf string `json:"on_behalf_of,omitempty"` @@ -472,6 +495,7 @@ type ChangeInfo struct { CherryPickOfPatchSet int `json:"cherry_pick_of_patch_set,omitempty"` ContainsGitConflicts bool `json:"contains_git_conflicts,omitempty"` BaseChange string `json:"base_change,omitempty"` + SubmitRecords []SubmitRecord `json:"submit_records"` } // LabelInfo entity contains information about a label on a change, always corresponding to the current patch set. From 020688cb06e8f5fe82f3de9b2258773127bf446d Mon Sep 17 00:00:00 2001 From: yangzhuolin Date: Sat, 31 Aug 2024 11:26:56 +0800 Subject: [PATCH 2/6] add submitrecord...; resolve the current eventinfo not support the oldvalue in approvals --- changes.go | 52 ++++++++++++++++++++++++++------------------- changes_revision.go | 6 +++--- events.go | 44 +++++++++++++++++++------------------- 3 files changed, 55 insertions(+), 47 deletions(-) diff --git a/changes.go b/changes.go index 6828178..1acc854 100644 --- a/changes.go +++ b/changes.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "net/http" + "strconv" ) // RevisionKind describes the change kind. @@ -57,12 +58,27 @@ type AbandonInput struct { Notify string `json:"notify,omitempty"` NotifyDetails []NotifyInfo `json:"notify_details,omitempty"` } +type IntStr string + +func (i IntStr) Int() int { + v, err := strconv.Atoi(string(i)) + if err != nil { + return 0 + } + return v +} + +func (i IntStr) String() string { + return string(i) +} // ApprovalInfo entity contains information about an approval from a user for a label on a change. type ApprovalInfo struct { AccountInfo - Value int `json:"value,omitempty"` - Date string `json:"date,omitempty"` + Value IntStr `json:"value,omitempty"` + OldValue IntStr `json:"old_value,omitempty"` + Description string `json:"description,omitempty"` + Date string `json:"date,omitempty"` } // CommitMessageInput entity contains information for changing the commit message of a change. @@ -223,35 +239,27 @@ type TopicInput struct { Topic string `json:"topic,omitempty"` } -type Account struct { - AccountId int `json:"_account_id"` - Name string `json:"name"` - Email string `json:"email"` - Username string `json:"username"` - Tags []string `json:"tags,omitempty"` -} - -type SubmitRecordLabel struct { - Label string `json:"label"` - Status string `json:"status"` - AppliedBy *Account `json:"applied_by,omitempty"` +type SubmitRecordInfoLabel struct { + Label string `json:"label,omitempty"` + Status string `json:"status,omitempty"` + AppliedBy AccountInfo `json:"applied_by,omitempty"` } -// SubmitRecord entity describes results from a submit_rule. -type SubmitRecord struct { - RuleName string `json:"rule_name"` - Status string `json:"status"` - Labels []SubmitRecordLabel `json:"labels"` +// SubmitRecordInfo entity describes results from a submit_rule. +type SubmitRecordInfo struct { + RuleName string `json:"rule_name,omitempty"` + Status string `json:"status,omitempty"` + Labels []SubmitRecordInfoLabel `json:"labels,omitempty"` Ok map[string]map[string]AccountInfo `json:"ok,omitempty"` Reject map[string]map[string]AccountInfo `json:"reject,omitempty"` Need map[string]interface{} `json:"need,omitempty"` May map[string]map[string]AccountInfo `json:"may,omitempty"` Impossible map[string]interface{} `json:"impossible,omitempty"` - Requirements []Requirement `json:"requirements,omitempty"` + Requirements []RequirementInfo `json:"requirements,omitempty"` ErrorMessage string `json:"error_message,omitempty"` } -type Requirement struct { +type RequirementInfo struct { Status string `json:"status"` FallbackText string `json:"fallback_text"` Type string `json:"type"` @@ -495,7 +503,7 @@ type ChangeInfo struct { CherryPickOfPatchSet int `json:"cherry_pick_of_patch_set,omitempty"` ContainsGitConflicts bool `json:"contains_git_conflicts,omitempty"` BaseChange string `json:"base_change,omitempty"` - SubmitRecords []SubmitRecord `json:"submit_records"` + SubmitRecords []SubmitRecordInfo `json:"submit_records,omitempty"` } // LabelInfo entity contains information about a label on a change, always corresponding to the current patch set. diff --git a/changes_revision.go b/changes_revision.go index 719e1bb..c4f5427 100644 --- a/changes_revision.go +++ b/changes_revision.go @@ -482,10 +482,10 @@ func (s *ChangesService) TestSubmitType(ctx context.Context, changeID, revisionI // Request body may be either the Prolog code as text/plain or a RuleInput object. // The query parameter filters may be set to SKIP to bypass parent project filters while testing a project-specific rule. // -// The response is a list of SubmitRecord entries describing the permutations that satisfy the tested submit rule. +// The response is a list of SubmitRecordInfo entries describing the permutations that satisfy the tested submit rule. // // Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#test-submit-rule -func (s *ChangesService) TestSubmitRule(ctx context.Context, changeID, revisionID string, input *RuleInput) (*[]SubmitRecord, *Response, error) { +func (s *ChangesService) TestSubmitRule(ctx context.Context, changeID, revisionID string, input *RuleInput) (*[]SubmitRecordInfo, *Response, error) { u := fmt.Sprintf("changes/%s/revisions/%s/test.submit_rule", changeID, revisionID) req, err := s.client.NewRequest(ctx, "POST", u, input) @@ -493,7 +493,7 @@ func (s *ChangesService) TestSubmitRule(ctx context.Context, changeID, revisionI return nil, nil, err } - v := new([]SubmitRecord) + v := new([]SubmitRecordInfo) resp, err := s.client.Do(req, v) if err != nil { return nil, resp, err diff --git a/events.go b/events.go index 73280e7..69d2fda 100644 --- a/events.go +++ b/events.go @@ -40,28 +40,28 @@ type RefUpdate struct { // // Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/cmd-stream-events.html#events type EventInfo struct { - Type string `json:"type"` - Change ChangeInfo `json:"change,omitempty"` - ChangeKey ChangeInfo `json:"changeKey,omitempty"` - PatchSet PatchSet `json:"patchSet,omitempty"` - EventCreatedOn int `json:"eventCreatedOn,omitempty"` - Reason string `json:"reason,omitempty"` - Abandoner AccountInfo `json:"abandoner,omitempty"` - Restorer AccountInfo `json:"restorer,omitempty"` - Submitter AccountInfo `json:"submitter,omitempty"` - Author AccountInfo `json:"author,omitempty"` - Uploader AccountInfo `json:"uploader,omitempty"` - Approvals []AccountInfo `json:"approvals,omitempty"` - Comment string `json:"comment,omitempty"` - Editor AccountInfo `json:"editor,omitempty"` - Added []string `json:"added,omitempty"` - Removed []string `json:"removed,omitempty"` - Hashtags []string `json:"hashtags,omitempty"` - RefUpdate RefUpdate `json:"refUpdate,omitempty"` - Project ProjectInfo `json:"project,omitempty"` - Reviewer AccountInfo `json:"reviewer,omitempty"` - OldTopic string `json:"oldTopic,omitempty"` - Changer AccountInfo `json:"changer,omitempty"` + Type string `json:"type"` + Change ChangeInfo `json:"change,omitempty"` + ChangeKey ChangeInfo `json:"changeKey,omitempty"` + PatchSet PatchSet `json:"patchSet,omitempty"` + EventCreatedOn int `json:"eventCreatedOn,omitempty"` + Reason string `json:"reason,omitempty"` + Abandoner AccountInfo `json:"abandoner,omitempty"` + Restorer AccountInfo `json:"restorer,omitempty"` + Submitter AccountInfo `json:"submitter,omitempty"` + Author AccountInfo `json:"author,omitempty"` + Uploader AccountInfo `json:"uploader,omitempty"` + Approvals []ApprovalInfo `json:"approvals,omitempty"` + Comment string `json:"comment,omitempty"` + Editor AccountInfo `json:"editor,omitempty"` + Added []string `json:"added,omitempty"` + Removed []string `json:"removed,omitempty"` + Hashtags []string `json:"hashtags,omitempty"` + RefUpdate RefUpdate `json:"refUpdate,omitempty"` + Project ProjectInfo `json:"project,omitempty"` + Reviewer AccountInfo `json:"reviewer,omitempty"` + OldTopic string `json:"oldTopic,omitempty"` + Changer AccountInfo `json:"changer,omitempty"` } // EventsLogService contains functions for querying the API provided From 8acfefdb9b8bfa98230842924e5c670a6b557397 Mon Sep 17 00:00:00 2001 From: yangzhuolin Date: Sat, 31 Aug 2024 18:26:54 +0800 Subject: [PATCH 3/6] resolve the IntStr can't unmarshal json --- .idea/.gitignore | 8 ++++++++ .idea/deployment.xml | 14 ++++++++++++++ .idea/go-gerrit.iml | 9 +++++++++ .idea/modules.xml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ changes.go | 14 ++++++++++++++ 6 files changed, 59 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/deployment.xml create mode 100644 .idea/go-gerrit.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/deployment.xml b/.idea/deployment.xml new file mode 100644 index 0000000..43fbddd --- /dev/null +++ b/.idea/deployment.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/go-gerrit.iml b/.idea/go-gerrit.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/go-gerrit.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..a3970a7 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/changes.go b/changes.go index 1acc854..3c57148 100644 --- a/changes.go +++ b/changes.go @@ -60,6 +60,20 @@ type AbandonInput struct { } type IntStr string +// UnmarshalJSON implements the json.Unmarshaler interface for IntStr +func (i *IntStr) UnmarshalJSON(data []byte) error { + // Remove quotes from the JSON string + str := string(data) + // Convert string to int + val, err := strconv.Atoi(str) + if err != nil { + return err + } + // Set the value + *i = IntStr(fmt.Sprintf("%d", val)) + return nil +} + func (i IntStr) Int() int { v, err := strconv.Atoi(string(i)) if err != nil { From 267b8cc0cfc475d99f3387a1f80d7c38d4ace3f4 Mon Sep 17 00:00:00 2001 From: yangzhuolin Date: Sat, 31 Aug 2024 18:30:33 +0800 Subject: [PATCH 4/6] Remove .idea folder from repository --- .gitignore | 4 +++- .idea/.gitignore | 8 -------- .idea/deployment.xml | 14 -------------- .idea/go-gerrit.iml | 9 --------- .idea/modules.xml | 8 -------- .idea/vcs.xml | 6 ------ 6 files changed, 3 insertions(+), 46 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/deployment.xml delete mode 100644 .idea/go-gerrit.iml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.gitignore b/.gitignore index 4c7d39e..787a7fd 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -/coverage.txt \ No newline at end of file +/coverage.txt + +.idea/ \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/deployment.xml b/.idea/deployment.xml deleted file mode 100644 index 43fbddd..0000000 --- a/.idea/deployment.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/go-gerrit.iml b/.idea/go-gerrit.iml deleted file mode 100644 index 5e764c4..0000000 --- a/.idea/go-gerrit.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index a3970a7..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From 17faa1dba0afc641a87a95e1f7799305b05b319c Mon Sep 17 00:00:00 2001 From: yangzhuolin Date: Wed, 4 Sep 2024 13:29:58 +0800 Subject: [PATCH 5/6] =?UTF-8?q?Intstr=20=E5=A4=84=E7=90=86=E8=BD=AC?= =?UTF-8?q?=E4=B9=89=E5=AD=97=E7=AC=A6=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- changes.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/changes.go b/changes.go index 3c57148..70f16fe 100644 --- a/changes.go +++ b/changes.go @@ -7,6 +7,7 @@ import ( "io" "net/http" "strconv" + "strings" ) // RevisionKind describes the change kind. @@ -62,14 +63,12 @@ type IntStr string // UnmarshalJSON implements the json.Unmarshaler interface for IntStr func (i *IntStr) UnmarshalJSON(data []byte) error { - // Remove quotes from the JSON string str := string(data) - // Convert string to int - val, err := strconv.Atoi(str) + str = strings.Trim(str, "\"") + val, err := strconv.Atoi(strings.Trim(str, "/")) if err != nil { return err } - // Set the value *i = IntStr(fmt.Sprintf("%d", val)) return nil } From 4ee8af4425afbc5adb76a19fa7dbfef1f51dd023 Mon Sep 17 00:00:00 2001 From: Ai-feier <84133253+Ai-feier@users.noreply.github.com> Date: Sun, 15 Sep 2024 16:34:50 +0800 Subject: [PATCH 6/6] Update changes.go --- changes.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changes.go b/changes.go index 70f16fe..f32c332 100644 --- a/changes.go +++ b/changes.go @@ -89,7 +89,7 @@ func (i IntStr) String() string { type ApprovalInfo struct { AccountInfo Value IntStr `json:"value,omitempty"` - OldValue IntStr `json:"old_value,omitempty"` + OldValue IntStr `json:"oldValue,omitempty"` Description string `json:"description,omitempty"` Date string `json:"date,omitempty"` }