Skip to content

Instantly share code, notes, and snippets.

@robherley
Created June 7, 2022 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robherley/f995df4d5a2998226757e9b49d691f31 to your computer and use it in GitHub Desktop.
Save robherley/f995df4d5a2998226757e9b49d691f31 to your computer and use it in GitHub Desktop.
GraphQL Status client for microplane
const queryStatusCheckRollupState = `query($owner: String!, $name: String!, $oid: GitObjectID) {
repository(owner: $owner, name: $name) {
object(oid: $oid) {
... on Commit {
statusCheckRollup {
state
}
}
}
}
}`
func getStatusCheckRollupState(ctx context.Context, client *github.Client, input Input) (string, error) {
gqlPath := client.BaseURL.String() + "graphql"
gqlReq := map[string]interface{}{
"query": queryStatusCheckRollupState,
"variables": map[string]string{
"owner": input.Repo.Owner,
"name": input.Repo.Name,
"oid": input.CommitSHA,
},
}
jsonReq, err := json.Marshal(gqlReq)
if err != nil {
return "", err
}
req, err := http.NewRequest(http.MethodPost, gqlPath, bytes.NewReader(jsonReq))
if err != nil {
return "", err
}
var result struct {
Data struct {
Repository struct {
Object struct {
StatusCheckRollup struct {
State string `json:"state"`
} `json:"statusCheckRollup"`
} `json:"object"`
} `json:"repository"`
} `json:"data"`
Errors []struct {
Message string `json:"message"`
} `json:"errors"`
}
resp, err := client.Do(ctx, req, &result)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return "", fmt.Errorf("unexpected response code from graphql: %v", resp.StatusCode)
}
if numErrors := len(result.Errors); numErrors > 0 {
msgs := make([]string, numErrors)
for i := range result.Errors {
msgs[i] = result.Errors[i].Message
}
return "", fmt.Errorf("unexpected errors from graphql: %+q", msgs)
}
return result.Data.Repository.Object.StatusCheckRollup.State, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment