From 6d650d68b116fd91f10230d156c5dc82521f440bd0b79ffb25910d8f16006658 Mon Sep 17 00:00:00 2001 From: Adam Majer Date: Mon, 22 Jul 2024 17:09:45 +0200 Subject: [PATCH] . --- bots-common/consts.go | 12 +++++++----- bots-common/obs_utils.go | 13 ++++++++++--- bots-common/obs_utils_test.go | 8 +++++--- bots-common/tokens.go | 20 +++++++++++++++++++- obs-staging-bot/main.go | 14 +++++++------- 5 files changed, 48 insertions(+), 19 deletions(-) diff --git a/bots-common/consts.go b/bots-common/consts.go index 8183ce6..c197775 100644 --- a/bots-common/consts.go +++ b/bots-common/consts.go @@ -1,10 +1,12 @@ -package common; +package common const ( - GiteaTokenEnv = "GITEA_TOKEN" - DefaultGitPrj = "_ObsPrj" - GiteaRequestHeader = "X-Gitea-Event-Type" + GiteaTokenEnv = "GITEA_TOKEN" + ObsUserEnv = "OBS_USER" + ObsPasswordEnv = "OBS_PW" + + DefaultGitPrj = "_ObsPrj" + GiteaRequestHeader = "X-Gitea-Event-Type" Bot_BuildReview = "autogits_obs_staging_bot" ) - diff --git a/bots-common/obs_utils.go b/bots-common/obs_utils.go index 67d6d31..ef6aa45 100644 --- a/bots-common/obs_utils.go +++ b/bots-common/obs_utils.go @@ -16,7 +16,7 @@ type ObsClient struct { cookie string } -func NewObsClient(host, username, password string) (*ObsClient, error) { +func NewObsClient(host string) (*ObsClient, error) { baseUrl, err := url.Parse("https://" + host) if err != nil { return nil, err @@ -25,8 +25,8 @@ func NewObsClient(host, username, password string) (*ObsClient, error) { return &ObsClient{ baseUrl: baseUrl, client: &http.Client{}, - user: username, - password: password, + user: obsUser, + password: obsPassword, }, nil } @@ -40,6 +40,10 @@ type RepositoryMeta struct { } } +type Flags struct { + Contents string `xml:",innerxml"` +} + type ProjectMeta struct { XMLName xml.Name `xml:"project"` Name string `xml:"name,attr"` @@ -47,6 +51,9 @@ type ProjectMeta struct { Description string `xml:"description"` ScmSync string `xml:"xmlsync"` Repositories []Repository `xml:"repository"` + + BuildFlags Flags `xml:"build"` + PublicFlags Flags `xml:"publish"` } func parseProjectMeta(data []byte) (*ProjectMeta, error) { diff --git a/bots-common/obs_utils_test.go b/bots-common/obs_utils_test.go index b547281..30ae16b 100644 --- a/bots-common/obs_utils_test.go +++ b/bots-common/obs_utils_test.go @@ -8,10 +8,12 @@ func TestParseProjectMeta(t *testing.T) { res, err := parseProjectMeta([]byte(metaPrjData)) if err != nil { - return t.Fatal(err) + t.Fatal(err) } - if res. + if len(res.BuildFlags.Contents) < 1 { + t.Fatalf("Build flags are not as expected") + } } func TestParsingOfBuildResults(t *testing.T) { @@ -29,7 +31,7 @@ func TestParsingOfBuildResults(t *testing.T) { } if res.Result[0].Binaries[0].Package != "Nudoku" || - len(res.Result[0].Binaries[0].Binary) != 0 { + len(res.Result[0].Binaries[0].Binary) != 0 { t.Fatal(res.Result[0].Binaries[0]) } diff --git a/bots-common/tokens.go b/bots-common/tokens.go index 47481c7..3dff183 100644 --- a/bots-common/tokens.go +++ b/bots-common/tokens.go @@ -6,7 +6,8 @@ import ( "os" ) -var giteaToken, obsToken string +var giteaToken string +var obsUser, obsPassword string func RequireGiteaSecretToken() error { giteaToken = os.Getenv(GiteaTokenEnv) @@ -23,6 +24,23 @@ func RequireGiteaSecretToken() error { } func RequireObsSecretToken() error { + obsPassword = os.Getenv(ObsUserEnv) + obsUser = os.Getenv(ObsPasswordEnv) + + if len(obsPassword) < 10 || len(obsUser) < 2 { + return fmt.Errorf("Missing OBS authentication: %s %s", ObsUserEnv, ObsPasswordEnv) + } + + err := os.Setenv(ObsUserEnv, "") + if err != nil { + return fmt.Errorf("Cannot reset %s: %v", ObsUserEnv, err) + } + + err = os.Setenv(ObsPasswordEnv, "") + if err != nil { + return fmt.Errorf("Cannot reset %s: %v", ObsPasswordEnv, err) + } + return nil } diff --git a/obs-staging-bot/main.go b/obs-staging-bot/main.go index 3945eae..441bcd8 100644 --- a/obs-staging-bot/main.go +++ b/obs-staging-bot/main.go @@ -55,6 +55,12 @@ func processPullNotification(h *common.RequestHandler, notification *models.Noti h.Log(reviewer.UserName) } + obsClient, err := common.NewObsClient("api.opensuse.org") + if err != nil { + h.LogPlainError(err) + return + } + for _, review := range reviews { h.Log("state: %s, body: %s, id:%d\n", string(review.State), review.Body, review.ID) @@ -64,6 +70,7 @@ func processPullNotification(h *common.RequestHandler, notification *models.Noti switch review.State { case common.ReviewStateUnknown, common.ReviewStateRequestReview: + // create build project, if doesn't exist, and add it to pending requests case common.ReviewStatePending: // waiting for build results case common.ReviewStateApproved: @@ -109,13 +116,6 @@ func main() { pollWorkNotifications() - c, _ := common.NewObsClient("api.opensuse.org", "autogits_obs_staging_bot", "?E3N9']$YaC9~uR1") - status, err := c.BuildStatus("home:adamm") - if err != nil { - log.Printf("err: %v\n", err) - } else { - log.Print(*status) - } stuck := make(chan int) <-stuck