Skip to content

Commit

Permalink
feature(main): fix webhook error (#9)
Browse files Browse the repository at this point in the history
Signed-off-by: cuisongliu <cuisongliu@qq.com>
  • Loading branch information
cuisongliu authored May 1, 2023
1 parent fb22c61 commit 86215c4
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 173 deletions.
1 change: 1 addition & 0 deletions .github/gh-bot.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
version: v1
debug: true
type: actions
bot:
prefix: /
spe: _
Expand Down
45 changes: 0 additions & 45 deletions cmd/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,63 +17,18 @@ limitations under the License.
package cmd

import (
"fmt"
"github.com/cuisongliu/logger"
"github.com/labring/gh-rebot/pkg/types"
"github.com/pkg/errors"
"os"
)

func preCheck() error {
if err := checkGithubEnv(); err != nil {
return err
}
return nil
}

func checkGithubEnv() error {
if types.GlobalsGithubVar.RunnerID == "" {
return fmt.Errorf("error: GITHUB_RUN_ID is not set. Please set the GITHUB_RUN_ID environment variable")
}
if types.GlobalsGithubVar.SafeRepo == "" {
return fmt.Errorf("error: not found repository.full_name in github event")
}
if types.GlobalsGithubVar.CommentBody == "" {
return fmt.Errorf("error: not found comment.body in github event")
}
if types.GlobalsGithubVar.IssueOrPRNumber == 0 {
return fmt.Errorf("error: not found issue.number or pull_request.number in github event")
}
return nil
}

func checkToken() {
var err error
types.GlobalsGithubVar, err = types.GetGHEnvToVar()
if err != nil {
logger.Error(err)
os.Exit(1)
}
logger.Debug("github env to var: %v", types.GlobalsGithubVar)
types.GlobalsBotConfig, err = types.LoadConfig(cfgFile)
if err != nil {
logger.Error(err)
os.Exit(1)
}
if err = types.GlobalsBotConfig.Validate(); err != nil {
logger.Error(err)
os.Exit(1)
}
logger.Cfg(types.GlobalsBotConfig.GetDebug(), false)
if err := checkGhToken(); err != nil {
logger.Error(err)
os.Exit(1)
}
}

func checkGhToken() error {
if _, ok := os.LookupEnv("GH_TOKEN"); !ok {
return errors.New("error: GH_TOKEN is not set. Please set the GH_TOKEN environment variable to enable authentication and access to the GitHub API")
}
return nil
}
3 changes: 0 additions & 3 deletions cmd/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ var commentCmd = &cobra.Command{
return nil
},
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := preCheck(); err != nil {
return err
}
return nil
},
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/gh/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package gh

import (
"fmt"
"github.com/labring/gh-rebot/pkg/types"
"github.com/labring/gh-rebot/pkg/utils"
"github.com/pkg/errors"
)

Expand All @@ -34,7 +34,7 @@ func Tag(tag string) error {
fmt.Sprintf(gitNewTag, tag),
fmt.Sprintf(gitPushRemote, tag),
}
if err = types.ExecShellForAny()(shells); err != nil {
if err = utils.ExecShellForAny()(shells); err != nil {
return err
}
return nil
Expand Down
10 changes: 5 additions & 5 deletions pkg/gh/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ func setPreGithub() error {
authStatus,
disablePrompt,
fmt.Sprintf(forkRepo, types.GlobalsBotConfig.GetRepoName(), types.GlobalsBotConfig.GetForkName(), types.GlobalsBotConfig.GetOrgCommand()),
types.RetryShell(fmt.Sprintf(checkRepo, types.GlobalsBotConfig.GetRepoName())),
types.RetryShell(fmt.Sprintf(cloneRepo, types.GlobalsBotConfig.GetRepoName())),
utils.RetryShell(fmt.Sprintf(checkRepo, types.GlobalsBotConfig.GetRepoName())),
utils.RetryShell(fmt.Sprintf(cloneRepo, types.GlobalsBotConfig.GetRepoName())),
fmt.Sprintf(configEmail, types.GlobalsBotConfig.GetEmail()),
fmt.Sprintf(configUser, types.GlobalsBotConfig.GetUsername()),
types.SecretShell(fmt.Sprintf(setToken, types.GlobalsBotConfig.GetUsername(), types.GlobalsBotConfig.GetToken(), types.GlobalsBotConfig.GetRepoName())),
types.SecretShell(fmt.Sprintf(gitAddRemote, types.GlobalsBotConfig.GetUsername(), types.GlobalsBotConfig.GetToken(), types.GlobalsBotConfig.GetForkName())),
utils.SecretShell(fmt.Sprintf(setToken, types.GlobalsBotConfig.GetUsername(), types.GlobalsBotConfig.GetToken(), types.GlobalsBotConfig.GetRepoName())),
utils.SecretShell(fmt.Sprintf(gitAddRemote, types.GlobalsBotConfig.GetUsername(), types.GlobalsBotConfig.GetToken(), types.GlobalsBotConfig.GetForkName())),
fmt.Sprintf(syncRepo),
}
if err := types.ExecShellForAny(types.GlobalsBotConfig.GetToken())(shells); err != nil {
if err := utils.ExecShellForAny(types.GlobalsBotConfig.GetToken())(shells); err != nil {
logger.Error("setPreGithub err:%v", err)
return err
}
Expand Down
33 changes: 8 additions & 25 deletions pkg/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,16 @@ type Release struct {
AllowOps []string `json:"allowOps"`
}

type Type string

const (
TypeAction Type = "action"
TypeWebhook Type = "webhook"
)

type Config struct {
Version string `json:"version"`
Type string `json:"type"`
Debug bool `json:"debug"`
Bot Bot `json:"bot"`
Repo Repo `json:"repo"`
Expand All @@ -58,31 +66,6 @@ type Config struct {
Release *Release `json:"release,omitempty"`
}

func (r *Config) Validate() error {
if r.Bot.Username == "" {
return fmt.Errorf("bot username is required")
}
if r.Bot.Email == "" {
return fmt.Errorf("bot email is required")
}
if r.GetRepoName() == "" {
return fmt.Errorf("repo name is required")
}
if r.GetForkName() == "" {
return fmt.Errorf("repo fork is required")
}
if r.Release != nil {
if r.Release.Action == "" {
return fmt.Errorf("release action is required")
}
if r.Release.Retry == "" {
return fmt.Errorf("release retry is required")
}
}

return nil
}

// GetPrefix returns the prefix for the bot
func (r *Config) GetPrefix() string {
if r.Bot.Prefix == "" {
Expand Down
5 changes: 4 additions & 1 deletion pkg/types/gh_env.go → pkg/types/gh_actions_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (g *GithubVar) GetRunnerURL() string {
return runnerURL
}

func GetGHEnvToVar() (*GithubVar, error) {
func ghEnvToVar() (*GithubVar, error) {
gVar := new(GithubVar)
gVar.RunnerID = os.Getenv("GITHUB_RUN_ID")
//gVar.SafeRepo = os.Getenv("GITHUB_REPOSITORY")
Expand All @@ -70,5 +70,8 @@ func GetGHEnvToVar() (*GithubVar, error) {
user, _, _ = unstructured.NestedString(mData, "comment", "user", "login")
}
gVar.SenderOrCommentUser = user
if err := gVar.validate(); err != nil {
return nil, err
}
return gVar, nil
}
25 changes: 23 additions & 2 deletions pkg/types/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package types

import (
"fmt"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/yaml"
"os"
"strings"
Expand All @@ -38,9 +39,29 @@ func ParseConfig(filePath string) (*Config, error) {
}
if token, ok := os.LookupEnv("GH_TOKEN"); ok {
config.Token = token
} else {
return nil, errors.New("error: GH_TOKEN is not set. Please set the GH_TOKEN environment variable to enable authentication and access to the GitHub API")
}
if config.Repo.Org {
config.Repo.OrgCommand = fmt.Sprintf(" --org %s ", strings.SplitN(config.GetRepoName(), "/", 2)[0])

if config.Type == "" {
config.Type = TypeAction
}

switch config.Type {
case TypeAction:
GlobalsGithubVar, err = ghEnvToVar()
if err != nil {
return nil, err
}
if config.Repo.Org {
config.Repo.OrgCommand = fmt.Sprintf(" --org %s ", strings.SplitN(config.GetRepoName(), "/", 2)[0])
}
case TypeWebhook:

}

if err = config.validate(); err != nil {
return nil, err
}
return config, nil
}
Expand Down
62 changes: 0 additions & 62 deletions pkg/types/shell.go

This file was deleted.

27 changes: 0 additions & 27 deletions pkg/types/shell_test.go

This file was deleted.

62 changes: 62 additions & 0 deletions pkg/types/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright 2023 cuisongliu@qq.com.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package types

import "fmt"

func (r *Config) validate() error {
if r.Bot.Username == "" {
return fmt.Errorf("bot username is required")
}
if r.Bot.Email == "" {
return fmt.Errorf("bot email is required")
}

if r.Type == TypeAction {
if r.GetRepoName() == "" {
return fmt.Errorf("repo name is required")
}
if r.GetForkName() == "" {
return fmt.Errorf("repo fork is required")
}
if r.Release != nil {
if r.Release.Action == "" {
return fmt.Errorf("release action is required")
}
if r.Release.Retry == "" {
return fmt.Errorf("release retry is required")
}
}
}
return nil
}

func (t *GithubVar) validate() error {
if t.RunnerID == "" {
return fmt.Errorf("error: GITHUB_RUN_ID is not set. Please set the GITHUB_RUN_ID environment variable")
}
if t.SafeRepo == "" {
return fmt.Errorf("error: not found repository.full_name in github event")
}
if t.CommentBody == "" {
return fmt.Errorf("error: not found comment.body in github event")
}
if t.IssueOrPRNumber == 0 {
return fmt.Errorf("error: not found issue.number or pull_request.number in github event")
}
return nil
}
Loading

0 comments on commit 86215c4

Please sign in to comment.