share prForRun
This commit is contained in:
parent
e7fa99b70f
commit
6f898dcbb3
2 changed files with 69 additions and 70 deletions
|
|
@ -270,3 +270,71 @@ func Symbol(cs *iostreams.ColorScheme, status Status, conclusion Conclusion) (st
|
||||||
|
|
||||||
return "-", cs.Yellow
|
return "-", cs.Yellow
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func PullRequestForRun(client *api.Client, repo ghrepo.Interface, run Run) (int, error) {
|
||||||
|
type response struct {
|
||||||
|
Repository struct {
|
||||||
|
PullRequests struct {
|
||||||
|
Nodes []struct {
|
||||||
|
Number int
|
||||||
|
HeadRepository struct {
|
||||||
|
Owner struct {
|
||||||
|
Login string
|
||||||
|
}
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Number int
|
||||||
|
}
|
||||||
|
|
||||||
|
variables := map[string]interface{}{
|
||||||
|
"owner": repo.RepoOwner(),
|
||||||
|
"repo": repo.RepoName(),
|
||||||
|
"headRefName": run.HeadBranch,
|
||||||
|
}
|
||||||
|
|
||||||
|
query := `
|
||||||
|
query PullRequestForRun($owner: String!, $repo: String!, $headRefName: String!) {
|
||||||
|
repository(owner: $owner, name: $repo) {
|
||||||
|
pullRequests(headRefName: $headRefName, first: 1, orderBy: { field: CREATED_AT, direction: DESC }) {
|
||||||
|
nodes {
|
||||||
|
number
|
||||||
|
headRepository {
|
||||||
|
owner {
|
||||||
|
login
|
||||||
|
}
|
||||||
|
name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}`
|
||||||
|
|
||||||
|
var resp response
|
||||||
|
|
||||||
|
err := client.GraphQL(repo.RepoHost(), query, variables, &resp)
|
||||||
|
if err != nil {
|
||||||
|
return -1, err
|
||||||
|
}
|
||||||
|
|
||||||
|
prs := resp.Repository.PullRequests.Nodes
|
||||||
|
if len(prs) == 0 {
|
||||||
|
return -1, fmt.Errorf("no matching PR found for %s", run.HeadBranch)
|
||||||
|
}
|
||||||
|
|
||||||
|
number := -1
|
||||||
|
|
||||||
|
for _, pr := range prs {
|
||||||
|
if pr.HeadRepository.Owner.Login == run.HeadRepository.Owner.Login && pr.HeadRepository.Name == run.HeadRepository.Name {
|
||||||
|
number = pr.Number
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if number == -1 {
|
||||||
|
return number, fmt.Errorf("no matching PR found for %s", run.HeadBranch)
|
||||||
|
}
|
||||||
|
|
||||||
|
return number, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -107,7 +107,7 @@ func runView(opts *ViewOptions) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
prNumber := ""
|
prNumber := ""
|
||||||
number, err := prForRun(client, repo, *run)
|
number, err := shared.PullRequestForRun(client, repo, *run)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
prNumber = fmt.Sprintf(" #%d", number)
|
prNumber = fmt.Sprintf(" #%d", number)
|
||||||
}
|
}
|
||||||
|
|
@ -165,7 +165,6 @@ func runView(opts *ViewOptions) error {
|
||||||
fmt.Fprintln(out)
|
fmt.Fprintln(out)
|
||||||
fmt.Fprintln(out, cs.Bold("ANNOTATIONS"))
|
fmt.Fprintln(out, cs.Bold("ANNOTATIONS"))
|
||||||
fmt.Fprintln(out, shared.RenderAnnotations(cs, annotations))
|
fmt.Fprintln(out, shared.RenderAnnotations(cs, annotations))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintln(out)
|
fmt.Fprintln(out)
|
||||||
|
|
@ -178,71 +177,3 @@ func runView(opts *ViewOptions) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func prForRun(client *api.Client, repo ghrepo.Interface, run shared.Run) (int, error) {
|
|
||||||
type response struct {
|
|
||||||
Repository struct {
|
|
||||||
PullRequests struct {
|
|
||||||
Nodes []struct {
|
|
||||||
Number int
|
|
||||||
HeadRepository struct {
|
|
||||||
Owner struct {
|
|
||||||
Login string
|
|
||||||
}
|
|
||||||
Name string
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Number int
|
|
||||||
}
|
|
||||||
|
|
||||||
variables := map[string]interface{}{
|
|
||||||
"owner": repo.RepoOwner(),
|
|
||||||
"repo": repo.RepoName(),
|
|
||||||
"headRefName": run.HeadBranch,
|
|
||||||
}
|
|
||||||
|
|
||||||
query := `
|
|
||||||
query PullRequestForRun($owner: String!, $repo: String!, $headRefName: String!) {
|
|
||||||
repository(owner: $owner, name: $repo) {
|
|
||||||
pullRequests(headRefName: $headRefName, first: 1, orderBy: { field: CREATED_AT, direction: DESC }) {
|
|
||||||
nodes {
|
|
||||||
number
|
|
||||||
headRepository {
|
|
||||||
owner {
|
|
||||||
login
|
|
||||||
}
|
|
||||||
name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}`
|
|
||||||
|
|
||||||
var resp response
|
|
||||||
|
|
||||||
err := client.GraphQL(repo.RepoHost(), query, variables, &resp)
|
|
||||||
if err != nil {
|
|
||||||
return -1, err
|
|
||||||
}
|
|
||||||
|
|
||||||
prs := resp.Repository.PullRequests.Nodes
|
|
||||||
if len(prs) == 0 {
|
|
||||||
return -1, fmt.Errorf("no matching PR found for %s", run.HeadBranch)
|
|
||||||
}
|
|
||||||
|
|
||||||
number := -1
|
|
||||||
|
|
||||||
for _, pr := range prs {
|
|
||||||
if pr.HeadRepository.Owner.Login == run.HeadRepository.Owner.Login && pr.HeadRepository.Name == run.HeadRepository.Name {
|
|
||||||
number = pr.Number
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if number == -1 {
|
|
||||||
return number, fmt.Errorf("no matching PR found for %s", run.HeadBranch)
|
|
||||||
}
|
|
||||||
|
|
||||||
return number, nil
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue