-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
147 changed files
with
12,397 additions
and
8,166 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -7,22 +7,11 @@ on: | |
- master | ||
|
||
jobs: | ||
test: | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
matrix: | ||
os: [ ubuntu-latest , macos-latest, windows-latest ] | ||
go-version: [ '1.21', '1.22' ] | ||
analyze: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Configure Windows | ||
if: matrix.os == 'windows-latest' | ||
run: git config --global core.autocrlf false # fixes go lint fmt error | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
- name: Setup Go | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: ${{ matrix.go-version }} | ||
- name: Lint | ||
uses: golangci/golangci-lint-action@v3 | ||
with: | ||
|
@@ -36,6 +25,23 @@ jobs: | |
autopilot | ||
bus bus/client | ||
worker worker/client | ||
test: | ||
needs: analyze | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
matrix: | ||
os: [ ubuntu-latest , macos-latest, windows-latest ] | ||
go-version: [ '1.21', '1.22' ] | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
- name: Setup Go | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: ${{ matrix.go-version }} | ||
- name: Configure Windows | ||
if: matrix.os == 'windows-latest' | ||
run: git config --global core.autocrlf false # fixes go lint fmt error | ||
- name: Configure MySQL | ||
if: matrix.os == 'ubuntu-latest' | ||
uses: mirromutth/[email protected] | ||
|
This file contains 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 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,175 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
"go.sia.tech/core/types" | ||
"go.sia.tech/renterd/webhooks" | ||
) | ||
|
||
const ( | ||
ModuleConsensus = "consensus" | ||
ModuleContract = "contract" | ||
ModuleContractSet = "contract_set" | ||
ModuleSetting = "setting" | ||
|
||
EventUpdate = "update" | ||
EventDelete = "delete" | ||
EventArchive = "archive" | ||
EventRenew = "renew" | ||
) | ||
|
||
var ( | ||
ErrUnknownEvent = errors.New("unknown event") | ||
) | ||
|
||
type ( | ||
EventConsensusUpdate struct { | ||
ConsensusState | ||
TransactionFee types.Currency `json:"transactionFee"` | ||
Timestamp time.Time `json:"timestamp"` | ||
} | ||
|
||
EventContractArchive struct { | ||
ContractID types.FileContractID `json:"contractID"` | ||
Reason string `json:"reason"` | ||
Timestamp time.Time `json:"timestamp"` | ||
} | ||
|
||
EventContractRenew struct { | ||
Renewal ContractMetadata `json:"renewal"` | ||
Timestamp time.Time `json:"timestamp"` | ||
} | ||
|
||
EventContractSetUpdate struct { | ||
Name string `json:"name"` | ||
ContractIDs []types.FileContractID `json:"contractIDs"` | ||
Timestamp time.Time `json:"timestamp"` | ||
} | ||
|
||
EventSettingUpdate struct { | ||
Key string `json:"key"` | ||
Update interface{} `json:"update"` | ||
Timestamp time.Time `json:"timestamp"` | ||
} | ||
|
||
EventSettingDelete struct { | ||
Key string `json:"key"` | ||
Timestamp time.Time `json:"timestamp"` | ||
} | ||
) | ||
|
||
var ( | ||
WebhookConsensusUpdate = func(url string, headers map[string]string) webhooks.Webhook { | ||
return webhooks.Webhook{ | ||
Event: EventUpdate, | ||
Headers: headers, | ||
Module: ModuleConsensus, | ||
URL: url, | ||
} | ||
} | ||
|
||
WebhookContractArchive = func(url string, headers map[string]string) webhooks.Webhook { | ||
return webhooks.Webhook{ | ||
Event: EventArchive, | ||
Headers: headers, | ||
Module: ModuleContract, | ||
URL: url, | ||
} | ||
} | ||
|
||
WebhookContractRenew = func(url string, headers map[string]string) webhooks.Webhook { | ||
return webhooks.Webhook{ | ||
Event: EventRenew, | ||
Headers: headers, | ||
Module: ModuleContract, | ||
URL: url, | ||
} | ||
} | ||
|
||
WebhookContractSetUpdate = func(url string, headers map[string]string) webhooks.Webhook { | ||
return webhooks.Webhook{ | ||
Event: EventUpdate, | ||
Headers: headers, | ||
Module: ModuleContractSet, | ||
URL: url, | ||
} | ||
} | ||
|
||
WebhookSettingUpdate = func(url string, headers map[string]string) webhooks.Webhook { | ||
return webhooks.Webhook{ | ||
Event: EventUpdate, | ||
Headers: headers, | ||
Module: ModuleSetting, | ||
URL: url, | ||
} | ||
} | ||
|
||
WebhookSettingDelete = func(url string, headers map[string]string) webhooks.Webhook { | ||
return webhooks.Webhook{ | ||
Event: EventDelete, | ||
Headers: headers, | ||
Module: ModuleSetting, | ||
URL: url, | ||
} | ||
} | ||
) | ||
|
||
func ParseEventWebhook(event webhooks.Event) (interface{}, error) { | ||
bytes, err := json.Marshal(event.Payload) | ||
if err != nil { | ||
return nil, err | ||
} | ||
switch event.Module { | ||
case ModuleContract: | ||
switch event.Event { | ||
case EventArchive: | ||
var e EventContractArchive | ||
if err := json.Unmarshal(bytes, &e); err != nil { | ||
return nil, err | ||
} | ||
return e, nil | ||
case EventRenew: | ||
var e EventContractRenew | ||
if err := json.Unmarshal(bytes, &e); err != nil { | ||
return nil, err | ||
} | ||
return e, nil | ||
} | ||
case ModuleContractSet: | ||
if event.Event == EventUpdate { | ||
var e EventContractSetUpdate | ||
if err := json.Unmarshal(bytes, &e); err != nil { | ||
return nil, err | ||
} | ||
return e, nil | ||
} | ||
case ModuleConsensus: | ||
if event.Event == EventUpdate { | ||
var e EventConsensusUpdate | ||
if err := json.Unmarshal(bytes, &e); err != nil { | ||
return nil, err | ||
} | ||
return e, nil | ||
} | ||
case ModuleSetting: | ||
switch event.Event { | ||
case EventUpdate: | ||
var e EventSettingUpdate | ||
if err := json.Unmarshal(bytes, &e); err != nil { | ||
return nil, err | ||
} | ||
return e, nil | ||
case EventDelete: | ||
var e EventSettingDelete | ||
if err := json.Unmarshal(bytes, &e); err != nil { | ||
return nil, err | ||
} | ||
return e, nil | ||
} | ||
} | ||
return nil, fmt.Errorf("%w: module %s event %s", ErrUnknownEvent, event.Module, event.Event) | ||
} |
This file contains 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 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 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.