-
Notifications
You must be signed in to change notification settings - Fork 145
/
link_github_repository.go
75 lines (57 loc) · 2.17 KB
/
link_github_repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package workflows
import (
"github.com/labstack/echo/v4"
"github.com/hatchet-dev/hatchet/api/v1/server/oas/apierrors"
"github.com/hatchet-dev/hatchet/api/v1/server/oas/gen"
"github.com/hatchet-dev/hatchet/api/v1/server/oas/transformers"
"github.com/hatchet-dev/hatchet/internal/integrations/vcs"
"github.com/hatchet-dev/hatchet/internal/repository"
"github.com/hatchet-dev/hatchet/internal/repository/prisma/db"
)
func (t *WorkflowService) WorkflowUpdateLinkGithub(ctx echo.Context, request gen.WorkflowUpdateLinkGithubRequestObject) (gen.WorkflowUpdateLinkGithubResponseObject, error) {
user := ctx.Get("user").(*db.UserModel)
workflow := ctx.Get("workflow").(*db.WorkflowModel)
// check that the user has access to the installation id
installationId := request.Body.InstallationId
_, err := t.config.Repository.Github().ReadGithubAppInstallationByID(installationId)
if err != nil {
return gen.WorkflowUpdateLinkGithub404JSONResponse(
apierrors.NewAPIErrors("Installation not found"),
), nil
}
if canAccess, err := t.config.Repository.Github().CanUserAccessInstallation(installationId, user.ID); err != nil || !canAccess {
return gen.WorkflowUpdateLinkGithub403JSONResponse(
apierrors.NewAPIErrors("User does not have access to the installation"),
), nil
}
_, err = t.config.Repository.Workflow().UpsertWorkflowDeploymentConfig(
workflow.ID,
&repository.UpsertWorkflowDeploymentConfigOpts{
GithubAppInstallationId: installationId,
GitRepoName: request.Body.GitRepoName,
GitRepoOwner: request.Body.GitRepoOwner,
GitRepoBranch: request.Body.GitRepoBranch,
},
)
if err != nil {
return nil, err
}
workflow, err = t.config.Repository.Workflow().GetWorkflowById(workflow.ID)
if err != nil {
return nil, err
}
vcsProvider := t.config.VCSProviders[vcs.VCSRepositoryKindGithub]
vcs, err := vcsProvider.GetVCSRepositoryFromWorkflow(workflow)
if err != nil {
return nil, err
}
err = vcs.SetupRepository(workflow.TenantID)
if err != nil {
return nil, err
}
resp, err := transformers.ToWorkflow(workflow, nil)
if err != nil {
return nil, err
}
return gen.WorkflowUpdateLinkGithub200JSONResponse(*resp), nil
}