Skip to content

Commit

Permalink
add user activity tool with GraphQL
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Hiller <[email protected]>
  • Loading branch information
dhiller committed Dec 2, 2024
1 parent 012befe commit 2907ee1
Show file tree
Hide file tree
Showing 4 changed files with 410 additions and 0 deletions.
87 changes: 87 additions & 0 deletions generators/cmd/contributions/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* This file is part of the KubeVirt project
*
* 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.
*
* Copyright the KubeVirt Authors.
*
*/

package main

import (
"context"
"flag"
"fmt"
"github.com/shurcooL/githubv4"
log "github.com/sirupsen/logrus"
"golang.org/x/oauth2"
"gopkg.in/yaml.v3"
"os"
"strings"
)

type options struct {
org string
repo string
username string
githubTokenPath string
}

func gatherOptions() (options, error) {
o := options{}
fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
fs.StringVar(&o.org, "org", "kubevirt", "org name")
fs.StringVar(&o.repo, "repo", "", "repo name")
fs.StringVar(&o.username, "username", "", "github handle")
fs.StringVar(&o.githubTokenPath, "github-token", "/etc/github/oauth", "path to github token to use")
err := fs.Parse(os.Args[1:])
return o, err
}

func init() {
log.SetFormatter(&log.JSONFormatter{})
log.SetLevel(log.DebugLevel)
}

func main() {
opts, err := gatherOptions()
if err != nil {
log.Fatalf("error parsing arguments %v: %v", os.Args[1:], err)
}

token, err := os.ReadFile(opts.githubTokenPath)
if err != nil {
log.Fatalf("failed to use github token path %s: %v", opts.githubTokenPath, err)
}
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: strings.TrimSpace(string(token))},
)
httpClient := oauth2.NewClient(context.Background(), src)
graphqlClient := githubv4.NewClient(httpClient)

id, err := getUserId(graphqlClient, opts.username)
if err != nil {
log.Fatalf("failed to query: %v", err)
}
activity, err := generateUserActivity(graphqlClient, opts.org, opts.repo, opts.username, id)
if err != nil {
log.Fatalf("failed to query: %v", err)
}

out, err := yaml.Marshal(activity)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(out))
}
Loading

0 comments on commit 2907ee1

Please sign in to comment.