Skip to content

Commit

Permalink
feat: implemented auto detection of protection mode
Browse files Browse the repository at this point in the history
  • Loading branch information
akhilmhdh committed Nov 8, 2021
1 parent 815e25d commit 93b9712
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 32 deletions.
128 changes: 128 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Contributor Covenant Code of Conduct

## Our Pledge

We as members, contributors, and leaders pledge to make participation in our
community a harassment-free experience for everyone, regardless of age, body
size, visible or invisible disability, ethnicity, sex characteristics, gender
identity and expression, level of experience, education, socio-economic status,
nationality, personal appearance, race, religion, or sexual identity
and orientation.

We pledge to act and interact in ways that contribute to an open, welcoming,
diverse, inclusive, and healthy community.

## Our Standards

Examples of behavior that contributes to a positive environment for our
community include:

- Demonstrating empathy and kindness toward other people
- Being respectful of differing opinions, viewpoints, and experiences
- Giving and gracefully accepting constructive feedback
- Accepting responsibility and apologizing to those affected by our mistakes,
and learning from the experience
- Focusing on what is best not just for us as individuals, but for the
overall community

Examples of unacceptable behavior include:

- The use of sexualized language or imagery, and sexual attention or
advances of any kind
- Trolling, insulting or derogatory comments, and personal or political attacks
- Public or private harassment
- Publishing others' private information, such as a physical or email
address, without their explicit permission
- Other conduct which could reasonably be considered inappropriate in a
professional setting

## Enforcement Responsibilities

Community leaders are responsible for clarifying and enforcing our standards of
acceptable behavior and will take appropriate and fair corrective action in
response to any behavior that they deem inappropriate, threatening, offensive,
or harmful.

Community leaders have the right and responsibility to remove, edit, or reject
comments, commits, code, wiki edits, issues, and other contributions that are
not aligned to this Code of Conduct, and will communicate reasons for moderation
decisions when appropriate.

## Scope

This Code of Conduct applies within all community spaces, and also applies when
an individual is officially representing the community in public spaces.
Examples of representing our community include using an official e-mail address,
posting via an official social media account, or acting as an appointed
representative at an online or offline event.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported to the community leaders responsible for enforcement at
[email protected].
All complaints will be reviewed and investigated promptly and fairly.

All community leaders are obligated to respect the privacy and security of the
reporter of any incident.

## Enforcement Guidelines

Community leaders will follow these Community Impact Guidelines in determining
the consequences for any action they deem in violation of this Code of Conduct:

### 1. Correction

**Community Impact**: Use of inappropriate language or other behavior deemed
unprofessional or unwelcome in the community.

**Consequence**: A private, written warning from community leaders, providing
clarity around the nature of the violation and an explanation of why the
behavior was inappropriate. A public apology may be requested.

### 2. Warning

**Community Impact**: A violation through a single incident or series
of actions.

**Consequence**: A warning with consequences for continued behavior. No
interaction with the people involved, including unsolicited interaction with
those enforcing the Code of Conduct, for a specified period of time. This
includes avoiding interactions in community spaces as well as external channels
like social media. Violating these terms may lead to a temporary or
permanent ban.

### 3. Temporary Ban

**Community Impact**: A serious violation of community standards, including
sustained inappropriate behavior.

**Consequence**: A temporary ban from any sort of interaction or public
communication with the community for a specified period of time. No public or
private interaction with the people involved, including unsolicited interaction
with those enforcing the Code of Conduct, is allowed during this period.
Violating these terms may lead to a permanent ban.

### 4. Permanent Ban

**Community Impact**: Demonstrating a pattern of violation of community
standards, including sustained inappropriate behavior, harassment of an
individual, or aggression toward or disparagement of classes of individuals.

**Consequence**: A permanent ban from any sort of public interaction within
the community.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage],
version 2.0, available at
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.

Community Impact Guidelines were inspired by [Mozilla's code of conduct
enforcement ladder](https://github.com/mozilla/diversity).

[homepage]: https://www.contributor-covenant.org

For answers to common questions about this code of conduct, see the FAQ at
https://www.contributor-covenant.org/faq. Translations are available at
https://www.contributor-covenant.org/translations.
37 changes: 37 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Contributing Guides

First of all, thank you for your effort to improve this action. This guide will help you regarding various aspects like putting issues, contributing a feature, etc.

## Code of Conduct

This project and everyone participating in it is governed by the [Code of Conduct](CODE_OF_CONDUCT.md). By participating, you are expected to uphold this code. Please read the [full text](CODE_OF_CONDUCT.md) so that you can read which actions may or may not be tolerated.

---

## Before Submitting a Pull Request

**Before submitting your pull request** make sure the following requirements are fulfilled:

- Fork the repository
- Create a branch from `master`
- Run `npm install` in the repository root
- Change necessary code for bug fix, a new feature
- Check linting and format it

```bash
npm run lint
npm run format
```

- Make sure all are test passing

```bash
npm run test
```

## Reporting an issue

Before submitting an issue, you need to make sure:

- Kindly provide an adequate description and a clear title
- If possible, share a URL towards the repository in which action is failing
51 changes: 32 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
name: A job to automate contrib in readme
steps:
- name: Contribute List
uses: akhilmhdh/[email protected]
uses: akhilmhdh/[email protected].1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```
Expand All @@ -41,15 +41,11 @@ That's it!
To add it to your to your existing workflow, append this to your current `.yml` workflow script.

```yml
- uses: akhilmhdh/[email protected]
- uses: akhilmhdh/[email protected].1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

> Currently GITHUB_TOKEN doesn't have permission to directly commit on a protected branch. To solve this you may need to replace the github action token with an admin acc token as mentioned in this post by [phips28](https://github.community/t/how-to-push-to-protected-branches-in-a-github-action/16101/10). Hopefully in the future GitHub can give an exception to actions in workflow.

> Now there is an alternative solution, if your branch is protected or there is status checks. You can set input `is_protected` to `true` in config and the action won't directly commit to the branch. Instead it will put a PR by creating a branch out of the branch that the action is running on. To avoid bloating with lot of branches either delete the branch on merge or set the option in github to delete source branch on merge automatically.

### Second Step

1. Add a `README.md` file
Expand Down Expand Up @@ -126,23 +122,40 @@ You can add these optional parameters in your action script to modify the appear

```yml
- name: Contribute List
uses: akhilmhdh/[email protected]
uses: akhilmhdh/[email protected].1
with:
image_size: 100
```

| Option | Default Value | Description | Required |
| --------------------- | ---------------------------------------- | ----------------------------------------------------------------------------------------------------------- | -------- |
| image_size | 100(px) | Size of square images in the stack | false |
| is_protected | false | If the branch to be updated is protected change this to true to change from directly commiting to PR update | false |
| readme_path | README.md | Path of the readme file you want to update | false |
| use_username | false | To use username instead of full name | false |
| columns_per_row | 6 | Number of columns in a row | false |
| collaborators | direct | Type of collaborators options: all/direct/outside | false |
| commit_message | contrib-readme-action has updated readme | Commit message of the github action | false |
| committer_username | contrib-readme-bot | Username on commit | false |
| committer_email | [email protected] | Email id of committer | false |
| pr_title_on_protected | contributors readme action update | Title of the PR that will be created if the branch is protected | false |
| Option | Default Value | Description | Required |
| --------------------- | ---------------------------------------- | --------------------------------------------------------------- | -------- |
| image_size | 100(px) | Size of square images in the stack | false |
| readme_path | README.md | Path of the readme file you want to update | false |
| use_username | false | To use username instead of full name | false |
| columns_per_row | 6 | Number of columns in a row | false |
| collaborators | direct | Type of collaborators options: all/direct/outside | false |
| commit_message | contrib-readme-action has updated readme | Commit message of the github action | false |
| committer_username | contrib-readme-bot | Username on commit | false |
| committer_email | [email protected] | Email id of committer | false |
| pr_title_on_protected | contributors readme action update | Title of the PR that will be created if the branch is protected | false |

> The action will update Readme as PR when the branch is protected, else it will directly commit it.

### Outputs

Following outputs are generated on the execution of this action.

| Output | Value | Description |
| ------ | ------ | ------------------------------------------------------------ |
| pr_id | string | Id of the generated PR when the branch is in protected mode. |

## Contributing

Please see [CONTRIBUTING.md](/CONTRIBUTING.md) for getting started with the contribution.

Make sure that you follow [CODE_OF_CONDUCT.md](/CODE_OF_CONDUCT.md) while contributing and engaging in the discussions.

**When contributing, please first discuss the change you wish to make via an issue on this repository before making the actual change**.

## License

Expand Down
7 changes: 3 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ inputs:
description: "Size of square images in the stack"
required: false
default: "100"
is_protected:
description: "Is the branch to be updated protected"
required: false
default: "false"
readme_path:
description: "Path of the readme file you want to update"
default: "README.md"
Expand Down Expand Up @@ -41,6 +37,9 @@ inputs:
description: "Title of the PR that will be created if the branch is protected"
default: "contributors readme action update"
required: false
outputs:
pr_id: # id of pr
description: "Id of the generated PR when the branch is in protected mode"
runs:
using: "node12"
main: "dist/index.js"
Expand Down
9 changes: 6 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8830,7 +8830,7 @@ async function run() {
const message = (0,core.getInput)('commit_message').trim();
const name = (0,core.getInput)('committer_username').trim();
const email = (0,core.getInput)('committer_email').trim();
const isProtected = (0,core.getBooleanInput)('is_protected');
const prTitle = (0,core.getInput)('pr_title_on_protected').trim();

const ref = github.context.ref;
const branch = github.context.ref.split('/').pop();
Expand All @@ -8844,6 +8844,8 @@ async function run() {

const nwo = process.env['GITHUB_REPOSITORY'] || '/';
const [owner, repo] = nwo.split('/');
const branchDetails = await src_octokit.rest.repos.getBranch({ owner, repo, branch });
const isProtected = branchDetails.data.protected;

const userInfo = await src_octokit.rest.users.getByUsername({ username: owner });
const isOrg = userInfo.data.type === 'Organization';
Expand Down Expand Up @@ -8972,13 +8974,14 @@ async function run() {
}
});

await src_octokit.rest.pulls.create({
const prDetails = await src_octokit.rest.pulls.create({
owner,
repo,
base: branch,
head: branchNameForPR,
title: 'contributors readme action update'
title: prTitle
});
(0,core.setOutput)('pr_id', prDetails.data.id);
} else {
await src_octokit.rest.repos.createOrUpdateFileContents({
owner,
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "contribution-automation-action",
"version": "2.2.1",
"version": "2.3.1",
"description": "A github action to automate contributors section of a readme",
"main": "index.js",
"scripts": {
Expand Down
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { nanoid } from 'nanoid';
import { getInput, getBooleanInput, setFailed } from '@actions/core';
import { getInput, setOutput, setFailed } from '@actions/core';
import { context } from '@actions/github';
import octokit from './octokit';

Expand All @@ -19,7 +19,6 @@ async function run() {
const message = getInput('commit_message').trim();
const name = getInput('committer_username').trim();
const email = getInput('committer_email').trim();
const isProtected = getBooleanInput('is_protected');
const prTitle = getInput('pr_title_on_protected').trim();

const ref = context.ref;
Expand All @@ -34,6 +33,8 @@ async function run() {

const nwo = process.env['GITHUB_REPOSITORY'] || '/';
const [owner, repo] = nwo.split('/');
const branchDetails = await octokit.rest.repos.getBranch({ owner, repo, branch });
const isProtected = branchDetails.data.protected;

const userInfo = await octokit.rest.users.getByUsername({ username: owner });
const isOrg = userInfo.data.type === 'Organization';
Expand Down Expand Up @@ -162,13 +163,14 @@ async function run() {
}
});

await octokit.rest.pulls.create({
const prDetails = await octokit.rest.pulls.create({
owner,
repo,
base: branch,
head: branchNameForPR,
title: prTitle
});
setOutput('pr_id', prDetails.data.id);
} else {
await octokit.rest.repos.createOrUpdateFileContents({
owner,
Expand Down

0 comments on commit 93b9712

Please sign in to comment.