-
Notifications
You must be signed in to change notification settings - Fork 0
[feat] Draft showcase implementation of Request entity #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| load("@rules_go//go:def.bzl", "go_library", "go_test") | ||
|
|
||
| go_library( | ||
| name = "entities", | ||
| srcs = ["request.go"], | ||
| importpath = "github.com/uber/submitqueue/entities", | ||
| visibility = ["//visibility:public"], | ||
| ) | ||
|
|
||
| go_test( | ||
| name = "entities_test", | ||
| srcs = ["request_test.go"], | ||
| embed = [":entities"], | ||
| deps = [ | ||
| "@com_github_stretchr_testify//assert", | ||
| "@com_github_stretchr_testify//require", | ||
| ], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| package entities | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| // RequestLandStrategy defines the possible source control integration methods. | ||
| type RequestLandStrategy int | ||
|
|
||
| const ( | ||
| // RequestLandStrategyDefault lets the server decide based on configuration. | ||
| RequestLandStrategyDefault RequestLandStrategy = 0 | ||
| // RequestLandStrategyRebase rebases commits onto the target branch before landing. | ||
| RequestLandStrategyRebase = 1 | ||
| // RequestLandStrategySquashRebase squashes commits into a single commit before rebase. | ||
| RequestLandStrategySquashRebase = 2 | ||
| // RequestLandStrategyMerge merges commits into the target branch by creating a separate merge commit, preserving the commit history along with hashes. | ||
| RequestLandStrategyMerge = 3 | ||
| ) | ||
|
|
||
| type RequestState int | ||
|
|
||
| // TODO: define all states | ||
| const ( | ||
| // RequestStateUnknown is the unreachable state. It is set by default when the structure is initialized. It should never be seen in the system. | ||
| RequestStateUnknown RequestState = 0 | ||
| // RequestStateNew is the initial state of a land request. It is confirmed by the system but the processing is not started yet. | ||
| RequestStateNew RequestState = 1 | ||
| // RequestStateProcessing is the state of a land request that is being processed. | ||
| RequestStateProcessing = 2 | ||
| // RequestStateLanded is the state of a land request that has been successfully processed and landed. This is the final state. | ||
| RequestStateLanded = 3 | ||
| // RequestStateError is the state of a land request that has encountered an error. This is the final state. | ||
| RequestStateError = 4 | ||
| ) | ||
|
|
||
| // Change represents a set of related code changes identified by one or more IDs from a particular code change provider, like Github Pull Requests. | ||
| // The object is immutable after creation. | ||
| type Change struct { | ||
| // Source is the code change provider (e.g., "github", "gerrit", "phabricator"). | ||
| Source string | ||
| // IDs is a list of change IDs, in a format specific to the code change provider, that should be landed together. | ||
| IDs []string | ||
| } | ||
|
|
||
| // Request defines a request to land (merge into target branch of the source control repository) a set of code changes. | ||
| // The object is immutable after creation. | ||
| type Request struct { | ||
| // **************** | ||
| // Immutable fields, fixed at request entity creation | ||
| // **************** | ||
|
|
||
| // Queue is the name of the queue processing the land request. Queue name is defined in the configuration and should be unique within the system. | ||
| Queue string | ||
| // Seq is an autoincrementing integer identifier for the land request. It is unique within the queue. | ||
| Seq int64 | ||
| // Change is a number of code changes (such as pull requests) to land into the target branch. Target branch is defined by the queue configuration. | ||
| Change Change | ||
| // LandStrategy is the source control integration strategy to use for this land operation. If not specified, the default queue strategy is used. | ||
| LandStrategy RequestLandStrategy | ||
|
|
||
| // **************** | ||
| // Following fields could be changed throughout the lifecycle of the request | ||
| // **************** | ||
|
|
||
| // State is the current state of the land request. | ||
| State RequestState | ||
| // Version is the version of the object. It is used for optimistic locking. | ||
| // Versioning starts at 1 and is incremented for each change to the object. | ||
| Version int32 | ||
| } | ||
|
|
||
| // GetID returns the globally unique identifier for the land request. | ||
| func (r *Request) GetID() string { | ||
| return fmt.Sprintf("%s/%d", r.Queue, r.Seq) | ||
| } | ||
|
|
||
| // ParseRequestID parses the globally unique identifier for the land request and returns the queue name and sequence number. | ||
| func ParseRequestID(id string) (queue string, seq int64, err error) { | ||
| parts := strings.Split(id, "/") | ||
| if len(parts) != 2 { | ||
| return "", 0, fmt.Errorf("invalid format of the request ID: %s; expected format: <queue>/<seq>", id) | ||
| } | ||
|
|
||
| seq, err = strconv.ParseInt(parts[1], 10, 64) | ||
| if err != nil { | ||
| return "", 0, fmt.Errorf("invalid sequence number in the request ID: %s; expected format: <queue>/<seq>; parsing error: %w", id, err) | ||
| } | ||
|
|
||
| if seq <= 0 { | ||
| return "", 0, fmt.Errorf("invalid sequence number in the request ID: %s; expected format: <queue>/<seq>; sequence number must be greater than 0 but got %d", id, seq) | ||
| } | ||
|
|
||
| return parts[0], seq, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| package entities | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestRequest_GetID(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| request Request | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "standard ID", | ||
| request: Request{Queue: "my-queue", Seq: 42}, | ||
| expected: "my-queue/42", | ||
| }, | ||
| { | ||
| name: "seq 1", | ||
| request: Request{Queue: "q", Seq: 1}, | ||
| expected: "q/1", | ||
| }, | ||
| { | ||
| name: "large seq", | ||
| request: Request{Queue: "prod", Seq: 9999999}, | ||
| expected: "prod/9999999", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| assert.Equal(t, tt.expected, tt.request.GetID()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestParseRequestID(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| id string | ||
| wantQueue string | ||
| wantSeq int64 | ||
| expectError bool | ||
| }{ | ||
| { | ||
| name: "valid ID", | ||
| id: "my-queue/42", | ||
| wantQueue: "my-queue", | ||
| wantSeq: 42, | ||
| }, | ||
| { | ||
| name: "seq 1", | ||
| id: "q/1", | ||
| wantQueue: "q", | ||
| wantSeq: 1, | ||
| }, | ||
| { | ||
| name: "missing separator", | ||
| id: "no-separator", | ||
| expectError: true, | ||
| }, | ||
| { | ||
| name: "too many separators", | ||
| id: "a/b/c", | ||
| expectError: true, | ||
| }, | ||
| { | ||
| name: "empty string", | ||
| id: "", | ||
| expectError: true, | ||
| }, | ||
| { | ||
| name: "non-numeric seq", | ||
| id: "queue/abc", | ||
| expectError: true, | ||
| }, | ||
| { | ||
| name: "zero seq", | ||
| id: "queue/0", | ||
| expectError: true, | ||
| }, | ||
| { | ||
| name: "negative seq", | ||
| id: "queue/-1", | ||
| expectError: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| queue, seq, err := ParseRequestID(tt.id) | ||
| if tt.expectError { | ||
| require.Error(t, err) | ||
| return | ||
| } | ||
| require.NoError(t, err) | ||
| assert.Equal(t, tt.wantQueue, queue) | ||
| assert.Equal(t, tt.wantSeq, seq) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGetID_ParseRequestID_Roundtrip(t *testing.T) { | ||
| req := &Request{Queue: "test-queue", Seq: 123} | ||
| queue, seq, err := ParseRequestID(req.GetID()) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, req.Queue, queue) | ||
| assert.Equal(t, req.Seq, seq) | ||
| } |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package storage | ||
|
|
||
| import "errors" | ||
|
|
||
| // ErrNotFound is returned by storage implementations when the requested record is not found in the database. | ||
| var ErrNotFound = errors.New("record not found") | ||
|
|
||
| // ErrVersionMismatch is returned by storage implementations when the expected entity version does not match the current version of the object. | ||
| // This is used to implement an optimistic locking mechanism, allowing multiple clients to update the same entity concurrently | ||
| // and either retry or implement idempotent operations. | ||
| var ErrVersionMismatch = errors.New("version mismatch") | ||
|
|
||
| // StoreFactory is an interface that defines methods for creating different stores.. | ||
| // Each store is responsible for performing atomic storage operations for a specific entity type. | ||
| type StoreFactory interface { | ||
| // GetRequestStore creates a new RequestStore instance. | ||
| GetRequestStore() RequestStore | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.