Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,30 @@
name: Jiho

# Controls when the workflow will run
on: pull_request

on:
schedule:
- cron: "0 * * * *"
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
hi:
# INFO: need permission
# https://stackoverflow.com/questions/70435286/resource-not-accessible-by-integration-on-github-post-repos-owner-repo-ac
permissions: write-all
# The type of runner that the job will run on
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v3

- name: Run a one-line script
run: echo Hello, world!
run: echo Hello, world!

- name: Rutn auto labeler
uses: ./ # Uses an action in the root directory
with:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5 changes: 4 additions & 1 deletion READMD.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
123213
# How to create git action

1. npm init
2. implement
13 changes: 13 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: "Auto label"
description: "Automatically label issues and pull requests based on title and body"
author: "peanut-lover"
inputs:
GH_TOKEN:
description: GitHub token used to make API requests
required: true
branding:
icon: "activity"
color: "purple"
runs:
using: "node16"
main: "./index.js"
50 changes: 50 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const MAX_PER_PAGE = 100;

const fetchAllPages = async (
request,
params,
maxCount = Number.POSITIVE_INFINITY
) => {
let [page, len, count] = [1, 0, 0];
const result = [];

do {
const { data } = await request({ ...params, per_page: MAX_PER_PAGE, page });

result.push(...data);

[page, len, count] = [page + 1, data.length, count + data.length];
} while (len === MAX_PER_PAGE && count < maxCount);

return result.slice(0, maxCount);
};

export const getPRList = async () => {
return fetchAllPages(global.octokit.rest.pulls.list, {
owner: global.owner,
repo: global.repo,
state: "open",
});
};

export const removeLabel = async (number, name) => {
const { data: labels } = await global.octokit.rest.issues.removeLabel({
owner: global.owner,
repo: global.repo,
issue_number: number,
name,
});

return labels;
};

export const addLabels = async (number, names) => {
const { data: labels } = await global.octokit.rest.issues.addLabels({
owner: global.owner,
repo: global.repo,
issue_number: number,
labels: names,
});

return labels;
};
76 changes: 76 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import * as core from "@actions/core";
import { initialize } from "./initialize.js";
import { getPRList, addLabels, removeLabel } from "./api.js";

const D_N_PATTERN = /^D-(\d+)$/;

const getNextLabel = (name) => {
const [, day] = name.match(D_N_PATTERN);
const currentDDay = parseInt(day);
const nextDDay = currentDDay <= 0 ? 0 : currentDDay - 1;

return `D-${nextDDay}`;
};

const extractLabelChanges = (prList) => {
return prList
.map(({ number, labels }) => ({
number,
dLabel: labels.find(({ name }) => D_N_PATTERN.test(name))?.name,
}))
.filter(({ dLabel }) => !!dLabel)
.map(({ number, dLabel }) => ({
prNumber: number,
currentLabel: dLabel,
nextLabel: getNextLabel(dLabel),
}));
};

const updateLabel = async ({ prNumber, currentLabel, nextLabel }) => {
if (prNumber === nextLabel) {
return false;
}

return Promise.all([
removeLabel(prNumber, currentLabel),
addLabels(prNumber, [nextLabel]),
]).then(
() => {
core.info(
`Successfully updated label for PR #${prNumber} from "${currentLabel}" to "${nextLabel}"`
);

return true;
},
(error) => {
core.warning(
`Failed to update label for PR #${prNumber}: ${error.message}`
);

throw error;
}
);
};

// Step1: Get Octokit object;
initialize();

// Step2. Get PRs
getPRList()
// Step3. Get PR number, current d-day label, next d-day label
.then((prList) => extractLabelChanges(prList))
// Step4. Get label change info about each PR
.then((changes) => {
// changes => { prNumber, currentLabel, nextLabel } []
return Promise.all(changes.map(updateLabel));
})
.then((results) => {
core.info(
`Successfully updated labels for all ${
results.filter(Boolean).length
} PRs.`
);
})
.catch((error) => {
core.setFailed(error.message);
});
19 changes: 19 additions & 0 deletions initialize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as core from "@actions/core";
import * as github from "@actions/github";

export const initialize = () => {
global.owner = github.context.repo.owner;
global.repo = github.context.repo.repo;

const githubToken = core.getInput("GH_TOKEN", { required: true });
if (!githubToken) {
throw new Error("Missing GH_TOKEN environment variable");
}
const octokit = github.getOctokit(githubToken);

global.octokit = octokit;

console.log(`Successfully initialized.`);
console.log(`global ower: ${JSON.stringify(global.owner)} `);
console.log(`global repo: ${JSON.stringify(global.repo)} `);
};
1 change: 1 addition & 0 deletions node_modules/.bin/uuid

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

9 changes: 9 additions & 0 deletions node_modules/@actions/core/LICENSE.md

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

Loading