Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

feat: add GitHub component #177

Merged
merged 38 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
1c0844f
feat: add github component to get all prs
YCK1130 Jun 24, 2024
a99617c
feat: add get single pr func
YCK1130 Jun 25, 2024
5fa41f6
feat: add get all review comments
YCK1130 Jun 27, 2024
076629d
fix: get pull request not found bug
YCK1130 Jun 28, 2024
b857047
feat: add create review comment in github component
YCK1130 Jun 28, 2024
f82bbd3
chore: adjust github component UI format
YCK1130 Jun 28, 2024
baf2f90
feat: add get commit and include commit info in get pr
YCK1130 Jun 28, 2024
b69185a
chore: modify readme
YCK1130 Jun 28, 2024
4c9a24f
chore: add mock client for testing
YCK1130 Jun 28, 2024
e052560
chore: add testing for 5 function
YCK1130 Jul 1, 2024
56f37fe
chore: remove empty test function
YCK1130 Jul 1, 2024
946e897
feat: add issue related funcs, and dummy testing func
YCK1130 Jul 1, 2024
f916c19
feat: add issue related testing, add pr filter option
YCK1130 Jul 1, 2024
0935131
feat: add create webhook
YCK1130 Jul 1, 2024
5816908
chore: update readme
YCK1130 Jul 1, 2024
95a57ca
chore: update readme
YCK1130 Jul 2, 2024
4832928
chore: update readme
YCK1130 Jul 2, 2024
6f1c7c9
chore: maintain consistency in readme
YCK1130 Jul 2, 2024
6d13702
feat: add default fill in function
YCK1130 Jul 2, 2024
ceca96c
chore: add error handling for review related task
YCK1130 Jul 2, 2024
dba9763
fix: fix testing case
YCK1130 Jul 2, 2024
18f8f0e
fix: move component to application
YCK1130 Jul 2, 2024
1f56891
fix: fix api consistency of get a pr
YCK1130 Jul 3, 2024
6674210
fix: format files
YCK1130 Jul 4, 2024
4a061bd
chore: update readme
YCK1130 Jul 4, 2024
a80445a
fix: make repo info to component struct
YCK1130 Jul 4, 2024
a0f6132
fix: change task name
YCK1130 Jul 8, 2024
bb9b70c
fix: refactor client struct
YCK1130 Jul 8, 2024
b34007c
fix: pass context from Execute
YCK1130 Jul 8, 2024
12306c8
fix: update task name
YCK1130 Jul 8, 2024
fc8da71
fix: add test case and fix repo info bug
YCK1130 Jul 8, 2024
31b9e38
chore: clarify description
YCK1130 Jul 8, 2024
08a2605
chore: remove confusing output in list pr
YCK1130 Jul 8, 2024
43cff96
fix: patch instillFormat field
YCK1130 Jul 9, 2024
a880e8e
chore: flatten the output of single output tasks
YCK1130 Jul 9, 2024
1525b9d
fix: change all params to kebab-case
YCK1130 Jul 11, 2024
ab33a01
fix: update description
YCK1130 Jul 11, 2024
6d9bdca
fix: format go files
YCK1130 Jul 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
266 changes: 266 additions & 0 deletions application/github/v0/README.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
---
title: "GitHub"
lang: "en-US"
draft: false
description: "Learn about how to set up a VDP GitHub component https://github.com/instill-ai/instill-core"
---

The GitHub component is an application component that allows users to do anything available on GitHub.
It can carry out the following tasks:

- [List Pull Requests](#list-pull-requests)
- [Get Pull Request](#get-pull-request)
- [Get Commit](#get-commit)
- [Get Review Comments](#get-review-comments)
- [Create Review Comment](#create-review-comment)
- [List Issues](#list-issues)
- [Get Issue](#get-issue)
- [Create Issue](#create-issue)
- [Create Webhook](#create-webhook)



## Release Stage

`Alpha`



## Configuration

The component configuration is defined and maintained [here](https://github.com/instill-ai/component/blob/main/application/github/v0/config/definition.json).




## Setup


| Field | Field ID | Type | Note |
| :--- | :--- | :--- | :--- |
| Token | `token` | string | Fill in your GitHub access token for advanced usages. For more information about how to create tokens, please refer to the https://github.com/settings/tokens. |




## Supported Tasks

### List Pull Requests

Get the list of all pull requests in a repository


| Input | ID | Type | Description |
| :--- | :--- | :--- | :--- |
| Task ID (required) | `task` | string | `TASK_LIST_PULL_REQUESTS` |
| Owner (required) | `owner` | string | Owner of the repository |
| Repository (required) | `repository` | string | Repository name |
| State | `state` | string | State of the PRs, including open, closed, all. Default is open |
| Sort | `sort` | string | Sort the PRs by created, updated, popularity, or long-running. Default is created |
| Direction | `direction` | string | Direction of the sort, including asc or desc. Default is desc |



| Output | ID | Type | Description |
| :--- | :--- | :--- | :--- |
| Pull Requests | `pull_requests` | array[object] | An array of PRs |






### Get Pull Request

Get a pull request from a repository, given the PR number. This will default to the latest PR if no PR number is provided


| Input | ID | Type | Description |
| :--- | :--- | :--- | :--- |
| Task ID (required) | `task` | string | `TASK_GET_PULL_REQUEST` |
| Owner (required) | `owner` | string | Owner of the repository |
| Repository (required) | `repository` | string | Repository name |
| PR Number | `pr_number` | integer | Number of the PR. `0` for the latest PR |



| Output | ID | Type | Description |
| :--- | :--- | :--- | :--- |
| Pull Request | `pull_request` | object | A pull request in GitHub |






### Get Commit

Get a commit from a repository, given the commit SHA


| Input | ID | Type | Description |
| :--- | :--- | :--- | :--- |
| Task ID (required) | `task` | string | `TASK_GET_COMMIT` |
| Owner (required) | `owner` | string | Owner of the repository |
| Repository (required) | `repository` | string | Repository name |
| Commit SHA (required) | `sha` | string | SHA of the commit |



| Output | ID | Type | Description |
| :--- | :--- | :--- | :--- |
| Commit | `commit` | object | A commit in GitHub |






### Get Review Comments

Get the review comments in a pull request


| Input | ID | Type | Description |
| :--- | :--- | :--- | :--- |
| Task ID (required) | `task` | string | `TASK_GET_REVIEW_COMMENTS` |
| Owner (required) | `owner` | string | Owner of the repository |
| Repository (required) | `repository` | string | Repository name |
| PR Number | `pr_number` | integer | Number of the PR. Default `0` is the latest PR |
| Sort | `sort` | string | Sort the comments by created, updated. Default is created |
| Direction | `direction` | string | Direction of the sort, including asc or desc. Default is desc |
| Since | `since` | string | Only comments updated at or after this time are returned. Default is 2021-01-01T00:00:00Z |



| Output | ID | Type | Description |
| :--- | :--- | :--- | :--- |
| Comments | `comments` | array[object] | An array of comments |






### Create Review Comment

Create a review comment in pull request.


| Input | ID | Type | Description |
| :--- | :--- | :--- | :--- |
| Task ID (required) | `task` | string | `TASK_CREATE_REVIEW_COMMENT` |
| Owner (required) | `owner` | string | Owner of the repository |
| Repository (required) | `repository` | string | Repository name |
| PR Number (required) | `pr_number` | integer | Number of the PR |
| Comment (required) | `comment` | object | The comment to be added |



| Output | ID | Type | Description |
| :--- | :--- | :--- | :--- |
| Review Comment | `comment` | object | The created comment |






### List Issues

Get the list of all issues in a repository


| Input | ID | Type | Description |
| :--- | :--- | :--- | :--- |
| Task ID (required) | `task` | string | `TASK_LIST_ISSUES` |
| Owner (required) | `owner` | string | Owner of the repository |
| Repository (required) | `repository` | string | Repository name |
| State | `state` | string | State of the issues, can be one of: open, closed, all. Default is open |
| Sort | `sort` | string | Sort the issues by created, updated, popularity, or long-running. Default is created |
| Direction | `direction` | string | Direction of the sort, can be one of: asc, desc. Default is desc |
| Since | `since` | string | Only issues updated at or after this time are returned. Default is 2021-01-01T00:00:00Z |
| No Pull Request | `no_pull_request` | boolean | Whether to not include pull requests in the issues. Default is false |



| Output | ID | Type | Description |
| :--- | :--- | :--- | :--- |
| Issues | `issues` | array[object] | An array of issues |






### Get Issue

Get an issue.


| Input | ID | Type | Description |
| :--- | :--- | :--- | :--- |
| Task ID (required) | `task` | string | `TASK_GET_ISSUE` |
| Owner (required) | `owner` | string | Owner of the repository |
| Repository (required) | `repository` | string | Repository name |
| Issue Number (required) | `issue_number` | integer | Number of the issue |



| Output | ID | Type | Description |
| :--- | :--- | :--- | :--- |
| Issue | `issue` | object | An issue in GitHub |






### Create Issue


| Input | ID | Type | Description |
| :--- | :--- | :--- | :--- |
| Task ID (required) | `task` | string | `TASK_CREATE_ISSUE` |
| Owner (required) | `owner` | string | Owner of the repository |
| Repository (required) | `repository` | string | Repository name |
| Issue title (required) | `title` | string | Title of the issue |
| Issue body (required) | `body` | string | Body of the issue |



| Output | ID | Type | Description |
| :--- | :--- | :--- | :--- |
| Issue | `issue` | object | The created issue |






### Create Webhook


| Input | ID | Type | Description |
| :--- | :--- | :--- | :--- |
| Task ID (required) | `task` | string | `TASK_CREATE_WEBHOOK` |
| Owner (required) | `owner` | string | Owner of the repository |
| Repository (required) | `repository` | string | Repository name |
| Webhook URL (required) | `hook_url` | string | URL to send the payload to |
| Events (required) | `events` | array[string] | Events to trigger the webhook. Please see https://docs.github.com/en/webhooks/webhook-events-and-payloads for more information |
| Active | `active` | boolean | Whether the webhook is active. Default is false |
| Content Type | `content_type` | string | Content type of the webhook, can be one of: json, form. Default is json |
| Hook Secret | `hook_secret` | string | If provided, the secret will be used as the key to generate the HMAC hex digest value for delivery signature headers. (see https://docs.github.com/en/webhooks/webhook-events-and-payloads#delivery-headers) |



| Output | ID | Type | Description |
| :--- | :--- | :--- | :--- |
| Webhook | `hook` | object | The created webhook |







3 changes: 3 additions & 0 deletions application/github/v0/assets/Github.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
90 changes: 90 additions & 0 deletions application/github/v0/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package github

import (
"context"
"fmt"
"net/http"

"github.com/google/go-github/v62/github"
"github.com/instill-ai/x/errmsg"
"golang.org/x/oauth2"
"google.golang.org/protobuf/types/known/structpb"
)

type RepoInfoInterface interface {
getOwner() (string, error)
getRepository() (string, error)
}

type RepoInfo struct {
Owner string `json:"owner"`
Repository string `json:"repository"`
}

func (info RepoInfo) getOwner() (string, error) {
if info.Owner == "" {
return "", errmsg.AddMessage(
fmt.Errorf("owner not provided"),
"owner not provided",
)
}
return info.Owner, nil
}
func (info RepoInfo) getRepository() (string, error) {
if info.Repository == "" {
return "", errmsg.AddMessage(
fmt.Errorf("repository not provided"),
"repository not provided",
)
}
return info.Repository, nil
}

type Client struct {
YCK1130 marked this conversation as resolved.
Show resolved Hide resolved
*github.Client
Repositories RepositoriesService
PullRequests PullRequestService
Issues IssuesService
}

func newClient(ctx context.Context, setup *structpb.Struct) Client {
token := getToken(setup)

var oauth2Client *http.Client
if token != "" {
tokenSource := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
oauth2Client = oauth2.NewClient(ctx, tokenSource)
}
client := github.NewClient(oauth2Client)
githubClient := Client{
Client: client,
Repositories: client.Repositories,
PullRequests: client.PullRequests,
Issues: client.Issues,
}
return githubClient
}

func parseTargetRepo(info RepoInfoInterface) (string, string, error) {
owner, ownerErr := info.getOwner()
repository, RepoErr := info.getRepository()
if ownerErr != nil && RepoErr != nil {
return "", "", errmsg.AddMessage(
fmt.Errorf("owner and repository not provided"),
"owner and repository not provided",
)
}
if ownerErr != nil {
return "", "", ownerErr
}
if RepoErr != nil {
return "", "", RepoErr
}
return owner, repository, nil
}

func getToken(setup *structpb.Struct) string {
return setup.GetFields()["token"].GetStringValue()
}
Loading
Loading