mirror of
https://github.com/zoriya/cish.git
synced 2025-12-06 07:16:16 +00:00
65 lines
1.6 KiB
Go
65 lines
1.6 KiB
Go
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"
|
|
|
|
gogithttp "github.com/go-git/go-git/v6/plumbing/transport/http"
|
|
)
|
|
|
|
type Repository struct {
|
|
Name string // Full repo name like zoriya/cish
|
|
CloneURL string
|
|
InstallationID int64
|
|
}
|
|
|
|
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: &gogithttp.BasicAuth{
|
|
Username: "cish", // yes, this can be anything except an empty string
|
|
Password: *installToken.Token,
|
|
},
|
|
Progress: os.Stdout,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
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
|
|
}
|