mirror of
https://github.com/zoriya/cish.git
synced 2026-08-16 02:44:46 +00:00
add git clone and job listing
This commit is contained in:
@@ -1,12 +1,17 @@
|
||||
package github
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/bradleyfalzon/ghinstallation"
|
||||
"github.com/go-git/go-git/v6"
|
||||
gogithub "github.com/google/go-github/github"
|
||||
|
||||
"github.com/go-git/go-git/v6/plumbing/transport/http"
|
||||
gogithttp "github.com/go-git/go-git/v6/plumbing/transport/http"
|
||||
)
|
||||
|
||||
type Repository struct {
|
||||
@@ -15,13 +20,27 @@ type Repository struct {
|
||||
InstallationID int64
|
||||
}
|
||||
|
||||
func (r *Repository) Clone(ref string) error {
|
||||
installToken := ""
|
||||
_, err := git.PlainClone(fmt.Sprintf("/tmp/%s", r.Name), &git.CloneOptions{
|
||||
func (r *Repository) ClonePath() string {
|
||||
return fmt.Sprintf("/tmp/%s", r.Name)
|
||||
}
|
||||
|
||||
func (r *Repository) Clone(appID int64, appPrivateKey, ref string) error {
|
||||
itr, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, appID, r.InstallationID, appPrivateKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
client := gogithub.NewClient(&http.Client{Transport: itr})
|
||||
installToken, _, err := client.Apps.CreateInstallationToken(context.Background(), r.InstallationID)
|
||||
if err != nil {
|
||||
fmt.Errorf("failed to get install token with err: %w", err)
|
||||
return err
|
||||
}
|
||||
_, err = git.PlainClone(r.ClonePath(), &git.CloneOptions{
|
||||
URL: r.CloneURL,
|
||||
Auth: &http.BasicAuth{
|
||||
Auth: &gogithttp.BasicAuth{
|
||||
Username: "cish", // yes, this can be anything except an empty string
|
||||
Password: installToken,
|
||||
Password: *installToken.Token,
|
||||
},
|
||||
Progress: os.Stdout,
|
||||
})
|
||||
@@ -30,3 +49,16 @@ func (r *Repository) Clone(ref string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Repository) ListJobs(eventType string) ([]string, error) {
|
||||
entries, err := os.ReadDir(r.CloneURL + "/.cish/" + eventType) // eventType = push | pull_request | tags
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return nil, err
|
||||
}
|
||||
res := []string{}
|
||||
for _, e := range entries {
|
||||
res = append(res, fmt.Sprintf("%s/.cish/%s/%s", r.Name, eventType, e.Name()))
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
@@ -17,11 +17,12 @@ func HandleEvent(event *github.WebhookEvent) error {
|
||||
log.Printf("Push to %s by %s",
|
||||
payload.Repository.ContentsURL,
|
||||
payload.PusherPerson.Name)
|
||||
// repo := Repository{
|
||||
// Name: payload.Repository.FullName,
|
||||
// CloneURL: payload.Repository.CloneURL,
|
||||
// InstallationID: payload.Installation.ID,
|
||||
// }
|
||||
repo := Repository{
|
||||
Name: payload.Repository.FullName,
|
||||
CloneURL: payload.Repository.CloneURL,
|
||||
InstallationID: payload.Installation.ID,
|
||||
}
|
||||
repo.Clone(1, ".pem", payload.Ref)
|
||||
// getJobsFromRepository()
|
||||
|
||||
case github.PullRequestEvent:
|
||||
|
||||
Reference in New Issue
Block a user