feat(all_files): adding plugin

Signed-off-by: Mark Sharpley <698889+markopolo123@users.noreply.github.com>
This commit is contained in:
Mark Sharpley 2022-10-14 20:57:13 +01:00
parent e08655b158
commit 30383e0c79
No known key found for this signature in database
6 changed files with 206 additions and 1 deletions

View file

@ -0,0 +1,46 @@
pipeline:
lint-go:
image: golangci/golangci-lint
group: lint-build
commands:
- golangci-lint run *.go --enable=gofumpt
when:
path: "*.go"
lint-docker:
image: hadolint/hadolint:latest-debian
group: lint-build
commands:
- hadolint --ignore DL3003 Dockerfile
when:
path: "Dockerfile"
build-go:
image: golang
group: lint-build
commands:
- go build
when:
path: ["Dockerfile", "*.go"]
build-docker-pr:
image: plugins/kaniko
settings:
repo: mcs94/gitea-comment
tags: latest
dockerfile: Dockerfile
no_push: true
when:
event: pull_request
branch: main
path: ["Dockerfile", "*.go"]
build-docker:
image: plugins/kaniko
settings:
repo: mcs94/gitea-comment
tags: latest
dockerfile: Dockerfile
username: mcs94
password:
from_secret: docker_password
when:
event: [push, tag]
branch: main
path: ["Dockerfile", "*.go"]

15
Dockerfile Normal file
View file

@ -0,0 +1,15 @@
# syntax=docker/dockerfile:1
## Build
FROM golang:1.19.2-alpine3.16 AS build
WORKDIR /app
COPY go.mod ./
# COPY go.sum ./
RUN go mod download
COPY *.go ./
RUN go build -o /main
## Deploy
FROM alpine:3.16.2
WORKDIR /
COPY --from=build /main /main
ENTRYPOINT ["/main"]

View file

@ -1,2 +1,48 @@
# gitea-comment-plugin
Woodpecker Plugin for Gitea PR Comments
A Woodpecker plugin to post comments onto a Gitea Pull Request.
Note this currently only works on `pull request` events.
## Usage/Examples
```yaml
pipeline:
comment:
image: mcs94/gitea-comment
settings:
gitea_address: https://gitea.url.goes.here
gitea_token:
from_secret: gitea_token
comment: >
✅ Build ${CI_BUILD_EVENT} of `${CI_REPO_NAME}` has status `${CI_BUILD_STATUS}`.
📝 Commit by ${CI_COMMIT_AUTHOR} on `${CI_COMMIT_BRANCH}`:
`${CI_COMMIT_MESSAGE}`
🌐 ${CI_BUILD_LINK}
when:
event: [pull_request]
```
Produces something with looks like the screenshot below on pull requests:
![comments](img/comments.png)
## Authors
* [@markopolo123](https://www.github.com/markopolo123)
## Running just the container
```bash
docker run \
-e PLUGIN_COMMENT="test comment" \
-e PLUGIN_GITEA_TOKEN="tokenhere" \
-e PLUGIN_GITEA_ADDRESS="https://gitea.url.here" \
-e CI_REPO_OWNER="repoowwer" \
-e CI_REPO_NAME="yourrepo" \
-e CI_COMMIT_PULL_REQUEST=8 \
test-gitea
```

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module gitea-comment
go 1.19

BIN
img/comments.png Normal file

Binary file not shown.

After

(image error) Size: 633 KiB

95
main.go Normal file
View file

@ -0,0 +1,95 @@
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"strconv"
)
type Payload struct {
Body string `json:"body"`
}
func LookupEnvOrString(key string, defaultVal string) string {
if val, ok := os.LookupEnv(key); ok {
return val
}
return defaultVal
}
func LookupEnvOrInt(key string, defaultVal int) int {
if val, ok := os.LookupEnv(key); ok {
v, err := strconv.Atoi(val)
if err != nil {
log.Fatalf("LookupEnvOrInt[%s]: %v", key, err)
}
return v
}
return defaultVal
}
func main() {
var giteaToken string
var giteaAddress string
var comment string
var repoOwner string
var repoName string
var prIndex int
flag.StringVar(&giteaToken, "gitea-token", LookupEnvOrString("PLUGIN_GITEA_TOKEN", giteaToken), "API token for Gitea")
flag.StringVar(&giteaAddress, "gitea-address", LookupEnvOrString("PLUGIN_GITEA_ADDRESS", giteaAddress), "Gitea URL")
flag.StringVar(&comment, "comment", LookupEnvOrString("PLUGIN_COMMENT", comment), "Comment for Gitea")
flag.StringVar(&repoOwner, "repo-owner", LookupEnvOrString("CI_REPO_OWNER", repoOwner), "Owner of the repository")
flag.StringVar(&repoName, "repo-name", LookupEnvOrString("CI_REPO_NAME", repoName), "Name of the repository")
flag.IntVar(&prIndex, "pr-index", LookupEnvOrInt("CI_COMMIT_PULL_REQUEST", prIndex), "Index of the PR")
flag.Parse()
if comment == "" {
panic("You must provide a comment")
}
if giteaToken == "" {
panic("You must provide a Gitea API Token")
}
if giteaAddress == "" {
panic("You must provide a Gitea URL")
}
if repoOwner == "" {
panic("You must provide an repo owner")
}
if repoName == "" {
panic("You must provide a repo name")
}
if prIndex == 0 {
panic("You must provide an index for PR")
}
data := Payload{
Body: comment,
}
payloadBytes, err := json.Marshal(data)
if err != nil {
panic(err)
}
body := bytes.NewReader(payloadBytes)
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/issues/%d/comments?access_token=%s", giteaAddress, repoOwner, repoName, prIndex, giteaToken)
req, err := http.NewRequest("POST", url, body)
if err != nil {
panic(err)
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
}