Skip to content

Commit

Permalink
github: imported package from acme demo
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
  • Loading branch information
aluzzardi committed Apr 25, 2020
1 parent f22b43e commit 0f63430
Show file tree
Hide file tree
Showing 12 changed files with 494 additions and 29 deletions.
84 changes: 84 additions & 0 deletions pkg/github/comment.cue
@@ -0,0 +1,84 @@
package github

import (
"b.l/bl"
)

Comment :: {
id: string
body: string
}

CommentFragment :: """
fragment CommentParts on IssueComment {
id
body
}
"""

AddComment :: {
subjectId: string
body: string
token: bl.Secret

comment: Comment & {
id: do.data.addComment.commentEdge.node.id
body: do.data.addComment.commentEdge.node.body
}

do: Query & {
query: """
mutation ($input: AddCommentInput!) {
addComment(input: $input) {
subject {
id
}
commentEdge {
node {
...CommentParts
}
}
}
}
\(CommentFragment)
"""

variable: input: {
"subjectId": subjectId
"body": body
}

"token": token
}
}

UpdateComment :: {
commentId: string
body: string
token: bl.Secret

comment: Comment & {
id: do.data.updateIssueComment.issueComment.id
body: do.data.updateIssueComment.issueComment.body
}

do: Query & {
query: """
mutation ($input: UpdateIssueCommentInput!) {
updateIssueComment(input: $input) {
issueComment {
...CommentParts
}
}
}
\(CommentFragment)
"""

variable: input: {
id: commentId
"body": body
}

"token": token
}
}
27 changes: 0 additions & 27 deletions pkg/github/github.cue

This file was deleted.

110 changes: 110 additions & 0 deletions pkg/github/pull_request.cue
@@ -0,0 +1,110 @@
package github

import (
"b.l/bl"
)

PullRequest :: {
id: string
state: string
number: int
title: string
headRepository: {
sshUrl: string
url: string
}
headRef: {
name: string
prefix: string
target: oid: string
}
}

// INTERNAL: GraphQL fragment shared across queries related to PullRequest
PullRequestParts :: """
fragment PullRequestParts on PullRequest {
id
state
number
title
headRepository {
sshUrl
url
}
headRef {
name
prefix
target {
oid
}
}
}
"""

GetPullRequest :: {
number: int

repo: {
owner: string
name: string
}

Query & {
query:
"""
query($owner: String!, $name: String!, $number: Int!) {
repository(owner: $owner, name: $name) {
pullRequest(number: $number) {
...PullRequestParts
}
}
}
\(PullRequestParts)
"""
variable: {
owner: repo.owner
name: repo.name
"number": number
}
}

data: _
pullRequest: PullRequest
pullRequest: data.repository.pullRequest
}

ListPullRequests :: {
repo: {
owner: string
name: string
}
pageSize: int | *25
token: bl.Secret
states: [string] | *[]

Query & {
query:
"""
query($owner: String!, $name: String!, $last: Int, $states: [PullRequestState!]) {
repository(owner: $owner, name: $name) {
pullRequests(last: $last, states: $states) {
nodes {
...PullRequestParts
}
}
}
}
\(PullRequestParts)
"""
variable: {
owner: repo.owner
name: repo.name
last: pageSize
"states": states
}
}

data: _
pullRequests: [...PullRequest]
pullRequests: data.repository.pullRequests.nodes
}
79 changes: 79 additions & 0 deletions pkg/github/pull_request_test.cue
@@ -0,0 +1,79 @@
package github

import (
"b.l/bl"
)

TestConfig: githubToken: bl.Secret

TestGetPullRequestMerged: {
query: GetPullRequest & {
token: TestConfig.githubToken
number: 1
repo: {
owner: "stackbrew-test"
name: "gh-test"
}
}

test: bl.BashScript & {
runPolicy: "always"
environment: state: query.pullRequest.state
code: """
test "$state" = "MERGED"
"""
}
}

TestGetPullRequestOpen: {
query: GetPullRequest & {
token: TestConfig.githubToken
number: 2
repo: {
owner: "stackbrew-test"
name: "gh-test"
}
}

test: bl.BashScript & {
runPolicy: "always"
environment: {
state: query.pullRequest.state
gitUrl: query.pullRequest.headRepository.url
commit: query.pullRequest.headRef.target.oid
}
code: """
test "$state" = "OPEN"
test "$gitUrl" = "https://github.com/stackbrew-test/gh-test"
test "$commit" = "16a9a0fffda42bfbab40cfba79ecef61c7343e65"
"""
}
}

TestListPullRequests: {
query: ListPullRequests & {
token: TestConfig.githubToken
pageSize: 10
states: ["MERGED"]
repo: {
owner: "stackbrew-test"
name: "gh-test"
}
}

test: bl.BashScript & {
runPolicy: "always"
environment: {
FIRST_PR_STATE: "\(query.pullRequests[0].state)"
for pr in query.pullRequests {
"PR_\(pr.number)_STATE": pr.state
}
}
code: """
env
test "$FIRST_PR_STATE" = "MERGED"
test "$PR_1_STATE" = "MERGED"
test -z "$PR_2_STATE"
"""
}
}
13 changes: 13 additions & 0 deletions pkg/github/query.cue
@@ -0,0 +1,13 @@
package github

// A client for the Github API v4, also known as the "Github GraphQL API"
// Reference: https://developer.github.com/v4/

import (
"stackbrew.io/graphql"
)

// GitHub v4 GraphQL Query
Query :: graphql.Query & {
url: "https://api.github.com/graphql"
}
39 changes: 39 additions & 0 deletions pkg/github/repository.cue
@@ -0,0 +1,39 @@
package github

import (
"b.l/bl"
"stackbrew.io/git"
)

Repository :: {
// Github repository name
name: string

// Github repository owner
owner: string

// Github API token
token: bl.Secret

PullRequest :: {
number: int

checkout: gitCloneAndCheckout.out

info: GetPullRequest & {
"number": number
"token": token
repo: {
"owner": owner
"name": name
}
}

gitCloneAndCheckout: git.Repository & {
url: info.pullRequest.headRepository.url
ref: info.pullRequest.headRef.target.oid
username: "apikey"
httpPassword: token
}
}
}

0 comments on commit 0f63430

Please sign in to comment.