-
Notifications
You must be signed in to change notification settings - Fork 0
feat(entities) Adds change provider entity and mysql backed store for it #44
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
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6b063a2
(entities) Adds change provider entity and mysql backed store for it
manjari25 076451a
fix
manjari25 49ab944
fix land test to add new change provider store
manjari25 f78e09e
version is not needed for the change provider
manjari25 a184bfa
add description for ChangeProvider entity
manjari25 c3406ad
remove queue from fields for ChangeProvider
manjari25 5312f79
update type of metadata
manjari25 5704c39
address comments
manjari25 9fdcaf4
fix
manjari25 5c64579
comment
manjari25 91e968e
Merge branch 'main' into manjari/entities
behinddwalls 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
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,15 @@ | ||
| package entity | ||
|
|
||
| // ChangeProvider represents a code change from an external provider (e.g., a GitHub pull request or Gerrit changelist) | ||
| // along with its associated metadata. The object is immutable after creation. | ||
| type ChangeProvider struct { | ||
| // RequestID is the globally unique identifier for the land request. Format: "<queue>/<counter_value>". | ||
| RequestID string | ||
| // ChangeProviderSrc defines the source of the change. For e.g. - Github, Gitlab etc. | ||
| ChangeProviderSrc string | ||
| // ChangeProviderID is the identifier specified by the change provider source. For e.g. - Github PR ID etc. | ||
| ChangeProviderID string | ||
| // Metadata is the interesting data from the change provider that we want to store. | ||
| // This is a freeform JSON object. | ||
| Metadata map[string]string | ||
| } | ||
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,26 @@ | ||
| package storage | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/uber/submitqueue/entity" | ||
| ) | ||
|
|
||
| // ChangeProviderStore is an interface that defines methods for managing change provider information in the database. | ||
| type ChangeProviderStore interface { | ||
| // Get retrieves information about a change by ID. | ||
| // Returns ErrNotFound if the change provider is not found. | ||
| // | ||
| // Note: The order of ChangeProvider entities here is not guaranteed to | ||
| // be the same as the request to which it belongs. The caller is repsonsible | ||
| // for inspecting and mapping the result of this function to the | ||
| // order of changes within the original request. | ||
| // | ||
| Get(ctx context.Context, requestID string) ([]entity.ChangeProvider, error) | ||
|
|
||
| // Create creates a new change provider. | ||
| Create(ctx context.Context, changeProvider entity.ChangeProvider) error | ||
|
|
||
| // There is no update function since once created, data is only ever read from this | ||
| // store. | ||
| } |
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,88 @@ | ||
| package mysql | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/go-sql-driver/mysql" | ||
|
|
||
| "github.com/uber/submitqueue/entity" | ||
| "github.com/uber/submitqueue/extension/storage" | ||
| ) | ||
|
|
||
| type changeProviderStore struct { | ||
| db *sql.DB | ||
| } | ||
|
|
||
| // NewChangeProviderStore creates a new MySQL-backed ChangeProviderStore. | ||
| func NewChangeProviderStore(db *sql.DB) storage.ChangeProviderStore { | ||
| return &changeProviderStore{db: db} | ||
| } | ||
|
|
||
| // Get retrieves change provider(s) by request ID. Returns ErrNotFound if the change provider is not found. | ||
|
behinddwalls marked this conversation as resolved.
|
||
| // | ||
| // Note: The order of ChangeProvider entities returned here is not guaranteed | ||
| // to be the same as the request to which it belongs. The caller is repsonsible | ||
| // for inspecting and mapping the result of this function to the | ||
| // order of changes within the original request. | ||
| // | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: empty line |
||
| func (s *changeProviderStore) Get(ctx context.Context, requestID string) ([]entity.ChangeProvider, error) { | ||
| rows, err := s.db.QueryContext(ctx, | ||
| "SELECT request_id, change_provider_src, change_provider_id, metadata FROM change_provider WHERE request_id = ?", | ||
| requestID, | ||
| ) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get change provider entities requestID=%s from the database: %w", requestID, err) | ||
| } | ||
| defer rows.Close() | ||
|
|
||
| var results []entity.ChangeProvider | ||
| for rows.Next() { | ||
| var cp entity.ChangeProvider | ||
| var metadataJSON []byte | ||
|
|
||
| if err := rows.Scan(&cp.RequestID, &cp.ChangeProviderSrc, &cp.ChangeProviderID, &metadataJSON); err != nil { | ||
| return nil, fmt.Errorf("failed to scan change provider entity requestID=%s from the database: %w", requestID, err) | ||
| } | ||
|
|
||
| if err := json.Unmarshal(metadataJSON, &cp.Metadata); err != nil { | ||
| return nil, fmt.Errorf("failed to unmarshal metadata for change provider entity requestID=%s from the database: %w", requestID, err) | ||
| } | ||
|
|
||
| results = append(results, cp) | ||
| } | ||
| if err := rows.Err(); err != nil { | ||
| return nil, fmt.Errorf("failed to iterate change provider entities requestID=%s from the database: %w", requestID, err) | ||
| } | ||
|
|
||
| if len(results) == 0 { | ||
| return nil, storage.WrapNotFound(fmt.Errorf("change provider entity requestID=%s", requestID)) | ||
| } | ||
|
|
||
| return results, nil | ||
| } | ||
|
|
||
| // Create creates a new change provider. Returns ErrAlreadyExists if the entry already exists. | ||
| func (s *changeProviderStore) Create(ctx context.Context, changeProvider entity.ChangeProvider) error { | ||
| metadataJSON, err := json.Marshal(changeProvider.Metadata) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to marshal metadata id=%s for Create change provider entity: %w", changeProvider.RequestID, err) | ||
| } | ||
|
|
||
| _, err = s.db.ExecContext(ctx, | ||
| "INSERT INTO change_provider (request_id, change_provider_src, change_provider_id, metadata) VALUES (?, ?, ?, ?)", | ||
| changeProvider.RequestID, changeProvider.ChangeProviderSrc, changeProvider.ChangeProviderID, metadataJSON, | ||
| ) | ||
| if err != nil { | ||
| var mysqlErr *mysql.MySQLError | ||
| if errors.As(err, &mysqlErr) && mysqlErr.Number == 1062 { | ||
| return fmt.Errorf("change provider entity id=%s: %w", changeProvider.RequestID, storage.ErrAlreadyExists) | ||
| } | ||
| return fmt.Errorf("failed to insert change provider entity id=%s: %w", changeProvider.RequestID, err) | ||
| } | ||
|
|
||
| return 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,7 @@ | ||
| CREATE TABLE IF NOT EXISTS change_provider ( | ||
| request_id VARCHAR(255) NOT NULL, | ||
| change_provider_src VARCHAR(255) NOT NULL, | ||
| change_provider_id VARCHAR(255) NOT NULL, | ||
| metadata JSON NOT NUll, | ||
| PRIMARY KEY (request_id,change_provider_src,change_provider_id) | ||
| ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
plz comment the struct itself
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done