Commit 443e23e61a10f34e9eccc3b6e91803a0996f5050
1 parent
f06d98e9
Exists in
master
and in
4 other branches
Add Repository#discover_default_branch and add it to PushObserver
Showing
2 changed files
with
33 additions
and
7 deletions
Show diff stats
app/roles/push_observer.rb
1 | +# Includes methods for handling Git Push events | ||
2 | +# | ||
3 | +# Triggered by PostReceive job | ||
1 | module PushObserver | 4 | module PushObserver |
2 | def observe_push(oldrev, newrev, ref, user) | 5 | def observe_push(oldrev, newrev, ref, user) |
3 | data = post_receive_data(oldrev, newrev, ref, user) | 6 | data = post_receive_data(oldrev, newrev, ref, user) |
@@ -84,11 +87,10 @@ module PushObserver | @@ -84,11 +87,10 @@ module PushObserver | ||
84 | data | 87 | data |
85 | end | 88 | end |
86 | 89 | ||
87 | - | ||
88 | - # This method will be called after each post receive | ||
89 | - # and only if user present in gitlab. | ||
90 | - # All callbacks for post receive should be placed here | 90 | + # This method will be called after each post receive and only if the provided |
91 | + # user is present in GitLab. | ||
91 | # | 92 | # |
93 | + # All callbacks for post receive should be placed here. | ||
92 | def trigger_post_receive(oldrev, newrev, ref, user) | 94 | def trigger_post_receive(oldrev, newrev, ref, user) |
93 | # Create push event | 95 | # Create push event |
94 | self.observe_push(oldrev, newrev, ref, user) | 96 | self.observe_push(oldrev, newrev, ref, user) |
@@ -101,5 +103,11 @@ module PushObserver | @@ -101,5 +103,11 @@ module PushObserver | ||
101 | 103 | ||
102 | # Create satellite | 104 | # Create satellite |
103 | self.satellite.create unless self.satellite.exists? | 105 | self.satellite.create unless self.satellite.exists? |
106 | + | ||
107 | + # Discover the default branch, but only if it hasn't already been set to | ||
108 | + # something else | ||
109 | + if default_branch.nil? | ||
110 | + update_attributes(default_branch: discover_default_branch) | ||
111 | + end | ||
104 | end | 112 | end |
105 | end | 113 | end |
app/roles/repository.rb
@@ -94,6 +94,24 @@ module Repository | @@ -94,6 +94,24 @@ module Repository | ||
94 | end.sort_by(&:name) | 94 | end.sort_by(&:name) |
95 | end | 95 | end |
96 | 96 | ||
97 | + # Discovers the default branch based on the repository's available branches | ||
98 | + # | ||
99 | + # - If no branches are present, returns nil | ||
100 | + # - If one branch is present, returns its name | ||
101 | + # - If two or more branches are present, returns the one that has a name | ||
102 | + # matching root_ref (default_branch or 'master' if default_branch is nil) | ||
103 | + def discover_default_branch | ||
104 | + branches = heads.collect(&:name) | ||
105 | + | ||
106 | + if branches.length == 0 | ||
107 | + nil | ||
108 | + elsif branches.length == 1 | ||
109 | + branches.first | ||
110 | + else | ||
111 | + branches.select { |v| v == root_ref }.first | ||
112 | + end | ||
113 | + end | ||
114 | + | ||
97 | def has_commits? | 115 | def has_commits? |
98 | !!commit | 116 | !!commit |
99 | end | 117 | end |
@@ -102,7 +120,7 @@ module Repository | @@ -102,7 +120,7 @@ module Repository | ||
102 | default_branch || "master" | 120 | default_branch || "master" |
103 | end | 121 | end |
104 | 122 | ||
105 | - def root_ref? branch | 123 | + def root_ref?(branch) |
106 | root_ref == branch | 124 | root_ref == branch |
107 | end | 125 | end |
108 | 126 | ||
@@ -111,7 +129,7 @@ module Repository | @@ -111,7 +129,7 @@ module Repository | ||
111 | # Already packed repo archives stored at | 129 | # Already packed repo archives stored at |
112 | # app_root/tmp/repositories/project_name/project_name-commit-id.tag.gz | 130 | # app_root/tmp/repositories/project_name/project_name-commit-id.tag.gz |
113 | # | 131 | # |
114 | - def archive_repo ref | 132 | + def archive_repo(ref) |
115 | ref = ref || self.root_ref | 133 | ref = ref || self.root_ref |
116 | commit = self.commit(ref) | 134 | commit = self.commit(ref) |
117 | return nil unless commit | 135 | return nil unless commit |
@@ -138,6 +156,6 @@ module Repository | @@ -138,6 +156,6 @@ module Repository | ||
138 | end | 156 | end |
139 | 157 | ||
140 | def http_url_to_repo | 158 | def http_url_to_repo |
141 | - http_url = [Gitlab.config.url, "/", path, ".git"].join() | 159 | + http_url = [Gitlab.config.url, "/", path, ".git"].join('') |
142 | end | 160 | end |
143 | end | 161 | end |