Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(azure): allow correct path to be derived for on premise installation #1863

Merged
merged 10 commits into from
Oct 26, 2021
Merged
23 changes: 17 additions & 6 deletions server/events/event_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,7 @@ func (e *EventParser) ParseAzureDevopsPull(pull *azuredevops.GitPullRequest) (pu
err = errors.New("url is null")
return
}

headBranch := pull.GetSourceRefName()
if headBranch == "" {
err = errors.New("sourceRefName (branch name) is null")
Expand Down Expand Up @@ -851,19 +852,22 @@ func (e *EventParser) ParseAzureDevopsRepo(adRepo *azuredevops.GitRepository) (m
teamProject := adRepo.GetProject()
parent := adRepo.GetParentRepository()
owner := ""

uri, err := url.Parse(adRepo.GetWebURL())
if err != nil {
return models.Repo{}, err
}

if parent != nil {
owner = parent.GetName()
} else {
uri, err := url.Parse(adRepo.GetWebURL())
if err != nil {
return models.Repo{}, err
}

if strings.Contains(uri.Host, "visualstudio.com") {
owner = strings.Split(uri.Host, ".")[0]
} else if strings.Contains(uri.Host, "dev.azure.com") {
owner = strings.Split(uri.Path, "/")[1]
} else {
owner = ""
owner = strings.Split(uri.Path, "/")[1] // to support owner for self hosted
}
}

Expand All @@ -872,7 +876,14 @@ func (e *EventParser) ParseAzureDevopsRepo(adRepo *azuredevops.GitRepository) (m
// https://docs.microsoft.com/en-us/azure/devops/release-notes/2018/sep-10-azure-devops-launch#switch-existing-organizations-to-use-the-new-domain-name-url
project := teamProject.GetName()
repo := adRepo.GetName()
cloneURL := fmt.Sprintf("https://dev.azure.com/%s/%s/_git/%s", owner, project, repo)

host := uri.Host
if host == "" {
host = "dev.azure.com"
}

cloneURL := fmt.Sprintf("https://%s/%s/%s/_git/%s", host, owner, project, repo)
fmt.Println("%", cloneURL)
fullName := fmt.Sprintf("%s/%s/%s", owner, project, repo)
return models.NewRepo(models.AzureDevops, fullName, cloneURL, e.AzureDevopsUser, e.AzureDevopsToken)
}
176 changes: 176 additions & 0 deletions server/events/event_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1313,3 +1313,179 @@ func TestParseAzureDevopsPull(t *testing.T) {
Equals(t, expBaseRepo, actBaseRepo)
Equals(t, expBaseRepo, actHeadRepo)
}

func TestParseAzureDevopsSelfHostedRepo(t *testing.T) {
// this should be successful
repo := ADSelfRepo
repo.ParentRepository = nil
r, err := parser.ParseAzureDevopsRepo(&repo)
Ok(t, err)
Equals(t, models.Repo{
Owner: "owner/project",
FullName: "owner/project/repo",
CloneURL: "https://azuredevops-user:[email protected]/owner/project/_git/repo",
SanitizedCloneURL: "https://azuredevops-user:<redacted>@devops.abc.com/owner/project/_git/repo",
Name: "repo",
VCSHost: models.VCSHost{
Hostname: "devops.abc.com",
Type: models.AzureDevops,
},
}, r)

}

func TestParseAzureDevopsSelfHostedPullEvent(t *testing.T) {
_, _, _, _, _, err := parser.ParseAzureDevopsPullEvent(ADSelfPullEvent)
Ok(t, err)

testPull := deepcopy.Copy(ADSelfPull).(azuredevops.GitPullRequest)
testPull.LastMergeSourceCommit.CommitID = nil
_, _, _, err = parser.ParseAzureDevopsPull(&testPull)
ErrEquals(t, "lastMergeSourceCommit.commitID is null", err)

testPull = deepcopy.Copy(ADSelfPull).(azuredevops.GitPullRequest)
testPull.URL = nil
_, _, _, err = parser.ParseAzureDevopsPull(&testPull)
ErrEquals(t, "url is null", err)
testEvent := deepcopy.Copy(ADSelfPullEvent).(azuredevops.Event)
resource := deepcopy.Copy(testEvent.Resource).(*azuredevops.GitPullRequest)
resource.CreatedBy = nil
testEvent.Resource = resource
_, _, _, _, _, err = parser.ParseAzureDevopsPullEvent(testEvent)
ErrEquals(t, "CreatedBy is null", err)

testEvent = deepcopy.Copy(ADSelfPullEvent).(azuredevops.Event)
resource = deepcopy.Copy(testEvent.Resource).(*azuredevops.GitPullRequest)
resource.CreatedBy.UniqueName = azuredevops.String("")
testEvent.Resource = resource
_, _, _, _, _, err = parser.ParseAzureDevopsPullEvent(testEvent)
ErrEquals(t, "CreatedBy.UniqueName is null", err)

actPull, evType, actBaseRepo, actHeadRepo, actUser, err := parser.ParseAzureDevopsPullEvent(ADSelfPullEvent)
Ok(t, err)
expBaseRepo := models.Repo{
Owner: "owner/project",
FullName: "owner/project/repo",
CloneURL: "https://azuredevops-user:[email protected]/owner/project/_git/repo",
SanitizedCloneURL: "https://azuredevops-user:<redacted>@devops.abc.com/owner/project/_git/repo",
Name: "repo",
VCSHost: models.VCSHost{
Hostname: "devops.abc.com",
Type: models.AzureDevops,
},
}
Equals(t, expBaseRepo, actBaseRepo)
Equals(t, expBaseRepo, actHeadRepo)
Equals(t, models.PullRequest{
URL: ADSelfPull.GetURL(),
Author: ADSelfPull.CreatedBy.GetUniqueName(),
HeadBranch: "feature/sourceBranch",
BaseBranch: "targetBranch",
HeadCommit: ADSelfPull.LastMergeSourceCommit.GetCommitID(),
Num: ADSelfPull.GetPullRequestID(),
State: models.OpenPullState,
BaseRepo: expBaseRepo,
}, actPull)
Equals(t, models.OpenedPullEvent, evType)
Equals(t, models.User{Username: "[email protected]"}, actUser)
}

func TestParseAzureDevopsSelfHostedPullEvent_EventType(t *testing.T) {
cases := []struct {
action string
exp models.PullRequestEventType
}{
{
action: "git.pullrequest.updated",
exp: models.UpdatedPullEvent,
},
{
action: "git.pullrequest.created",
exp: models.OpenedPullEvent,
},
{
action: "git.pullrequest.updated",
exp: models.ClosedPullEvent,
},
{
action: "anything_else",
exp: models.OtherPullEvent,
},
}

for _, c := range cases {
t.Run(c.action, func(t *testing.T) {
event := deepcopy.Copy(ADSelfPullEvent).(azuredevops.Event)
if c.exp == models.ClosedPullEvent {
event = deepcopy.Copy(ADSelfPullClosedEvent).(azuredevops.Event)
}
event.EventType = c.action
_, actType, _, _, _, err := parser.ParseAzureDevopsPullEvent(event)
Ok(t, err)
Equals(t, c.exp, actType)
})
}
}

func TestParseAzureSelfHostedDevopsPull(t *testing.T) {
testPull := deepcopy.Copy(ADSelfPull).(azuredevops.GitPullRequest)
testPull.LastMergeSourceCommit.CommitID = nil
_, _, _, err := parser.ParseAzureDevopsPull(&testPull)
ErrEquals(t, "lastMergeSourceCommit.commitID is null", err)

testPull = deepcopy.Copy(ADSelfPull).(azuredevops.GitPullRequest)
testPull.URL = nil
_, _, _, err = parser.ParseAzureDevopsPull(&testPull)
ErrEquals(t, "url is null", err)

testPull = deepcopy.Copy(ADSelfPull).(azuredevops.GitPullRequest)
testPull.SourceRefName = nil
_, _, _, err = parser.ParseAzureDevopsPull(&testPull)
ErrEquals(t, "sourceRefName (branch name) is null", err)

testPull = deepcopy.Copy(ADSelfPull).(azuredevops.GitPullRequest)
testPull.TargetRefName = nil
_, _, _, err = parser.ParseAzureDevopsPull(&testPull)
ErrEquals(t, "targetRefName (branch name) is null", err)

testPull = deepcopy.Copy(ADSelfPull).(azuredevops.GitPullRequest)
testPull.CreatedBy = nil
_, _, _, err = parser.ParseAzureDevopsPull(&testPull)
ErrEquals(t, "CreatedBy is null", err)

testPull = deepcopy.Copy(ADSelfPull).(azuredevops.GitPullRequest)
testPull.CreatedBy.UniqueName = nil
_, _, _, err = parser.ParseAzureDevopsPull(&testPull)
ErrEquals(t, "CreatedBy.UniqueName is null", err)

testPull = deepcopy.Copy(ADSelfPull).(azuredevops.GitPullRequest)
testPull.PullRequestID = nil
_, _, _, err = parser.ParseAzureDevopsPull(&testPull)
ErrEquals(t, "pullRequestId is null", err)

actPull, actBaseRepo, actHeadRepo, err := parser.ParseAzureDevopsPull(&ADSelfPull)
Ok(t, err)
expBaseRepo := models.Repo{
Owner: "owner/project",
FullName: "owner/project/repo",
CloneURL: "https://azuredevops-user:[email protected]/owner/project/_git/repo",
SanitizedCloneURL: "https://azuredevops-user:<redacted>@devops.abc.com/owner/project/_git/repo",
Name: "repo",
VCSHost: models.VCSHost{
Hostname: "devops.abc.com",
Type: models.AzureDevops,
},
}
Equals(t, models.PullRequest{
URL: ADSelfPull.GetURL(),
Author: ADSelfPull.CreatedBy.GetUniqueName(),
HeadBranch: "feature/sourceBranch",
BaseBranch: "targetBranch",
HeadCommit: ADSelfPull.LastMergeSourceCommit.GetCommitID(),
Num: ADSelfPull.GetPullRequestID(),
State: models.OpenPullState,
BaseRepo: expBaseRepo,
}, actPull)
Equals(t, expBaseRepo, actBaseRepo)
Equals(t, expBaseRepo, actHeadRepo)
}
Loading