...

Source file src/github.com/xanzy/go-gitlab/pipeline_schedules.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  // PipelineSchedulesService handles communication with the pipeline
    26  // schedules related methods of the GitLab API.
    27  //
    28  // GitLab API docs: https://docs.gitlab.com/ee/api/pipeline_schedules.html
    29  type PipelineSchedulesService struct {
    30  	client *Client
    31  }
    32  
    33  // PipelineSchedule represents a pipeline schedule.
    34  //
    35  // GitLab API docs:
    36  // https://docs.gitlab.com/ee/api/pipeline_schedules.html
    37  type PipelineSchedule struct {
    38  	ID           int                 `json:"id"`
    39  	Description  string              `json:"description"`
    40  	Ref          string              `json:"ref"`
    41  	Cron         string              `json:"cron"`
    42  	CronTimezone string              `json:"cron_timezone"`
    43  	NextRunAt    *time.Time          `json:"next_run_at"`
    44  	Active       bool                `json:"active"`
    45  	CreatedAt    *time.Time          `json:"created_at"`
    46  	UpdatedAt    *time.Time          `json:"updated_at"`
    47  	Owner        *User               `json:"owner"`
    48  	LastPipeline *LastPipeline       `json:"last_pipeline"`
    49  	Variables    []*PipelineVariable `json:"variables"`
    50  }
    51  
    52  // LastPipeline represents the last pipeline ran by schedule
    53  // this will be returned only for individual schedule get operation
    54  type LastPipeline struct {
    55  	ID     int    `json:"id"`
    56  	SHA    string `json:"sha"`
    57  	Ref    string `json:"ref"`
    58  	Status string `json:"status"`
    59  }
    60  
    61  // ListPipelineSchedulesOptions represents the available ListPipelineTriggers() options.
    62  //
    63  // GitLab API docs:
    64  // https://docs.gitlab.com/ee/api/pipeline_schedules.html#get-all-pipeline-schedules
    65  type ListPipelineSchedulesOptions ListOptions
    66  
    67  // ListPipelineSchedules gets a list of project triggers.
    68  //
    69  // GitLab API docs:
    70  // https://docs.gitlab.com/ee/api/pipeline_schedules.html#get-all-pipeline-schedules
    71  func (s *PipelineSchedulesService) ListPipelineSchedules(pid interface{}, opt *ListPipelineSchedulesOptions, options ...RequestOptionFunc) ([]*PipelineSchedule, *Response, error) {
    72  	project, err := parseID(pid)
    73  	if err != nil {
    74  		return nil, nil, err
    75  	}
    76  	u := fmt.Sprintf("projects/%s/pipeline_schedules", PathEscape(project))
    77  
    78  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
    79  	if err != nil {
    80  		return nil, nil, err
    81  	}
    82  
    83  	var ps []*PipelineSchedule
    84  	resp, err := s.client.Do(req, &ps)
    85  	if err != nil {
    86  		return nil, resp, err
    87  	}
    88  
    89  	return ps, resp, nil
    90  }
    91  
    92  // GetPipelineSchedule gets a pipeline schedule.
    93  //
    94  // GitLab API docs:
    95  // https://docs.gitlab.com/ee/api/pipeline_schedules.html#get-a-single-pipeline-schedule
    96  func (s *PipelineSchedulesService) GetPipelineSchedule(pid interface{}, schedule int, options ...RequestOptionFunc) (*PipelineSchedule, *Response, error) {
    97  	project, err := parseID(pid)
    98  	if err != nil {
    99  		return nil, nil, err
   100  	}
   101  	u := fmt.Sprintf("projects/%s/pipeline_schedules/%d", PathEscape(project), schedule)
   102  
   103  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   104  	if err != nil {
   105  		return nil, nil, err
   106  	}
   107  
   108  	p := new(PipelineSchedule)
   109  	resp, err := s.client.Do(req, p)
   110  	if err != nil {
   111  		return nil, resp, err
   112  	}
   113  
   114  	return p, resp, nil
   115  }
   116  
   117  // ListPipelinesTriggeredByScheduleOptions represents the available
   118  // ListPipelinesTriggeredBySchedule() options.
   119  //
   120  // GitLab API docs:
   121  // https://docs.gitlab.com/ee/api/pipeline_schedules.html#get-all-pipelines-triggered-by-a-pipeline-schedule
   122  type ListPipelinesTriggeredByScheduleOptions ListOptions
   123  
   124  // ListPipelinesTriggeredBySchedule gets all pipelines triggered by a pipeline
   125  // schedule.
   126  //
   127  // GitLab API docs:
   128  // https://docs.gitlab.com/ee/api/pipeline_schedules.html#get-all-pipelines-triggered-by-a-pipeline-schedule
   129  func (s *PipelineSchedulesService) ListPipelinesTriggeredBySchedule(pid interface{}, schedule int, opt *ListPipelinesTriggeredByScheduleOptions, options ...RequestOptionFunc) ([]*Pipeline, *Response, error) {
   130  	project, err := parseID(pid)
   131  	if err != nil {
   132  		return nil, nil, err
   133  	}
   134  	u := fmt.Sprintf("projects/%s/pipeline_schedules/%d/pipelines", PathEscape(project), schedule)
   135  
   136  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   137  	if err != nil {
   138  		return nil, nil, err
   139  	}
   140  
   141  	var p []*Pipeline
   142  	resp, err := s.client.Do(req, &p)
   143  	if err != nil {
   144  		return nil, resp, err
   145  	}
   146  
   147  	return p, resp, nil
   148  }
   149  
   150  // CreatePipelineScheduleOptions represents the available
   151  // CreatePipelineSchedule() options.
   152  //
   153  // GitLab API docs:
   154  // https://docs.gitlab.com/ee/api/pipeline_schedules.html#create-a-new-pipeline-schedule
   155  type CreatePipelineScheduleOptions struct {
   156  	Description  *string `url:"description" json:"description"`
   157  	Ref          *string `url:"ref" json:"ref"`
   158  	Cron         *string `url:"cron" json:"cron"`
   159  	CronTimezone *string `url:"cron_timezone,omitempty" json:"cron_timezone,omitempty"`
   160  	Active       *bool   `url:"active,omitempty" json:"active,omitempty"`
   161  }
   162  
   163  // CreatePipelineSchedule creates a pipeline schedule.
   164  //
   165  // GitLab API docs:
   166  // https://docs.gitlab.com/ee/api/pipeline_schedules.html#create-a-new-pipeline-schedule
   167  func (s *PipelineSchedulesService) CreatePipelineSchedule(pid interface{}, opt *CreatePipelineScheduleOptions, options ...RequestOptionFunc) (*PipelineSchedule, *Response, error) {
   168  	project, err := parseID(pid)
   169  	if err != nil {
   170  		return nil, nil, err
   171  	}
   172  	u := fmt.Sprintf("projects/%s/pipeline_schedules", PathEscape(project))
   173  
   174  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   175  	if err != nil {
   176  		return nil, nil, err
   177  	}
   178  
   179  	p := new(PipelineSchedule)
   180  	resp, err := s.client.Do(req, p)
   181  	if err != nil {
   182  		return nil, resp, err
   183  	}
   184  
   185  	return p, resp, nil
   186  }
   187  
   188  // EditPipelineScheduleOptions represents the available
   189  // EditPipelineSchedule() options.
   190  //
   191  // GitLab API docs:
   192  // https://docs.gitlab.com/ee/api/pipeline_schedules.html#edit-a-pipeline-schedule
   193  type EditPipelineScheduleOptions struct {
   194  	Description  *string `url:"description,omitempty" json:"description,omitempty"`
   195  	Ref          *string `url:"ref,omitempty" json:"ref,omitempty"`
   196  	Cron         *string `url:"cron,omitempty" json:"cron,omitempty"`
   197  	CronTimezone *string `url:"cron_timezone,omitempty" json:"cron_timezone,omitempty"`
   198  	Active       *bool   `url:"active,omitempty" json:"active,omitempty"`
   199  }
   200  
   201  // EditPipelineSchedule edits a pipeline schedule.
   202  //
   203  // GitLab API docs:
   204  // https://docs.gitlab.com/ee/api/pipeline_schedules.html#edit-a-pipeline-schedule
   205  func (s *PipelineSchedulesService) EditPipelineSchedule(pid interface{}, schedule int, opt *EditPipelineScheduleOptions, options ...RequestOptionFunc) (*PipelineSchedule, *Response, error) {
   206  	project, err := parseID(pid)
   207  	if err != nil {
   208  		return nil, nil, err
   209  	}
   210  	u := fmt.Sprintf("projects/%s/pipeline_schedules/%d", PathEscape(project), schedule)
   211  
   212  	req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
   213  	if err != nil {
   214  		return nil, nil, err
   215  	}
   216  
   217  	p := new(PipelineSchedule)
   218  	resp, err := s.client.Do(req, p)
   219  	if err != nil {
   220  		return nil, resp, err
   221  	}
   222  
   223  	return p, resp, nil
   224  }
   225  
   226  // TakeOwnershipOfPipelineSchedule sets the owner of the specified
   227  // pipeline schedule to the user issuing the request.
   228  //
   229  // GitLab API docs:
   230  // https://docs.gitlab.com/ee/api/pipeline_schedules.html#take-ownership-of-a-pipeline-schedule
   231  func (s *PipelineSchedulesService) TakeOwnershipOfPipelineSchedule(pid interface{}, schedule int, options ...RequestOptionFunc) (*PipelineSchedule, *Response, error) {
   232  	project, err := parseID(pid)
   233  	if err != nil {
   234  		return nil, nil, err
   235  	}
   236  	u := fmt.Sprintf("projects/%s/pipeline_schedules/%d/take_ownership", PathEscape(project), schedule)
   237  
   238  	req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
   239  	if err != nil {
   240  		return nil, nil, err
   241  	}
   242  
   243  	p := new(PipelineSchedule)
   244  	resp, err := s.client.Do(req, p)
   245  	if err != nil {
   246  		return nil, resp, err
   247  	}
   248  
   249  	return p, resp, nil
   250  }
   251  
   252  // DeletePipelineSchedule deletes a pipeline schedule.
   253  //
   254  // GitLab API docs:
   255  // https://docs.gitlab.com/ee/api/pipeline_schedules.html#delete-a-pipeline-schedule
   256  func (s *PipelineSchedulesService) DeletePipelineSchedule(pid interface{}, schedule int, options ...RequestOptionFunc) (*Response, error) {
   257  	project, err := parseID(pid)
   258  	if err != nil {
   259  		return nil, err
   260  	}
   261  	u := fmt.Sprintf("projects/%s/pipeline_schedules/%d", PathEscape(project), schedule)
   262  
   263  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   264  	if err != nil {
   265  		return nil, err
   266  	}
   267  
   268  	return s.client.Do(req, nil)
   269  }
   270  
   271  // RunPipelineSchedule triggers a new scheduled pipeline to run immediately.
   272  //
   273  // Gitlab API docs:
   274  // https://docs.gitlab.com/ee/api/pipeline_schedules.html#run-a-scheduled-pipeline-immediately
   275  func (s *PipelineSchedulesService) RunPipelineSchedule(pid interface{}, schedule int, options ...RequestOptionFunc) (*Response, error) {
   276  	project, err := parseID(pid)
   277  	if err != nil {
   278  		return nil, err
   279  	}
   280  	u := fmt.Sprintf("projects/%s/pipeline_schedules/%d/play", PathEscape(project), schedule)
   281  
   282  	req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
   283  	if err != nil {
   284  		return nil, err
   285  	}
   286  
   287  	return s.client.Do(req, nil)
   288  }
   289  
   290  // CreatePipelineScheduleVariableOptions represents the available
   291  // CreatePipelineScheduleVariable() options.
   292  //
   293  // GitLab API docs:
   294  // https://docs.gitlab.com/ee/api/pipeline_schedules.html#create-a-new-pipeline-schedule
   295  type CreatePipelineScheduleVariableOptions struct {
   296  	Key          *string `url:"key" json:"key"`
   297  	Value        *string `url:"value" json:"value"`
   298  	VariableType *string `url:"variable_type,omitempty" json:"variable_type,omitempty"`
   299  }
   300  
   301  // CreatePipelineScheduleVariable creates a pipeline schedule variable.
   302  //
   303  // GitLab API docs:
   304  // https://docs.gitlab.com/ee/api/pipeline_schedules.html#create-a-new-pipeline-schedule
   305  func (s *PipelineSchedulesService) CreatePipelineScheduleVariable(pid interface{}, schedule int, opt *CreatePipelineScheduleVariableOptions, options ...RequestOptionFunc) (*PipelineVariable, *Response, error) {
   306  	project, err := parseID(pid)
   307  	if err != nil {
   308  		return nil, nil, err
   309  	}
   310  	u := fmt.Sprintf("projects/%s/pipeline_schedules/%d/variables", PathEscape(project), schedule)
   311  
   312  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   313  	if err != nil {
   314  		return nil, nil, err
   315  	}
   316  
   317  	p := new(PipelineVariable)
   318  	resp, err := s.client.Do(req, p)
   319  	if err != nil {
   320  		return nil, resp, err
   321  	}
   322  
   323  	return p, resp, nil
   324  }
   325  
   326  // EditPipelineScheduleVariableOptions represents the available
   327  // EditPipelineScheduleVariable() options.
   328  //
   329  // GitLab API docs:
   330  // https://docs.gitlab.com/ee/api/pipeline_schedules.html#edit-a-pipeline-schedule-variable
   331  type EditPipelineScheduleVariableOptions struct {
   332  	Value        *string `url:"value" json:"value"`
   333  	VariableType *string `url:"variable_type,omitempty" json:"variable_type,omitempty"`
   334  }
   335  
   336  // EditPipelineScheduleVariable creates a pipeline schedule variable.
   337  //
   338  // GitLab API docs:
   339  // https://docs.gitlab.com/ee/api/pipeline_schedules.html#edit-a-pipeline-schedule-variable
   340  func (s *PipelineSchedulesService) EditPipelineScheduleVariable(pid interface{}, schedule int, key string, opt *EditPipelineScheduleVariableOptions, options ...RequestOptionFunc) (*PipelineVariable, *Response, error) {
   341  	project, err := parseID(pid)
   342  	if err != nil {
   343  		return nil, nil, err
   344  	}
   345  	u := fmt.Sprintf("projects/%s/pipeline_schedules/%d/variables/%s", PathEscape(project), schedule, key)
   346  
   347  	req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
   348  	if err != nil {
   349  		return nil, nil, err
   350  	}
   351  
   352  	p := new(PipelineVariable)
   353  	resp, err := s.client.Do(req, p)
   354  	if err != nil {
   355  		return nil, resp, err
   356  	}
   357  
   358  	return p, resp, nil
   359  }
   360  
   361  // DeletePipelineScheduleVariable creates a pipeline schedule variable.
   362  //
   363  // GitLab API docs:
   364  // https://docs.gitlab.com/ee/api/pipeline_schedules.html#delete-a-pipeline-schedule-variable
   365  func (s *PipelineSchedulesService) DeletePipelineScheduleVariable(pid interface{}, schedule int, key string, options ...RequestOptionFunc) (*PipelineVariable, *Response, error) {
   366  	project, err := parseID(pid)
   367  	if err != nil {
   368  		return nil, nil, err
   369  	}
   370  	u := fmt.Sprintf("projects/%s/pipeline_schedules/%d/variables/%s", PathEscape(project), schedule, key)
   371  
   372  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   373  	if err != nil {
   374  		return nil, nil, err
   375  	}
   376  
   377  	p := new(PipelineVariable)
   378  	resp, err := s.client.Do(req, p)
   379  	if err != nil {
   380  		return nil, resp, err
   381  	}
   382  
   383  	return p, resp, nil
   384  }
   385  

View as plain text