github.com/google/go-github/v64@v64.0.0/github/apps.go (about)

     1  // Copyright 2016 The go-github AUTHORS. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package github
     7  
     8  import (
     9  	"context"
    10  	"fmt"
    11  )
    12  
    13  // AppsService provides access to the installation related functions
    14  // in the GitHub API.
    15  //
    16  // GitHub API docs: https://docs.github.com/rest/apps/
    17  type AppsService service
    18  
    19  // App represents a GitHub App.
    20  type App struct {
    21  	ID                 *int64                   `json:"id,omitempty"`
    22  	Slug               *string                  `json:"slug,omitempty"`
    23  	NodeID             *string                  `json:"node_id,omitempty"`
    24  	Owner              *User                    `json:"owner,omitempty"`
    25  	Name               *string                  `json:"name,omitempty"`
    26  	Description        *string                  `json:"description,omitempty"`
    27  	ExternalURL        *string                  `json:"external_url,omitempty"`
    28  	HTMLURL            *string                  `json:"html_url,omitempty"`
    29  	CreatedAt          *Timestamp               `json:"created_at,omitempty"`
    30  	UpdatedAt          *Timestamp               `json:"updated_at,omitempty"`
    31  	Permissions        *InstallationPermissions `json:"permissions,omitempty"`
    32  	Events             []string                 `json:"events,omitempty"`
    33  	InstallationsCount *int                     `json:"installations_count,omitempty"`
    34  }
    35  
    36  // InstallationToken represents an installation token.
    37  type InstallationToken struct {
    38  	Token        *string                  `json:"token,omitempty"`
    39  	ExpiresAt    *Timestamp               `json:"expires_at,omitempty"`
    40  	Permissions  *InstallationPermissions `json:"permissions,omitempty"`
    41  	Repositories []*Repository            `json:"repositories,omitempty"`
    42  }
    43  
    44  // InstallationTokenOptions allow restricting a token's access to specific repositories.
    45  type InstallationTokenOptions struct {
    46  	// The IDs of the repositories that the installation token can access.
    47  	// Providing repository IDs restricts the access of an installation token to specific repositories.
    48  	RepositoryIDs []int64 `json:"repository_ids,omitempty"`
    49  
    50  	// The names of the repositories that the installation token can access.
    51  	// Providing repository names restricts the access of an installation token to specific repositories.
    52  	Repositories []string `json:"repositories,omitempty"`
    53  
    54  	// The permissions granted to the access token.
    55  	// The permissions object includes the permission names and their access type.
    56  	Permissions *InstallationPermissions `json:"permissions,omitempty"`
    57  }
    58  
    59  type InstallationTokenListRepoOptions struct {
    60  	// The IDs of the repositories that the installation token can access.
    61  	// Providing repository IDs restricts the access of an installation token to specific repositories.
    62  	RepositoryIDs []int64 `json:"repository_ids"`
    63  
    64  	// The names of the repositories that the installation token can access.
    65  	// Providing repository names restricts the access of an installation token to specific repositories.
    66  	Repositories []string `json:"repositories,omitempty"`
    67  
    68  	// The permissions granted to the access token.
    69  	// The permissions object includes the permission names and their access type.
    70  	Permissions *InstallationPermissions `json:"permissions,omitempty"`
    71  }
    72  
    73  // InstallationPermissions lists the repository and organization permissions for an installation.
    74  //
    75  // Permission names taken from:
    76  //
    77  //	https://docs.github.com/enterprise-server@3.0/rest/apps#create-an-installation-access-token-for-an-app
    78  //	https://docs.github.com/rest/apps#create-an-installation-access-token-for-an-app
    79  type InstallationPermissions struct {
    80  	Actions                       *string `json:"actions,omitempty"`
    81  	ActionsVariables              *string `json:"actions_variables,omitempty"`
    82  	Administration                *string `json:"administration,omitempty"`
    83  	Blocking                      *string `json:"blocking,omitempty"`
    84  	Checks                        *string `json:"checks,omitempty"`
    85  	Contents                      *string `json:"contents,omitempty"`
    86  	ContentReferences             *string `json:"content_references,omitempty"`
    87  	Deployments                   *string `json:"deployments,omitempty"`
    88  	Emails                        *string `json:"emails,omitempty"`
    89  	Environments                  *string `json:"environments,omitempty"`
    90  	Followers                     *string `json:"followers,omitempty"`
    91  	Issues                        *string `json:"issues,omitempty"`
    92  	Metadata                      *string `json:"metadata,omitempty"`
    93  	Members                       *string `json:"members,omitempty"`
    94  	OrganizationAdministration    *string `json:"organization_administration,omitempty"`
    95  	OrganizationCustomProperties  *string `json:"organization_custom_properties,omitempty"`
    96  	OrganizationCustomRoles       *string `json:"organization_custom_roles,omitempty"`
    97  	OrganizationCustomOrgRoles    *string `json:"organization_custom_org_roles,omitempty"`
    98  	OrganizationHooks             *string `json:"organization_hooks,omitempty"`
    99  	OrganizationPackages          *string `json:"organization_packages,omitempty"`
   100  	OrganizationPlan              *string `json:"organization_plan,omitempty"`
   101  	OrganizationPreReceiveHooks   *string `json:"organization_pre_receive_hooks,omitempty"`
   102  	OrganizationProjects          *string `json:"organization_projects,omitempty"`
   103  	OrganizationSecrets           *string `json:"organization_secrets,omitempty"`
   104  	OrganizationSelfHostedRunners *string `json:"organization_self_hosted_runners,omitempty"`
   105  	OrganizationUserBlocking      *string `json:"organization_user_blocking,omitempty"`
   106  	Packages                      *string `json:"packages,omitempty"`
   107  	Pages                         *string `json:"pages,omitempty"`
   108  	PullRequests                  *string `json:"pull_requests,omitempty"`
   109  	RepositoryHooks               *string `json:"repository_hooks,omitempty"`
   110  	RepositoryProjects            *string `json:"repository_projects,omitempty"`
   111  	RepositoryPreReceiveHooks     *string `json:"repository_pre_receive_hooks,omitempty"`
   112  	Secrets                       *string `json:"secrets,omitempty"`
   113  	SecretScanningAlerts          *string `json:"secret_scanning_alerts,omitempty"`
   114  	SecurityEvents                *string `json:"security_events,omitempty"`
   115  	SingleFile                    *string `json:"single_file,omitempty"`
   116  	Statuses                      *string `json:"statuses,omitempty"`
   117  	TeamDiscussions               *string `json:"team_discussions,omitempty"`
   118  	VulnerabilityAlerts           *string `json:"vulnerability_alerts,omitempty"`
   119  	Workflows                     *string `json:"workflows,omitempty"`
   120  }
   121  
   122  // InstallationRequest represents a pending GitHub App installation request.
   123  type InstallationRequest struct {
   124  	ID        *int64     `json:"id,omitempty"`
   125  	NodeID    *string    `json:"node_id,omitempty"`
   126  	Account   *User      `json:"account,omitempty"`
   127  	Requester *User      `json:"requester,omitempty"`
   128  	CreatedAt *Timestamp `json:"created_at,omitempty"`
   129  }
   130  
   131  // Installation represents a GitHub Apps installation.
   132  type Installation struct {
   133  	ID                     *int64                   `json:"id,omitempty"`
   134  	NodeID                 *string                  `json:"node_id,omitempty"`
   135  	AppID                  *int64                   `json:"app_id,omitempty"`
   136  	AppSlug                *string                  `json:"app_slug,omitempty"`
   137  	TargetID               *int64                   `json:"target_id,omitempty"`
   138  	Account                *User                    `json:"account,omitempty"`
   139  	AccessTokensURL        *string                  `json:"access_tokens_url,omitempty"`
   140  	RepositoriesURL        *string                  `json:"repositories_url,omitempty"`
   141  	HTMLURL                *string                  `json:"html_url,omitempty"`
   142  	TargetType             *string                  `json:"target_type,omitempty"`
   143  	SingleFileName         *string                  `json:"single_file_name,omitempty"`
   144  	RepositorySelection    *string                  `json:"repository_selection,omitempty"`
   145  	Events                 []string                 `json:"events,omitempty"`
   146  	SingleFilePaths        []string                 `json:"single_file_paths,omitempty"`
   147  	Permissions            *InstallationPermissions `json:"permissions,omitempty"`
   148  	CreatedAt              *Timestamp               `json:"created_at,omitempty"`
   149  	UpdatedAt              *Timestamp               `json:"updated_at,omitempty"`
   150  	HasMultipleSingleFiles *bool                    `json:"has_multiple_single_files,omitempty"`
   151  	SuspendedBy            *User                    `json:"suspended_by,omitempty"`
   152  	SuspendedAt            *Timestamp               `json:"suspended_at,omitempty"`
   153  }
   154  
   155  // Attachment represents a GitHub Apps attachment.
   156  type Attachment struct {
   157  	ID    *int64  `json:"id,omitempty"`
   158  	Title *string `json:"title,omitempty"`
   159  	Body  *string `json:"body,omitempty"`
   160  }
   161  
   162  // ContentReference represents a reference to a URL in an issue or pull request.
   163  type ContentReference struct {
   164  	ID        *int64  `json:"id,omitempty"`
   165  	NodeID    *string `json:"node_id,omitempty"`
   166  	Reference *string `json:"reference,omitempty"`
   167  }
   168  
   169  func (i Installation) String() string {
   170  	return Stringify(i)
   171  }
   172  
   173  // Get a single GitHub App. Passing the empty string will get
   174  // the authenticated GitHub App.
   175  //
   176  // Note: appSlug is just the URL-friendly name of your GitHub App.
   177  // You can find this on the settings page for your GitHub App
   178  // (e.g., https://github.com/settings/apps/:app_slug).
   179  //
   180  // GitHub API docs: https://docs.github.com/rest/apps/apps#get-an-app
   181  // GitHub API docs: https://docs.github.com/rest/apps/apps#get-the-authenticated-app
   182  //
   183  //meta:operation GET /app
   184  //meta:operation GET /apps/{app_slug}
   185  func (s *AppsService) Get(ctx context.Context, appSlug string) (*App, *Response, error) {
   186  	var u string
   187  	if appSlug != "" {
   188  		u = fmt.Sprintf("apps/%v", appSlug)
   189  	} else {
   190  		u = "app"
   191  	}
   192  
   193  	req, err := s.client.NewRequest("GET", u, nil)
   194  	if err != nil {
   195  		return nil, nil, err
   196  	}
   197  
   198  	app := new(App)
   199  	resp, err := s.client.Do(ctx, req, app)
   200  	if err != nil {
   201  		return nil, resp, err
   202  	}
   203  
   204  	return app, resp, nil
   205  }
   206  
   207  // ListInstallationRequests lists the pending installation requests that the current GitHub App has.
   208  //
   209  // GitHub API docs: https://docs.github.com/rest/apps/apps#list-installation-requests-for-the-authenticated-app
   210  //
   211  //meta:operation GET /app/installation-requests
   212  func (s *AppsService) ListInstallationRequests(ctx context.Context, opts *ListOptions) ([]*InstallationRequest, *Response, error) {
   213  	u, err := addOptions("app/installation-requests", opts)
   214  	if err != nil {
   215  		return nil, nil, err
   216  	}
   217  
   218  	req, err := s.client.NewRequest("GET", u, nil)
   219  	if err != nil {
   220  		return nil, nil, err
   221  	}
   222  
   223  	var i []*InstallationRequest
   224  	resp, err := s.client.Do(ctx, req, &i)
   225  	if err != nil {
   226  		return nil, resp, err
   227  	}
   228  
   229  	return i, resp, nil
   230  }
   231  
   232  // ListInstallations lists the installations that the current GitHub App has.
   233  //
   234  // GitHub API docs: https://docs.github.com/rest/apps/apps#list-installations-for-the-authenticated-app
   235  //
   236  //meta:operation GET /app/installations
   237  func (s *AppsService) ListInstallations(ctx context.Context, opts *ListOptions) ([]*Installation, *Response, error) {
   238  	u, err := addOptions("app/installations", opts)
   239  	if err != nil {
   240  		return nil, nil, err
   241  	}
   242  
   243  	req, err := s.client.NewRequest("GET", u, nil)
   244  	if err != nil {
   245  		return nil, nil, err
   246  	}
   247  
   248  	var i []*Installation
   249  	resp, err := s.client.Do(ctx, req, &i)
   250  	if err != nil {
   251  		return nil, resp, err
   252  	}
   253  
   254  	return i, resp, nil
   255  }
   256  
   257  // GetInstallation returns the specified installation.
   258  //
   259  // GitHub API docs: https://docs.github.com/rest/apps/apps#get-an-installation-for-the-authenticated-app
   260  //
   261  //meta:operation GET /app/installations/{installation_id}
   262  func (s *AppsService) GetInstallation(ctx context.Context, id int64) (*Installation, *Response, error) {
   263  	return s.getInstallation(ctx, fmt.Sprintf("app/installations/%v", id))
   264  }
   265  
   266  // ListUserInstallations lists installations that are accessible to the authenticated user.
   267  //
   268  // GitHub API docs: https://docs.github.com/rest/apps/installations#list-app-installations-accessible-to-the-user-access-token
   269  //
   270  //meta:operation GET /user/installations
   271  func (s *AppsService) ListUserInstallations(ctx context.Context, opts *ListOptions) ([]*Installation, *Response, error) {
   272  	u, err := addOptions("user/installations", opts)
   273  	if err != nil {
   274  		return nil, nil, err
   275  	}
   276  
   277  	req, err := s.client.NewRequest("GET", u, nil)
   278  	if err != nil {
   279  		return nil, nil, err
   280  	}
   281  
   282  	var i struct {
   283  		Installations []*Installation `json:"installations"`
   284  	}
   285  	resp, err := s.client.Do(ctx, req, &i)
   286  	if err != nil {
   287  		return nil, resp, err
   288  	}
   289  
   290  	return i.Installations, resp, nil
   291  }
   292  
   293  // SuspendInstallation suspends the specified installation.
   294  //
   295  // GitHub API docs: https://docs.github.com/rest/apps/apps#suspend-an-app-installation
   296  //
   297  //meta:operation PUT /app/installations/{installation_id}/suspended
   298  func (s *AppsService) SuspendInstallation(ctx context.Context, id int64) (*Response, error) {
   299  	u := fmt.Sprintf("app/installations/%v/suspended", id)
   300  
   301  	req, err := s.client.NewRequest("PUT", u, nil)
   302  	if err != nil {
   303  		return nil, err
   304  	}
   305  
   306  	return s.client.Do(ctx, req, nil)
   307  }
   308  
   309  // UnsuspendInstallation unsuspends the specified installation.
   310  //
   311  // GitHub API docs: https://docs.github.com/rest/apps/apps#unsuspend-an-app-installation
   312  //
   313  //meta:operation DELETE /app/installations/{installation_id}/suspended
   314  func (s *AppsService) UnsuspendInstallation(ctx context.Context, id int64) (*Response, error) {
   315  	u := fmt.Sprintf("app/installations/%v/suspended", id)
   316  
   317  	req, err := s.client.NewRequest("DELETE", u, nil)
   318  	if err != nil {
   319  		return nil, err
   320  	}
   321  
   322  	return s.client.Do(ctx, req, nil)
   323  }
   324  
   325  // DeleteInstallation deletes the specified installation.
   326  //
   327  // GitHub API docs: https://docs.github.com/rest/apps/apps#delete-an-installation-for-the-authenticated-app
   328  //
   329  //meta:operation DELETE /app/installations/{installation_id}
   330  func (s *AppsService) DeleteInstallation(ctx context.Context, id int64) (*Response, error) {
   331  	u := fmt.Sprintf("app/installations/%v", id)
   332  
   333  	req, err := s.client.NewRequest("DELETE", u, nil)
   334  	if err != nil {
   335  		return nil, err
   336  	}
   337  
   338  	return s.client.Do(ctx, req, nil)
   339  }
   340  
   341  // CreateInstallationToken creates a new installation token.
   342  //
   343  // GitHub API docs: https://docs.github.com/rest/apps/apps#create-an-installation-access-token-for-an-app
   344  //
   345  //meta:operation POST /app/installations/{installation_id}/access_tokens
   346  func (s *AppsService) CreateInstallationToken(ctx context.Context, id int64, opts *InstallationTokenOptions) (*InstallationToken, *Response, error) {
   347  	u := fmt.Sprintf("app/installations/%v/access_tokens", id)
   348  
   349  	req, err := s.client.NewRequest("POST", u, opts)
   350  	if err != nil {
   351  		return nil, nil, err
   352  	}
   353  
   354  	t := new(InstallationToken)
   355  	resp, err := s.client.Do(ctx, req, t)
   356  	if err != nil {
   357  		return nil, resp, err
   358  	}
   359  
   360  	return t, resp, nil
   361  }
   362  
   363  // CreateInstallationTokenListRepos creates a new installation token with a list of all repositories in an installation which is not possible with CreateInstallationToken.
   364  //
   365  // It differs from CreateInstallationToken by taking InstallationTokenListRepoOptions as a parameter which does not omit RepositoryIDs if that field is nil or an empty array.
   366  //
   367  // GitHub API docs: https://docs.github.com/rest/apps/apps#create-an-installation-access-token-for-an-app
   368  //
   369  //meta:operation POST /app/installations/{installation_id}/access_tokens
   370  func (s *AppsService) CreateInstallationTokenListRepos(ctx context.Context, id int64, opts *InstallationTokenListRepoOptions) (*InstallationToken, *Response, error) {
   371  	u := fmt.Sprintf("app/installations/%v/access_tokens", id)
   372  
   373  	req, err := s.client.NewRequest("POST", u, opts)
   374  	if err != nil {
   375  		return nil, nil, err
   376  	}
   377  
   378  	t := new(InstallationToken)
   379  	resp, err := s.client.Do(ctx, req, t)
   380  	if err != nil {
   381  		return nil, resp, err
   382  	}
   383  
   384  	return t, resp, nil
   385  }
   386  
   387  // CreateAttachment creates a new attachment on user comment containing a url.
   388  //
   389  // GitHub API docs: https://docs.github.com/enterprise-server@3.3/rest/reference/apps#create-a-content-attachment
   390  //
   391  //meta:operation POST /repos/{owner}/{repo}/content_references/{content_reference_id}/attachments
   392  func (s *AppsService) CreateAttachment(ctx context.Context, contentReferenceID int64, title, body string) (*Attachment, *Response, error) {
   393  	u := fmt.Sprintf("content_references/%v/attachments", contentReferenceID)
   394  	payload := &Attachment{Title: String(title), Body: String(body)}
   395  	req, err := s.client.NewRequest("POST", u, payload)
   396  	if err != nil {
   397  		return nil, nil, err
   398  	}
   399  
   400  	// TODO: remove custom Accept headers when APIs fully launch.
   401  	req.Header.Set("Accept", mediaTypeContentAttachmentsPreview)
   402  
   403  	m := &Attachment{}
   404  	resp, err := s.client.Do(ctx, req, m)
   405  	if err != nil {
   406  		return nil, resp, err
   407  	}
   408  
   409  	return m, resp, nil
   410  }
   411  
   412  // FindOrganizationInstallation finds the organization's installation information.
   413  //
   414  // GitHub API docs: https://docs.github.com/rest/apps/apps#get-an-organization-installation-for-the-authenticated-app
   415  //
   416  //meta:operation GET /orgs/{org}/installation
   417  func (s *AppsService) FindOrganizationInstallation(ctx context.Context, org string) (*Installation, *Response, error) {
   418  	return s.getInstallation(ctx, fmt.Sprintf("orgs/%v/installation", org))
   419  }
   420  
   421  // FindRepositoryInstallation finds the repository's installation information.
   422  //
   423  // GitHub API docs: https://docs.github.com/rest/apps/apps#get-a-repository-installation-for-the-authenticated-app
   424  //
   425  //meta:operation GET /repos/{owner}/{repo}/installation
   426  func (s *AppsService) FindRepositoryInstallation(ctx context.Context, owner, repo string) (*Installation, *Response, error) {
   427  	return s.getInstallation(ctx, fmt.Sprintf("repos/%v/%v/installation", owner, repo))
   428  }
   429  
   430  // FindRepositoryInstallationByID finds the repository's installation information.
   431  //
   432  // Note: FindRepositoryInstallationByID uses the undocumented GitHub API endpoint "GET /repositories/{repository_id}/installation".
   433  //
   434  //meta:operation GET /repositories/{repository_id}/installation
   435  func (s *AppsService) FindRepositoryInstallationByID(ctx context.Context, id int64) (*Installation, *Response, error) {
   436  	return s.getInstallation(ctx, fmt.Sprintf("repositories/%d/installation", id))
   437  }
   438  
   439  // FindUserInstallation finds the user's installation information.
   440  //
   441  // GitHub API docs: https://docs.github.com/rest/apps/apps#get-a-user-installation-for-the-authenticated-app
   442  //
   443  //meta:operation GET /users/{username}/installation
   444  func (s *AppsService) FindUserInstallation(ctx context.Context, user string) (*Installation, *Response, error) {
   445  	return s.getInstallation(ctx, fmt.Sprintf("users/%v/installation", user))
   446  }
   447  
   448  func (s *AppsService) getInstallation(ctx context.Context, url string) (*Installation, *Response, error) {
   449  	req, err := s.client.NewRequest("GET", url, nil)
   450  	if err != nil {
   451  		return nil, nil, err
   452  	}
   453  
   454  	i := new(Installation)
   455  	resp, err := s.client.Do(ctx, req, i)
   456  	if err != nil {
   457  		return nil, resp, err
   458  	}
   459  
   460  	return i, resp, nil
   461  }