mirror of
				https://codeberg.org/forgejo/forgejo.git
				synced 2025-10-30 22:11:07 +00:00 
			
		
		
		
	- Massive replacement of changing `code.gitea.io/gitea` to `forgejo.org`. - Resolves forgejo/discussions#258 Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7337 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Reviewed-by: Michael Kriese <michael.kriese@gmx.de> Reviewed-by: Beowulf <beowulf@beocode.eu> Reviewed-by: Panagiotis "Ivory" Vasilopoulos <git@n0toose.net> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
		
			
				
	
	
		
			123 lines
		
	
	
	
		
			3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			123 lines
		
	
	
	
		
			3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright 2023 The Forgejo Authors. All rights reserved.
 | |
| // SPDX-License-Identifier: MIT
 | |
| 
 | |
| package forgefed
 | |
| 
 | |
| import (
 | |
| 	"net/url"
 | |
| 
 | |
| 	"forgejo.org/modules/validation"
 | |
| 
 | |
| 	"github.com/valyala/fastjson"
 | |
| )
 | |
| 
 | |
| // ToDo: Search for full text SourceType and Source, also in .md files
 | |
| type (
 | |
| 	SoftwareNameType string
 | |
| )
 | |
| 
 | |
| const (
 | |
| 	ForgejoSourceType SoftwareNameType = "forgejo"
 | |
| 	GiteaSourceType   SoftwareNameType = "gitea"
 | |
| )
 | |
| 
 | |
| var KnownSourceTypes = []any{
 | |
| 	ForgejoSourceType, GiteaSourceType,
 | |
| }
 | |
| 
 | |
| // ------------------------------------------------ NodeInfoWellKnown ------------------------------------------------
 | |
| 
 | |
| // NodeInfo data type
 | |
| // swagger:model
 | |
| type NodeInfoWellKnown struct {
 | |
| 	Href string
 | |
| }
 | |
| 
 | |
| // Factory function for NodeInfoWellKnown. Created struct is asserted to be valid.
 | |
| func NewNodeInfoWellKnown(body []byte) (NodeInfoWellKnown, error) {
 | |
| 	result, err := NodeInfoWellKnownUnmarshalJSON(body)
 | |
| 	if err != nil {
 | |
| 		return NodeInfoWellKnown{}, err
 | |
| 	}
 | |
| 
 | |
| 	if valid, err := validation.IsValid(result); !valid {
 | |
| 		return NodeInfoWellKnown{}, err
 | |
| 	}
 | |
| 
 | |
| 	return result, nil
 | |
| }
 | |
| 
 | |
| func NodeInfoWellKnownUnmarshalJSON(data []byte) (NodeInfoWellKnown, error) {
 | |
| 	p := fastjson.Parser{}
 | |
| 	val, err := p.ParseBytes(data)
 | |
| 	if err != nil {
 | |
| 		return NodeInfoWellKnown{}, err
 | |
| 	}
 | |
| 	href := string(val.GetStringBytes("links", "0", "href"))
 | |
| 	return NodeInfoWellKnown{Href: href}, nil
 | |
| }
 | |
| 
 | |
| // Validate collects error strings in a slice and returns this
 | |
| func (node NodeInfoWellKnown) Validate() []string {
 | |
| 	var result []string
 | |
| 	result = append(result, validation.ValidateNotEmpty(node.Href, "Href")...)
 | |
| 
 | |
| 	parsedURL, err := url.Parse(node.Href)
 | |
| 	if err != nil {
 | |
| 		result = append(result, err.Error())
 | |
| 		return result
 | |
| 	}
 | |
| 
 | |
| 	if parsedURL.Host == "" {
 | |
| 		result = append(result, "Href has to be absolute")
 | |
| 	}
 | |
| 
 | |
| 	result = append(result, validation.ValidateOneOf(parsedURL.Scheme, []any{"http", "https"}, "parsedURL.Scheme")...)
 | |
| 
 | |
| 	if parsedURL.RawQuery != "" {
 | |
| 		result = append(result, "Href may not contain query")
 | |
| 	}
 | |
| 
 | |
| 	return result
 | |
| }
 | |
| 
 | |
| // ------------------------------------------------ NodeInfo ------------------------------------------------
 | |
| 
 | |
| // NodeInfo data type
 | |
| // swagger:model
 | |
| type NodeInfo struct {
 | |
| 	SoftwareName SoftwareNameType
 | |
| }
 | |
| 
 | |
| func NodeInfoUnmarshalJSON(data []byte) (NodeInfo, error) {
 | |
| 	p := fastjson.Parser{}
 | |
| 	val, err := p.ParseBytes(data)
 | |
| 	if err != nil {
 | |
| 		return NodeInfo{}, err
 | |
| 	}
 | |
| 	source := string(val.GetStringBytes("software", "name"))
 | |
| 	result := NodeInfo{}
 | |
| 	result.SoftwareName = SoftwareNameType(source)
 | |
| 	return result, nil
 | |
| }
 | |
| 
 | |
| func NewNodeInfo(body []byte) (NodeInfo, error) {
 | |
| 	result, err := NodeInfoUnmarshalJSON(body)
 | |
| 	if err != nil {
 | |
| 		return NodeInfo{}, err
 | |
| 	}
 | |
| 
 | |
| 	if valid, err := validation.IsValid(result); !valid {
 | |
| 		return NodeInfo{}, err
 | |
| 	}
 | |
| 	return result, nil
 | |
| }
 | |
| 
 | |
| // Validate collects error strings in a slice and returns this
 | |
| func (node NodeInfo) Validate() []string {
 | |
| 	var result []string
 | |
| 	result = append(result, validation.ValidateNotEmpty(string(node.SoftwareName), "node.SoftwareName")...)
 | |
| 	result = append(result, validation.ValidateOneOf(node.SoftwareName, KnownSourceTypes, "node.SoftwareName")...)
 | |
| 
 | |
| 	return result
 | |
| }
 |