Skip to content

Commit

Permalink
Add support for GitHub deployment reference
Browse files Browse the repository at this point in the history
  • Loading branch information
carloscastrojumo committed Oct 2, 2023
1 parent c7e26ba commit c300de1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
24 changes: 22 additions & 2 deletions pkg/services/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type GitHubDeployment struct {
LogURL string `json:"logURL,omitempty"`
RequiredContexts []string `json:"requiredContexts"`
AutoMerge *bool `json:"autoMerge,omitempty"`
Reference string `json:"reference,omitempty"`
}

type GitHubPullRequestComment struct {
Expand Down Expand Up @@ -101,7 +102,7 @@ func (g *GitHubNotification) GetTemplater(name string, f texttemplate.FuncMap) (
}
}

var deploymentState, environment, environmentURL, logURL *texttemplate.Template
var deploymentState, environment, environmentURL, reference, logURL *texttemplate.Template
if g.Deployment != nil {
deploymentState, err = texttemplate.New(name).Funcs(f).Parse(g.Deployment.State)
if err != nil {
Expand All @@ -118,6 +119,11 @@ func (g *GitHubNotification) GetTemplater(name string, f texttemplate.FuncMap) (
return nil, err
}

reference, err = texttemplate.New(name).Funcs(f).Parse(g.Deployment.Reference)
if err != nil {
return nil, err
}

Check warning on line 125 in pkg/services/github.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/github.go#L124-L125

Added lines #L124 - L125 were not covered by tests

logURL, err = texttemplate.New(name).Funcs(f).Parse(g.Deployment.LogURL)
if err != nil {
return nil, err
Expand Down Expand Up @@ -211,6 +217,13 @@ func (g *GitHubNotification) GetTemplater(name string, f texttemplate.FuncMap) (
} else {
notification.GitHub.Deployment.AutoMerge = g.Deployment.AutoMerge
}

var referenceData bytes.Buffer
if err := reference.Execute(&referenceData, vars); err != nil {
return err
}

Check warning on line 224 in pkg/services/github.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/github.go#L223-L224

Added lines #L223 - L224 were not covered by tests
notification.GitHub.Deployment.Reference = referenceData.String()

notification.GitHub.Deployment.RequiredContexts = g.Deployment.RequiredContexts
}

Expand Down Expand Up @@ -327,12 +340,19 @@ func (g gitHubService) Send(notification Notification, _ Destination) error {
u := strings.Split(fullNameByRepoURL(notification.GitHub.repoURL), "/")
// maximum is 140 characters
description := trunc(notification.Message, 140)

// if no reference is provided, use the revision
ref := notification.GitHub.Deployment.Reference
if ref == "" {
ref = notification.GitHub.revision
}

Check warning on line 348 in pkg/services/github.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/github.go#L343-L348

Added lines #L343 - L348 were not covered by tests

deployment, _, err := g.client.Repositories.CreateDeployment(
context.Background(),
u[0],
u[1],
&github.DeploymentRequest{
Ref: &notification.GitHub.revision,
Ref: &ref,

Check warning on line 355 in pkg/services/github.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/github.go#L355

Added line #L355 was not covered by tests
Environment: &notification.GitHub.Deployment.Environment,
RequiredContexts: &notification.GitHub.Deployment.RequiredContexts,
AutoMerge: notification.GitHub.Deployment.AutoMerge,
Expand Down
2 changes: 2 additions & 0 deletions pkg/services/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func TestGetTemplater_GitHub_Deployment(t *testing.T) {
RepoURLPath: "{{.sync.spec.git.repo}}",
RevisionPath: "{{.sync.status.lastSyncedCommit}}",
Deployment: &GitHubDeployment{
Reference: "v0.0.1",
State: "success",
Environment: "production",
EnvironmentURL: "https://argoproj.github.io",
Expand Down Expand Up @@ -159,6 +160,7 @@ func TestGetTemplater_GitHub_Deployment(t *testing.T) {
assert.Equal(t, "https://argoproj.github.io/log", notification.GitHub.Deployment.LogURL)
assert.Len(t, notification.GitHub.Deployment.RequiredContexts, 0)
assert.Equal(t, &f, notification.GitHub.Deployment.AutoMerge)
assert.Equal(t, "v0.0.1", notification.GitHub.Deployment.Reference)
}

func TestNewGitHubService_GitHubOptions(t *testing.T) {
Expand Down

0 comments on commit c300de1

Please sign in to comment.