forked from Clever/microplane
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fetch everything that makes a PR green, including the Status API and the Checks API. Fixes Clever#107
- Loading branch information
Ben Iofel
committed
Jun 7, 2022
1 parent
de9876a
commit edb5999
Showing
9 changed files
with
194 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package lib | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/hasura/go-graphql-client" | ||
) | ||
|
||
type GithubStatusContext struct { | ||
Context string | ||
TargetURL *string | ||
} | ||
|
||
type GithubPRStatus struct { | ||
// "error", "expected", "failure", "pending", or "success" | ||
State string | ||
|
||
// Results from the Status Checks. Does not include results from the Checks API (incl. Github Actions) | ||
Statuses []GithubStatusContext | ||
} | ||
|
||
func GetGithubPRStatus(ctx context.Context, repoLimiter *time.Ticker, repo Repo, prNumber int) (GithubPRStatus, error) { | ||
p := NewProviderFromConfig(repo.ProviderConfig) | ||
graphqlClient, err := p.GithubGraphqlClient(ctx) | ||
if err != nil { | ||
return GithubPRStatus{}, err | ||
} | ||
|
||
// Fetch the rolled up status checks from github | ||
// This includes both the Statuses API and Checks API | ||
var query struct { | ||
Repository struct { | ||
PullRequest struct { | ||
Commits struct { | ||
Nodes []struct { | ||
Commit struct { | ||
StatusCheckRollup struct { | ||
State string | ||
Contexts struct { | ||
Nodes []struct { | ||
__typename string | ||
StatusContext struct { | ||
Context string | ||
TargetURL string | ||
} `graphql:"... on StatusContext"` | ||
} | ||
} `graphql:"contexts(first: 10)"` | ||
} | ||
} | ||
} | ||
} `graphql:"commits(last: 1)"` | ||
} `graphql:"pullRequest(number: $number)"` | ||
} `graphql:"repository(owner: $owner, name: $name)"` | ||
} | ||
|
||
<-repoLimiter.C | ||
err = graphqlClient.Query(ctx, &query, map[string]interface{}{ | ||
"owner": graphql.String(repo.Owner), | ||
"name": graphql.String(repo.Name), | ||
"number": graphql.Int(prNumber), | ||
}) | ||
if err != nil { | ||
return GithubPRStatus{}, err | ||
} | ||
|
||
commits := query.Repository.PullRequest.Commits.Nodes | ||
if len(commits) != 1 { | ||
return GithubPRStatus{}, fmt.Errorf("unexpected number of commits in PR") | ||
} | ||
|
||
lastCommit := commits[0].Commit.StatusCheckRollup | ||
var statuses []GithubStatusContext | ||
|
||
for _, status := range lastCommit.Contexts.Nodes { | ||
if status.__typename == "StatusContext" { | ||
statuses = append(statuses, GithubStatusContext{ | ||
Context: status.StatusContext.Context, | ||
TargetURL: &status.StatusContext.TargetURL, | ||
}) | ||
} | ||
} | ||
|
||
return GithubPRStatus{ | ||
State: strings.ToLower(lastCommit.State), | ||
Statuses: statuses, | ||
}, 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
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