...

Source file src/github.com/xanzy/go-gitlab/project_vulnerabilities.go

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2021, Sander van Harmelen
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  //
    16  
    17  package gitlab
    18  
    19  import (
    20  	"fmt"
    21  	"net/http"
    22  	"time"
    23  )
    24  
    25  // ProjectVulnerabilitiesService handles communication with the projects
    26  // vulnerabilities related methods of the GitLab API.
    27  //
    28  // GitLab API docs: https://docs.gitlab.com/ee/api/project_vulnerabilities.html
    29  type ProjectVulnerabilitiesService struct {
    30  	client *Client
    31  }
    32  
    33  // Project represents a GitLab project vulnerability.
    34  //
    35  // GitLab API docs: https://docs.gitlab.com/ee/api/project_vulnerabilities.html
    36  type ProjectVulnerability struct {
    37  	AuthorID                int        `json:"author_id"`
    38  	Confidence              string     `json:"confidence"`
    39  	CreatedAt               *time.Time `json:"created_at"`
    40  	Description             string     `json:"description"`
    41  	DismissedAt             *time.Time `json:"dismissed_at"`
    42  	DismissedByID           int        `json:"dismissed_by_id"`
    43  	DueDate                 *time.Time `json:"due_date"`
    44  	Finding                 *Finding   `json:"finding"`
    45  	ID                      int        `json:"id"`
    46  	LastEditedAt            *time.Time `json:"last_edited_at"`
    47  	LastEditedByID          int        `json:"last_edited_by_id"`
    48  	Project                 *Project   `json:"project"`
    49  	ProjectDefaultBranch    string     `json:"project_default_branch"`
    50  	ReportType              string     `json:"report_type"`
    51  	ResolvedAt              *time.Time `json:"resolved_at"`
    52  	ResolvedByID            int        `json:"resolved_by_id"`
    53  	ResolvedOnDefaultBranch bool       `json:"resolved_on_default_branch"`
    54  	Severity                string     `json:"severity"`
    55  	StartDate               *time.Time `json:"start_date"`
    56  	State                   string     `json:"state"`
    57  	Title                   string     `json:"title"`
    58  	UpdatedAt               *time.Time `json:"updated_at"`
    59  	UpdatedByID             int        `json:"updated_by_id"`
    60  }
    61  
    62  // Project represents a GitLab project vulnerability finding.
    63  //
    64  // GitLab API docs: https://docs.gitlab.com/ee/api/project_vulnerabilities.html
    65  type Finding struct {
    66  	Confidence          string     `json:"confidence"`
    67  	CreatedAt           *time.Time `json:"created_at"`
    68  	ID                  int        `json:"id"`
    69  	LocationFingerprint string     `json:"location_fingerprint"`
    70  	MetadataVersion     string     `json:"metadata_version"`
    71  	Name                string     `json:"name"`
    72  	PrimaryIdentifierID int        `json:"primary_identifier_id"`
    73  	ProjectFingerprint  string     `json:"project_fingerprint"`
    74  	ProjectID           int        `json:"project_id"`
    75  	RawMetadata         string     `json:"raw_metadata"`
    76  	ReportType          string     `json:"report_type"`
    77  	ScannerID           int        `json:"scanner_id"`
    78  	Severity            string     `json:"severity"`
    79  	UpdatedAt           *time.Time `json:"updated_at"`
    80  	UUID                string     `json:"uuid"`
    81  	VulnerabilityID     int        `json:"vulnerability_id"`
    82  }
    83  
    84  // ListProjectVulnerabilitiesOptions represents the available
    85  // ListProjectVulnerabilities() options.
    86  //
    87  // GitLab API docs:
    88  // https://docs.gitlab.com/ee/api/project_vulnerabilities.html#list-project-vulnerabilities
    89  type ListProjectVulnerabilitiesOptions struct {
    90  	ListOptions
    91  }
    92  
    93  // ListProjectVulnerabilities gets a list of all project vulnerabilities.
    94  //
    95  // GitLab API docs:
    96  // https://docs.gitlab.com/ee/api/project_vulnerabilities.html#list-project-vulnerabilities
    97  func (s *ProjectVulnerabilitiesService) ListProjectVulnerabilities(pid interface{}, opt *ListProjectVulnerabilitiesOptions, options ...RequestOptionFunc) ([]*ProjectVulnerability, *Response, error) {
    98  	project, err := parseID(pid)
    99  	if err != nil {
   100  		return nil, nil, err
   101  	}
   102  	u := fmt.Sprintf("projects/%s/vulnerabilities", PathEscape(project))
   103  
   104  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   105  	if err != nil {
   106  		return nil, nil, err
   107  	}
   108  
   109  	var p []*ProjectVulnerability
   110  	resp, err := s.client.Do(req, &p)
   111  	if err != nil {
   112  		return nil, resp, err
   113  	}
   114  
   115  	return p, resp, nil
   116  }
   117  
   118  // CreateVulnerabilityOptions represents the available CreateVulnerability()
   119  // options.
   120  //
   121  // GitLab API docs:
   122  // https://docs.gitlab.com/ee/api/project_vulnerabilities.html#new-vulnerability
   123  type CreateVulnerabilityOptions struct {
   124  	FindingID *int `url:"finding_id,omitempty" json:"finding_id,omitempty"`
   125  }
   126  
   127  // CreateVulnerability creates a new vulnerability on the selected project.
   128  //
   129  // GitLab API docs:
   130  // https://docs.gitlab.com/ee/api/project_vulnerabilities.html#new-vulnerability
   131  func (s *ProjectVulnerabilitiesService) CreateVulnerability(pid interface{}, opt *CreateVulnerabilityOptions, options ...RequestOptionFunc) (*ProjectVulnerability, *Response, error) {
   132  	project, err := parseID(pid)
   133  	if err != nil {
   134  		return nil, nil, err
   135  	}
   136  	u := fmt.Sprintf("projects/%s/vulnerabilities", PathEscape(project))
   137  
   138  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   139  	if err != nil {
   140  		return nil, nil, err
   141  	}
   142  
   143  	p := new(ProjectVulnerability)
   144  	resp, err := s.client.Do(req, p)
   145  	if err != nil {
   146  		return nil, resp, err
   147  	}
   148  
   149  	return p, resp, nil
   150  }
   151  

View as plain text