git_utils.go 921 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package commonutils
  2. import (
  3. "context"
  4. "errors"
  5. "net/http"
  6. "github.com/google/go-github/v41/github"
  7. )
  8. var ErrNoWorkflowRuns = errors.New("no previous workflow runs found")
  9. var ErrWorkflowNotFound = errors.New("no workflow found, file missing")
  10. func GetLatestWorkflowRun(client *github.Client, owner, repo, filename, branch string) (*github.WorkflowRun, error) {
  11. workflowRuns, ghResponse, err := client.Actions.ListWorkflowRunsByFileName(
  12. context.Background(), owner, repo, filename, &github.ListWorkflowRunsOptions{
  13. Branch: branch,
  14. ListOptions: github.ListOptions{
  15. Page: 1,
  16. PerPage: 1,
  17. },
  18. },
  19. )
  20. if ghResponse != nil && ghResponse.StatusCode == http.StatusNotFound {
  21. return nil, ErrWorkflowNotFound
  22. }
  23. if err != nil {
  24. return nil, err
  25. }
  26. if workflowRuns == nil || workflowRuns.GetTotalCount() == 0 {
  27. return nil, ErrNoWorkflowRuns
  28. }
  29. return workflowRuns.WorkflowRuns[0], nil
  30. }