You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
5.5 KiB

package main
import (
"code.gitea.io/sdk/gitea"
"encoding/base64"
"fmt"
"os"
"time"
)
const (
endpoint = "https://try.gitea.io"
secret = "giteaSecret"
)
func generate(ch chan struct{}) {
defer func() {
ch <- struct{}{}
}()
token := os.Getenv("GITEA_TOKEN")
if token == "" {
fmt.Println("$GITEA_TOKEN required")
return
}
tokenAlt := os.Getenv("GITEA_TOKEN_ALT")
if tokenAlt == "" {
fmt.Println("$GITEA_TOKEN_ALT required")
return
}
webhookURL := os.Getenv("WEBHOOK_URL")
if webhookURL == "" {
fmt.Println("$WEBHOOK_URL required")
return
}
client, err := gitea.NewClient(endpoint, gitea.SetToken(token))
if err != nil {
panic(err)
}
clientAlt, err := gitea.NewClient(endpoint, gitea.SetToken(tokenAlt))
if err != nil {
panic(err)
}
altUser, _, err := clientAlt.GetMyUserInfo()
if err != nil {
panic(err)
}
// Create repo
repo, _, err := client.CreateRepo(gitea.CreateRepoOption{
Name: "gitea-webhook",
Private: true,
AutoInit: true,
Readme: "Default",
IssueLabels: "Default",
})
if err != nil {
panic(err)
}
// (defer) Delete repos
defer func() {
// Sleep to wait for webhooks to finish
time.Sleep(time.Second * 5)
if _, err := client.DeleteRepo(repo.Owner.UserName, repo.Name); err != nil {
fmt.Println("could not delete test repo")
}
if _, err := clientAlt.DeleteRepo(altUser.UserName, repo.Name); err != nil {
fmt.Println("could not delete alt test repo")
}
}()
// Create webhook
if _, _, err := client.CreateRepoHook(repo.Owner.UserName, repo.Name, gitea.CreateHookOption{
Type: gitea.HookTypeGitea,
Config: map[string]string{
"content_type": "json",
"url": webhookURL,
"secret": secret,
},
Events: []string{"push", "create", "pull_request", "issues", "delete", "fork", "repository", "release"},
Active: true,
}); err != nil {
panic(err)
}
milestone, _, err := client.CreateMilestone(repo.Owner.UserName, repo.Name, gitea.CreateMilestoneOption{
Title: "Milestone 1",
Description: "Milestone Description",
State: gitea.StateOpen,
})
if err != nil {
panic(err)
}
labels, _, err := client.ListRepoLabels(repo.Owner.UserName, repo.Name, gitea.ListLabelsOptions{})
if err != nil {
panic(err)
}
if len(labels) == 0 {
panic("no labels found")
}
if _, err := client.AddCollaborator(repo.Owner.UserName, repo.Name, altUser.UserName, gitea.AddCollaboratorOption{}); err != nil {
panic(err)
}
// Create
if _, _, err := client.CreateBranch(repo.Owner.UserName, repo.Name, gitea.CreateBranchOption{
BranchName: "main",
OldBranchName: "master",
}); err != nil {
panic(err)
}
// Delete
defer func() {
if _, _, err := client.DeleteRepoBranch(repo.Owner.UserName, repo.Name, "main"); err != nil {
panic(err)
}
}()
// Create file for PR later
if _, _, err := client.CreateFile(repo.Owner.UserName, repo.Name, "FILE.txt", gitea.CreateFileOptions{
FileOptions: gitea.FileOptions{
BranchName: "main",
},
Content: base64.StdEncoding.EncodeToString([]byte("New file!")),
}); err != nil {
panic(err)
}
// Fork
if _, _, err := clientAlt.CreateFork(repo.Owner.UserName, repo.Name, gitea.CreateForkOption{}); err != nil {
panic(err)
}
// Release
if _, _, err := client.CreateRelease(repo.Owner.UserName, repo.Name, gitea.CreateReleaseOption{
TagName: "release",
Target: repo.DefaultBranch,
Title: "Release 1",
Note: "Release Notes",
}); err != nil {
panic(err)
}
// Issue, assign, label, milestone
issue, _, err := client.CreateIssue(repo.Owner.UserName, repo.Name, gitea.CreateIssueOption{
Title: "Issue 1",
Body: "Issue Body",
Assignees: []string{repo.Owner.UserName},
Milestone: milestone.ID,
Labels: []int64{labels[0].ID},
})
if err != nil {
panic(err)
}
// Issue comment
if _, _, err := client.CreateIssueComment(repo.Owner.UserName, repo.Name, issue.Index, gitea.CreateIssueCommentOption{
Body: "Issue Comment",
}); err != nil {
panic(err)
}
// Pull request, assign, label, milestone
pr, _, err := client.CreatePullRequest(repo.Owner.UserName, repo.Name, gitea.CreatePullRequestOption{
Head: "main",
Base: "master",
Title: "Pull Request 1",
Body: "Pull Request Body",
Assignee: repo.Owner.UserName,
Assignees: nil,
Milestone: milestone.ID,
Labels: []int64{labels[0].ID},
})
if err != nil {
panic(err)
}
// PR comment
if _, _, err := client.CreateIssueComment(repo.Owner.UserName, repo.Name, pr.Index, gitea.CreateIssueCommentOption{
Body: "Pull Request Comment",
}); err != nil {
panic(err)
}
// PR review reject
if _, _, err := clientAlt.CreatePullReview(repo.Owner.UserName, repo.Name, pr.Index, gitea.CreatePullReviewOptions{
State: gitea.ReviewStateRequestChanges,
Body: "Please make changes",
}); err != nil {
panic(err)
}
// PR review approve
if _, _, err := clientAlt.CreatePullReview(repo.Owner.UserName, repo.Name, pr.Index, gitea.CreatePullReviewOptions{
State: gitea.ReviewStateApproved,
Body: "LGTM",
}); err != nil {
panic(err)
}
// FIXME? Pull request review comments don't show up
// PR sync
prFile, _, err := client.GetContents(repo.Owner.UserName, repo.Name, "main", "FILE.txt")
if err != nil {
panic(err)
}
if _, _, err := client.UpdateFile(repo.Owner.UserName, repo.Name, "FILE.txt", gitea.UpdateFileOptions{
FileOptions: gitea.FileOptions{
BranchName: "main",
},
SHA: prFile.SHA,
Content: base64.StdEncoding.EncodeToString([]byte("New updated file!")),
}); err != nil {
panic(err)
}
}