Skip to content

Commit

Permalink
Merge pull request #12 from ocadotechnology/alternate-gitlab-url
Browse files Browse the repository at this point in the history
Handle alternate ssh host for GitLab
  • Loading branch information
Dbzman authored May 29, 2020
2 parents 13f919c + 8dc59eb commit d153029
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ All you have to do is to configure a "System Hook" for **Push events** or **Tag

## This application

**All of the following settings are required!**
### Mandatory settings

| Environment Variable | Description | Example |
| -------------------- | ----------- | ------- |
Expand All @@ -36,3 +36,8 @@ All you have to do is to configure a "System Hook" for **Push events** or **Tag
|`CRUCIBLE_PROJECT_REFRESH_INTERVAL`|How often the repository list should be refreshed. (In minutes)|60|
|`CRUCIBLE_PROJECT_LIMIT`|Limit of how many projects should be fetched from Crucible in one request.|100|
|`GITLAB_TOKEN`|A token in the GitLab webhook which will be used for validation in the app.|some_token|

### Optional settings
| Environment Variable | Description | Example |
| -------------------- | ----------- | ------- |
|`GITLAB_HOSTNAMES`|Space-separated list of host names used by GitLab|`example.com example.org altssh.example.com:443`|
4 changes: 2 additions & 2 deletions crucible.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (cache *CrucibleRepositoriesCache) isEmpty() bool {
return cache.getRepositoriesCount() == 0
}

func (cache *CrucibleRepositoriesCache) updateFactory(settings CrucibleSettings) func() {
func (cache *CrucibleRepositoriesCache) updateFactory(settings CrucibleSettings, normalizeGitUrl func(string) string) func() {
return func() {
var repositoriesMap = make(map[string]string)
var start uint32
Expand All @@ -62,7 +62,7 @@ func (cache *CrucibleRepositoriesCache) updateFactory(settings CrucibleSettings)
start = repositories.Start + repositories.Size

for _, repo := range repositories.Values {
normalizedUrl := NormalizeGitUrl(repo.Git.Location)
normalizedUrl := normalizeGitUrl(repo.Git.Location)
repositoriesMap[normalizedUrl] = repo.Name
}
}
Expand Down
14 changes: 11 additions & 3 deletions gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@ type GitLabHookProject struct {
}

type GitLabSettings struct {
Token string
Token string
HostNames []string
}

func NormalizeGitUrl(url string) string {
func NormalizeGitUrl(url string, gitLabHostNames []string) string {
url = strings.TrimPrefix(url, "http://")
url = strings.TrimPrefix(url, "https://")
url = strings.TrimPrefix(url, "ssh://")
url = strings.TrimPrefix(url, "git@")
if len(gitLabHostNames) > 1 {
canonicalHostName := gitLabHostNames[0]
for _, altHostName := range gitLabHostNames[1:] {
url = strings.Replace(url, altHostName, canonicalHostName, -1)
}
}
url = strings.TrimSuffix(url, ".git")
url = strings.Replace(url, ":", "/", -1)
return url
Expand Down Expand Up @@ -57,5 +65,5 @@ func GetNormalizedGitUrlFromRequest(request *http.Request, gitLabSettings GitLab
return "", fmt.Errorf("could not read body: %v", decoderError)
}

return NormalizeGitUrl(gitLabHook.Project.WebUrl), nil
return NormalizeGitUrl(gitLabHook.Project.WebUrl, gitLabSettings.HostNames), nil
}
17 changes: 11 additions & 6 deletions gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ import (

func TestNormalizeGitUrl(t *testing.T) {
cases := []struct {
gitUrl, normalizedUrl string
gitUrl string
gitLabHostNames []string
normalizedUrl string
}{
{"http://example.com/jsmith/example", "example.com/jsmith/example"},
{"https://example.com/jsmith/example", "example.com/jsmith/example"},
{"[email protected]:jsmith/example.git", "example.com/jsmith/example"},
{"", ""},
{"http://example.com/jsmith/example", []string{}, "example.com/jsmith/example"},
{"https://example.com/jsmith/example", []string{}, "example.com/jsmith/example"},
{"[email protected]:jsmith/example.git", []string{}, "example.com/jsmith/example"},
{"", []string{}, ""},
{"http://example.org/jsmith/example", []string{"example.com", "example.org"}, "example.com/jsmith/example"},
{"http://example.com/jsmith/example", []string{"example.com", "example.org"}, "example.com/jsmith/example"},
{"ssh://[email protected]:443/jsmith/example", []string{"example.com", "altssh.example.com:443"}, "example.com/jsmith/example"},
}
for _, c := range cases {
got := NormalizeGitUrl(c.gitUrl)
got := NormalizeGitUrl(c.gitUrl, c.gitLabHostNames)
if got != c.normalizedUrl {
t.Errorf("NormalizeGitUrl(%q) == %q, expected %q", c.gitUrl, got, c.normalizedUrl)
}
Expand Down
9 changes: 7 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"os"
"strconv"
"strings"
"time"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -83,10 +84,14 @@ func main() {
ProjectLimit: int(projectLimit),
}
gitLabSettings := GitLabSettings{
Token: os.Getenv("GITLAB_TOKEN"),
Token: os.Getenv("GITLAB_TOKEN"),
HostNames: strings.Fields(os.Getenv("GITLAB_HOSTNAMES")),
}

crucibleCache := &CrucibleRepositoriesCache{}
updateCache := crucibleCache.updateFactory(crucibleSettings)
updateCache := crucibleCache.updateFactory(crucibleSettings, func(url string) string {
return NormalizeGitUrl(url, gitLabSettings.HostNames)
})
updateCache()
go cron(updateCache, time.Minute*time.Duration(crucibleSettings.ProjectRefreshInterval))

Expand Down

0 comments on commit d153029

Please sign in to comment.