| 102 | } |
| 103 | |
| 104 | func checkRun(opts *CheckOptions) error { |
| 105 | httpClient, err := opts.HttpClient() |
| 106 | if err != nil { |
| 107 | return err |
| 108 | } |
| 109 | client := api.NewClientFromHTTP(httpClient) |
| 110 | |
| 111 | repoI, err := opts.BaseRepo() |
| 112 | if err != nil { |
| 113 | return fmt.Errorf("could not determine repo to use: %w", err) |
| 114 | } |
| 115 | |
| 116 | git := opts.Git |
| 117 | |
| 118 | if opts.Default { |
| 119 | repo, err := api.GitHubRepo(client, repoI) |
| 120 | if err != nil { |
| 121 | return fmt.Errorf("could not get repository information: %w", err) |
| 122 | } |
| 123 | opts.Branch = repo.DefaultBranchRef.Name |
| 124 | } |
| 125 | |
| 126 | if opts.Branch == "" { |
| 127 | opts.Branch, err = git.CurrentBranch(context.Background()) |
| 128 | if err != nil { |
| 129 | return fmt.Errorf("could not determine current branch: %w", err) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if opts.WebMode { |
| 134 | // the query string parameter may have % signs in it, so it must be carefully used with Printf functions |
| 135 | queryString := fmt.Sprintf("?ref=%s", url.QueryEscape("refs/heads/"+opts.Branch)) |
| 136 | rawUrl := ghrepo.GenerateRepoURL(repoI, "rules") |
| 137 | |
| 138 | if opts.IO.IsStdoutTTY() { |
| 139 | fmt.Fprintf(opts.IO.Out, "Opening %s in your browser.\n", text.DisplayURL(rawUrl)) |
| 140 | } |
| 141 | |
| 142 | return opts.Browser.Browse(rawUrl + queryString) |
| 143 | } |
| 144 | |
| 145 | var rules []shared.RulesetRule |
| 146 | |
| 147 | endpoint := fmt.Sprintf("repos/%s/%s/rules/branches/%s", repoI.RepoOwner(), repoI.RepoName(), url.PathEscape(opts.Branch)) |
| 148 | |
| 149 | if err = client.REST(repoI.RepoHost(), "GET", endpoint, nil, &rules); err != nil { |
| 150 | return fmt.Errorf("GET %s failed: %w", endpoint, err) |
| 151 | } |
| 152 | |
| 153 | w := opts.IO.Out |
| 154 | |
| 155 | fmt.Fprintf(w, "%d rules apply to branch %s in repo %s/%s\n", len(rules), opts.Branch, repoI.RepoOwner(), repoI.RepoName()) |
| 156 | |
| 157 | if len(rules) > 0 { |
| 158 | fmt.Fprint(w, "\n") |
| 159 | fmt.Fprint(w, shared.ParseRulesForDisplay(rules)) |
| 160 | } |
| 161 | |