Skip to content

Commit

Permalink
cmd/app: Scaffold GitHub application
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Augustus <[email protected]>
  • Loading branch information
justaugustus committed Jan 29, 2023
1 parent ba15589 commit f26a485
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
79 changes: 79 additions & 0 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f26a485

Please sign in to comment.