Module: GitHub::Workflows Private

Extended by:
T::Helpers
Included in:
GitHub
Defined in:
utils/github/workflows.rb

Overview

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

Locate workflow runs and downloadable build artifacts.

Instance Method Summary collapse

Instance Method Details

#get_artifact_urls(workflow_array) ⇒ Array<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'utils/github/workflows.rb', line 82

def get_artifact_urls(workflow_array)
  check_suite, user, repo, pr, workflow_id, scopes, artifact_pattern = *workflow_array
  if check_suite.empty?
    raise API::Error, <<~EOS
      No matching check suite found for these criteria!
        Pull request: #{pr}
        Workflow:     #{workflow_id}
    EOS
  end

  last_check = check_suite.fetch(-1)
  status = last_check["status"].sub("_", " ").downcase
  if status != "completed"
    raise API::Error, <<~EOS
      The newest workflow run for ##{pr} is still #{status}!
        #{Formatter.url last_check["workflowRun"]["url"]}
    EOS
  end

  run_id = last_check["workflowRun"]["databaseId"]
  artifacts = []
  per_page = 50
  API.paginate_rest("#{API_URL}/repos/#{user}/#{repo}/actions/runs/#{run_id}/artifacts",
                    per_page:, scopes:) do |result|
    result = result["artifacts"]
    artifacts.concat(result)
    break if result.length < per_page
  end

  matching_artifacts =
    artifacts
    .group_by { |art| art["name"] }
    .select { |name| File.fnmatch?(artifact_pattern, name, File::FNM_EXTGLOB) }
    .map { |_, arts| arts.max_by { |art| art["created_at"] } }

  if matching_artifacts.empty?
    raise API::Error, <<~EOS
      No artifacts with the pattern `#{artifact_pattern}` were found!
        #{Formatter.url last_check["workflowRun"]["url"]}
    EOS
  end

  matching_artifacts.map { |art| art["archive_download_url"] }
end

#get_workflow_run(user, repo, pull_request, workflow_id: "tests.yml", artifact_pattern: "bottles{,_*}", head_sha: nil) ⇒ WorkflowArray

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • user (String)
  • repo (String)
  • pull_request (String)
  • workflow_id (String) (defaults to: "tests.yml")
  • artifact_pattern (String) (defaults to: "bottles{,_*}")
  • head_sha (String, nil) (defaults to: nil)

Returns:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'utils/github/workflows.rb', line 21

def get_workflow_run(user, repo, pull_request, workflow_id: "tests.yml", artifact_pattern: "bottles{,_*}",
                     head_sha: nil)
  scopes = CREATE_ISSUE_FORK_OR_PR_SCOPES

  # GraphQL unfortunately has no way to get the workflow yml name, so we need an extra REST call.
  workflow_api_url = "#{API_URL}/repos/#{user}/#{repo}/actions/workflows/#{workflow_id}"
  workflow_payload = API.open_rest(workflow_api_url, scopes:)
  workflow_id_num = workflow_payload["id"]

  query = <<~EOS
    query ($user: String!, $repo: String!, $pr: Int!) {
      repository(owner: $user, name: $repo) {
        pullRequest(number: $pr) {
          commits(last: 1) {
            nodes {
              commit {
                oid
                checkSuites(first: 100) {
                  nodes {
                    status,
                    workflowRun {
                      databaseId,
                      url,
                      workflow {
                        databaseId
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  EOS
  variables = {
    user:,
    repo:,
    pr:   pull_request.to_i,
  }
  result = API.open_graphql(query, variables:, scopes:)

  commit_node = result["repository"]["pullRequest"]["commits"]["nodes"].first
  check_suite = if commit_node.present?
    commit = commit_node["commit"]
    if head_sha.present? && commit["oid"].downcase != head_sha.downcase
      raise API::Error, "Pull request ##{pull_request} is at #{commit["oid"]} but expected #{head_sha}."
    end

    commit["checkSuites"]["nodes"].select do |suite|
      suite.dig("workflowRun", "workflow", "databaseId") == workflow_id_num
    end
  else
    []
  end

  [check_suite, user, repo, pull_request, workflow_id, scopes, artifact_pattern]
end