2024-03-24 20:52:32 +03:00
package github
import (
2024-04-06 15:18:43 +03:00
"bytes"
2024-03-24 20:52:32 +03:00
"context"
2024-04-06 15:18:43 +03:00
"encoding/json"
"errors"
2024-04-02 22:54:15 +03:00
"fmt"
2024-04-06 15:18:43 +03:00
"io"
"net/http"
"os/exec"
"strings"
"text/template"
"time"
2024-03-24 20:52:32 +03:00
2024-04-06 15:18:43 +03:00
"github.com/go-git/go-git/v5"
gitconfig "github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
httpauth "github.com/go-git/go-git/v5/plumbing/transport/http"
2024-12-07 02:35:30 +03:00
"go.unistack.org/micro/v3/logger"
"go.unistack.org/pkgdash/internal/configcli"
"go.unistack.org/pkgdash/internal/modules"
2024-03-24 20:52:32 +03:00
)
2024-04-06 15:18:43 +03:00
var ErrPRNotExist = errors . New ( "pull request does not exist" )
2024-03-24 20:52:32 +03:00
type Github struct {
2024-12-07 02:35:30 +03:00
logger logger . Logger
2024-04-06 15:18:43 +03:00
URL string
Username string
Password string
PRTitle string
PRBody string
Repository string
Owner string
pulls [ ] * githubPull
2024-04-21 16:26:11 +03:00
baseRef * plumbing . Reference
2024-03-24 20:52:32 +03:00
}
2024-12-07 02:35:30 +03:00
func NewGithub ( cfg configcli . Config , log logger . Logger ) * Github {
2024-03-24 20:52:32 +03:00
return & Github {
2024-12-07 02:35:30 +03:00
logger : log ,
2024-04-06 15:18:43 +03:00
URL : cfg . Source . APIURL ,
Username : cfg . Source . Username ,
Password : cfg . Source . Password ,
PRTitle : cfg . PullRequestTitle ,
PRBody : cfg . PullRequestBody ,
Repository : cfg . Source . Repository ,
Owner : cfg . Source . Owner ,
2024-03-24 20:52:32 +03:00
}
}
2024-04-06 15:18:43 +03:00
type githubPull struct {
URL string ` json:"url" `
Title string ` json:"title" `
Base struct {
Ref string ` json:"ref" `
} ` json:"base" `
Head struct {
Ref string ` json:"ref" `
} ` json:"head" `
ID int64 ` json:"id" `
}
2024-04-02 22:54:15 +03:00
func ( g * Github ) Name ( ) string {
return "github"
}
2024-03-24 20:52:32 +03:00
func ( g * Github ) RequestOpen ( ctx context . Context , branch string , path string , mod modules . Update ) error {
2024-12-07 02:35:30 +03:00
g . logger . Debug ( ctx , fmt . Sprintf ( "RequestOpen start, mod title: %s" , path ) )
2024-04-06 15:18:43 +03:00
var buf [ ] byte
var err error
// создания шаблона названия для пулл реквеста
tplTitle , err := template . New ( "pull_request_title" ) . Parse ( g . PRTitle )
if err != nil {
2024-12-07 02:35:30 +03:00
g . logger . Fatal ( ctx , fmt . Sprintf ( "failed to parse template: %v" , err ) )
2024-04-06 15:18:43 +03:00
}
wTitle := bytes . NewBuffer ( nil )
// создания шаблона тела для пулл реквеста
tplBody , err := template . New ( "pull_request_body" ) . Parse ( g . PRTitle )
if err != nil {
2024-12-07 02:35:30 +03:00
g . logger . Fatal ( ctx , fmt . Sprintf ( "failed to parse template: %v" , err ) )
2024-04-06 15:18:43 +03:00
}
wBody := bytes . NewBuffer ( nil )
data := map [ string ] string {
"Name" : path ,
"VersionOld" : mod . Module . Version ,
"VersionNew" : mod . Version ,
}
if err = tplTitle . Execute ( wTitle , data ) ; err != nil {
2024-12-07 02:35:30 +03:00
g . logger . Fatal ( ctx , fmt . Sprintf ( "failed to execute template: %v" , err ) )
2024-04-06 15:18:43 +03:00
}
if err = tplBody . Execute ( wBody , data ) ; err != nil {
2024-12-07 02:35:30 +03:00
g . logger . Fatal ( ctx , fmt . Sprintf ( "failed to execute template: %v" , err ) )
2024-04-06 15:18:43 +03:00
}
// открытие гит репозитория с опцией обхода репозитория для нахождения .git
repo , err := git . PlainOpenWithOptions ( "." , & git . PlainOpenOptions { DetectDotGit : true } )
if err != nil {
2024-12-07 02:35:30 +03:00
g . logger . Fatal ( ctx , fmt . Sprintf ( "failed to open repo: %v" , err ) )
2024-04-06 15:18:43 +03:00
}
2024-12-07 02:35:30 +03:00
// извлекаем ссылки с объектами из удаленного объекта??
2024-04-06 15:18:43 +03:00
if err = repo . FetchContext ( ctx , & git . FetchOptions {
// Auth: &httpauth.BasicAuth{Username: g.Username, Password: g.Password},
Force : true ,
} ) ; err != nil && err != git . NoErrAlreadyUpToDate {
2024-12-07 02:35:30 +03:00
g . logger . Fatal ( ctx , fmt . Sprintf ( "failed to fetch repo : %v" , err ) )
} // обновляем репозиторий
2024-04-06 15:18:43 +03:00
var headRef * plumbing . Reference // вроде ссылка на гит
2024-04-21 16:26:11 +03:00
if g . baseRef == nil {
g . baseRef , err = repo . Head ( )
if err != nil {
2024-12-07 02:35:30 +03:00
g . logger . Fatal ( ctx , fmt . Sprintf ( "Error head: %s" , err ) )
2024-04-21 16:26:11 +03:00
}
}
2024-12-07 02:35:30 +03:00
refIter , err := repo . Branches ( ) // получение веток
2024-04-06 15:18:43 +03:00
if err != nil {
2024-12-07 02:35:30 +03:00
g . logger . Fatal ( ctx , fmt . Sprintf ( "failed to get branches: %v" , err ) )
2024-04-06 15:18:43 +03:00
return err
}
for {
ref , err := refIter . Next ( )
if err != nil {
break
}
2024-12-07 02:35:30 +03:00
if ref . Name ( ) . Short ( ) == branch { // todo вот тут возможно нужно переделать
2024-04-06 15:18:43 +03:00
headRef = ref
break
}
2024-12-07 02:35:30 +03:00
} // перебираем получение ветки и когда находим нужную выходим из цикла записав ветку в headRef
2024-04-06 15:18:43 +03:00
refIter . Close ( )
if headRef == nil {
2024-12-07 02:35:30 +03:00
g . logger . Fatal ( ctx , "failed to get repo branch head" )
2024-04-06 15:18:43 +03:00
return err
} // Н е получили нужную ветку
2024-12-07 02:35:30 +03:00
g . logger . Info ( ctx , fmt . Sprintf ( "repo head %s" , headRef ) )
2024-04-06 15:18:43 +03:00
2024-12-07 02:35:30 +03:00
wtree , err := repo . Worktree ( ) // todo вроде рабочее дерево не нужно
2024-04-06 15:18:43 +03:00
if err != nil {
2024-12-07 02:35:30 +03:00
g . logger . Fatal ( ctx , fmt . Sprintf ( "failed to get worktree: %v" , err ) )
2024-04-06 15:18:43 +03:00
}
2024-12-07 02:35:30 +03:00
defer g . checkout ( * wtree , * g . baseRef )
2024-04-06 15:18:43 +03:00
2024-12-07 02:35:30 +03:00
g . pulls , err = g . GetPulls ( ctx , g . URL , g . Owner , g . Repository , g . Password )
if err != nil && err != ErrPRNotExist {
g . logger . Error ( ctx , fmt . Sprintf ( "GetPulls error: %s" , err ) )
2024-04-06 15:18:43 +03:00
return err
}
for _ , pull := range g . pulls {
if strings . Contains ( pull . Title , path ) && strings . Contains ( pull . Base . Ref , branch ) {
2024-12-07 02:35:30 +03:00
g . logger . Info ( ctx , fmt . Sprintf ( "PR for %s exists %s, call RequestUpdate" , path , pull . URL ) )
2024-04-06 15:18:43 +03:00
return g . RequestUpdate ( ctx , branch , path , mod )
} // хотим проверить есть ли пулл реквест для этой ветки, если есть то выходим
}
2024-12-07 02:35:30 +03:00
g . logger . Info ( ctx , fmt . Sprintf ( "update %s from %s to %s" , path , mod . Module . Version , mod . Version ) )
2024-04-06 15:18:43 +03:00
2024-12-07 02:35:30 +03:00
g . logger . Info ( ctx , "reset worktree" )
2024-04-06 15:18:43 +03:00
if err = wtree . Reset ( & git . ResetOptions { Commit : headRef . Hash ( ) , Mode : git . HardReset } ) ; err != nil {
2024-12-07 02:35:30 +03:00
g . logger . Error ( ctx , fmt . Sprintf ( "failed to reset repo branch: %v" , err ) )
} // вроде меняем ветку todo вроде можно удалить
2024-04-06 15:18:43 +03:00
if err = wtree . PullContext ( ctx , & git . PullOptions {
Auth : & httpauth . BasicAuth { Username : g . Username , Password : g . Password } ,
Depth : 1 ,
// RemoteURL :
Force : true ,
RemoteName : "origin" ,
} ) ; err != nil && err != git . NoErrAlreadyUpToDate {
2024-12-07 02:35:30 +03:00
g . logger . Error ( ctx , fmt . Sprintf ( "failed to pull repo: %v" , err ) ) // подтягиваем изменения с удаленого репозитория
2024-04-06 15:18:43 +03:00
}
2024-12-07 02:35:30 +03:00
g . logger . Info ( ctx , fmt . Sprintf ( "checkout ref %s" , headRef ) )
2024-04-06 15:18:43 +03:00
if err = wtree . Checkout ( & git . CheckoutOptions {
Hash : headRef . Hash ( ) ,
Branch : plumbing . NewBranchReferenceName ( fmt . Sprintf ( "pkgdash/go_modules/%s-%s" , path , mod . Version ) ) ,
Create : true ,
Force : true ,
} ) ; err != nil {
2024-12-07 02:35:30 +03:00
g . logger . Error ( ctx , fmt . Sprintf ( "failed to checkout tree: %v" , err ) )
2024-04-06 15:18:43 +03:00
return err
2024-12-07 02:35:30 +03:00
} // создаем новую ветку
2024-04-06 15:18:43 +03:00
epath , err := exec . LookPath ( "go" )
if errors . Is ( err , exec . ErrDot ) {
err = nil
}
if err != nil {
2024-12-07 02:35:30 +03:00
g . logger . Fatal ( ctx , fmt . Sprintf ( "failed to find go command: %v" , err ) )
2024-04-06 15:18:43 +03:00
} // ищем go файл
var cmd * exec . Cmd
var out [ ] byte
cmd = exec . CommandContext ( ctx , epath , "mod" , "edit" , fmt . Sprintf ( "-droprequire=%s" , mod . Module . Path ) )
if out , err = cmd . CombinedOutput ( ) ; err != nil {
2024-12-07 02:35:30 +03:00
g . logger . Fatal ( ctx , fmt . Sprintf ( "failed to run go mod edit: %s err: %v" , out , err ) )
2024-04-06 15:18:43 +03:00
}
cmd = exec . CommandContext ( ctx , epath , "mod" , "edit" , fmt . Sprintf ( "-require=%s@%s" , path , mod . Version ) )
if out , err = cmd . CombinedOutput ( ) ; err != nil {
2024-12-07 02:35:30 +03:00
g . logger . Fatal ( ctx , fmt . Sprintf ( "failed to run go mod edit: %s err: %v" , out , err ) )
2024-04-06 15:18:43 +03:00
} // пытаемся выполнить команду go mod edit с новой версией модуля
cmd = exec . CommandContext ( ctx , epath , "mod" , "tidy" )
if out , err = cmd . CombinedOutput ( ) ; err != nil {
2024-12-07 02:35:30 +03:00
g . logger . Fatal ( ctx , fmt . Sprintf ( "failed to run go mod tidy: %s err: %v" , out , err ) )
2024-04-06 15:18:43 +03:00
} // пытаемся выполнить команду go mod tidy пытаемся подтянуть новую версию модуля
2024-12-07 02:35:30 +03:00
g . logger . Info ( ctx , "worktree add go.mod" )
2024-04-06 15:18:43 +03:00
if _ , err = wtree . Add ( "go.mod" ) ; err != nil {
2024-12-07 02:35:30 +03:00
g . logger . Fatal ( ctx , fmt . Sprintf ( "failed to add file: %v" , err ) )
2024-04-06 15:18:43 +03:00
}
2024-12-07 02:35:30 +03:00
g . logger . Info ( ctx , "worktree add go.sum" )
2024-04-06 15:18:43 +03:00
if _ , err = wtree . Add ( "go.sum" ) ; err != nil {
2024-12-07 02:35:30 +03:00
g . logger . Fatal ( ctx , fmt . Sprintf ( "failed to add file: %v" , err ) )
2024-04-06 15:18:43 +03:00
}
2024-12-07 02:35:30 +03:00
g . logger . Info ( ctx , "worktree commit" )
2024-04-06 15:18:43 +03:00
_ , err = wtree . Commit ( wTitle . String ( ) , & git . CommitOptions {
Parents : [ ] plumbing . Hash { headRef . Hash ( ) } ,
Author : & object . Signature {
Name : "gitea-actions" ,
Email : "info@unistack.org" ,
When : time . Now ( ) ,
} ,
} ) // хотим за коммитить изменения
if err != nil {
2024-12-07 02:35:30 +03:00
g . logger . Fatal ( ctx , fmt . Sprintf ( "failed to commit: %v" , err ) )
2024-04-06 15:18:43 +03:00
}
2024-12-07 02:35:30 +03:00
refspec := gitconfig . RefSpec ( fmt . Sprintf ( "+refs/heads/pkgdash/go_modules/%s-%s:refs/heads/pkgdash/go_modules/%s-%s" , path , mod . Version , path , mod . Version ) ) // todo как будто нужно переделать
2024-04-06 15:18:43 +03:00
2024-12-07 02:35:30 +03:00
g . logger . Info ( ctx , fmt . Sprintf ( "try to push refspec %s" , refspec ) )
2024-04-06 15:18:43 +03:00
if err = repo . PushContext ( ctx , & git . PushOptions {
RefSpecs : [ ] gitconfig . RefSpec { refspec } ,
Auth : & httpauth . BasicAuth { Username : g . Username , Password : g . Password } ,
Force : true ,
} ) ; err != nil {
2024-12-07 02:35:30 +03:00
g . logger . Fatal ( ctx , fmt . Sprintf ( "failed to push repo branch: %v" , err ) )
2024-04-06 15:18:43 +03:00
} // пытаемся за пушить изменения
body := map [ string ] string {
"base" : branch ,
"body" : wBody . String ( ) ,
"head" : fmt . Sprintf ( "pkgdash/go_modules/%s-%s" , path , mod . Version ) ,
"title" : wTitle . String ( ) ,
}
2024-12-07 02:35:30 +03:00
g . logger . Info ( ctx , fmt . Sprintf ( "raw body: %#+v" , body ) )
2024-04-06 15:18:43 +03:00
buf , err = json . Marshal ( body )
if err != nil {
return err
}
2024-12-07 02:35:30 +03:00
g . logger . Info ( ctx , fmt . Sprintf ( "marshal body: %s" , buf ) )
2024-04-06 15:18:43 +03:00
req , err := http . NewRequestWithContext (
ctx ,
http . MethodPost ,
fmt . Sprintf ( "https://%s/api/v1/repos/%s/%s/pulls" , g . URL , g . Owner , g . Repository ) ,
bytes . NewReader ( buf ) ,
)
if err != nil {
return err
}
req . Header . Add ( "Accept" , "application/json" )
req . Header . Add ( "Content-Type" , "application/json" )
2024-04-21 16:26:11 +03:00
req . Header . Add ( "Authorization" , "Bearer " + g . Password )
2024-04-06 15:18:43 +03:00
rsp , err := http . DefaultClient . Do ( req )
if err != nil {
return err
2024-12-07 02:35:30 +03:00
} // Вроде создаем новый реквест на создание пулл реквеста
2024-04-06 15:18:43 +03:00
if rsp . StatusCode != http . StatusCreated {
buf , _ = io . ReadAll ( rsp . Body )
return fmt . Errorf ( "unknown error: %s" , buf )
}
2024-12-07 02:35:30 +03:00
g . logger . Info ( ctx , fmt . Sprintf ( "PR create for %s-%s" , path , mod . Version ) )
2024-04-06 15:18:43 +03:00
repo , err = git . PlainOpenWithOptions ( "." , & git . PlainOpenOptions { DetectDotGit : true } )
if err != nil {
2024-12-07 02:35:30 +03:00
g . logger . Fatal ( ctx , fmt . Sprintf ( "failed to open repo: %v" , err ) )
2024-04-06 15:18:43 +03:00
}
return nil
2024-03-24 20:52:32 +03:00
}
2024-12-07 02:35:30 +03:00
2024-03-24 20:52:32 +03:00
func ( g * Github ) RequestClose ( ctx context . Context , branch string , path string ) error {
2024-04-02 22:54:15 +03:00
return fmt . Errorf ( "implement me" )
2024-03-24 20:52:32 +03:00
}
2024-12-07 02:35:30 +03:00
2024-03-24 20:52:32 +03:00
func ( g * Github ) RequestUpdate ( ctx context . Context , branch string , path string , mod modules . Update ) error {
2024-04-02 22:54:15 +03:00
return fmt . Errorf ( "implement me" )
2024-03-24 20:52:32 +03:00
}
2024-12-07 02:35:30 +03:00
2024-03-24 20:52:32 +03:00
func ( g * Github ) RequestList ( ctx context . Context , branch string ) ( map [ string ] string , error ) {
2024-12-07 02:35:30 +03:00
g . logger . Debug ( ctx , fmt . Sprintf ( "RequestList for %s" , branch ) )
2024-04-21 16:26:11 +03:00
var err error
2024-12-07 02:35:30 +03:00
g . pulls , err = g . GetPulls ( ctx , g . URL , g . Owner , g . Repository , g . Password )
2024-04-21 16:26:11 +03:00
if err != nil {
2024-12-07 02:35:30 +03:00
g . logger . Error ( ctx , fmt . Sprintf ( "GetPulls error: %s" , err ) )
2024-04-21 16:26:11 +03:00
return nil , err
}
var path string
rMap := make ( map [ string ] string )
for _ , pull := range g . pulls {
2024-12-07 02:35:30 +03:00
if ! strings . HasPrefix ( pull . Title , "Bump " ) || pull . Base . Ref != branch { // добавляем только реквесты бота по обновлению модулей
2024-04-21 16:26:11 +03:00
continue
}
2024-12-07 02:35:30 +03:00
path = strings . Split ( pull . Title , " " ) [ 1 ] // todo Работет только для дефолтного шаблона
2024-04-21 16:26:11 +03:00
rMap [ path ] = pull . Title
}
return rMap , nil
2024-03-24 20:52:32 +03:00
}
2024-04-06 15:18:43 +03:00
2024-12-07 02:35:30 +03:00
func ( g * Github ) GetPulls ( ctx context . Context , url , owner , repo , password string ) ( [ ] * githubPull , error ) {
2024-04-06 15:18:43 +03:00
var pullsAll [ ] * githubPull
page := 1
for {
pulls := make ( [ ] * githubPull , 0 , 10 )
req , err := http . NewRequestWithContext (
ctx ,
http . MethodGet ,
fmt . Sprintf ( "https://%s/api/v1/repos/%s/%s/pulls?state=open&page=%v" , url , owner , repo , page ) ,
nil )
if err != nil {
return nil , err
2024-12-07 02:35:30 +03:00
} // вроде запроса к репозиторию
2024-04-06 15:18:43 +03:00
req . Header . Add ( "Accept" , "application/json" )
req . Header . Add ( "Content-Type" , "application/json" )
2024-04-21 16:26:11 +03:00
req . Header . Add ( "Authorization" , "Bearer " + password )
2024-04-06 15:18:43 +03:00
rsp , err := http . DefaultClient . Do ( req ) // выполнение запроса
if err != nil {
return nil , err
}
buf , _ := io . ReadAll ( rsp . Body )
switch rsp . StatusCode {
case http . StatusOK :
if err = json . Unmarshal ( buf , & pulls ) ; err != nil {
2024-12-07 02:35:30 +03:00
g . logger . Error ( ctx , fmt . Sprintf ( "failed to decode response %s err: %v" , buf , err ) )
2024-04-06 15:18:43 +03:00
return nil , err
}
pullsAll = append ( pullsAll , pulls ... )
page ++
case http . StatusNotFound :
2024-12-07 02:35:30 +03:00
g . logger . Info ( ctx , fmt . Sprintf ( "pull-request is not exist for %s" , repo ) )
2024-04-06 15:18:43 +03:00
return nil , ErrPRNotExist
default :
return nil , fmt . Errorf ( "unknown error: %s" , buf )
}
if len ( pulls ) == 0 {
break
}
}
return pullsAll , nil
}
2024-04-21 16:26:11 +03:00
2024-12-07 02:35:30 +03:00
func ( g * Github ) checkout ( w git . Worktree , ref plumbing . Reference ) {
2024-04-21 16:26:11 +03:00
ctx := context . Background ( )
2024-12-07 02:35:30 +03:00
g . logger . Debug ( ctx , fmt . Sprintf ( "Checkout: %s" , ref . Name ( ) . Short ( ) ) )
2024-04-21 16:26:11 +03:00
if err := w . Checkout ( & git . CheckoutOptions {
Branch : ref . Name ( ) ,
Create : false ,
Force : true ,
Keep : false ,
} ) ; err != nil {
2024-12-07 02:35:30 +03:00
g . logger . Error ( ctx , fmt . Sprintf ( "failed to reset: %v" , err ) )
2024-04-21 16:26:11 +03:00
}
}