-
-
Notifications
You must be signed in to change notification settings - Fork 324
Add a plugin to poll Gitea pull requests #1431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fx-chun
wants to merge
3
commits into
NixOS:master
Choose a base branch
from
fx-chun:add-gitea-pulls
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| # Allow building based on Gitea pull requests. | ||
| # | ||
| # Example input: | ||
| # "pulls": { | ||
| # "type": "giteapulls", | ||
| # "value": "example.com alice repo" | ||
| # "emailresponsible": false | ||
| # } | ||
|
|
||
| package Hydra::Plugin::GiteaPulls; | ||
|
|
||
| use strict; | ||
| use warnings; | ||
| use parent 'Hydra::Plugin'; | ||
| use HTTP::Request; | ||
| use LWP::UserAgent; | ||
| use JSON::MaybeXS; | ||
| use Hydra::Helper::CatalystUtils; | ||
| use File::Temp; | ||
| use POSIX qw(strftime); | ||
|
|
||
| sub supportedInputTypes { | ||
| my ($self, $inputTypes) = @_; | ||
| $inputTypes->{'giteapulls'} = 'Open Gitea Pull Requests'; | ||
| } | ||
|
|
||
| sub _iterate { | ||
| my ($url, $auth, $pulls, $ua) = @_; | ||
|
|
||
| my $req = HTTP::Request->new('GET', $url); | ||
| $req->header('Authorization' => 'token ' . $auth) if defined $auth; | ||
|
|
||
| my $res = $ua->request($req); | ||
| my $content = $res->decoded_content; | ||
| die "Error pulling from the gitea pulls API: $content\n" | ||
| unless $res->is_success; | ||
|
|
||
| my $pulls_list = decode_json $content; | ||
|
|
||
| foreach my $pull (@$pulls_list) { | ||
| $pulls->{$pull->{number}} = $pull; | ||
| } | ||
|
|
||
| # TODO Make Link header parsing more robust!!! | ||
| my @links = split ',', ($res->header("Link") // ""); | ||
| my $next = ""; | ||
| foreach my $link (@links) { | ||
| my ($url, $rel) = split ";", $link; | ||
| if (trim($rel) eq 'rel="next"') { | ||
| $next = substr trim($url), 1, -1; | ||
| last; | ||
| } | ||
| } | ||
| _iterate($next, $auth, $pulls, $ua) unless $next eq ""; | ||
| } | ||
|
|
||
| sub fetchInput { | ||
| my ($self, $type, $name, $value, $project, $jobset) = @_; | ||
| return undef if $type ne "giteapulls"; | ||
|
|
||
| my ($baseUrl, $owner, $repo, $proto) = split ' ', $value; | ||
| if (not defined $proto) { # the protocol handler is exposed as an option in order to do integration testing | ||
| $proto = "https" | ||
| } | ||
| my $auth = $self->{config}->{gitea_authorization}->{$owner}; | ||
|
|
||
| my $ua = LWP::UserAgent->new(); | ||
| my %pulls; | ||
| _iterate("$proto://$baseUrl/api/v1/repos/$owner/$repo/pulls?limit=100", $auth, \%pulls, $ua); | ||
|
|
||
| my $tempdir = File::Temp->newdir("gitea-pulls" . "XXXXX", TMPDIR => 1); | ||
| my $filename = "$tempdir/gitea-pulls.json"; | ||
| open(my $fh, ">", $filename) or die "Cannot open $filename for writing: $!"; | ||
| print $fh encode_json \%pulls; | ||
| close $fh; | ||
|
|
||
| my $storePath = trim(`nix-store --add "$filename"` | ||
| or die "cannot copy path $filename to the Nix store.\n"); | ||
| chomp $storePath; | ||
| my $timestamp = time; | ||
| return { storePath => $storePath, revision => strftime "%Y%m%d%H%M%S", gmtime($timestamp) }; | ||
| } | ||
|
|
||
| 1; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this json already sorted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
context: #1522