| // Copyright 2019 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package checkout |
| |
| import ( |
| "fmt" |
| "net/url" |
| "strings" |
| |
| "go.fuchsia.dev/infra/cmd/build_init/checkout" |
| |
| buildbucketpb "go.chromium.org/luci/buildbucket/proto" |
| ) |
| |
| // ResolveRepo returns the host and project associated with the build's input. |
| // |
| // If the build input has a GitilesCommit, return its host and project. |
| // If the build input only has a GerritChange, return its host and project. |
| // If the build input has neither, return the host and project based on the |
| // provided fallback remote. |
| func ResolveRepo(buildInput *buildbucketpb.Build_Input, fallbackRemote string) (string, string, error) { |
| var host, project string |
| if buildInput.GitilesCommit != nil { |
| return buildInput.GitilesCommit.Host, buildInput.GitilesCommit.Project, nil |
| } |
| if len(buildInput.GerritChanges) > 0 { |
| host = checkout.RemoveCodeReviewSuffix(buildInput.GerritChanges[0].Host) |
| project = buildInput.GerritChanges[0].Project |
| } else { |
| remoteURL, err := url.Parse(fallbackRemote) |
| if err != nil { |
| return "", "", fmt.Errorf("could not parse remote %s as URL: %v", fallbackRemote, err) |
| } |
| host = remoteURL.Host |
| project = strings.TrimLeft(remoteURL.Path, "/") |
| } |
| return host, project, nil |
| } |
| |
| // Resolve a build input's integration URL. |
| func ResolveIntegrationURL(buildInput *buildbucketpb.Build_Input) (*url.URL, error) { |
| var host string |
| if changes := buildInput.GetGerritChanges(); changes != nil { |
| host = checkout.RemoveCodeReviewSuffix(changes[0].GetHost()) |
| } else if gitilesCommit := buildInput.GetGitilesCommit(); gitilesCommit != nil { |
| host = gitilesCommit.GetHost() |
| } else { |
| return nil, fmt.Errorf("could not resolve host") |
| } |
| return &url.URL{ |
| Scheme: "https", |
| Host: host, |
| Path: "integration", |
| }, nil |
| } |
| |
| // Check whether a build input has a GerritChange from `remote`. |
| func HasRepoChange(buildInput *buildbucketpb.Build_Input, remote string) (bool, error) { |
| if len(buildInput.GerritChanges) == 0 { |
| return false, nil |
| } |
| remoteURL, err := url.Parse(remote) |
| if err != nil { |
| return false, fmt.Errorf("could not parse URL %s", remote) |
| } |
| change := buildInput.GerritChanges[0] |
| if change.Project == strings.TrimLeft(remoteURL.Path, "/") && checkout.RemoveCodeReviewSuffix(change.GetHost()) == remoteURL.Host { |
| return true, nil |
| } |
| return false, nil |
| } |