Skip to content

Commit

Permalink
transfer logic
Browse files Browse the repository at this point in the history
  • Loading branch information
wd60622 committed Oct 26, 2024
0 parents commit 43d3df4
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM ubuntu:latest

COPY get-closing-labels.sh entrypoint.sh /

RUN apt-get update && apt-get install -y \
curl \
jq \
git \
gh \
&& rm -rf /var/lib/apt/lists/*

ENTRYPOINT ["/entrypoint.sh"]
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Sync Closing Labels

GitHub action to add closing labels to a pull request
27 changes: 27 additions & 0 deletions actions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: "Sync Closing Labels"
description: 'A GitHub action to sync closing labels for a pull request'
inputs:
exclude:
description: "A list of comma separate labels to not add"
required: false
default: ""
respect-unlabeled:
description: "If true, removing label will keep the label removed"
required: false
default: "true"
owner:
description: "The owner of the repository"
required: true
default: ${{ github.repository_owner }}
repo:
description: "The repository name"
required: true
default: ${{ github.event.repository.name }}
pr_number:
description: "The pull request number"
required: true
default: ${{ github.event.number }}
runs:
using: 'docker'
image: 'ubuntu:latest'
entrypoint: '/entrypoint.sh'
21 changes: 21 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh -l

./get-closing-labels.sh -o $INPUT_OWNER -n $INPUT_NAME -p $INPUT_PR_NUMBER | jq \
--arg ignore "$INPUT_IGNORE" \
--arg respect_unlabeled "$INPUT_RESPECT_UNLABELED" \
'def split_ignore: if $ignore == "" then [] else $ignore | split(",") end;
.closing as $closing |
.removed as $removed |
split_ignore as $ignore_split |
{
closing: $closing,
removed: $removed,
ignore_split: $ignore_split,
result: (
if $respect_unlabeled == "true"
then $closing - $removed
else $closing end
- $ignore_split
)
} | .result | join(",")'

83 changes: 83 additions & 0 deletions get-closing-labels.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/bin/bash

# Function to display usage
usage() {
echo "Usage: $0 -o owner -n name -p pr_number"
exit
}

# Parse command-line arguments
while getopts ":o:n:p:" opt; do
case $opt in
o) owner="$OPTARG"
;;
n) name="$OPTARG"
;;
p) pr_number="$OPTARG"
;;
\?) echo "Invalid option -$OPTARG" >&2
usage
;;
:) echo "Option -$OPTARG requires an argument." >&2
usage
;;
esac
done

# Check if all required arguments are provided
if [ -z "$owner" ] || [ -z "$name" ] || [ -z "$pr_number" ]; then
usage
fi

response=$(gh api graphql \
--paginate \
-F number=$pr_number -F owner=$owner -F name=$name \
-f query='
query($endCursor: String, $owner: String!, $name: String!, $number: Int!) {
repository(owner: $owner, name: $name) {
pullRequest(number: $number) {
timelineItems(first: 10, after: $endCursor) {
nodes {
__typename
... on UnlabeledEvent {
label {
name
}
}
}
pageInfo {
endCursor
hasNextPage
}
}
closingIssuesReferences(first: 10, after: $endCursor) {
nodes {
labels(first: 10) {
nodes {
name
}
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
}
' --jq '
{
closing: (.data.repository.pullRequest.closingIssuesReferences.nodes
| map(.labels.nodes | map(.name))
| flatten
| unique),
removed: (.data.repository.pullRequest.timelineItems.nodes
| map(select(.__typename == "UnlabeledEvent") | .label.name)
| select(length > 0)
| unique)
}
'
)

echo $response

0 comments on commit 43d3df4

Please # to comment.