Skip to content

Commit

Permalink
Merge branch 'feature/expression-validation' into feature/expression-…
Browse files Browse the repository at this point in the history
…validation
  • Loading branch information
Tetrergeru authored Nov 3, 2023
2 parents 5245495 + d0cc902 commit 9f2f59d
Show file tree
Hide file tree
Showing 218 changed files with 9,098 additions and 2,217 deletions.
9 changes: 9 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

# Check if any of the specified files are staged for commit
if git diff --name-only --cached | grep -E 'api/'; then
echo "Format swaggo annotations (swag fmt)"
swag fmt
fi

make lint
4 changes: 2 additions & 2 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ For full local deployment of all services, including web, graphite and metrics r
docker-compose up
```

Before pushing your changes don't forget about linter:
Before pushing, don't forget to write this command please, it will activate the pre-commit hook on the linter and auto formatting swagger documentation:

```bash
make lint
git config --local core.hooksPath .githooks/
```
6 changes: 3 additions & 3 deletions .github/workflows/docker-feature.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
pull-requests: write
strategy:
matrix:
servis: [api, checker, cli, notifier, filter]
services: [api, checker, cli, notifier, filter]
runs-on: ubuntu-22.04
if: ${{github.event.issue.pull_request != null && startsWith(github.event.comment.body, '/build') && github.event.comment.author_association == 'MEMBER'}}
steps:
Expand Down Expand Up @@ -64,12 +64,12 @@ jobs:
uses: docker/build-push-action@v4
with:
context: "https://github.com/${{fromJSON(steps.get-pr.outputs.result).head.repo.full_name}}.git#${{fromJSON(steps.get-pr.outputs.result).head.ref}}"
file: ./Dockerfile.${{matrix.servis}}
file: ./Dockerfile.${{matrix.services}}
build-args: |
MoiraVersion=${{ env.DOCKER_TAG }}
GIT_COMMIT=${{ fromJSON(steps.get-pr.outputs.result).head.sha }}
push: true
tags: moira/${{matrix.servis}}-unstable:${{env.DOCKER_TAG}}
tags: moira/${{matrix.services}}-unstable:${{env.DOCKER_TAG}}

- name: Comment PR with build tag
uses: mshick/add-pr-comment@v2
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/docker-nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-22.04
strategy:
matrix:
servis: [api, checker, cli, notifier, filter]
services: [api, checker, cli, notifier, filter]
steps:

- name: Set up Docker Buildx
Expand All @@ -28,13 +28,13 @@ jobs:
- name: Build and push
uses: docker/build-push-action@v4
with:
file: ./Dockerfile.${{matrix.servis}}
file: ./Dockerfile.${{matrix.services}}
build-args: |
MoiraVersion=${{env.DOCKER_TAG}}
GIT_COMMIT=${{github.sha}}
push: true
tags: moira/${{matrix.servis}}-nightly:${{env.DOCKER_TAG}}

tags: moira/${{matrix.services}}-nightly:${{env.DOCKER_TAG}},moira/${{matrix.services}}-nightly:latest
- name: Comment PR with build tag
uses: mshick/add-pr-comment@v2
if: always()
Expand Down
7 changes: 3 additions & 4 deletions .github/workflows/docker-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-22.04
strategy:
matrix:
servis: [api, checker, cli, notifier, filter]
services: [api, checker, cli, notifier, filter]
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
Expand All @@ -27,10 +27,9 @@ jobs:
- name: Build and push
uses: docker/build-push-action@v4
with:
file: ./Dockerfile.${{matrix.servis}}
file: ./Dockerfile.${{matrix.services}}
build-args: |
MoiraVersion=${{env.DOCKER_TAG}}
GIT_COMMIT=${{github.sha}}
push: true
tags: moira/${{matrix.servis}}:${{env.DOCKER_TAG}},moira/${{matrix.servis}}:latest

tags: moira/${{matrix.services}}:${{env.DOCKER_TAG}},moira/${{matrix.services}}:latest
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ jobs:
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.52.2
version: v1.54.2
only-new-issues: true

4 changes: 2 additions & 2 deletions .github/workflows/publish-packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ jobs:
uses: docker://antonyurchenko/git-release:latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DRAFT_RELEASE: "false"
PRE_RELEASE: "false"
DRAFT_RELEASE: "true"
PRE_RELEASE: "true"
CHANGELOG_FILE: "none"
ALLOW_EMPTY_CHANGELOG: "true"
with:
Expand Down
27 changes: 27 additions & 0 deletions .github/workflows/swagger-delete.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Delete spec version from SwaggerHub
defaults:
run:
working-directory: .
on:
push:
branches:
- master
- release/[0-9]+.[0-9]+.[0-9]+

jobs:
removespec:
name: Delete api from SwaggerHub
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '16.16.0'
- run: npm i --location=global swaggerhub-cli
- run: |
VERSION=`echo ${GITHUB_REF_NAME}| sed 's#[^a-zA-Z0-9_\.\-]#_#g'`
SWAGGERHUB_API_KEY=${{secrets.SWAGGERHUB_TOKEN}} swaggerhub api:unpublish "Moira/moira-alert/${VERSION}" || true
SWAGGERHUB_API_KEY=${{secrets.SWAGGERHUB_TOKEN}} swaggerhub api:delete "Moira/moira-alert/${VERSION}" || true
# The `|| true` at the end of the calls is necessary to keep the job from crashing
# when deleting documentation that hasn't been created yet, but if you see something wrong happening,
# remove `|| true` from the command
60 changes: 60 additions & 0 deletions .github/workflows/swagger-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Publish spec version to SwaggerHub

on:
push:
branches:
- master
- release/[0-9]+.[0-9]+.[0-9]+

jobs:
validate-spec:
name: Validate spec file
runs-on: ubuntu-22.04
defaults:
run:
working-directory: .

steps:
- uses: actions/checkout@v3

- uses: actions/setup-go@v4
with:
go-version-file: go.mod
cache-dependency-path: go.sum
- run: make install-swag

- uses: actions/setup-node@v3
with:
node-version: '16.16.0'
- run: npm install --location=global @openapitools/openapi-generator-cli
- run: make spec
- run: make validate-spec

- name: Save build artifact
uses: actions/upload-artifact@v3
with:
name: specfile
path: docs/swagger.yaml

publishspec:
name: Upload generated OpenAPI description
runs-on: ubuntu-22.04
needs: validate-spec
defaults:
run:
working-directory: .

steps:
- uses: actions/checkout@v3

- name: Download spec file artifact
uses: actions/download-artifact@v3
with:
name: specfile
path: docs

- uses: actions/setup-node@v3
- run: npm i --location=global swaggerhub-cli
- run: |
VERSION=`echo ${GITHUB_REF_NAME}| sed 's#[^a-zA-Z0-9_\.\-]#_#g'`
SWAGGERHUB_API_KEY=${{secrets.SWAGGERHUB_TOKEN}} swaggerhub api:create "Moira/moira-alert/${VERSION}" -f ./docs/swagger.yaml --published=publish --visibility=public
28 changes: 28 additions & 0 deletions .github/workflows/swagger-validate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Validate OpenAPI on PR

on:
- pull_request

jobs:
mergespec:
name: Validate spec file
runs-on: ubuntu-22.04
defaults:
run:
working-directory: .

steps:
- uses: actions/checkout@v3

- uses: actions/setup-go@v4
with:
go-version-file: go.mod
cache-dependency-path: go.sum
- run: make install-swag

- uses: actions/setup-node@v3
with:
node-version: '16.16.0'
- run: npm install --location=global @openapitools/openapi-generator-cli
- run: make spec
- run: make validate-spec
7 changes: 7 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ linters:
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint
disable-all: true
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- dogsled
- errcheck
- goconst
- gocyclo
Expand All @@ -34,8 +38,11 @@ linters:
- gosimple
- govet
- ineffassign
- makezero
- misspell
- nilerr
- prealloc
- promlinter
- staticcheck
- typecheck
- unconvert
Expand Down
5 changes: 4 additions & 1 deletion Dockerfile.api
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
FROM golang:1.18 as builder
FROM golang:1.20 as builder

COPY go.mod go.sum /go/src/github.com/moira-alert/moira/
WORKDIR /go/src/github.com/moira-alert/moira
RUN go mod download
RUN go install github.com/swaggo/swag/cmd/[email protected]

COPY . /go/src/github.com/moira-alert/moira/

RUN /go/bin/swag init -g api/handler/handler.go

ARG GO_VERSION="GoVersion"
ARG GIT_COMMIT="git_Commit"
ARG MoiraVersion="MoiraVersion"
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.checker
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.18 as builder
FROM golang:1.20 as builder

COPY go.mod go.sum /go/src/github.com/moira-alert/moira/
WORKDIR /go/src/github.com/moira-alert/moira
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.cli
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.18 as builder
FROM golang:1.20 as builder

COPY go.mod go.sum /go/src/github.com/moira-alert/moira/
WORKDIR /go/src/github.com/moira-alert/moira
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.filter
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.18 as builder
FROM golang:1.20 as builder

COPY go.mod go.sum /go/src/github.com/moira-alert/moira/
WORKDIR /go/src/github.com/moira-alert/moira
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.notifier
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.18 as builder
FROM golang:1.20 as builder

COPY go.mod go.sum /go/src/github.com/moira-alert/moira/
WORKDIR /go/src/github.com/moira-alert/moira
Expand Down
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ lint:
mock:
. ./generate_mocks.sh

.PHONY: install-swag
install-swag:
go install github.com/swaggo/swag/cmd/[email protected]

.PHONY: spec
spec:
echo "Generating Swagger documentation"
swag init -g api/handler/handler.go
swag fmt

.PHONY: validate-spec
validate-spec:
openapi-generator-cli validate -i docs/swagger.yaml

.PHONY: test
test:
echo 'mode: atomic' > coverage.txt && go list ./... | xargs -n1 -I{} sh -c 'go test -race -v -bench=. -covermode=atomic -coverprofile=coverage.tmp {} && tail -n +2 coverage.tmp >> coverage.txt' && rm coverage.tmp
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Moira 2.0 ![Build Status](https://github.com/moira-alert/moira/actions/workflows/test.yml/badge.svg?branch=master) [![codecov](https://codecov.io/gh/moira-alert/moira/branch/master/graph/badge.svg)](https://codecov.io/gh/moira-alert/moira) [![Documentation Status](https://readthedocs.org/projects/moira/badge/?version=latest)](http://moira.readthedocs.io/en/latest/?badge=latest) [![Telegram](https://img.shields.io/badge/telegram-join%20chat-3796cd.svg)](https://t.me/moira_alert) [![Go Report Card](https://goreportcard.com/badge/github.com/moira-alert/moira)](https://goreportcard.com/report/github.com/moira-alert/moira)

Moira is a real-time alerting tool, based on [Graphite](https://graphite.readthedocs.io) data.
Moira is a real-time alerting tool, based on [Graphite](https://graphite.readthedocs.io) or [Prometheus](https://prometheus.io/)/[VictoriaMetrics](https://victoriametrics.com/) metrics.

## Installation

Expand Down Expand Up @@ -41,6 +41,7 @@ Code in this repository is the backend part of Moira monitoring application.
* [doc](https://github.com/moira-alert/doc) is the documentation (hosted on [Read the Docs](https://moira.readthedocs.io)).
* [moira-trigger-role](https://github.com/moira-alert/moira-trigger-role) is the Ansible role you can use to manage triggers.
* [python-moira-client](https://github.com/moira-alert/python-moira-client) is the Python API client.
* [go-client](https://github.com/moira-alert/client-go) is the GO API client.

## Contact us

Expand Down
9 changes: 5 additions & 4 deletions api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import "time"

// Config for api configuration variables.
type Config struct {
EnableCORS bool
Listen string
LocalMetricTTL time.Duration
RemoteMetricTTL time.Duration
EnableCORS bool
Listen string
GraphiteLocalMetricTTL time.Duration
GraphiteRemoteMetricTTL time.Duration
PrometheusRemoteMetricTTL time.Duration
}

// WebConfig is container for web ui configuration parameters.
Expand Down
18 changes: 18 additions & 0 deletions api/controller/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ func GetAllContacts(database moira.Database) (*dto.ContactList, *api.ErrorRespon
return &contactsList, nil
}

// GetContactById gets notification contact by its id string
func GetContactById(database moira.Database, contactID string) (*dto.Contact, *api.ErrorResponse) {
contact, err := database.GetContact(contactID)
if err != nil {
return nil, api.ErrorInternalServer(err)
}

contactToReturn := &dto.Contact{
ID: contact.ID,
User: contact.User,
TeamID: contact.Team,
Type: contact.Type,
Value: contact.Value,
}

return contactToReturn, nil
}

// CreateContact creates new notification contact for current user
func CreateContact(dataBase moira.Database, contact *dto.Contact, userLogin, teamID string) *api.ErrorResponse {
if userLogin != "" && teamID != "" {
Expand Down
Loading

0 comments on commit 9f2f59d

Please sign in to comment.