From f26a4855dda4636dc0849a72860ca3b053f81c6a Mon Sep 17 00:00:00 2001 From: Stephen Augustus Date: Sun, 29 Jan 2023 17:26:55 -0500 Subject: [PATCH] cmd/app: Scaffold GitHub application Signed-off-by: Stephen Augustus --- cmd/app/main.go | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 4 +-- 2 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 cmd/app/main.go diff --git a/cmd/app/main.go b/cmd/app/main.go new file mode 100644 index 0000000..99abdde --- /dev/null +++ b/cmd/app/main.go @@ -0,0 +1,79 @@ +// Copyright 2023 uwu-tools Authors +// +// 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. +// +// SPDX-License-Identifier: Apache-2.0 + +// Derived from https://github.com/airconduct/go-probot/blob/main/example/main.go. +package main + +import ( + "context" + + "github.com/airconduct/go-probot" + probotgh "github.com/airconduct/go-probot/github" + "github.com/google/go-github/v48/github" + "github.com/spf13/pflag" +) + +func main() { + app := probot.NewGithubAPP() + app.AddFlags(pflag.CommandLine) + pflag.Parse() + + // Add a handler for events "issue_comment.created" + app.On(probotgh.Event.IssueComment_created).WithHandler( + probotgh.IssueCommentHandler( + func(ctx probotgh.IssueCommentContext) { + payload := ctx.Payload() + ctx.Logger().Info("Get IssueComment event", "payload", payload) + owner := payload.Repo.Owner.GetLogin() + repo := payload.Repo.GetName() + issueNumber := *payload.Issue.Number + + // If any error happen, the error message will be logged and sent as response + ctx.Must( + ctx.Client().Issues.CreateComment( + ctx, owner, repo, issueNumber, &github.IssueComment{ + Body: github.String("Reply to this comment."), + }, + ), + ) + }, + ), + ) + + // Add a handler for multiple events + app.On( + probotgh.Event.PullRequest_opened, // pull_request.opened + probotgh.Event.PullRequest_edited, // pull_request.edited + probotgh.Event.PullRequest_synchronize, // pull_request.synchronize + probotgh.Event.PullRequest_labeled, // pull_request.labeled + probotgh.Event.PullRequest_assigned, // pull_request.assigned + ).WithHandler(probotgh.PullRequestHandler( + func(ctx probotgh.PullRequestContext) { + payload := ctx.Payload() + ctx.Logger().Info( + "Do something", + "action", + payload.GetAction(), + "PullRequest labels", + payload.PullRequest.Labels, + ) + }, + )) + + if err := app.Run(context.Background()); err != nil { + panic(err) + } +} diff --git a/go.mod b/go.mod index d74781e..63df67d 100644 --- a/go.mod +++ b/go.mod @@ -9,9 +9,11 @@ require ( github.com/caarlos0/env/v6 v6.10.1 github.com/ghodss/yaml v1.0.0 github.com/google/go-cmp v0.5.9 + github.com/google/go-github/v48 v48.2.0 github.com/sethvargo/go-githubactions v1.1.0 github.com/sirupsen/logrus v1.9.0 github.com/spf13/cobra v1.6.1 + github.com/spf13/pflag v1.0.5 k8s.io/apimachinery v0.24.10 k8s.io/test-infra v0.0.0-20230116172652-b51567b824d9 sigs.k8s.io/release-utils v0.7.3 @@ -68,7 +70,6 @@ require ( github.com/google/gnostic v0.5.7-v3refs // indirect github.com/google/go-containerregistry v0.8.1-0.20220216220642-00c59d91847c // indirect github.com/google/go-github/v29 v29.0.2 // indirect - github.com/google/go-github/v48 v48.2.0 // indirect github.com/google/go-querystring v1.1.0 // indirect github.com/google/gofuzz v1.2.1-0.20210504230335-f78f29fc09ea // indirect github.com/google/uuid v1.3.0 // indirect @@ -103,7 +104,6 @@ require ( github.com/sethvargo/go-envconfig v0.8.0 // indirect github.com/shurcooL/githubv4 v0.0.0-20221229060216-a8d4a561cc93 // indirect github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29 // indirect - github.com/spf13/pflag v1.0.5 // indirect github.com/tektoncd/pipeline v0.36.0 // indirect github.com/trivago/tgo v1.0.7 // indirect go.opencensus.io v0.23.0 // indirect