implement voting system in aur.archlinux.org
This commit is contained in:
parent
916961580a
commit
6003e84621
8 changed files with 187 additions and 13 deletions
|
|
@ -114,6 +114,9 @@ Then run `sudo pacman -Sy` to sync.
|
|||
| `BUILD_IMAGE` | `aur-buildimage:latest` | Docker image used by the Docker agent runner |
|
||||
| `REPO_NAME` | `aur-custom` | Pacman repository name |
|
||||
| `REPO_URL` | — | Public base URL for the repo (used in dashboard snippet) |
|
||||
| `AUR_USERNAME` | — | aur.archlinux.org username (used for auto-vote) |
|
||||
| `AUR_PASSWORD` | — | aur.archlinux.org password |
|
||||
| `AUR_AUTO_VOTE` | `false` | Set to `true` to vote for AUR packages after a successful build |
|
||||
| `OIDC_ISSUER` | — | OIDC issuer URL (leave empty to disable) |
|
||||
| `OIDC_CLIENT_ID` | — | OIDC client ID |
|
||||
| `OIDC_CLIENT_SECRET` | — | OIDC client secret |
|
||||
|
|
|
|||
|
|
@ -9,10 +9,12 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"jonasled.dev/jonasled/go-libs/log"
|
||||
"jonasled.dev/jonasled/k8s-aur-builder/database"
|
||||
"jonasled.dev/jonasled/k8s-aur-builder/internal/aur"
|
||||
"jonasled.dev/jonasled/k8s-aur-builder/internal/logbus"
|
||||
"jonasled.dev/jonasled/k8s-aur-builder/internal/registry"
|
||||
"jonasled.dev/jonasled/k8s-aur-builder/models"
|
||||
|
|
@ -260,6 +262,27 @@ func persistSuccessResult(build *models.Build, req *UpdateJobRequest) {
|
|||
}
|
||||
database.Db.Create(&pf)
|
||||
}
|
||||
go autoVoteAUR(build.PackageID)
|
||||
}
|
||||
|
||||
// autoVoteAUR votes for the AUR package behind a successfully built package
|
||||
// when AUR_AUTO_VOTE is enabled and AUR credentials are configured.
|
||||
func autoVoteAUR(packageID uuid.UUID) {
|
||||
if settingOr("AUR_AUTO_VOTE", "false") != "true" {
|
||||
return
|
||||
}
|
||||
var pkg models.Package
|
||||
if err := database.Db.First(&pkg, queryByID, packageID).Error; err != nil {
|
||||
return
|
||||
}
|
||||
if pkg.SourceType != models.SourceTypeAUR || pkg.AURName == "" {
|
||||
return
|
||||
}
|
||||
if err := aur.Vote(settingOr("AUR_USERNAME", ""), settingOr("AUR_PASSWORD", ""), pkg.AURName); err != nil {
|
||||
log.Log.Warnf("AUR auto-vote for %s failed: %v", pkg.AURName, err)
|
||||
return
|
||||
}
|
||||
log.Log.Infof("Voted for AUR package %s", pkg.AURName)
|
||||
}
|
||||
|
||||
func DownloadSource(c *gin.Context) {
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import (
|
|||
|
||||
"github.com/gin-gonic/gin"
|
||||
"jonasled.dev/jonasled/k8s-aur-builder/database"
|
||||
"jonasled.dev/jonasled/k8s-aur-builder/internal/aur"
|
||||
"jonasled.dev/jonasled/k8s-aur-builder/internal/auth"
|
||||
k8sclient "jonasled.dev/jonasled/k8s-aur-builder/internal/k8s"
|
||||
"jonasled.dev/jonasled/k8s-aur-builder/internal/registry"
|
||||
|
|
@ -94,6 +95,22 @@ func TestK8s(c *gin.Context) {
|
|||
c.JSON(http.StatusOK, gin.H{"message": msg})
|
||||
}
|
||||
|
||||
// TestAUR godoc
|
||||
// @Summary Test the configured aur.archlinux.org credentials (admin only)
|
||||
// @Tags settings
|
||||
// @Security BearerAuth
|
||||
// @Produce json
|
||||
// @Success 200 {object} map[string]string
|
||||
// @Failure 500 {object} map[string]string
|
||||
// @Router /settings/test/aur [post]
|
||||
func TestAUR(c *gin.Context) {
|
||||
if err := aur.TestLogin(settingOr("AUR_USERNAME", ""), settingOr("AUR_PASSWORD", "")); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"message": "AUR login successful"})
|
||||
}
|
||||
|
||||
// CleanupArtifacts godoc
|
||||
// @Summary Delete redundant package files, keeping only the latest per package/arch (admin only)
|
||||
// @Tags settings
|
||||
|
|
|
|||
|
|
@ -123,6 +123,7 @@ func NewRouter() *gin.Engine {
|
|||
settingsGroup.PUT("", handlers.UpdateSettings)
|
||||
settingsGroup.POST("/test/s3", handlers.TestS3)
|
||||
settingsGroup.POST("/test/k8s", handlers.TestK8s)
|
||||
settingsGroup.POST("/test/aur", handlers.TestAUR)
|
||||
settingsGroup.POST("/maintenance/cleanup", handlers.CleanupArtifacts)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ const aurRPCBase = "https://aur.archlinux.org/rpc/v5"
|
|||
|
||||
type PackageInfo struct {
|
||||
Name string `json:"Name"`
|
||||
PackageBase string `json:"PackageBase"`
|
||||
Version string `json:"Version"`
|
||||
Description string `json:"Description"`
|
||||
URLPath string `json:"URLPath"`
|
||||
|
|
|
|||
96
backend/internal/aur/vote.go
Normal file
96
backend/internal/aur/vote.go
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
package aur
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const aurWebBase = "https://aur.archlinux.org"
|
||||
|
||||
// newSession logs in to aur.archlinux.org and returns an HTTP client holding
|
||||
// the resulting AURSID session cookie. Redirects are not followed so the
|
||||
// response to each request can be inspected.
|
||||
func newSession(username, password string) (*http.Client, error) {
|
||||
if username == "" || password == "" {
|
||||
return nil, errors.New("AUR username and password are not configured")
|
||||
}
|
||||
|
||||
jar, err := cookiejar.New(nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
client := &http.Client{
|
||||
Jar: jar,
|
||||
Timeout: 30 * time.Second,
|
||||
CheckRedirect: func(*http.Request, []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
},
|
||||
}
|
||||
|
||||
resp, err := client.PostForm(aurWebBase+"/login", url.Values{
|
||||
"user": {username},
|
||||
"passwd": {password},
|
||||
"next": {"/"},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("AUR login request failed: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// A successful login redirects (303) and sets the AURSID cookie; a failed
|
||||
// one re-renders the login form with status 200 and no session cookie.
|
||||
base, _ := url.Parse(aurWebBase)
|
||||
for _, c := range jar.Cookies(base) {
|
||||
if c.Name == "AURSID" {
|
||||
return client, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("AUR login failed: invalid username or password")
|
||||
}
|
||||
|
||||
// TestLogin verifies that the given credentials can log in to the AUR.
|
||||
func TestLogin(username, password string) error {
|
||||
_, err := newSession(username, password)
|
||||
return err
|
||||
}
|
||||
|
||||
// Vote logs in with the given credentials and votes for the AUR package. It is
|
||||
// idempotent: voting for a package that is already voted for is not an error.
|
||||
func Vote(username, password, aurName string) error {
|
||||
info, err := FetchPackageInfo(aurName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
base := info.PackageBase
|
||||
if base == "" {
|
||||
base = info.Name
|
||||
}
|
||||
|
||||
client, err := newSession(username, password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp, err := client.PostForm(
|
||||
fmt.Sprintf("%s/pkgbase/%s/vote", aurWebBase, url.PathEscape(base)),
|
||||
url.Values{},
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("AUR vote request failed: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode >= 400 {
|
||||
return fmt.Errorf("AUR vote for %q failed: HTTP %d", base, resp.StatusCode)
|
||||
}
|
||||
// An expired/invalid session is bounced to the login page.
|
||||
if strings.HasPrefix(resp.Header.Get("Location"), "/login") {
|
||||
return fmt.Errorf("AUR vote for %q failed: not logged in", base)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -5,6 +5,7 @@ export const updateSettings = (data: Record<string, string>) =>
|
|||
api.put<Record<string, string>>('/settings', data).then((r) => r.data);
|
||||
export const testS3 = () => api.post<{ message: string }>('/settings/test/s3').then((r) => r.data);
|
||||
export const testK8s = () => api.post<{ message: string }>('/settings/test/k8s').then((r) => r.data);
|
||||
export const testAUR = () => api.post<{ message: string }>('/settings/test/aur').then((r) => r.data);
|
||||
|
||||
export interface RepoConfig {
|
||||
repo_name: string;
|
||||
|
|
|
|||
|
|
@ -7,12 +7,14 @@ import TextField from '@mui/material/TextField';
|
|||
import Button from '@mui/material/Button';
|
||||
import Alert from '@mui/material/Alert';
|
||||
import Divider from '@mui/material/Divider';
|
||||
import FormControlLabel from '@mui/material/FormControlLabel';
|
||||
import Switch from '@mui/material/Switch';
|
||||
import CircularProgress from '@mui/material/CircularProgress';
|
||||
import LoadingButton from '@mui/lab/LoadingButton';
|
||||
import CheckCircleOutlineIcon from '@mui/icons-material/CheckCircleOutlined';
|
||||
import ErrorOutlineIcon from '@mui/icons-material/ErrorOutlined';
|
||||
import { useQuery, useMutation } from '@tanstack/react-query';
|
||||
import { getSettings, updateSettings, testS3, testK8s, cleanupArtifacts } from '../api/settings';
|
||||
import { getSettings, updateSettings, testS3, testK8s, testAUR, cleanupArtifacts } from '../api/settings';
|
||||
|
||||
type TestState = { status: 'idle' | 'loading' | 'ok' | 'error'; message: string };
|
||||
|
||||
|
|
@ -41,6 +43,7 @@ function useTestButton(fn: () => Promise<{ message: string }>) {
|
|||
|
||||
const SETTING_GROUPS: {
|
||||
title: string;
|
||||
description?: string;
|
||||
testFn?: () => Promise<{ message: string }>;
|
||||
keys: { key: string; label: string; type: string; placeholder?: string }[];
|
||||
}[] = [
|
||||
|
|
@ -69,6 +72,17 @@ const SETTING_GROUPS: {
|
|||
{ key: 'REPO_NAME', label: 'Pacman Repository Name', type: 'text', placeholder: 'aur-custom' },
|
||||
],
|
||||
},
|
||||
{
|
||||
title: 'AUR Account',
|
||||
description:
|
||||
'Credentials for aur.archlinux.org. When auto-vote is enabled, every AUR package is voted for after a successful build.',
|
||||
testFn: testAUR,
|
||||
keys: [
|
||||
{ key: 'AUR_USERNAME', label: 'AUR Username', type: 'text' },
|
||||
{ key: 'AUR_PASSWORD', label: 'AUR Password', type: 'password' },
|
||||
{ key: 'AUR_AUTO_VOTE', label: 'Automatically vote for built AUR packages', type: 'boolean' },
|
||||
],
|
||||
},
|
||||
{
|
||||
title: 'OIDC',
|
||||
keys: [
|
||||
|
|
@ -122,19 +136,37 @@ function SettingsGroup({
|
|||
)}
|
||||
</Box>
|
||||
<Divider sx={{ mb: 2 }} />
|
||||
{group.description && (
|
||||
<Typography variant="body2" sx={{ color: 'text.secondary', mb: 2 }}>
|
||||
{group.description}
|
||||
</Typography>
|
||||
)}
|
||||
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 2 }}>
|
||||
{group.keys.map(({ key, label, type, placeholder }) => (
|
||||
<TextField
|
||||
key={key}
|
||||
label={label}
|
||||
type={type}
|
||||
value={values[key] ?? ''}
|
||||
onChange={(e) => onChange(key, e.target.value)}
|
||||
placeholder={placeholder}
|
||||
fullWidth
|
||||
size="small"
|
||||
/>
|
||||
))}
|
||||
{group.keys.map(({ key, label, type, placeholder }) =>
|
||||
type === 'boolean' ? (
|
||||
<FormControlLabel
|
||||
key={key}
|
||||
label={label}
|
||||
control={
|
||||
<Switch
|
||||
checked={values[key] === 'true'}
|
||||
onChange={(e) => onChange(key, e.target.checked ? 'true' : 'false')}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
) : (
|
||||
<TextField
|
||||
key={key}
|
||||
label={label}
|
||||
type={type}
|
||||
value={values[key] ?? ''}
|
||||
onChange={(e) => onChange(key, e.target.value)}
|
||||
placeholder={placeholder}
|
||||
fullWidth
|
||||
size="small"
|
||||
/>
|
||||
),
|
||||
)}
|
||||
</Box>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue