Skip to content

Commit

Permalink
feat: add support for repo_name workflow dispatch input (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
derberg authored Feb 24, 2022
1 parent 3340e6e commit e48270b
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 14 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ This action can be triggered by:

<img src="workflow_dispatch.jpg" alt="flow diagram" width="40%">

This action also allows you to specify a single repository that action should run against. You can add the following input trigger in your workflow:

```yml
workflow_dispatch:
inputs:
repo_name:
description: |
You can specify name of the repository where workflows should be pushed manually. As long as repository is not ignored by workflow settings.
If you do not specify exact repository name, the workflow will try to replicate all missing changes to all repositories.
required: false
```
As a result you will be able to run your workflow manually by providing specific repository name. If name is not provided, action will fallback to run against all repositores. If wrong repo name is provided under `repo_name`, the action will fail.

<img src="workflow_dispatch_custom_repo.png" alt="flow diagram" width="40%">

## Action Flow

<img src="diagram.png" alt="flow diagram" width="40%">
Expand Down Expand Up @@ -69,7 +85,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- uses: derberg/global-workflows-support@v0.6.0
- uses: derberg/global-workflows-support@v0.7.0
with:
github_token: ${{ secrets.CUSTOM_TOKEN }}
files_to_ignore: name_of_file_where_this_action_is_used.yml
Expand All @@ -95,7 +111,7 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v2
- name: Replicating global workflow
uses: derberg/global-workflows-support@v0.6.0
uses: derberg/global-workflows-support@v0.7.0
with:
github_token: ${{ secrets.CUSTOM_TOKEN }}
files_to_ignore: name_of_file_where_this_action_is_used.yml
Expand Down
50 changes: 44 additions & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1692,7 +1692,7 @@ module.exports = require("os");

const core = __webpack_require__(186);

module.exports = { getCommitFiles, getReposList, createPr };
module.exports = { getCommitFiles, getReposList, createPr, getRepo };

async function getCommitFiles(octokit, commitId, owner, repo) {
const { data: { files } } = await octokit.repos.getCommit({
Expand All @@ -1704,6 +1704,32 @@ async function getCommitFiles(octokit, commitId, owner, repo) {
return files;
}

async function getRepo(octokit, owner, repo) {
core.info(`Getting details of manually selected ${repo} repository`);

const { data } = await octokit.repos.get({
owner,
repo
});

const repoDetails = {
name: data.name,
url: data.html_url,
id: data.node_id,
defaultBranch: data.default_branch,
private: data.private,
archived: data.archived,
topics: data.topics,
};

core.debug(`DEBUG: Repo ${repo} full response`);
core.debug(JSON.stringify(data, null, 2));
core.debug(`DEBUG: Repo ${repo} response that will be returned`);
core.debug(JSON.stringify(repoDetails, null, 2));

return repoDetails;
}

async function getReposList(octokit, owner) {
let isUser;
let response;
Expand Down Expand Up @@ -13304,15 +13330,20 @@ const { retry } = __webpack_require__(298);
const { GitHub, getOctokitOptions } = __webpack_require__(30);

const { createBranch, clone, push, areFilesChanged, getBranches } = __webpack_require__(374);
const { getReposList, createPr } = __webpack_require__(119);
const { getReposList, createPr, getRepo } = __webpack_require__(119);
const { getListOfFilesToReplicate, copyChangedFiles, getListOfReposToIgnore, getBranchName, isInitialized } = __webpack_require__(918);

const triggerEventName = process.env.GITHUB_EVENT_NAME;
const eventPayload = require(process.env.GITHUB_EVENT_PATH);

/* eslint-disable sonarjs/cognitive-complexity */
async function run() {
if (triggerEventName !== 'push' && triggerEventName !== 'workflow_dispatch') return core.setFailed('This GitHub Action works only when triggered by "push" or "workflow_dispatch" webhooks.');
const isPush = triggerEventName === 'push';
if (isPush) core.info('Workflow started on push event');
const isWorkflowDispatch = triggerEventName === 'workflow_dispatch';
if (isWorkflowDispatch) core.info('Workflow started on workflow_dispatch event');

if (!isPush && !isWorkflowDispatch) return core.setFailed('This GitHub Action works only when triggered by "push" or "workflow_dispatch" webhooks.');

core.debug('DEBUG: full payload of the event that triggered the action:');
core.debug(JSON.stringify(eventPayload, null, 2));
Expand All @@ -13326,6 +13357,7 @@ async function run() {
const committerUsername = core.getInput('committer_username');
const committerEmail = core.getInput('committer_email');
const commitMessage = core.getInput('commit_message');
const repoNameManual = eventPayload.inputs && eventPayload.inputs.repo_name;

const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/');

Expand All @@ -13349,10 +13381,16 @@ async function run() {
return;

/*
* 2. Getting list of all repos owned by the owner/org
* 2. Getting list of all repos owned by the owner/org
* or just replicating to the one provided manually
*/
const reposList = await getReposList(myOctokit, owner);

let reposList = [];
if (isWorkflowDispatch && repoNameManual) {
reposList.push(await getRepo(myOctokit, owner, repoNameManual));
} else {
reposList = await getReposList(myOctokit, owner);
}

/*
* 3. Getting list of repos that should be ignored
*/
Expand Down
28 changes: 27 additions & 1 deletion lib/api-calls.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const core = require('@actions/core');

module.exports = { getCommitFiles, getReposList, createPr };
module.exports = { getCommitFiles, getReposList, createPr, getRepo };

async function getCommitFiles(octokit, commitId, owner, repo) {
const { data: { files } } = await octokit.repos.getCommit({
Expand All @@ -12,6 +12,32 @@ async function getCommitFiles(octokit, commitId, owner, repo) {
return files;
}

async function getRepo(octokit, owner, repo) {
core.info(`Getting details of manually selected ${repo} repository`);

const { data } = await octokit.repos.get({
owner,
repo
});

const repoDetails = {
name: data.name,
url: data.html_url,
id: data.node_id,
defaultBranch: data.default_branch,
private: data.private,
archived: data.archived,
topics: data.topics,
};

core.debug(`DEBUG: Repo ${repo} full response`);
core.debug(JSON.stringify(data, null, 2));
core.debug(`DEBUG: Repo ${repo} response that will be returned`);
core.debug(JSON.stringify(repoDetails, null, 2));

return repoDetails;
}

async function getReposList(octokit, owner) {
let isUser;
let response;
Expand Down
22 changes: 17 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ const { retry } = require('@octokit/plugin-retry');
const { GitHub, getOctokitOptions } = require('@actions/github/lib/utils');

const { createBranch, clone, push, areFilesChanged, getBranches } = require('./git');
const { getReposList, createPr } = require('./api-calls');
const { getReposList, createPr, getRepo } = require('./api-calls');
const { getListOfFilesToReplicate, copyChangedFiles, getListOfReposToIgnore, getBranchName, isInitialized } = require('./utils');

const triggerEventName = process.env.GITHUB_EVENT_NAME;
const eventPayload = require(process.env.GITHUB_EVENT_PATH);

/* eslint-disable sonarjs/cognitive-complexity */
async function run() {
if (triggerEventName !== 'push' && triggerEventName !== 'workflow_dispatch') return core.setFailed('This GitHub Action works only when triggered by "push" or "workflow_dispatch" webhooks.');
const isPush = triggerEventName === 'push';
if (isPush) core.info('Workflow started on push event');
const isWorkflowDispatch = triggerEventName === 'workflow_dispatch';
if (isWorkflowDispatch) core.info('Workflow started on workflow_dispatch event');

if (!isPush && !isWorkflowDispatch) return core.setFailed('This GitHub Action works only when triggered by "push" or "workflow_dispatch" webhooks.');

core.debug('DEBUG: full payload of the event that triggered the action:');
core.debug(JSON.stringify(eventPayload, null, 2));
Expand All @@ -28,6 +33,7 @@ async function run() {
const committerUsername = core.getInput('committer_username');
const committerEmail = core.getInput('committer_email');
const commitMessage = core.getInput('commit_message');
const repoNameManual = eventPayload.inputs && eventPayload.inputs.repo_name;

const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/');

Expand All @@ -51,10 +57,16 @@ async function run() {
return;

/*
* 2. Getting list of all repos owned by the owner/org
* 2. Getting list of all repos owned by the owner/org
* or just replicating to the one provided manually
*/
const reposList = await getReposList(myOctokit, owner);

let reposList = [];
if (isWorkflowDispatch && repoNameManual) {
reposList.push(await getRepo(myOctokit, owner, repoNameManual));
} else {
reposList = await getReposList(myOctokit, owner);
}

/*
* 3. Getting list of repos that should be ignored
*/
Expand Down
Binary file added workflow_dispatch_custom_repo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e48270b

Please sign in to comment.