Skip to content

Commit

Permalink
feat(ci): use CodeQL analysis
Browse files Browse the repository at this point in the history
Use default CodeQL template from GitHub to check
code quality.

Signed-off-by: Gaurav Mishra <[email protected]>
  • Loading branch information
GMishx committed May 20, 2024
1 parent c86521f commit cca1074
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 13 deletions.
93 changes: 93 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# SPDX-FileCopyrightText: GitHub
# SPDX-License-Identifier: MIT
# Taken as default template from
# https://github.com/actions/starter-workflows/blob/main/code-scanning/codeql.yml
#
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
schedule:
- cron: '17 12 * * 6'

jobs:
analyze:
name: Analyze (${{ matrix.language }})
# Runner size impacts CodeQL analysis time. To learn more, please see:
# - https://gh.io/recommended-hardware-resources-for-running-codeql
# - https://gh.io/supported-runners-and-hardware-resources
# - https://gh.io/using-larger-runners (GitHub.com only)
# Consider using larger runners or machines with greater resources for possible analysis time improvements.
runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }}
timeout-minutes: ${{ (matrix.language == 'swift' && 120) || 360 }}
permissions:
# required for all workflows
security-events: write

# required to fetch internal or private CodeQL packs
packages: read

# only required for workflows in private repositories
actions: read
contents: read

strategy:
fail-fast: false
matrix:
include:
- language: go
build-mode: autobuild
# CodeQL supports the following values keywords for 'language': 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'swift'
# Use `c-cpp` to analyze code written in C, C++ or both
# Use 'java-kotlin' to analyze code written in Java, Kotlin or both
# Use 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both
# To learn more about changing the languages that are analyzed or customizing the build mode for your analysis,
# see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning.
# If you are analyzing a compiled language, you can modify the 'build-mode' for that language to customize how
# your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages
steps:
- name: Checkout repository
uses: actions/checkout@v4

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality

# If the analyze step fails for one of the languages you are analyzing with
# "We were unable to automatically build your code", modify the matrix above
# to set the build mode to "manual" for that language. Then modify this step
# to build your code.
# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
- if: matrix.build-mode == 'manual'
shell: bash
run: |
go build ./cmd/laas
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
with:
category: "/language:${{matrix.language}}"
6 changes: 3 additions & 3 deletions pkg/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ func PaginationMiddleware() gin.HandlerFunc {
var page models.PaginationInput
parsedPage, err := strconv.ParseInt(c.Query("page"), 10, 64)
if err == nil {
page.Page = int(parsedPage)
page.Page = parsedPage
}
parsedLimit, err := strconv.ParseInt(c.Query("limit"), 10, 64)
if err == nil {
page.Limit = int(parsedLimit)
page.Limit = parsedLimit
}

if page.Page == 0 {
Expand Down Expand Up @@ -193,7 +193,7 @@ func PaginationMiddleware() gin.HandlerFunc {
metaObject.Page = page.Page
metaObject.Limit = page.Limit
metaObject.ResourceCount = paginationMeta.ResourceCount
metaObject.TotalPages = int(math.Ceil(float64(paginationMeta.ResourceCount) / float64(page.Limit)))
metaObject.TotalPages = int64(math.Ceil(float64(paginationMeta.ResourceCount) / float64(page.Limit)))
// Can go next
if metaObject.Page < metaObject.TotalPages {
params.Set("page", strconv.FormatInt(int64(metaObject.Page+1), 10))
Expand Down
14 changes: 7 additions & 7 deletions pkg/models/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,17 @@ type ImportLicensesResponse struct {
// about the retrieved license data.
type PaginationMeta struct {
ResourceCount int `json:"resource_count" example:"200"`
TotalPages int `json:"total_pages,omitempty" example:"20"`
Page int `json:"page,omitempty" example:"10"`
Limit int `json:"limit,omitempty" example:"10"`
TotalPages int64 `json:"total_pages,omitempty" example:"20"`
Page int64 `json:"page,omitempty" example:"10"`
Limit int64 `json:"limit,omitempty" example:"10"`
Next string `json:"next,omitempty" example:"/api/v1/licenses?limit=10&page=11"`
Previous string `json:"previous,omitempty" example:"/api/v1/licenses?limit=10&page=9"`
}

// The PaginationInput struct represents the input required for pagination.
type PaginationInput struct {
Page int `json:"page" example:"10"`
Limit int `json:"limit" example:"10"`
Page int64 `json:"page" example:"10"`
Limit int64 `json:"limit" example:"10"`
}

// PaginationParse interface processes the pagination input.
Expand All @@ -220,12 +220,12 @@ type PaginationParse interface {
}

// GetOffset returns the offset value for gorm.
func (p PaginationInput) GetOffset() int {
func (p PaginationInput) GetOffset() int64 {
return (p.Page - 1) * p.Limit
}

// GetLimit returns the limit value for gorm.
func (p PaginationInput) GetLimit() int {
func (p PaginationInput) GetLimit() int64 {
return p.Limit
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (

var (
// DefaultPage Set default page to 1
DefaultPage = 1
DefaultPage int64 = 1
// DefaultLimit Set default max limit to 20
DefaultLimit = 20
DefaultLimit int64 = 20
)

// The Converter function takes an input of type models.LicenseJson and converts it into a
Expand Down Expand Up @@ -174,7 +174,7 @@ func PreparePaginateResponse(c *gin.Context, query *gorm.DB,
}
pagination := pageVar.(models.PaginationInput)

query.Offset(pagination.GetOffset()).Limit(pagination.GetLimit())
query.Offset(int(pagination.GetOffset())).Limit(int(pagination.GetLimit()))

var paginationMeta models.PaginationMeta
paginationMeta.ResourceCount = int(totalRows)
Expand Down

0 comments on commit cca1074

Please sign in to comment.