diff --git a/.github/workflows/build-deploy.yml b/.github/workflows/build-deploy.yml new file mode 100644 index 0000000..fc5017a --- /dev/null +++ b/.github/workflows/build-deploy.yml @@ -0,0 +1,68 @@ +name: Build & Deploy + +on: + push: + branches: + - main + workflow_dispatch: + schedule: + - cron: '10 17 * * 4' + +permissions: + id-token: write + contents: read + packages: write + +jobs: + package: + uses: Chia-Network/actions/.github/workflows/docker-build.yaml@main + + deploy: + name: Deploy ${{ matrix.mode.name }} + needs: + - package + runs-on: [k8s-public] + container: + image: registry.gitlab.com/cmmarslender/kubectl-helm:v3 + strategy: + fail-fast: false + matrix: + mode: + - name: label-prs + steps: + - uses: actions/checkout@v4 + + - name: Vault Login + uses: Chia-Network/actions/vault/login@main + with: + vault_url: ${{ secrets.VAULT_URL }} + role_name: github-github-bot + + - name: Get secrets from vault + uses: hashicorp/vault-action@v2 + with: + url: ${{ secrets.VAULT_URL }} + token: ${{ env.VAULT_TOKEN }} + secrets: | + secret/data/fmt/k8s/fmt-k8s-internal api_server_url | K8S_API_SERVER_URL; + secret/data/github_users/chiaautomation/github-bot token | BOT_GITHUB_TOKEN; + secret/data/github/teams internal | INTERNAL_TEAM_NAME; + + - name: Login to k8s cluster + uses: Chia-Network/actions/vault/k8s-login@main + with: + vault_url: ${{ secrets.VAULT_URL }} + vault_token: ${{ env.VAULT_TOKEN }} + backend_name: fmt-k8s-internal + role_name: github-actions + cluster_url: ${{ env.K8S_API_SERVER_URL }} + + - uses: Chia-Network/actions/helm/deploy@main + env: + DOCKER_TAG: "sha-${{ github.sha }}" + with: + namespace: "github-bot" + app_name: "github-bot-${{ matrix.mode.name }}" + helm_chart_repo: "https://chia-network.github.io/helm-charts" + helm_chart: "generic" + helm_values: "./k8s/${{ matrix.mode.name }}.yml" diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..fbb446c --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,14 @@ +name: Build Docker Images + +on: + pull_request: + workflow_call: + +concurrency: + # SHA is added to the end if on `main` to let all main workflows run + group: ${{ github.ref }}-${{ github.workflow }}-${{ github.event_name }}-${{ (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/heads/release/') || startsWith(github.ref, 'refs/heads/long_lived/')) && github.sha || '' }} + cancel-in-progress: true + +jobs: + package: + uses: Chia-Network/actions/.github/workflows/docker-build.yaml@main diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..45a84b3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM golang:1 as builder + +COPY . /app +WORKDIR /app +RUN make build + +FROM gcr.io/distroless/static-debian12 + +COPY --from=builder /app/bin/github-bot /github-bot + +ENTRYPOINT ["/github-bot", "--config", "/config.yml"] diff --git a/cmd/labelPRs.go b/cmd/labelPRs.go index 68266d0..15ce856 100644 --- a/cmd/labelPRs.go +++ b/cmd/labelPRs.go @@ -2,6 +2,7 @@ package cmd import ( "log" + "time" "github.com/google/go-github/v60/github" "github.com/spf13/cobra" @@ -21,9 +22,22 @@ var labelPRsCmd = &cobra.Command{ log.Fatalf("error loading config: %s\n", err.Error()) } client := github.NewClient(nil).WithAuthToken(cfg.GithubToken) - err = label.PullRequests(client, cfg.InternalTeam, cfg.LabelConfig) - if err != nil { - log.Fatalln(err.Error()) + + loop := viper.GetBool("loop") + loopDuration := viper.GetDuration("loop-time") + for { + log.Println("Labeling Pull Requests") + err = label.PullRequests(client, cfg.InternalTeam, cfg.LabelConfig) + if err != nil { + log.Fatalln(err.Error()) + } + + if !loop { + break + } + + log.Printf("Waiting %s for next iteration\n", loopDuration.String()) + time.Sleep(loopDuration) } }, } diff --git a/cmd/root.go b/cmd/root.go index 424d307..e12a607 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "strings" + "time" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -26,12 +27,20 @@ func Execute() { } func init() { - var cfgFile string + var ( + cfgFile string + loop bool + loopTime time.Duration + ) cobra.OnInitialize(initConfig) rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "config.yml", "config file to load") + rootCmd.PersistentFlags().BoolVar(&loop, "loop", false, "Use this var to periodically check on a loop") + rootCmd.PersistentFlags().DurationVar(&loopTime, "loop-time", 1*time.Hour, "The amount of time to wait between each iteration of the loop") cobra.CheckErr(viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))) + cobra.CheckErr(viper.BindPFlag("loop", rootCmd.PersistentFlags().Lookup("loop"))) + cobra.CheckErr(viper.BindPFlag("loop-time", rootCmd.PersistentFlags().Lookup("loop-time"))) } // initConfig reads in config file and ENV variables if set. diff --git a/k8s/label-prs.yml.j2 b/k8s/label-prs.yml.j2 new file mode 100644 index 0000000..65ca20b --- /dev/null +++ b/k8s/label-prs.yml.j2 @@ -0,0 +1,24 @@ +replicaCount: 1 +image: + repository: ghcr.io/chia-network/github-bot + tag: {{ DOCKER_TAG }} + +deployment: + args: + - label-prs + - --loop + +# Creates a secret with the following values, and mounts as a file into the main deployment container +secretFile: + mountPath: "/config.yml" + stringValues: + github_token: "{{ BOT_GITHUB_TOKEN }}" + internal_team: "{{ INTERNAL_TEAM_NAME }}" + label_internal: "" + label_external: "community-pr" + label_check_repos: + - name: "Chia-Network/chia-blockchain" + minimum_number: 17788 + label_skip_users: + - "dependabot[bot]" +