Skip to content

Commit

Permalink
Added more options, now using stencil templates
Browse files Browse the repository at this point in the history
  • Loading branch information
erikaheidi committed Oct 7, 2022
1 parent 3e1e114 commit 8705057
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 23 deletions.
64 changes: 60 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
# Generate / Update CONTRIBUTORS File - GitHub Action

This GitHub Action updates a CONTRIBUTORS file with the top contributors from the specified project, pulling contents from the GitHub API.
This GitHub Action updates a CONTRIBUTORS file with the top contributors from the specified project, pulling contents from the GitHub API. You can use it in combination with an action to push changes directly into your project's main branch, or with an action that creates a PR with the updated file.

## Example Usage
### Supported Configuration via Environment Variables:

- CONTRIB_REPOSITORY: The repository you want to pull contributors from. This is mandatory.
- CONTRIB_OUTPUT_FILE: The file you want to generate, default set to `CONTRIBUTORS.md`.
- CONTRIB_TEMPLATE: The [stencil](https://github.com/minicli/stencil) template to use - you can use this to customize the generated markdown file. Default set to the included `contributors.tpl` file.
- CONTRIB_IGNORE: A comma-separated string with users to ignore. Default set to `github-actions[bot],renovate-bot,dependabot`.

## Basic Usage

This action is made to use in conjunction with [test-room-7/action-update-file](https://github.com/marketplace/actions/update-files-on-github) in order to automatically commit an updated CONTRIBUTORS file in a fixed interval.

Expand All @@ -18,7 +25,7 @@ jobs:
main:
runs-on: ubuntu-latest
steps:
- uses: minicli/[email protected]
- uses: minicli/action-contributors@v3.2.1
name: "Update a projects CONTRIBUTORS file"
env:
CONTRIB_REPOSITORY: 'minicli/minicli'
Expand Down Expand Up @@ -49,7 +56,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: minicli/[email protected]
- uses: minicli/action-contributors@v3.2.1
name: "Update a projects CONTRIBUTORS file"
env:
CONTRIB_REPOSITORY: 'minicli/docs'
Expand All @@ -61,3 +68,52 @@ jobs:
title: "[automated] Update Contributors File"
token: ${{ secrets.GITHUB_TOKEN }}
```

## Using a Custom Template

This action uses a simple templating lib that allows some customization for the final generated markdown file.

Here's how the default template looks like:

```markdown
# Top Contributors: {{ repo }}
Shout out to our top contributors!
{{ content }}
_Last updated: {{ updated }}_
```

The app will pass on the following variables to the template, which you can use as you prefer:
- `repo`: the repository from where the data is coming from. Ex: `minicli/minicli`
- `content`: the full list of contributors
- `updated`: date and time the file was generated in RFC822 format

To use a custom template, create a `.tpl` file based on the default template and include the `CONTRIB_TEMPLATE` env var to your workflow. This file must be committed to the same repository where the workflow is defined.

Here is an example of a workflow that uses a custom template called `mytemplate.tpl`, located at the root of the repository where the workflow is set:

```yaml
name: Update CONTRIBUTORS file
on:
schedule:
- cron: "0 0 1 * *"
workflow_dispatch:
jobs:
main:
runs-on: ubuntu-latest
steps:
- uses: minicli/[email protected]
name: "Update a projects CONTRIBUTORS file"
env:
CONTRIB_REPOSITORY: 'minicli/minicli'
CONTRIB_OUTPUT_FILE: 'CONTRIBUTORS.md'
CONTRIB_TEMPLATE: ${{ github.workspace }}/mytemplate
- name: Commit changes
uses: test-room-7/action-update-file@v1
with:
file-path: 'CONTRIBUTORS.md'
commit-msg: Update Contributors
github-token: ${{ secrets.GITHUB_TOKEN }}
```

4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"require": {
"php": "^8.1",
"minicli/minicli": "^2.2",
"minicli/curly": "^0.1.2"
"minicli/curly": "^0.1.2",
"minicli/stencil": "^0.1.1"
}
}
50 changes: 48 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions contributors.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Top Contributors: {{ repo }}
Shout out to our top contributors!

{{ content }}

_Last updated: {{ updated }}_
40 changes: 24 additions & 16 deletions minicli
Original file line number Diff line number Diff line change
@@ -1,55 +1,63 @@
#!/usr/bin/php
<?php

if(php_sapi_name() !== 'cli') {
if (php_sapi_name() !== 'cli') {
exit;
}

require __DIR__ . '/vendor/autoload.php';

use Minicli\App;
use Minicli\Curly\Client;
use Minicli\Stencil;

$contribRepository = getenv('CONTRIB_REPOSITORY');

$ignoreUsers = getenv('CONTRIB_IGNORE') ?: 'github-actions[bot],renovate-bot,dependabot';
$app = new App([
'app_path' => __DIR__ . '/app/Command',
'repository' => $contribRepository,
'repository' => getenv('CONTRIB_REPOSITORY') ?: null,
'output_file' => getenv('CONTRIB_OUTPUT_FILE') ?: 'CONTRIBUTORS.md',
'ignore_users' => [ 'github-actions[bot]' ]
'template' => getenv('CONTRIB_TEMPLATE') ?: 'contributors',
'ignore_users' => explode(',', $ignoreUsers)
]);

if (null === $contribRepository) {
$app->getPrinter()->error("CONTRIB_REPOSITORY not set.");
return 1;
}

$app->registerCommand('update-contributors', function () use ($app) {

if (!$app->config->has('repository')) {
$app->getPrinter()->error("Missing CONTRIB_REPOSITORY environment variable.");
return 1;
}

$app->getPrinter()->info('Fetching top contributors...');

$client = new Client();
$response = $client->get(
"https://api.github.com/repos/" . $app->config->repository. "/contributors",
['Accept: application/vnd.github.v3+json', 'User-Agent: Curly']
"https://api.github.com/repos/" . $app->config->repository . "/contributors",
['Accept: application/vnd.github.v3+json', 'User-Agent: Curly']
);

if ($response['code'] != 200) {
$app->getPrinter()->error("an error occurred: " . $response['code']);
return 1;
}

$content = "# Contributors\n\n";
$content .= "Shout out to our top contributors!\n\n";
$stencil = new Stencil(__DIR__);

$content = "";
foreach (json_decode($response['body']) as $item) {
if (!in_array($item->login, $app->config->ignore_users)) {
$content .= "- [$item->login]($item->html_url)\n";
}
}

$output = $stencil->applyTemplate($app->config->template, [
'repo' => $app->config->repository,
'content' => $content,
'updated' => (new DateTimeImmutable())->format(DateTimeInterface::RFC822)
]);

try {
$contrib_file = fopen($app->config->output_file, 'w+');
fwrite($contrib_file, $content);
fwrite($contrib_file, $output);
fclose($contrib_file);
} catch (Exception $exception) {
$app->getPrinter()->error("An error occurred while trying to save the contrib file.");
Expand All @@ -60,4 +68,4 @@ $app->registerCommand('update-contributors', function () use ($app) {
return 0;
});

$app->runCommand($argv);
$app->runCommand($argv);

0 comments on commit 8705057

Please sign in to comment.