Skip to content

Commit e6be51b

Browse files
authored
fix(repohub): GetFiles should fetch Git updates from origin only for specified branch (#159)
Right now, when fetching the YAML files for workflow editor, we fetch Git remote updates from all branches in the repository. That can take a while to run for big repositories, which can lead to 500s in workflow editor page. Here, we limit the `git fetch origin` command to fetch changes only for the branch we are interested.
1 parent b381290 commit e6be51b

File tree

13 files changed

+110
-23
lines changed

13 files changed

+110
-23
lines changed

front/lib/front/models/repohub.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ defmodule Front.Models.Repohub do
33

44
alias InternalApi.Repository.RepositoryService.Stub
55

6-
def fetch_sempahore_files(repository_id, initial_yaml, commit_sha \\ "") do
6+
def fetch_semaphore_files(repository_id, initial_yaml, commit_sha \\ "", reference \\ "") do
77
Watchman.benchmark("external.repohub.find_all_yaml_files.duration", fn ->
88
request =
99
InternalApi.Repository.GetFilesRequest.new(
1010
repository_id: repository_id,
11-
revision: InternalApi.Repository.Revision.new(commit_sha: commit_sha),
11+
revision:
12+
InternalApi.Repository.Revision.new(
13+
commit_sha: commit_sha,
14+
reference: reference
15+
),
1216
selectors: extract_selectors(initial_yaml),
1317
include_content: true
1418
)

front/lib/front_web/controllers/project_controller.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ defmodule FrontWeb.ProjectController do
198198

199199
fetch_yaml_files =
200200
Async.run(
201-
fn -> Repohub.fetch_sempahore_files(project.repo_id, project.initial_pipeline_file) end,
201+
fn -> Repohub.fetch_semaphore_files(project.repo_id, project.initial_pipeline_file) end,
202202
metric: "workflow.edit.load_all_yaml_files"
203203
)
204204

front/lib/front_web/controllers/workflow_controller.ex

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,38 @@ defmodule FrontWeb.WorkflowController do
117117
end
118118

119119
defp fetch_yaml_files(repo_id, hook, initial_yaml) do
120-
if hook.type == "pr" do
121-
Repohub.fetch_sempahore_files(repo_id, initial_yaml, hook.pr_sha)
122-
else
123-
Repohub.fetch_sempahore_files(repo_id, initial_yaml, hook.head_commit_sha)
120+
cond do
121+
hook.type == "pr" and hook.forked_pr ->
122+
Repohub.fetch_semaphore_files(
123+
repo_id,
124+
initial_yaml,
125+
hook.pr_sha,
126+
"refs/pulls/#{hook.pr_number}/head"
127+
)
128+
129+
hook.type == "pr" and !hook.forked_pr ->
130+
Repohub.fetch_semaphore_files(
131+
repo_id,
132+
initial_yaml,
133+
hook.pr_sha,
134+
"refs/heads/#{hook.pr_branch_name}"
135+
)
136+
137+
hook.type == "branch" ->
138+
Repohub.fetch_semaphore_files(
139+
repo_id,
140+
initial_yaml,
141+
hook.pr_sha,
142+
"refs/heads/#{hook.branch_name}"
143+
)
144+
145+
hook.type == "tag" ->
146+
Repohub.fetch_semaphore_files(
147+
repo_id,
148+
initial_yaml,
149+
hook.pr_sha,
150+
"refs/tags/#{hook.tag_name}"
151+
)
124152
end
125153
end
126154

repohub/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ARG GO_VERSION=1.21
1+
ARG GO_VERSION=1.22
22
ARG UBUNTU_VERSION=22.04
33
ARG BUILDER_IMAGE="golang:${GO_VERSION}"
44
ARG RUNNER_IMAGE="ubuntu:${UBUNTU_VERSION}"

repohub/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module github.com/semaphoreio/semaphore/repohub
22

3-
go 1.21
3+
go 1.22
44

55
require (
66
github.com/bmatcuk/doublestar v1.1.5
7-
github.com/golang-jwt/jwt/v5 v5.2.1
7+
github.com/golang-jwt/jwt/v5 v5.2.2
88
github.com/golang/protobuf v1.5.4
99
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.2.0
1010
github.com/jinzhu/gorm v1.9.16

repohub/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DP
1010
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=
1111
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
1212
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
13-
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
14-
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
13+
github.com/golang-jwt/jwt/v5 v5.2.2 h1:Rl4B7itRWVtYIHFrSNd7vhTiz9UpLdi6gZhZ3wEeDy8=
14+
github.com/golang-jwt/jwt/v5 v5.2.2/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
1515
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY=
1616
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
1717
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=

repohub/pkg/fetcher/fetcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (f *Fetcher) Sync(index int, totalRepoCount int, r *models.Repository) bool
9797
Password: token,
9898
}
9999

100-
op := gitrekt.NewUpdateOrCloneOperation(repo)
100+
op := gitrekt.NewUpdateOrCloneOperation(repo, "")
101101

102102
log.Printf("Syncing repo %d/%d START: Repo %s from %s",
103103
index,

repohub/pkg/gitrekt/commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (op *CommitOperation) Run() (*Revision, error) {
100100
err := op.Validate()
101101
check(err, "Invalid parameters")
102102

103-
repoPath, err := UpdateOrClone(op.Repository)
103+
repoPath, err := UpdateOrClone(op.Repository, nil)
104104
check(err, "Failed to clone repository")
105105

106106
r, err := git.OpenRepository(repoPath)

repohub/pkg/gitrekt/list_changed_files.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func ListChangedFiles(repo *Repository, base Revision, head Revision, comparison
2222

2323
log.Printf("ListChangedFiles Started. Repo: %s", repo.HttpURL)
2424

25-
repoPath, err := UpdateOrClone(repo)
25+
repoPath, err := UpdateOrClone(repo, nil)
2626
if err != nil {
2727
return nil, err
2828
}

repohub/pkg/gitrekt/search.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func Search(repo *Repository, rev Revision, options *SearchOptions) ([]*File, er
3131
rev,
3232
)
3333

34-
repoPath, err := UpdateOrClone(repo)
34+
repoPath, err := UpdateOrClone(repo, &rev)
3535
if err != nil {
3636
return nil, err
3737
}

0 commit comments

Comments
 (0)