Skip to content

Commit

Permalink
fix: use ghcp (#13)
Browse files Browse the repository at this point in the history
* fix: use ghcp

* fix: fix lint error
  • Loading branch information
suzuki-shunsuke authored May 25, 2023
1 parent d316acf commit 3c3c114
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist
config.yaml
data.json
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@ Renovate alternative only for aqua-registry. Overcome Renovate's scalability iss
## GitHub Access Token

- `GITHUB_TOKEN`
- `write:packages`
- `pull-requests:write`
- `contents:write`
- `AQUA_REGISTRY_UPDATER_CONTAINER_REGISTRY_TOKEN`
- classic personal access token
- `write:packages`
- `read:org`

## Requirements

- GitHub CLI
- int128/ghcp

## LICENSE

Expand Down
3 changes: 2 additions & 1 deletion cmd/aqua-registry-updater/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func main() {
}

func core(ctx context.Context, logE *logrus.Entry) error {
crToken := os.Getenv("AQUA_REGISTRY_UPDATER_CONTAINER_REGISTRY_TOKEN")
token := os.Getenv("GITHUB_TOKEN")
repoOwner, repoName, found := strings.Cut(os.Getenv("GITHUB_REPOSITORY"), "/")
if !found {
Expand All @@ -37,6 +38,6 @@ func core(ctx context.Context, logE *logrus.Entry) error {
ctx, stop := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM)
defer stop()
return ctrl.Update(ctx, logE, &controller.Param{ //nolint:wrapcheck
GitHubToken: token,
GitHubToken: crToken,
})
}
32 changes: 7 additions & 25 deletions pkg/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"

"github.com/google/go-github/v52/github"
Expand Down Expand Up @@ -89,7 +90,7 @@ func (ctrl *Controller) Update(ctx context.Context, logE *logrus.Entry, param *P
return
}
logE.Info("pushing data.json to the container registry")
if err := pushFiles(ctx, repo, tag); err != nil {
if err := pushFiles(context.Background(), repo, tag); err != nil {
logerr.WithError(logE, err).Error("push data.json to the container registry")
}
}()
Expand Down Expand Up @@ -149,11 +150,8 @@ func (ctrl *Controller) listPkgYAML() ([]string, error) {
return pkgPaths, nil
}

func (ctrl *Controller) handlePackage(ctx context.Context, logE *logrus.Entry, pkg *Package) (bool, error) { //nolint:cyclop,funlen
func (ctrl *Controller) handlePackage(ctx context.Context, logE *logrus.Entry, pkg *Package) (bool, error) { //nolint:cyclop
pkgPath := filepath.Join("pkgs", pkg.Name, "pkg.yaml")
if err := ctrl.exec(ctx, "git", "checkout", "main"); err != nil {
return true, fmt.Errorf("git checkout main: %w", err)
}
body, err := afero.ReadFile(ctrl.fs, pkgPath)
if err != nil {
return false, fmt.Errorf("read pkg.yaml: %w", err)
Expand Down Expand Up @@ -189,18 +187,9 @@ func (ctrl *Controller) handlePackage(ctx context.Context, logE *logrus.Entry, p

prTitle := fmt.Sprintf("chore: update %s %s to %s", pkg.Name, currentVersion, newVersion)
branch := fmt.Sprintf("aqua-registry-updater-%s-%s", pkg.Name, newVersion)
if err := ctrl.exec(ctx, "git", "checkout", "-b", branch); err != nil {
if err := ctrl.exec(ctx, "ghcp", "commit", "-r", fmt.Sprintf("%s/%s", ctrl.param.RepoOwner, ctrl.param.RepoName), "-b", branch, "-m", prTitle, pkgPath); err != nil {
return true, fmt.Errorf("create a branch: %w", err)
}
if err := ctrl.exec(ctx, "git", "add", pkgPath); err != nil {
return true, fmt.Errorf("git add %s: %w", pkgPath, err)
}
if err := ctrl.exec(ctx, "git", "commit", "-m", prTitle); err != nil {
return true, fmt.Errorf(`git commit -m "%s": %w`, prTitle, err)
}
if err := ctrl.exec(ctx, "git", "push", "origin", branch); err != nil {
return true, fmt.Errorf(`git push origin %s: %w`, branch, err)
}
prNumber, err := ctrl.createPR(ctx, repoOwner, repoName, &ParamCreatePR{
NewVersion: newVersion,
CurrentVersion: currentVersion,
Expand All @@ -216,10 +205,8 @@ func (ctrl *Controller) handlePackage(ctx context.Context, logE *logrus.Entry, p
logerr.WithError(logE, err).Warn("compare version")
}
if automerged {
if _, _, err := ctrl.pull.Merge(ctx, ctrl.param.RepoOwner, ctrl.param.RepoName, prNumber, "", &github.PullRequestOptions{
MergeMethod: "squash",
}); err != nil {
logerr.WithError(logE, err).Warn("enable auto-merge")
if err := ctrl.exec(ctx, "gh", "-R", fmt.Sprintf("%s/%s", ctrl.param.RepoOwner, ctrl.param.RepoName), "pr", "merge", "-s", "--auto", strconv.Itoa(prNumber)); err != nil {
return true, fmt.Errorf("enable auto-merge: %w", err)
}
}
return true, nil
Expand Down Expand Up @@ -325,18 +312,14 @@ func (ctrl *Controller) createPR(ctx context.Context, repoOwner, repoName string
return pr.GetNumber(), nil
}

func (ctrl *Controller) exec(ctx context.Context, command string, args ...string) error { //nolint:unparam
func (ctrl *Controller) exec(ctx context.Context, command string, args ...string) error {
cmd := exec.Command(command, args...)
cmd.Stdout = ctrl.stdout
cmd.Stderr = ctrl.stderr
runner := timeout.NewRunner(0)
return runner.Run(ctx, cmd) //nolint:wrapcheck
}

// type RepositoriesService interface {
// GetLatestRelease(ctx context.Context, owner, repo string) (*github.RepositoryRelease, *github.Response, error)
// }

func NewGitHub(ctx context.Context, token string) *github.Client {
return github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
Expand Down Expand Up @@ -375,7 +358,6 @@ type Controller struct {

type PullRequestsService interface {
Create(ctx context.Context, owner, repo string, pull *github.NewPullRequest) (*github.PullRequest, *github.Response, error)
Merge(ctx context.Context, owner string, repo string, number int, commitMessage string, options *github.PullRequestOptions) (*github.PullRequestMergeResult, *github.Response, error)
}

func New(fs afero.Fs, param *ParamNew, pull PullRequestsService) *Controller {
Expand Down

0 comments on commit 3c3c114

Please sign in to comment.