PushToGithub implements localv1connect.LocalServiceHandler. It assumes that the current project is not a git repo, it should generally be called after DeployValidation.
(ctx context.Context, r *connect.Request[localv1.PushToGithubRequest])
| 130 | // PushToGithub implements localv1connect.LocalServiceHandler. |
| 131 | // It assumes that the current project is not a git repo, it should generally be called after DeployValidation. |
| 132 | func (s *Server) PushToGithub(ctx context.Context, r *connect.Request[localv1.PushToGithubRequest]) (*connect.Response[localv1.PushToGithubResponse], error) { |
| 133 | // Get authenticated admin client |
| 134 | if !s.app.ch.IsAuthenticated() { |
| 135 | return nil, errors.New("must authenticate before performing this action") |
| 136 | } |
| 137 | c, err := s.app.ch.Client() |
| 138 | if err != nil { |
| 139 | return nil, err |
| 140 | } |
| 141 | |
| 142 | // Check if the project already has a Git repo |
| 143 | initGit := false |
| 144 | remote, err := gitutil.ExtractGitRemote(s.app.ProjectPath, "", false) |
| 145 | if err != nil { |
| 146 | if errors.Is(err, gitutil.ErrNotAGitRepository) { |
| 147 | initGit = true |
| 148 | } else if !errors.Is(err, gitutil.ErrGitRemoteNotFound) { |
| 149 | return nil, err |
| 150 | } |
| 151 | } |
| 152 | if remote.Name != "" { |
| 153 | return nil, errors.New("git repository is already initialized with a remote") |
| 154 | } |
| 155 | |
| 156 | gitStatus, err := c.GetGithubUserStatus(ctx, &adminv1.GetGithubUserStatusRequest{}) |
| 157 | if err != nil { |
| 158 | return nil, err |
| 159 | } |
| 160 | if !gitStatus.HasAccess { |
| 161 | // generally this should not happen as IsGithubConnected should be true before pushing to git |
| 162 | return nil, fmt.Errorf("rill git app should be installed by user before pushing by visiting %s", gitStatus.GrantAccessUrl) |
| 163 | } |
| 164 | |
| 165 | // if r.Msg.Account is empty, githubAccount will be "" which is equivalent to using default github account which is same as github username |
| 166 | var githubAccount string |
| 167 | if r.Msg.Account == "" || r.Msg.Account == gitStatus.Account { |
| 168 | githubAccount = "" |
| 169 | } else { |
| 170 | githubAccount = r.Msg.Account |
| 171 | } |
| 172 | |
| 173 | // check if we have write permission on the github account |
| 174 | // this is a safety check as DeployValidation should take care of this |
| 175 | if githubAccount == "" { |
| 176 | if gitStatus.UserInstallationPermission != adminv1.GithubPermission_GITHUB_PERMISSION_WRITE { |
| 177 | return nil, fmt.Errorf("rill github app should be installed with write permission on user personal account by visiting %s", gitStatus.GrantAccessUrl) |
| 178 | } |
| 179 | } else { |
| 180 | valid := false |
| 181 | for o, p := range gitStatus.OrganizationInstallationPermissions { |
| 182 | if o == githubAccount && p == adminv1.GithubPermission_GITHUB_PERMISSION_WRITE { |
| 183 | valid = true |
| 184 | break |
| 185 | } |
| 186 | } |
| 187 | if !valid { |
| 188 | return nil, fmt.Errorf("rill github app should be installed with write permission on organization %q by visiting %s", githubAccount, gitStatus.GrantAccessUrl) |
| 189 | } |
nothing calls this directly
no test coverage detected