giteaops

package module
v1.7.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2025 License: GPL-3.0 Imports: 9 Imported by: 0

README

giteaops


Description

This package contains all of the gitea API calls to manage organizations, repositories and branches on a gitea server (gitea, not git) :

  • Organizations: Create, Delete, List
  • Repositories: Create, Delete, Transfer ownership, Clone, List
  • Branches: Create, Delete
  • Users: Create, Delete, List, Get User info, Generate Auth token, Change Passwd

A note about cloning repos :

There is no API calls to clone a gitea repo, therefore we rely on the git command to do so.

Installing the package

Pretty simple, as with most GO packages : go get github.com/jeanfrancoisgratton/giteaops from the same directory that you find go.mod

Usage

Pre-requisites

You must have a valid gitea auth token on the remote gitea server. As most of the API calls require admin privileges, create your token accordingly.

Package initialization

First, your client software must initialize the package with a command such as:

import (
	"github.com/jeanfrancoisgratton/giteaops"
    cerr "github.com/jeanfrancoisgratton/customError"
)

func InitManager (serverURL, authToken string) *cerr.CustomError {
    gm, err := giteaops.NewGiteaManager(serverURL, authToken)
    if err != nil {
        return &cerr.CustomError{Title: "Failed to create Gitea manager", Message: err.Error()}
    } else {
		return gm, nil
}

The two return values are thus:

  • gm: *GiteaManager, the entry point to the giteaops package
  • err: *cerr.CustomError, my custom wrapper around error (more on that later)
Example of operation: delete a repository

Let's put a working example where we initialize the GiteaManager and use it to call gitea's API and delete a repository

import (
	"github.com/jeanfrancoisgratton/giteaops"
	cerr "github.com/jeanfrancoisgratton/customError"
	"os"
)

func giteaDispatcher() {
	baseURL := "https://mygiteaserver:1234"
	token := "some_token"
	owner := "my_org_or_username"
	repo := "my_repo"
	
	if err := deleteRepo (baseURL, token, owner, repo); err != nil {
		fmt.Println(err.Error())
		os.Exit(0)
    }
}

// Simple deleteRepo()
func deleteRepo(baseURL, token, owner, repo string) *cerr.CustomError {
    var gm *GiteaManager
	var err *cerr.CustomError
	
	// initialize the package's manager
	if gm, err = giteaops.NewGiteaManager(serverURL, authToken); err != nil {
        return &cerr.CustomError{Title: "Failed to create Gitea manager", Message: err.Error()}
    }
	
	// Delete the repository
	return gm.DeleteRepository(owner, repo)
}

The code is quite simple: I've provided dummy values for the required variables, and have shown how simple handling errors with CustomError is.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChangeBranch added in v1.4.0

func ChangeBranch(branchName string) error

Switch to another branch

func CloneRepository added in v1.5.0

func CloneRepository(repoURL, targetDir string) *cerr.CustomError

CloneRepository shells out to the system's git client to clone a repo

func FindGitRoot added in v1.7.1

func FindGitRoot(startPath string) (string, error)

This is needed to find the directory where the .git/ directory sits

func GetCurrentBranch added in v1.4.0

func GetCurrentBranch() (string, *cerr.CustomError)

Find the current branch in the local workspace

Types

type GiteaManager

type GiteaManager struct {
	Client *gitea.Client
}

func NewGiteaManager

func NewGiteaManager(baseURL, token string) (*GiteaManager, *cerr.CustomError)

NewGiteaManager creates a new Gitea client

func (*GiteaManager) AddUserToOrganization added in v1.6.0

func (gm *GiteaManager) AddUserToOrganization(orgName, username string) *cerr.CustomError

AddUserToOrganization this is a work around the fact that there is not a real "AddUserToOrg" function in the SDK so far We have to rely on AddTeamMember for now.

func (*GiteaManager) ChangePassword added in v1.3.0

func (gm *GiteaManager) ChangePassword(username, newPassword string) *cerr.CustomError

There is no "non-admin" API calls to change a user password, there's only AdminEditUser, which requires an admin token

func (*GiteaManager) CreateBranch

func (gm *GiteaManager) CreateBranch(owner, repoName, newBranch, fromBranch string) (*gitea.Branch, *cerr.CustomError)

CreateBranch creates a new branch from an existing one Equivalent to git checkout fromBranch && git checkout -b newBranch

func (*GiteaManager) CreateOrganization

func (gm *GiteaManager) CreateOrganization(orgName, fullName, description string, visibility gitea.VisibleType) (*gitea.Organization, *cerr.CustomError)

CreateOrganization creates a new organization

func (*GiteaManager) CreateRepository

func (gm *GiteaManager) CreateRepository(owner, name, desc string, isPrivate, autoInit bool, defaultBranch string) (*gitea.Repository, *cerr.CustomError)

func (*GiteaManager) CreateUser

func (gm *GiteaManager) CreateUser(username, email, password, orgname string) (*gitea.User, *cerr.CustomError)

CreateUser creates a new user (admin only)

func (*GiteaManager) DeleteBranch

func (gm *GiteaManager) DeleteBranch(owner, repoName, branchName string) *cerr.CustomError

DeleteBranch deletes a branch

func (*GiteaManager) DeleteOrganization

func (gm *GiteaManager) DeleteOrganization(orgName string) *cerr.CustomError

DeleteOrganization deletes an organization (treated as user)

func (*GiteaManager) DeleteRepository

func (gm *GiteaManager) DeleteRepository(owner, repo string) *cerr.CustomError

func (*GiteaManager) DeleteUser

func (gm *GiteaManager) DeleteUser(username string) *cerr.CustomError

DeleteUser deletes a user (admin only)

func (*GiteaManager) DeleteUserFromOrganization added in v1.6.0

func (gm *GiteaManager) DeleteUserFromOrganization(orgName, username string) *cerr.CustomError

RemoteUserFromOrganization is the reverse of AddUserToOrganization

func (*GiteaManager) GenerateUserToken added in v1.2.0

func (gm *GiteaManager) GenerateUserToken(tokenName string, roToken bool) (*gitea.AccessToken, *cerr.CustomError)

GenerateUserToken will generate the token only for the currently logged in user TODO: we only generate ADMIN or READ-ONLY tokens for now, this needs fixing ASAP

func (*GiteaManager) GetUserInfo added in v1.2.0

func (gm *GiteaManager) GetUserInfo(username string) (*gitea.User, []string, *cerr.CustomError)

Get specific information about a given user we return the user type from the gitea SDK, but we need to also return a []string containing all the orgs the user belongs to, as it's not part of the gitea.User type

func (*GiteaManager) ListBranches added in v1.4.0

func (gm *GiteaManager) ListBranches(owner, repoName string) ([]*gitea.Branch, *cerr.CustomError)

ListBranches lists all branches on the server for a given user (org) and repo

func (*GiteaManager) ListOrganizations added in v1.2.0

func (gm *GiteaManager) ListOrganizations() ([]*gitea.Organization, *cerr.CustomError)

func (*GiteaManager) ListOrgsForUser added in v1.6.0

func (gm *GiteaManager) ListOrgsForUser(username string) ([]string, *cerr.CustomError)

Lists all orgs that a given user is part of

func (*GiteaManager) ListRepos added in v1.3.0

func (gm *GiteaManager) ListRepos(scope RepoScope, name string) ([]*gitea.Repository, *cerr.CustomError)

ListRepos lists all repos according to the chosen scope: user-level, org-level or global (server-wide)

func (*GiteaManager) ListUsers added in v1.2.0

func (gm *GiteaManager) ListUsers(org string) ([]*gitea.User, *cerr.CustomError)

ListUsers lists all users in a given organization, or globally

func (*GiteaManager) TransferRepository

func (gm *GiteaManager) TransferRepository(currentOwner, repoName, newOwner string) (*gitea.Repository, *cerr.CustomError)

type RepoScope added in v1.3.0

type RepoScope int

RepoScope manages at which level ListRepos operates: user level, organization level or global (server-wide)

const (
	ScopeUser RepoScope = iota
	ScopeOrg
	ScopeGlobal
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL