Add tests for GetRevision

Signed-off-by: Steven Kriegler <sk.bunsenbrenner@gmail.com>
This commit is contained in:
justusbunsi 2022-07-11 15:53:28 +02:00
parent 525fa03065
commit d943a7f420
No known key found for this signature in database
GPG key ID: 82B29BF2507F9F8B
2 changed files with 38 additions and 5 deletions

View file

@ -7,6 +7,10 @@ import (
sqSdk "gitea-sonarqube-pr-bot/internal/clients/sonarqube"
)
type properties struct {
OriginalCommit string `json:"sonar.analysis.sqbot,omitempty"`
}
type Webhook struct {
ServerUrl string `json:"serverUrl"`
Revision string `json:"revision"`
@ -27,10 +31,8 @@ type Webhook struct {
Status string
} `json:"conditions"`
} `json:"qualityGate"`
Properties *struct {
OriginalCommit string `json:"sonar.analysis.sqbot,omitempty"`
} `json:"properties,omitempty"`
PRIndex int
Properties *properties `json:"properties,omitempty"`
PRIndex int
}
func (w *Webhook) GetRevision() string {

View file

@ -14,11 +14,12 @@ func TestNewWebhook(t *testing.T) {
RegExp: regexp.MustCompile(`^PR-(\d+)$`),
}
raw := []byte(`{ "serverUrl": "https://example.com/sonarqube", "taskId": "AXouyxDpizdp4B1K", "status": "SUCCESS", "analysedAt": "2021-05-21T12:12:07+0000", "revision": "f84442009c09b1adc278b6aa80a3853419f54007", "changedAt": "2021-05-21T12:12:07+0000", "project": { "key": "pr-bot", "name": "PR Bot", "url": "https://example.com/sonarqube/dashboard?id=pr-bot" }, "branch": { "name": "PR-1337", "type": "PULL_REQUEST", "isMain": false, "url": "https://example.com/sonarqube/dashboard?id=pr-bot&pullRequest=PR-1337" }, "qualityGate": { "name": "PR Bot", "status": "OK", "conditions": [ { "metric": "new_reliability_rating", "operator": "GREATER_THAN", "value": "1", "status": "OK", "errorThreshold": "1" }, { "metric": "new_security_rating", "operator": "GREATER_THAN", "value": "1", "status": "OK", "errorThreshold": "1" }, { "metric": "new_maintainability_rating", "operator": "GREATER_THAN", "value": "1", "status": "OK", "errorThreshold": "1" }, { "metric": "new_security_hotspots_reviewed", "operator": "LESS_THAN", "status": "NO_VALUE", "errorThreshold": "100" } ] }, "properties": {} }`)
raw := []byte(`{ "serverUrl": "https://example.com/sonarqube", "taskId": "AXouyxDpizdp4B1K", "status": "SUCCESS", "analysedAt": "2021-05-21T12:12:07+0000", "revision": "f84442009c09b1adc278b6aa80a3853419f54007", "changedAt": "2021-05-21T12:12:07+0000", "project": { "key": "pr-bot", "name": "PR Bot", "url": "https://example.com/sonarqube/dashboard?id=pr-bot" }, "branch": { "name": "PR-1337", "type": "PULL_REQUEST", "isMain": false, "url": "https://example.com/sonarqube/dashboard?id=pr-bot&pullRequest=PR-1337" }, "qualityGate": { "name": "PR Bot", "status": "OK", "conditions": [ { "metric": "new_reliability_rating", "operator": "GREATER_THAN", "value": "1", "status": "OK", "errorThreshold": "1" }, { "metric": "new_security_rating", "operator": "GREATER_THAN", "value": "1", "status": "OK", "errorThreshold": "1" }, { "metric": "new_maintainability_rating", "operator": "GREATER_THAN", "value": "1", "status": "OK", "errorThreshold": "1" }, { "metric": "new_security_hotspots_reviewed", "operator": "LESS_THAN", "status": "NO_VALUE", "errorThreshold": "100" } ] }, "properties": { "sonar.analysis.sqbot": "a84442009c09b1adc278b6bb80a3853419f54007" } }`)
response, ok := New(raw)
assert.NotNil(t, response)
assert.Equal(t, 1337, response.PRIndex)
assert.Equal(t, "a84442009c09b1adc278b6bb80a3853419f54007", response.Properties.OriginalCommit)
assert.True(t, ok)
t.Cleanup(func() {
@ -47,3 +48,33 @@ func TestNewWebhookInvalidBranchName(t *testing.T) {
settings.Pattern = nil
})
}
func TestWebhookGetRevision(t *testing.T) {
t.Run("Default revision", func(t *testing.T) {
w := Webhook{
Revision: "225fa0306c0ab83297d0cb5db0717b194ccb2e76",
}
assert.Equal(t, w.Revision, w.GetRevision())
})
t.Run("Default revision due to incomplete properties", func(t *testing.T) {
w := Webhook{
Revision: "225fa0306c0ab83297d0cb5db0717b194ccb2e76",
Properties: &properties{},
}
assert.Equal(t, w.Revision, w.GetRevision())
})
t.Run("Original commit from properties", func(t *testing.T) {
w := Webhook{
Revision: "225fa0306c0ab83297d0cb5db0717b194ccb2e76",
Properties: &properties{
OriginalCommit: "a9fe6800b0bbb70748aff53a011d8c09bbff42fe",
},
}
assert.Equal(t, w.Properties.OriginalCommit, w.GetRevision())
})
}