...

Source file src/github.com/xanzy/go-gitlab/commits.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  	"net/url"
    23  	"time"
    24  )
    25  
    26  // CommitsService handles communication with the commit related methods
    27  // of the GitLab API.
    28  //
    29  // GitLab API docs: https://docs.gitlab.com/ee/api/commits.html
    30  type CommitsService struct {
    31  	client *Client
    32  }
    33  
    34  // Commit represents a GitLab commit.
    35  //
    36  // GitLab API docs: https://docs.gitlab.com/ee/api/commits.html
    37  type Commit struct {
    38  	ID             string            `json:"id"`
    39  	ShortID        string            `json:"short_id"`
    40  	Title          string            `json:"title"`
    41  	AuthorName     string            `json:"author_name"`
    42  	AuthorEmail    string            `json:"author_email"`
    43  	AuthoredDate   *time.Time        `json:"authored_date"`
    44  	CommitterName  string            `json:"committer_name"`
    45  	CommitterEmail string            `json:"committer_email"`
    46  	CommittedDate  *time.Time        `json:"committed_date"`
    47  	CreatedAt      *time.Time        `json:"created_at"`
    48  	Message        string            `json:"message"`
    49  	ParentIDs      []string          `json:"parent_ids"`
    50  	Stats          *CommitStats      `json:"stats"`
    51  	Status         *BuildStateValue  `json:"status"`
    52  	LastPipeline   *PipelineInfo     `json:"last_pipeline"`
    53  	ProjectID      int               `json:"project_id"`
    54  	Trailers       map[string]string `json:"trailers"`
    55  	WebURL         string            `json:"web_url"`
    56  }
    57  
    58  // CommitStats represents the number of added and deleted files in a commit.
    59  //
    60  // GitLab API docs: https://docs.gitlab.com/ee/api/commits.html
    61  type CommitStats struct {
    62  	Additions int `json:"additions"`
    63  	Deletions int `json:"deletions"`
    64  	Total     int `json:"total"`
    65  }
    66  
    67  func (c Commit) String() string {
    68  	return Stringify(c)
    69  }
    70  
    71  // ListCommitsOptions represents the available ListCommits() options.
    72  //
    73  // GitLab API docs: https://docs.gitlab.com/ee/api/commits.html#list-repository-commits
    74  type ListCommitsOptions struct {
    75  	ListOptions
    76  	RefName     *string    `url:"ref_name,omitempty" json:"ref_name,omitempty"`
    77  	Since       *time.Time `url:"since,omitempty" json:"since,omitempty"`
    78  	Until       *time.Time `url:"until,omitempty" json:"until,omitempty"`
    79  	Path        *string    `url:"path,omitempty" json:"path,omitempty"`
    80  	Author      *string    `url:"author,omitempty" json:"author,omitempty"`
    81  	All         *bool      `url:"all,omitempty" json:"all,omitempty"`
    82  	WithStats   *bool      `url:"with_stats,omitempty" json:"with_stats,omitempty"`
    83  	FirstParent *bool      `url:"first_parent,omitempty" json:"first_parent,omitempty"`
    84  	Trailers    *bool      `url:"trailers,omitempty" json:"trailers,omitempty"`
    85  }
    86  
    87  // ListCommits gets a list of repository commits in a project.
    88  //
    89  // GitLab API docs: https://docs.gitlab.com/ee/api/commits.html#list-repository-commits
    90  func (s *CommitsService) ListCommits(pid interface{}, opt *ListCommitsOptions, options ...RequestOptionFunc) ([]*Commit, *Response, error) {
    91  	project, err := parseID(pid)
    92  	if err != nil {
    93  		return nil, nil, err
    94  	}
    95  	u := fmt.Sprintf("projects/%s/repository/commits", PathEscape(project))
    96  
    97  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
    98  	if err != nil {
    99  		return nil, nil, err
   100  	}
   101  
   102  	var c []*Commit
   103  	resp, err := s.client.Do(req, &c)
   104  	if err != nil {
   105  		return nil, resp, err
   106  	}
   107  
   108  	return c, resp, nil
   109  }
   110  
   111  // CommitRef represents the reference of branches/tags in a commit.
   112  //
   113  // GitLab API docs:
   114  // https://docs.gitlab.com/ee/api/commits.html#get-references-a-commit-is-pushed-to
   115  type CommitRef struct {
   116  	Type string `json:"type"`
   117  	Name string `json:"name"`
   118  }
   119  
   120  // GetCommitRefsOptions represents the available GetCommitRefs() options.
   121  //
   122  // GitLab API docs:
   123  // https://docs.gitlab.com/ee/api/commits.html#get-references-a-commit-is-pushed-to
   124  type GetCommitRefsOptions struct {
   125  	ListOptions
   126  	Type *string `url:"type,omitempty" json:"type,omitempty"`
   127  }
   128  
   129  // GetCommitRefs gets all references (from branches or tags) a commit is pushed to
   130  //
   131  // GitLab API docs:
   132  // https://docs.gitlab.com/ee/api/commits.html#get-references-a-commit-is-pushed-to
   133  func (s *CommitsService) GetCommitRefs(pid interface{}, sha string, opt *GetCommitRefsOptions, options ...RequestOptionFunc) ([]*CommitRef, *Response, error) {
   134  	project, err := parseID(pid)
   135  	if err != nil {
   136  		return nil, nil, err
   137  	}
   138  	u := fmt.Sprintf("projects/%s/repository/commits/%s/refs", PathEscape(project), url.PathEscape(sha))
   139  
   140  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   141  	if err != nil {
   142  		return nil, nil, err
   143  	}
   144  
   145  	var cs []*CommitRef
   146  	resp, err := s.client.Do(req, &cs)
   147  	if err != nil {
   148  		return nil, resp, err
   149  	}
   150  
   151  	return cs, resp, nil
   152  }
   153  
   154  // GetCommit gets a specific commit identified by the commit hash or name of a
   155  // branch or tag.
   156  //
   157  // GitLab API docs: https://docs.gitlab.com/ee/api/commits.html#get-a-single-commit
   158  func (s *CommitsService) GetCommit(pid interface{}, sha string, options ...RequestOptionFunc) (*Commit, *Response, error) {
   159  	project, err := parseID(pid)
   160  	if err != nil {
   161  		return nil, nil, err
   162  	}
   163  	if sha == "" {
   164  		return nil, nil, fmt.Errorf("SHA must be a non-empty string")
   165  	}
   166  	u := fmt.Sprintf("projects/%s/repository/commits/%s", PathEscape(project), url.PathEscape(sha))
   167  
   168  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   169  	if err != nil {
   170  		return nil, nil, err
   171  	}
   172  
   173  	c := new(Commit)
   174  	resp, err := s.client.Do(req, c)
   175  	if err != nil {
   176  		return nil, resp, err
   177  	}
   178  
   179  	return c, resp, nil
   180  }
   181  
   182  // CreateCommitOptions represents the available options for a new commit.
   183  //
   184  // GitLab API docs: https://docs.gitlab.com/ee/api/commits.html#create-a-commit-with-multiple-files-and-actions
   185  type CreateCommitOptions struct {
   186  	Branch        *string                `url:"branch,omitempty" json:"branch,omitempty"`
   187  	CommitMessage *string                `url:"commit_message,omitempty" json:"commit_message,omitempty"`
   188  	StartBranch   *string                `url:"start_branch,omitempty" json:"start_branch,omitempty"`
   189  	StartSHA      *string                `url:"start_sha,omitempty" json:"start_sha,omitempty"`
   190  	StartProject  *string                `url:"start_project,omitempty" json:"start_project,omitempty"`
   191  	Actions       []*CommitActionOptions `url:"actions" json:"actions"`
   192  	AuthorEmail   *string                `url:"author_email,omitempty" json:"author_email,omitempty"`
   193  	AuthorName    *string                `url:"author_name,omitempty" json:"author_name,omitempty"`
   194  	Stats         *bool                  `url:"stats,omitempty" json:"stats,omitempty"`
   195  	Force         *bool                  `url:"force,omitempty" json:"force,omitempty"`
   196  }
   197  
   198  // CommitActionOptions represents the available options for a new single
   199  // file action.
   200  //
   201  // GitLab API docs: https://docs.gitlab.com/ee/api/commits.html#create-a-commit-with-multiple-files-and-actions
   202  type CommitActionOptions struct {
   203  	Action          *FileActionValue `url:"action,omitempty" json:"action,omitempty"`
   204  	FilePath        *string          `url:"file_path,omitempty" json:"file_path,omitempty"`
   205  	PreviousPath    *string          `url:"previous_path,omitempty" json:"previous_path,omitempty"`
   206  	Content         *string          `url:"content,omitempty" json:"content,omitempty"`
   207  	Encoding        *string          `url:"encoding,omitempty" json:"encoding,omitempty"`
   208  	LastCommitID    *string          `url:"last_commit_id,omitempty" json:"last_commit_id,omitempty"`
   209  	ExecuteFilemode *bool            `url:"execute_filemode,omitempty" json:"execute_filemode,omitempty"`
   210  }
   211  
   212  // CreateCommit creates a commit with multiple files and actions.
   213  //
   214  // GitLab API docs: https://docs.gitlab.com/ee/api/commits.html#create-a-commit-with-multiple-files-and-actions
   215  func (s *CommitsService) CreateCommit(pid interface{}, opt *CreateCommitOptions, options ...RequestOptionFunc) (*Commit, *Response, error) {
   216  	project, err := parseID(pid)
   217  	if err != nil {
   218  		return nil, nil, err
   219  	}
   220  	u := fmt.Sprintf("projects/%s/repository/commits", PathEscape(project))
   221  
   222  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   223  	if err != nil {
   224  		return nil, nil, err
   225  	}
   226  
   227  	c := new(Commit)
   228  	resp, err := s.client.Do(req, &c)
   229  	if err != nil {
   230  		return nil, resp, err
   231  	}
   232  
   233  	return c, resp, nil
   234  }
   235  
   236  // Diff represents a GitLab diff.
   237  //
   238  // GitLab API docs: https://docs.gitlab.com/ee/api/commits.html
   239  type Diff struct {
   240  	Diff        string `json:"diff"`
   241  	NewPath     string `json:"new_path"`
   242  	OldPath     string `json:"old_path"`
   243  	AMode       string `json:"a_mode"`
   244  	BMode       string `json:"b_mode"`
   245  	NewFile     bool   `json:"new_file"`
   246  	RenamedFile bool   `json:"renamed_file"`
   247  	DeletedFile bool   `json:"deleted_file"`
   248  }
   249  
   250  func (d Diff) String() string {
   251  	return Stringify(d)
   252  }
   253  
   254  // GetCommitDiffOptions represents the available GetCommitDiff() options.
   255  //
   256  // GitLab API docs:
   257  // https://docs.gitlab.com/ee/api/commits.html#get-the-diff-of-a-commit
   258  type GetCommitDiffOptions struct {
   259  	ListOptions
   260  	Unidiff *bool `url:"unidiff,omitempty" json:"unidiff,omitempty"`
   261  }
   262  
   263  // GetCommitDiff gets the diff of a commit in a project..
   264  //
   265  // GitLab API docs:
   266  // https://docs.gitlab.com/ee/api/commits.html#get-the-diff-of-a-commit
   267  func (s *CommitsService) GetCommitDiff(pid interface{}, sha string, opt *GetCommitDiffOptions, options ...RequestOptionFunc) ([]*Diff, *Response, error) {
   268  	project, err := parseID(pid)
   269  	if err != nil {
   270  		return nil, nil, err
   271  	}
   272  	u := fmt.Sprintf("projects/%s/repository/commits/%s/diff", PathEscape(project), url.PathEscape(sha))
   273  
   274  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   275  	if err != nil {
   276  		return nil, nil, err
   277  	}
   278  
   279  	var d []*Diff
   280  	resp, err := s.client.Do(req, &d)
   281  	if err != nil {
   282  		return nil, resp, err
   283  	}
   284  
   285  	return d, resp, nil
   286  }
   287  
   288  // CommitComment represents a GitLab commit comment.
   289  //
   290  // GitLab API docs: https://docs.gitlab.com/ee/api/commits.html
   291  type CommitComment struct {
   292  	Note     string `json:"note"`
   293  	Path     string `json:"path"`
   294  	Line     int    `json:"line"`
   295  	LineType string `json:"line_type"`
   296  	Author   Author `json:"author"`
   297  }
   298  
   299  // Author represents a GitLab commit author
   300  type Author struct {
   301  	ID        int        `json:"id"`
   302  	Username  string     `json:"username"`
   303  	Email     string     `json:"email"`
   304  	Name      string     `json:"name"`
   305  	State     string     `json:"state"`
   306  	Blocked   bool       `json:"blocked"`
   307  	CreatedAt *time.Time `json:"created_at"`
   308  }
   309  
   310  func (c CommitComment) String() string {
   311  	return Stringify(c)
   312  }
   313  
   314  // GetCommitCommentsOptions represents the available GetCommitComments() options.
   315  //
   316  // GitLab API docs:
   317  // https://docs.gitlab.com/ee/api/commits.html#get-the-comments-of-a-commit
   318  type GetCommitCommentsOptions ListOptions
   319  
   320  // GetCommitComments gets the comments of a commit in a project.
   321  //
   322  // GitLab API docs:
   323  // https://docs.gitlab.com/ee/api/commits.html#get-the-comments-of-a-commit
   324  func (s *CommitsService) GetCommitComments(pid interface{}, sha string, opt *GetCommitCommentsOptions, options ...RequestOptionFunc) ([]*CommitComment, *Response, error) {
   325  	project, err := parseID(pid)
   326  	if err != nil {
   327  		return nil, nil, err
   328  	}
   329  	u := fmt.Sprintf("projects/%s/repository/commits/%s/comments", PathEscape(project), url.PathEscape(sha))
   330  
   331  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   332  	if err != nil {
   333  		return nil, nil, err
   334  	}
   335  
   336  	var c []*CommitComment
   337  	resp, err := s.client.Do(req, &c)
   338  	if err != nil {
   339  		return nil, resp, err
   340  	}
   341  
   342  	return c, resp, nil
   343  }
   344  
   345  // PostCommitCommentOptions represents the available PostCommitComment()
   346  // options.
   347  //
   348  // GitLab API docs:
   349  // https://docs.gitlab.com/ee/api/commits.html#post-comment-to-commit
   350  type PostCommitCommentOptions struct {
   351  	Note     *string `url:"note,omitempty" json:"note,omitempty"`
   352  	Path     *string `url:"path" json:"path"`
   353  	Line     *int    `url:"line" json:"line"`
   354  	LineType *string `url:"line_type" json:"line_type"`
   355  }
   356  
   357  // PostCommitComment adds a comment to a commit. Optionally you can post
   358  // comments on a specific line of a commit. Therefor both path, line_new and
   359  // line_old are required.
   360  //
   361  // GitLab API docs:
   362  // https://docs.gitlab.com/ee/api/commits.html#post-comment-to-commit
   363  func (s *CommitsService) PostCommitComment(pid interface{}, sha string, opt *PostCommitCommentOptions, options ...RequestOptionFunc) (*CommitComment, *Response, error) {
   364  	project, err := parseID(pid)
   365  	if err != nil {
   366  		return nil, nil, err
   367  	}
   368  	u := fmt.Sprintf("projects/%s/repository/commits/%s/comments", PathEscape(project), url.PathEscape(sha))
   369  
   370  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   371  	if err != nil {
   372  		return nil, nil, err
   373  	}
   374  
   375  	c := new(CommitComment)
   376  	resp, err := s.client.Do(req, c)
   377  	if err != nil {
   378  		return nil, resp, err
   379  	}
   380  
   381  	return c, resp, nil
   382  }
   383  
   384  // GetCommitStatusesOptions represents the available GetCommitStatuses() options.
   385  //
   386  // GitLab API docs: https://docs.gitlab.com/ee/api/commits.html#list-the-statuses-of-a-commit
   387  type GetCommitStatusesOptions struct {
   388  	ListOptions
   389  	Ref   *string `url:"ref,omitempty" json:"ref,omitempty"`
   390  	Stage *string `url:"stage,omitempty" json:"stage,omitempty"`
   391  	Name  *string `url:"name,omitempty" json:"name,omitempty"`
   392  	All   *bool   `url:"all,omitempty" json:"all,omitempty"`
   393  }
   394  
   395  // CommitStatus represents a GitLab commit status.
   396  //
   397  // GitLab API docs: https://docs.gitlab.com/ee/api/commits.html#commit-status
   398  type CommitStatus struct {
   399  	ID           int        `json:"id"`
   400  	SHA          string     `json:"sha"`
   401  	Ref          string     `json:"ref"`
   402  	Status       string     `json:"status"`
   403  	CreatedAt    *time.Time `json:"created_at"`
   404  	StartedAt    *time.Time `json:"started_at"`
   405  	FinishedAt   *time.Time `json:"finished_at"`
   406  	Name         string     `json:"name"`
   407  	AllowFailure bool       `json:"allow_failure"`
   408  	Coverage     float64    `json:"coverage"`
   409  	PipelineId   int        `json:"pipeline_id"`
   410  	Author       Author     `json:"author"`
   411  	Description  string     `json:"description"`
   412  	TargetURL    string     `json:"target_url"`
   413  }
   414  
   415  // GetCommitStatuses gets the statuses of a commit in a project.
   416  //
   417  // GitLab API docs: https://docs.gitlab.com/ee/api/commits.html#list-the-statuses-of-a-commit
   418  func (s *CommitsService) GetCommitStatuses(pid interface{}, sha string, opt *GetCommitStatusesOptions, options ...RequestOptionFunc) ([]*CommitStatus, *Response, error) {
   419  	project, err := parseID(pid)
   420  	if err != nil {
   421  		return nil, nil, err
   422  	}
   423  	u := fmt.Sprintf("projects/%s/repository/commits/%s/statuses", PathEscape(project), url.PathEscape(sha))
   424  
   425  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   426  	if err != nil {
   427  		return nil, nil, err
   428  	}
   429  
   430  	var cs []*CommitStatus
   431  	resp, err := s.client.Do(req, &cs)
   432  	if err != nil {
   433  		return nil, resp, err
   434  	}
   435  
   436  	return cs, resp, nil
   437  }
   438  
   439  // SetCommitStatusOptions represents the available SetCommitStatus() options.
   440  //
   441  // GitLab API docs: https://docs.gitlab.com/ee/api/commits.html#set-the-pipeline-status-of-a-commit
   442  type SetCommitStatusOptions struct {
   443  	State       BuildStateValue `url:"state" json:"state"`
   444  	Ref         *string         `url:"ref,omitempty" json:"ref,omitempty"`
   445  	Name        *string         `url:"name,omitempty" json:"name,omitempty"`
   446  	Context     *string         `url:"context,omitempty" json:"context,omitempty"`
   447  	TargetURL   *string         `url:"target_url,omitempty" json:"target_url,omitempty"`
   448  	Description *string         `url:"description,omitempty" json:"description,omitempty"`
   449  	Coverage    *float64        `url:"coverage,omitempty" json:"coverage,omitempty"`
   450  	PipelineID  *int            `url:"pipeline_id,omitempty" json:"pipeline_id,omitempty"`
   451  }
   452  
   453  // SetCommitStatus sets the status of a commit in a project.
   454  //
   455  // GitLab API docs: https://docs.gitlab.com/ee/api/commits.html#set-the-pipeline-status-of-a-commit
   456  func (s *CommitsService) SetCommitStatus(pid interface{}, sha string, opt *SetCommitStatusOptions, options ...RequestOptionFunc) (*CommitStatus, *Response, error) {
   457  	project, err := parseID(pid)
   458  	if err != nil {
   459  		return nil, nil, err
   460  	}
   461  	u := fmt.Sprintf("projects/%s/statuses/%s", PathEscape(project), url.PathEscape(sha))
   462  
   463  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   464  	if err != nil {
   465  		return nil, nil, err
   466  	}
   467  
   468  	cs := new(CommitStatus)
   469  	resp, err := s.client.Do(req, &cs)
   470  	if err != nil {
   471  		return nil, resp, err
   472  	}
   473  
   474  	return cs, resp, nil
   475  }
   476  
   477  // ListMergeRequestsByCommit gets merge request associated with a commit.
   478  //
   479  // GitLab API docs:
   480  // https://docs.gitlab.com/ee/api/commits.html#list-merge-requests-associated-with-a-commit
   481  func (s *CommitsService) ListMergeRequestsByCommit(pid interface{}, sha string, options ...RequestOptionFunc) ([]*MergeRequest, *Response, error) {
   482  	project, err := parseID(pid)
   483  	if err != nil {
   484  		return nil, nil, err
   485  	}
   486  	u := fmt.Sprintf("projects/%s/repository/commits/%s/merge_requests", PathEscape(project), url.PathEscape(sha))
   487  
   488  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   489  	if err != nil {
   490  		return nil, nil, err
   491  	}
   492  
   493  	var mrs []*MergeRequest
   494  	resp, err := s.client.Do(req, &mrs)
   495  	if err != nil {
   496  		return nil, resp, err
   497  	}
   498  
   499  	return mrs, resp, nil
   500  }
   501  
   502  // CherryPickCommitOptions represents the available CherryPickCommit() options.
   503  //
   504  // GitLab API docs: https://docs.gitlab.com/ee/api/commits.html#cherry-pick-a-commit
   505  type CherryPickCommitOptions struct {
   506  	Branch  *string `url:"branch,omitempty" json:"branch,omitempty"`
   507  	DryRun  *bool   `url:"dry_run,omitempty" json:"dry_run,omitempty"`
   508  	Message *string `url:"message,omitempty" json:"message,omitempty"`
   509  }
   510  
   511  // CherryPickCommit cherry picks a commit to a given branch.
   512  //
   513  // GitLab API docs: https://docs.gitlab.com/ee/api/commits.html#cherry-pick-a-commit
   514  func (s *CommitsService) CherryPickCommit(pid interface{}, sha string, opt *CherryPickCommitOptions, options ...RequestOptionFunc) (*Commit, *Response, error) {
   515  	project, err := parseID(pid)
   516  	if err != nil {
   517  		return nil, nil, err
   518  	}
   519  	u := fmt.Sprintf("projects/%s/repository/commits/%s/cherry_pick", PathEscape(project), url.PathEscape(sha))
   520  
   521  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   522  	if err != nil {
   523  		return nil, nil, err
   524  	}
   525  
   526  	c := new(Commit)
   527  	resp, err := s.client.Do(req, &c)
   528  	if err != nil {
   529  		return nil, resp, err
   530  	}
   531  
   532  	return c, resp, nil
   533  }
   534  
   535  // RevertCommitOptions represents the available RevertCommit() options.
   536  //
   537  // GitLab API docs: https://docs.gitlab.com/ee/api/commits.html#revert-a-commit
   538  type RevertCommitOptions struct {
   539  	Branch *string `url:"branch,omitempty" json:"branch,omitempty"`
   540  }
   541  
   542  // RevertCommit reverts a commit in a given branch.
   543  //
   544  // GitLab API docs: https://docs.gitlab.com/ee/api/commits.html#revert-a-commit
   545  func (s *CommitsService) RevertCommit(pid interface{}, sha string, opt *RevertCommitOptions, options ...RequestOptionFunc) (*Commit, *Response, error) {
   546  	project, err := parseID(pid)
   547  	if err != nil {
   548  		return nil, nil, err
   549  	}
   550  	u := fmt.Sprintf("projects/%s/repository/commits/%s/revert", PathEscape(project), url.PathEscape(sha))
   551  
   552  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   553  	if err != nil {
   554  		return nil, nil, err
   555  	}
   556  
   557  	c := new(Commit)
   558  	resp, err := s.client.Do(req, &c)
   559  	if err != nil {
   560  		return nil, resp, err
   561  	}
   562  
   563  	return c, resp, nil
   564  }
   565  
   566  // GPGSignature represents a Gitlab commit's GPG Signature.
   567  //
   568  // GitLab API docs:
   569  // https://docs.gitlab.com/ee/api/commits.html#get-gpg-signature-of-a-commit
   570  type GPGSignature struct {
   571  	KeyID              int    `json:"gpg_key_id"`
   572  	KeyPrimaryKeyID    string `json:"gpg_key_primary_keyid"`
   573  	KeyUserName        string `json:"gpg_key_user_name"`
   574  	KeyUserEmail       string `json:"gpg_key_user_email"`
   575  	VerificationStatus string `json:"verification_status"`
   576  	KeySubkeyID        int    `json:"gpg_key_subkey_id"`
   577  }
   578  
   579  // GetGPGSignature gets a GPG signature of a commit.
   580  //
   581  // GitLab API docs: https://docs.gitlab.com/ee/api/commits.html#get-gpg-signature-of-a-commit
   582  func (s *CommitsService) GetGPGSignature(pid interface{}, sha string, options ...RequestOptionFunc) (*GPGSignature, *Response, error) {
   583  	project, err := parseID(pid)
   584  	if err != nil {
   585  		return nil, nil, err
   586  	}
   587  	u := fmt.Sprintf("projects/%s/repository/commits/%s/signature", PathEscape(project), url.PathEscape(sha))
   588  
   589  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   590  	if err != nil {
   591  		return nil, nil, err
   592  	}
   593  
   594  	sig := new(GPGSignature)
   595  	resp, err := s.client.Do(req, &sig)
   596  	if err != nil {
   597  		return nil, resp, err
   598  	}
   599  
   600  	return sig, resp, nil
   601  }
   602  

View as plain text