-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/google/go-github/v39/github" | ||
"regexp" | ||
) | ||
|
||
type Commit struct { | ||
Repository Repository | ||
SHA string | ||
} | ||
|
||
var patternCommitURL = regexp.MustCompile(`^https://api\.github\.com/repos/(.+?)/(.+?)/commits/([0-9a-f]+)$`) | ||
|
||
func ParseCommitURL(s string) *Commit { | ||
m := patternCommitURL.FindStringSubmatch(s) | ||
if len(m) != 4 { | ||
return nil | ||
} | ||
return &Commit{ | ||
Repository: Repository{Owner: m[1], Name: m[2]}, | ||
SHA: m[3], | ||
} | ||
} | ||
|
||
type CheckRun struct { | ||
Name string | ||
Status string | ||
Conclusion string | ||
Title string | ||
Summary string | ||
} | ||
|
||
func (c *client) CreateCheckRun(ctx context.Context, commit Commit, cr CheckRun) error { | ||
o := github.CreateCheckRunOptions{ | ||
HeadSHA: commit.SHA, | ||
Name: cr.Name, | ||
Status: github.String(cr.Status), | ||
Output: &github.CheckRunOutput{ | ||
Title: github.String(cr.Title), | ||
Summary: github.String(cr.Summary), | ||
}, | ||
} | ||
if cr.Conclusion != "" { | ||
o.Conclusion = github.String(cr.Conclusion) | ||
} | ||
_, _, err := c.rest.Checks.CreateCheckRun(ctx, commit.Repository.Owner, commit.Repository.Name, o) | ||
if err != nil { | ||
return fmt.Errorf("GitHub API error: %w", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package notification | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/argoproj/gitops-engine/pkg/health" | ||
synccommon "github.com/argoproj/gitops-engine/pkg/sync/common" | ||
"github.com/go-logr/logr" | ||
"github.com/int128/argocd-commenter/pkg/github" | ||
"strings" | ||
) | ||
|
||
func (c client) CheckRun(ctx context.Context, e Event) error { | ||
logger := logr.FromContextOrDiscard(ctx) | ||
|
||
commitURL := e.Application.Annotations["argocd-commenter.int128.github.io/commit-url"] | ||
commit := github.ParseCommitURL(commitURL) | ||
if commit == nil { | ||
return nil | ||
} | ||
|
||
cr := generateCheckRun(e) | ||
if cr == nil { | ||
logger.Info("nothing to update the check run", "event", e) | ||
return nil | ||
} | ||
|
||
logger.Info("updating the check run", "checkRun", commitURL) | ||
if err := c.ghc.CreateCheckRun(ctx, *commit, *cr); err != nil { | ||
return fmt.Errorf("unable to create a check run: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func generateCheckRun(e Event) *github.CheckRun { | ||
applicationURL := fmt.Sprintf("%s/applications/%s", e.ArgoCDURL, e.Application.Name) | ||
externalURLs := strings.Join(e.Application.Status.Summary.ExternalURLs, "\n") | ||
|
||
var checkRun github.CheckRun | ||
checkRun.Name = e.Application.Name | ||
checkRun.Summary = fmt.Sprintf(` | ||
## Argo CD | ||
%s | ||
## External URL | ||
%s | ||
`, applicationURL, externalURLs) | ||
|
||
if e.PhaseIsChanged { | ||
if e.Application.Status.OperationState == nil { | ||
return nil | ||
} | ||
switch e.Application.Status.OperationState.Phase { | ||
case synccommon.OperationRunning: | ||
checkRun.Status = "in_progress" | ||
checkRun.Title = fmt.Sprintf("Syncing: %s", e.Application.Status.OperationState.Message) | ||
return &checkRun | ||
case synccommon.OperationSucceeded: | ||
checkRun.Status = "completed" | ||
checkRun.Conclusion = "success" | ||
checkRun.Title = fmt.Sprintf("Synced: %s", e.Application.Status.OperationState.Message) | ||
return &checkRun | ||
case synccommon.OperationFailed: | ||
checkRun.Status = "completed" | ||
checkRun.Conclusion = "failure" | ||
checkRun.Title = fmt.Sprintf("Sync Failed: %s", e.Application.Status.OperationState.Message) | ||
return &checkRun | ||
case synccommon.OperationError: | ||
checkRun.Status = "completed" | ||
checkRun.Conclusion = "failure" | ||
checkRun.Title = fmt.Sprintf("Sync Error: %s", e.Application.Status.OperationState.Message) | ||
return &checkRun | ||
} | ||
} | ||
|
||
if e.HealthIsChanged { | ||
switch e.Application.Status.Health.Status { | ||
case health.HealthStatusProgressing: | ||
checkRun.Status = "in_progress" | ||
checkRun.Title = fmt.Sprintf("Progressing: %s", e.Application.Status.Health.Message) | ||
return &checkRun | ||
case health.HealthStatusHealthy: | ||
checkRun.Status = "completed" | ||
checkRun.Conclusion = "success" | ||
checkRun.Title = fmt.Sprintf("Healthy: %s", e.Application.Status.Health.Message) | ||
return &checkRun | ||
case health.HealthStatusDegraded: | ||
checkRun.Status = "completed" | ||
checkRun.Conclusion = "failure" | ||
checkRun.Title = fmt.Sprintf("Degraded: %s", e.Application.Status.Health.Message) | ||
return &checkRun | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters