From bd0af4d51a6de0b525687f6056bef8a7158b646e Mon Sep 17 00:00:00 2001 From: Aleksandr Razumov Date: Mon, 9 Dec 2024 12:15:28 +0300 Subject: [PATCH] fix: use workflow runs --- .github/workflows/e2e.yml | 2 ++ _oas/openapi.yaml | 16 +++++++---- integration/integration_test.go | 13 +++++---- internal/api/handler.go | 4 +-- internal/oas/oas_faker_gen.go | 12 +++++++- internal/oas/oas_json_gen.go | 50 +++++++++++++++++++++++++++------ internal/oas/oas_schemas_gen.go | 36 +++++++++++++++++++----- 7 files changed, 104 insertions(+), 29 deletions(-) diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index 41ab881..9a4b2d3 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -43,4 +43,6 @@ jobs: E2E: "1" GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_JOB_ID: ${{ github.job }} + GITHUB_RUN_ID: ${{ github.run_id }} + GITHUB_RUN_ATTEMPT: ${{ github.run_attempt }} run: go test -timeout 15m -v -run TestIntegration ./... diff --git a/_oas/openapi.yaml b/_oas/openapi.yaml index 16ea2e5..b140793 100644 --- a/_oas/openapi.yaml +++ b/_oas/openapi.yaml @@ -83,9 +83,9 @@ paths: - repo_owner - repo_name - commit_sha - - job_id - - job_run_id - - job_run_number + - job + - run_id + - run_attempt properties: repo_owner: type: string @@ -95,10 +95,14 @@ paths: type: string description: "Repository name" example: "repo" - job_id: - type: integer + job: + type: string description: "Job ID" - example: 123456789 + run_id: + type: integer + format: int64 + run_attempt: + type: integer responses: 200: description: "Telegram account acquired" diff --git a/integration/integration_test.go b/integration/integration_test.go index 33e97fe..413ea34 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -81,8 +81,9 @@ func TestIntegration(t *testing.T) { t.Skip("E2E=1 not set") } - jobID, err := strconv.Atoi(os.Getenv("GITHUB_JOB_ID")) - require.NoError(t, err) + jobID := os.Getenv("GITHUB_JOB_ID") + runID, _ := strconv.ParseInt(os.Getenv("GITHUB_RUN_ID"), 10, 64) + attempt, _ := strconv.Atoi(os.Getenv("GITHUB_RUN_ATTEMPT")) ctx := context.Background() client, err := oas.NewClient("https://bot.gotd.dev", securitySource{}) @@ -94,9 +95,11 @@ func TestIntegration(t *testing.T) { res, err := backoff.RetryNotifyWithData(func() (*oas.AcquireTelegramAccountOK, error) { return client.AcquireTelegramAccount(ctx, &oas.AcquireTelegramAccountReq{ - RepoOwner: "gotd", - RepoName: "bot", - JobID: jobID, + RepoOwner: "gotd", + RepoName: "bot", + RunID: runID, + Job: jobID, + RunAttempt: attempt, }) }, bo, func(err error, duration time.Duration) { t.Logf("Error: %v, retrying in %v", err, duration) diff --git a/internal/api/handler.go b/internal/api/handler.go index 72777df..79c905b 100644 --- a/internal/api/handler.go +++ b/internal/api/handler.go @@ -33,13 +33,13 @@ func (h Handler) AcquireTelegramAccount(ctx context.Context, req *oas.AcquireTel if err != nil { return nil, errors.Wrap(err, "get repo") } - job, _, err := client.Actions.GetWorkflowJobByID(ctx, req.RepoOwner, req.RepoName, int64(req.JobID)) + wr, _, err := client.Actions.GetWorkflowRunByID(ctx, req.RepoOwner, req.RepoName, req.RunID) if err != nil { return nil, errors.Wrap(err, "get job") } zctx.From(ctx).Info("AcquireTelegramAccount", zap.String("repo", repo.GetFullName()), - zap.String("job", job.GetName()), + zap.String("run", wr.GetName()), ) lease, err := h.manager.Acquire() diff --git a/internal/oas/oas_faker_gen.go b/internal/oas/oas_faker_gen.go index 7bcc8e1..4739b6c 100644 --- a/internal/oas/oas_faker_gen.go +++ b/internal/oas/oas_faker_gen.go @@ -36,7 +36,17 @@ func (s *AcquireTelegramAccountReq) SetFake() { } { { - s.JobID = int(0) + s.Job = "string" + } + } + { + { + s.RunID = int64(0) + } + } + { + { + s.RunAttempt = int(0) } } } diff --git a/internal/oas/oas_json_gen.go b/internal/oas/oas_json_gen.go index b76d977..76e401d 100644 --- a/internal/oas/oas_json_gen.go +++ b/internal/oas/oas_json_gen.go @@ -142,15 +142,25 @@ func (s *AcquireTelegramAccountReq) encodeFields(e *jx.Encoder) { e.Str(s.RepoName) } { - e.FieldStart("job_id") - e.Int(s.JobID) + e.FieldStart("job") + e.Str(s.Job) + } + { + e.FieldStart("run_id") + e.Int64(s.RunID) + } + { + e.FieldStart("run_attempt") + e.Int(s.RunAttempt) } } -var jsonFieldsNameOfAcquireTelegramAccountReq = [3]string{ +var jsonFieldsNameOfAcquireTelegramAccountReq = [5]string{ 0: "repo_owner", 1: "repo_name", - 2: "job_id", + 2: "job", + 3: "run_id", + 4: "run_attempt", } // Decode decodes AcquireTelegramAccountReq from json. @@ -186,17 +196,41 @@ func (s *AcquireTelegramAccountReq) Decode(d *jx.Decoder) error { }(); err != nil { return errors.Wrap(err, "decode field \"repo_name\"") } - case "job_id": + case "job": requiredBitSet[0] |= 1 << 2 + if err := func() error { + v, err := d.Str() + s.Job = string(v) + if err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"job\"") + } + case "run_id": + requiredBitSet[0] |= 1 << 3 + if err := func() error { + v, err := d.Int64() + s.RunID = int64(v) + if err != nil { + return err + } + return nil + }(); err != nil { + return errors.Wrap(err, "decode field \"run_id\"") + } + case "run_attempt": + requiredBitSet[0] |= 1 << 4 if err := func() error { v, err := d.Int() - s.JobID = int(v) + s.RunAttempt = int(v) if err != nil { return err } return nil }(); err != nil { - return errors.Wrap(err, "decode field \"job_id\"") + return errors.Wrap(err, "decode field \"run_attempt\"") } default: return d.Skip() @@ -208,7 +242,7 @@ func (s *AcquireTelegramAccountReq) Decode(d *jx.Decoder) error { // Validate required fields. var failures []validate.FieldError for i, mask := range [1]uint8{ - 0b00000111, + 0b00011111, } { if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { // Mask only required fields and check equality to mask using XOR. diff --git a/internal/oas/oas_schemas_gen.go b/internal/oas/oas_schemas_gen.go index c9f1add..d686e4f 100644 --- a/internal/oas/oas_schemas_gen.go +++ b/internal/oas/oas_schemas_gen.go @@ -45,7 +45,9 @@ type AcquireTelegramAccountReq struct { // Repository name. RepoName string `json:"repo_name"` // Job ID. - JobID int `json:"job_id"` + Job string `json:"job"` + RunID int64 `json:"run_id"` + RunAttempt int `json:"run_attempt"` } // GetRepoOwner returns the value of RepoOwner. @@ -58,9 +60,19 @@ func (s *AcquireTelegramAccountReq) GetRepoName() string { return s.RepoName } -// GetJobID returns the value of JobID. -func (s *AcquireTelegramAccountReq) GetJobID() int { - return s.JobID +// GetJob returns the value of Job. +func (s *AcquireTelegramAccountReq) GetJob() string { + return s.Job +} + +// GetRunID returns the value of RunID. +func (s *AcquireTelegramAccountReq) GetRunID() int64 { + return s.RunID +} + +// GetRunAttempt returns the value of RunAttempt. +func (s *AcquireTelegramAccountReq) GetRunAttempt() int { + return s.RunAttempt } // SetRepoOwner sets the value of RepoOwner. @@ -73,9 +85,19 @@ func (s *AcquireTelegramAccountReq) SetRepoName(val string) { s.RepoName = val } -// SetJobID sets the value of JobID. -func (s *AcquireTelegramAccountReq) SetJobID(val int) { - s.JobID = val +// SetJob sets the value of Job. +func (s *AcquireTelegramAccountReq) SetJob(val string) { + s.Job = val +} + +// SetRunID sets the value of RunID. +func (s *AcquireTelegramAccountReq) SetRunID(val int64) { + s.RunID = val +} + +// SetRunAttempt sets the value of RunAttempt. +func (s *AcquireTelegramAccountReq) SetRunAttempt(val int) { + s.RunAttempt = val } // Error occurred while processing request.